summaryrefslogtreecommitdiffstats
path: root/privatemsg.install
blob: ff2e6f1495d5498a9adcda5dda3cad00a7d6bb63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
// $Id$

function privatemsg_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {privatemsg} (
          id            int(10) unsigned NOT NULL primary key,
          author        int(10) unsigned NOT NULL,
          recipient     int(10) unsigned NOT NULL,
          subject       varchar(64) NOT NULL,
          message       text NOT NULL,
          timestamp     int(11) unsigned NOT NULL,
          newmsg        tinyint unsigned NOT NULL,
          hostname      varchar(255) NOT NULL,
          folder        int(10) unsigned NOT NULL DEFAULT '0',
          author_del    tinyint unsigned NOT NULL DEFAULT '0',
          recipient_del tinyint unsigned NOT NULL DEFAULT '0',
          format        int(4) NOT NULL DEFAULT '0',
          key (recipient),
          key (folder)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {privatemsg_folder} (
          fid           int(10) unsigned NOT NULL primary key,
          uid           int(10) unsigned NOT NULL,
          name          varchar(255) NOT NULL
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {privatemsg_archive} (
          id            int(10) unsigned NOT NULL primary key,
          author        int(10) unsigned NOT NULL,
          recipient     int(10) unsigned NOT NULL,
          subject       VARCHAR(64) NOT NULL,
          message       text NOT NULL,
          timestamp     int(11) unsigned NOT NULL,
          hostname      varchar(255) NOT NULL,
          folder        int(10) unsigned NOT NULL,
          format        int(4) NOT NULL DEFAULT '0',
          key (recipient)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {privatemsg} (
          id            integer NOT NULL,
          author        integer NOT NULL,
          recipient     integer NOT NULL,
          subject       varchar(64) NOT NULL,
          message       text NOT NULL,
          timestamp     integer NOT NULL,
          newmsg        smallint NOT NULL,
          hostname      varchar(255) NOT NULL,
          format        smallint NOT NULL DEFAULT '0',
          folder        integer NOT NULL DEFAULT '0',
          author_del    smallint NOT NULL DEFAULT '0',
          recipient_del smallint NOT NULL DEFAULT '0',
          PRIMARY KEY (id)
        )");
      db_query("CREATE INDEX {privatemsg_folder_index} ON {privatemsg}(folder)");
      db_query("CREATE TABLE {privatemsg_folder} (
          fid           integer NOT NULL,
          uid           integer NOT NULL,
          name          varchar(255) not null,
          PRIMARY KEY (fid)
        )");
      db_query("CREATE TABLE {privatemsg_archive} (
          id            integer NOT NULL,
          author        integer NOT NULL,
          recipient     integer NOT NULL,
          subject       varchar(64) NOT NULL,
          message       text NOT NULL,
          timestamp     integer NOT NULL,
          hostname      varchar(255) NOT NULL,
          format        smallint NOT NULL DEFAULT '0',
          folder        integer NOT NULL,
          PRIMARY KEY (id)
        )");
      db_query("CREATE INDEX {privatemsg_archive_recipient} ON {privatemsg_archive}(recipient)");
      db_query("create or replace function unix_timestamp(timestamp with time zone)
        returns int as '
        declare
           date alias for " .'$1'. ";
           timezero timestamp;
           offset interval;
        begin
           timezero := timestamp ''1970-1-1 00:00'' at time zone ''utc'';
           offset := date-timezero;

           return (extract(''days'' from offset)*86400+
                   extract(''hours'' from offset)*3600+
                   extract(''minutes'' from offset)*60+
                   extract(''seconds'' from offset))::int;
        end;
        ' language 'plpgsql'");
      db_query("create or replace function unix_timestamp(timestamp without time zone)
        returns int as '
        declare
           date alias for " .'$1'. ";
           timezero timestamp;
           offset interval;
        begin
           timezero := timestamp ''1970-1-1 00:00'' at time zone ''utc'';
           offset := date-timezero;

           return (extract(''days'' from offset)*86400+
                   extract(''hours'' from offset)*3600+
                   extract(''minutes'' from offset)*60+
                   extract(''seconds'' from offset))::int;
        end;
        ' language 'plpgsql'");
      break;
  }
  // Sent messages folder
  db_query("INSERT INTO {privatemsg_folder} (fid, uid, name) VALUES (1, 0, 'Sent')");
  do {
    $i = db_next_id('{privatemsg_folder}_fid');
  }
  while ($i < 1);  // In case this api ever changes to start at zero..
}

function privatemsg_uninstall() {
  db_query("DROP TABLE {privatemsg}");
  db_query("DROP TABLE {privatemsg_folder}");
  db_query("DROP TABLE {privatemsg_archive}");
  db_query("DELETE FROM {variable} WHERE name LIKE 'privatemsg_%'");
  cache_clear_all('variables', 'cache');
}

/* Upgrade on mysql from versions before 22-May-2003:
   Create privatemsg_archive/privatemsg_folder tables and insert one row, shown above^
   ALTER TABLE privatemsg ADD folder int(10) unsigned NOT NULL;
   ALTER TABLE privatemsg ADD author_del tinyint unsigned NOT NULL;
   ALTER TABLE privatemsg ADD recipient_del tinyint unsigned NOT NULL;
   ALTER TABLE privatemsg ADD INDEX(folder);
   ALTER TABLE privatemsg CHANGE hostname hostname varchar(255) NOT NULL;
   Continue with steps below, but skip ALTER line for privatemsg_archive..
 *
 * Upgrade on mysql from versions before 29-Apr-2005:
   ALTER TABLE privatemsg CHANGE new newmsg tinyint UNSIGNED NOT NULL;
   ALTER TABLE privatemsg ADD format int(4) NOT NULL DEFAULT '0';
   ALTER TABLE privatemsg_archive ADD format int(4) NOT NULL DEFAULT '0';
   UPDATE privatemsg SET format=1;
   UPDATE privatemsg_archive SET format=1;
 */

function privatemsg_update_1() {
  return _system_update_utf8(array('privatemsg', 'privatemsg_archive', 'privatemsg_folder'));
}

function privatemsg_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $sql = 'ALTER TABLE {%s} MODIFY %s int(10) unsigned NOT NULL';
      $seq = "INSERT INTO {sequences} (name, id) VALUES ('%s', %d)";
      break;
    case 'pgsql':
      $sql = 'ALTER TABLE {%s} ALTER COLUMN %s SET DEFAULT NULL';
      $seq = 'CREATE SEQUENCE %s_seq INCREMENT 1 START %d';
      break;
    default:
      return $ret;
  }
  foreach (array('privatemsg' => 'id', 'privatemsg_folder' => 'fid') as $table => $id) {
    $ret[] = update_sql(sprintf($sql, $table, $id));
    $max = db_result(db_query('SELECT max('. $id .') FROM {'. $table .'}'));
    if ($table == 'privatemsg') {
      $max = max($max, db_result(db_query('SELECT max(id) FROM {privatemsg_archive}')));
    }
    $ret[] = update_sql(sprintf($seq, '{'. $table .'}_'. $id, $max));
  }
  $ret[] = update_sql(sprintf($sql, 'privatemsg_archive', 'id'));
  return $ret;
}
?>