5 * update.php - automatic system update
7 * Automatically update database schemas and any other development changes such that
8 * copying the latest files from the source code repository will always perform a clean
9 * and painless upgrade.
11 * Each function in this file is named update_nnnn() where nnnn is an increasing number
12 * which began counting at 1000.
14 * At the top of the file "boot.php" is a define for DB_UPDATE_VERSION. Any time there is a change
15 * to the database schema or one which requires an upgrade path from the existing application,
16 * the DB_UPDATE_VERSION is incremented.
18 * The current DB_UPDATE_VERSION is stored in the config area of the database. If the application starts up
19 * and DB_UPDATE_VERSION is greater than the last stored build number, we will process every update function
20 * in order from the currently stored value to the new DB_UPDATE_VERSION. This is expected to bring the system
21 * up to current without requiring re-installation or manual intervention.
23 * Once the upgrade functions have completed, the current DB_UPDATE_VERSION is stored as the current value.
24 * The DB_UPDATE_VERSION will always be one greater than the last numbered script in this file.
26 * If you change the database schema, the following are required:
27 * 1. Update the file database.sql to match the new schema.
28 * 2. Update this file by adding a new function at the end with the number of the current DB_UPDATE_VERSION.
29 * This function should modify the current database schema and perform any other steps necessary
30 * to ensure that upgrade is silent and free from requiring interaction.
31 * 3. Increment the DB_UPDATE_VERSION in boot.php
32 * 4. TEST the upgrade prior to checkin and filing a pull request.
38 function update_1000() {
40 q("ALTER TABLE `item` DROP `like`, DROP `dislike` ");
42 q("ALTER TABLE `item` ADD `verb` CHAR( 255 ) NOT NULL AFTER `body` ,
43 ADD `object-type` CHAR( 255 ) NOT NULL AFTER `verb` ,
44 ADD `object` TEXT NOT NULL AFTER `object-type` ");
46 q("ALTER TABLE `intro` ADD `duplex` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `knowyou` ");
47 q("ALTER TABLE `contact` ADD `duplex` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `rel` ");
48 q("ALTER TABLE `contact` CHANGE `issued-pubkey` `issued-pubkey` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL");
49 q("ALTER TABLE `contact` ADD `term-date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `avatar-date`");
52 function update_1001() {
53 q("ALTER TABLE `item` ADD `wall` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `type` ");
54 q("ALTER TABLE `item` ADD INDEX ( `wall` )");
57 function update_1002() {
58 q("ALTER TABLE `item` ADD `gravity` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `wall` ");
61 function update_1003() {
62 q("ALTER TABLE `contact` DROP `issued-pubkey` , DROP `ret-id` , DROP `ret-pubkey` ");
63 q("ALTER TABLE `contact` ADD `usehub` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `ret-aes`");
64 q("ALTER TABLE `contact` ADD `hub-verify` CHAR( 255 ) NOT NULL AFTER `usehub`");
65 q("ALTER TABLE `contact` ADD INDEX ( `uid` ) , ADD INDEX ( `self` ), ADD INDEX ( `issued-id` ), ADD INDEX ( `dfrn-id` )");
66 q("ALTER TABLE `contact` ADD INDEX ( `blocked` ), ADD INDEX ( `readonly` )");
69 function update_1004() {
70 q("ALTER TABLE `contact` ADD `subhub` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `usehub`");
73 function update_1005() {
75 q("ALTER TABLE `user` ADD `spubkey` TEXT NOT NULL AFTER `prvkey` ,
76 ADD `sprvkey` TEXT NOT NULL AFTER `spubkey`");
80 function update_1006() {
82 // create 's' keys for everybody that does not have one
84 $r = q("SELECT * FROM `user` WHERE `spubkey` = '' ");
87 $sres=openssl_pkey_new(array('encrypt_key' => false ));
89 openssl_pkey_export($sres, $sprvkey);
90 $spkey = openssl_pkey_get_details($sres);
91 $spubkey = $spkey["key"];
92 $r = q("UPDATE `user` SET `spubkey` = '%s', `sprvkey` = '%s'
93 WHERE `uid` = %d LIMIT 1",
102 function update_1007() {
103 q("ALTER TABLE `user` ADD `page-flags` INT NOT NULL DEFAULT '0' AFTER `notify-flags`");
104 q("ALTER TABLE `user` ADD INDEX ( `nickname` )");
107 function update_1008() {
108 q("ALTER TABLE `profile` ADD `with` TEXT NOT NULL AFTER `marital` ");
111 function update_1009() {
112 q("ALTER TABLE `user` ADD `allow_location` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `default-location` ");
115 function update_1010() {
116 q("ALTER TABLE `contact` ADD `lrdd` CHAR( 255 ) NOT NULL AFTER `url` ");
119 function update_1011() {
120 q("ALTER TABLE `contact` ADD `nick` CHAR( 255 ) NOT NULL AFTER `name` ");
121 $r = q("SELECT * FROM `contact` WHERE 1");
124 q("UPDATE `contact` SET `nick` = '%s' WHERE `id` = %d LIMIT 1",
125 dbesc(basename($rr['url'])),
132 function update_1012() {
133 q("ALTER TABLE `item` ADD `inform` MEDIUMTEXT NOT NULL AFTER `tag` ");
136 function update_1013() {
137 q("ALTER TABLE `item` ADD `target-type` CHAR( 255 ) NOT NULL
138 AFTER `object` , ADD `target` TEXT NOT NULL AFTER `target-type`");
141 function update_1014() {
142 require_once('include/Photo.php');
143 q("ALTER TABLE `contact` ADD `micro` TEXT NOT NULL AFTER `thumb` ");
144 $r = q("SELECT * FROM `photo` WHERE `scale` = 4");
147 $ph = new Photo($rr['data']);
148 if($ph->is_valid()) {
150 $ph->store($rr['uid'],$rr['contact-id'],$rr['resource-id'],$rr['filename'],$rr['album'],6,(($rr['profile']) ? 1 : 0));
154 $r = q("SELECT * FROM `contact` WHERE 1");
157 if(stristr($rr['thumb'],'avatar'))
158 q("UPDATE `contact` SET `micro` = '%s' WHERE `id` = %d LIMIT 1",
159 dbesc(str_replace('avatar','micro',$rr['thumb'])),
162 q("UPDATE `contact` SET `micro` = '%s' WHERE `id` = %d LIMIT 1",
163 dbesc(str_replace('5.jpg','6.jpg',$rr['thumb'])),
169 function update_1015() {
170 q("ALTER TABLE `item` CHANGE `body` `body` mediumtext NOT NULL");
173 function update_1016() {
174 q("ALTER TABLE `user` ADD `openid` CHAR( 255 ) NOT NULL AFTER `email` ");
177 function update_1017() {
179 q(" CREATE TABLE IF NOT EXISTS `clients` (
180 `client_id` VARCHAR( 20 ) NOT NULL ,
181 `pw` VARCHAR( 20 ) NOT NULL ,
182 `redirect_uri` VARCHAR( 200 ) NOT NULL ,
183 PRIMARY KEY ( `client_id` )
184 ) ENGINE = MYISAM DEFAULT CHARSET=utf8 ");
186 q(" CREATE TABLE IF NOT EXISTS `tokens` (
187 `id` VARCHAR( 40 ) NOT NULL ,
188 `client_id` VARCHAR( 20 ) NOT NULL ,
189 `expires` INT NOT NULL ,
190 `scope` VARCHAR( 200 ) NOT NULL ,
192 ) ENGINE = MYISAM DEFAULT CHARSET=utf8 ");
194 q("CREATE TABLE IF NOT EXISTS `auth_codes` (
195 `id` VARCHAR( 40 ) NOT NULL ,
196 `client_id` VARCHAR( 20 ) NOT NULL ,
197 `redirect_uri` VARCHAR( 200 ) NOT NULL ,
198 `expires` INT NOT NULL ,
199 `scope` VARCHAR( 250 ) NOT NULL ,
201 ) ENGINE = MYISAM DEFAULT CHARSET=utf8 ");
205 function update_1018() {
206 q("CREATE TABLE IF NOT EXISTS `queue` (
207 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
209 `created` DATETIME NOT NULL ,
210 `last` DATETIME NOT NULL ,
211 `content` MEDIUMTEXT NOT NULL
212 ) ENGINE = MYISAM DEFAULT CHARSET=utf8 ");
215 function update_1019() {
216 q("ALTER TABLE `mail` DROP `delivered`");
217 q("ALTER TABLE `profile` ADD `showwith` TINYINT(1) NOT NULL DEFAULT '0' AFTER `marital` ");
220 function update_1020() {
221 q("ALTER TABLE `profile` DROP `showwith`");
222 q("ALTER TABLE `item` ADD `thr-parent` CHAR( 255 ) NOT NULL AFTER `parent-uri` ");
225 function update_1021() {
226 q("ALTER TABLE `profile_check` ADD `sec` CHAR( 255 ) NOT NULL AFTER `dfrn_id` ");
227 q("ALTER TABLE `profile_check` ADD `cid` INT(10) unsigned NOT NULL DEFAULT '0' AFTER `uid`");
228 q("ALTER TABLE `item` ADD `private` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `deny_gid` ");
231 function update_1022() {
232 q("CREATE TABLE `pconfig` (
233 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
234 `uid` INT NOT NULL DEFAULT '0',
235 `cat` CHAR( 255 ) NOT NULL ,
236 `k` CHAR( 255 ) NOT NULL ,
237 `v` MEDIUMTEXT NOT NULL
238 ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci ");
241 function update_1023() {
242 q("ALTER TABLE `user` ADD `register_date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `timezone` ,
243 ADD `login_date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `register_date` ");
246 function update_1024() {
247 q("ALTER TABLE `profile` ADD `keywords` TEXT NOT NULL AFTER `religion` ");
250 function update_1025() {
251 q("ALTER TABLE `user` ADD `maxreq` int(11) NOT NULL DEFAULT '10' AFTER `pwdreset` ");
254 function update_1026() {
255 q("CREATE TABLE IF NOT EXISTS `hook` (
256 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
257 `hook` CHAR( 255 ) NOT NULL ,
258 `file` CHAR( 255 ) NOT NULL ,
259 `function` CHAR( 255 ) NOT NULL
260 ) ENGINE = MYISAM DEFAULT CHARSET=utf8 ");
264 function update_1027() {
265 q("CREATE TABLE IF NOT EXISTS `addon` (
266 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
267 `name` CHAR( 255 ) NOT NULL ,
268 `version` CHAR( 255 ) NOT NULL ,
269 `installed` TINYINT( 1 ) NOT NULL DEFAULT '0'
270 ) ENGINE = MYISAM DEFAULT CHARSET=utf8 ");
273 function update_1028() {
274 q("ALTER TABLE `user` ADD `openidserver` text NOT NULL AFTER `deny_gid` ");
277 function update_1029() {
278 q("ALTER TABLE `contact` ADD `info` MEDIUMTEXT NOT NULL AFTER `reason` ");
281 function update_1030() {
282 q("ALTER TABLE `contact` ADD `bdyear` CHAR( 4 ) NOT NULL COMMENT 'birthday notify flag' AFTER `profile-id` ");
284 q("CREATE TABLE IF NOT EXISTS `event` (
285 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
288 `created` DATETIME NOT NULL ,
289 `edited` DATETIME NOT NULL ,
290 `start` DATETIME NOT NULL ,
291 `finish` DATETIME NOT NULL ,
292 `desc` TEXT NOT NULL ,
293 `location` TEXT NOT NULL ,
294 `type` CHAR( 255 ) NOT NULL ,
295 `adjust` TINYINT( 1 ) NOT NULL DEFAULT '1',
296 `allow_cid` MEDIUMTEXT NOT NULL ,
297 `allow_gid` MEDIUMTEXT NOT NULL ,
298 `deny_cid` MEDIUMTEXT NOT NULL ,
299 `deny_gid` MEDIUMTEXT NOT NULL
300 ) ENGINE = MYISAM DEFAULT CHARSET=utf8 ");
305 function update_1031() {
306 // Repair any bad links that slipped into the item table
307 $r = q("SELECT `id`, `object` FROM `item` WHERE `object` != '' ");
308 if($r && count($r)) {
310 if(strstr($rr['object'],'type="http')) {
311 q("UPDATE `item` SET `object` = '%s' WHERE `id` = %d LIMIT 1",
312 dbesc(str_replace('type="http','href="http',$rr['object'])),
320 function update_1032() {
321 q("ALTER TABLE `profile` ADD `pdesc` CHAR( 255 ) NOT NULL AFTER `name` ");
324 function update_1033() {
325 q("CREATE TABLE IF NOT EXISTS `cache` (
326 `k` CHAR( 255 ) NOT NULL PRIMARY KEY ,
328 `updated` DATETIME NOT NULL
329 ) ENGINE = MYISAM DEFAULT CHARSET=utf8;");
333 function update_1034() {
335 // If you have any of these parent-less posts they can cause problems, and
336 // we need to delete them. You can't see them anyway.
337 // Legitimate items will usually get re-created on the next
338 // pull from the hub.
339 // But don't get rid of a post that may have just come in
340 // and may not yet have the parent id set.
342 q("DELETE FROM `item` WHERE `parent` = 0 AND `created` < UTC_TIMESTAMP() - INTERVAL 2 MINUTE");
347 function update_1035() {
349 q("ALTER TABLE `contact` ADD `success_update` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `last-update` ");
353 function update_1036() {
355 $r = dbq("SELECT * FROM `contact` WHERE `network` = 'dfrn' && `photo` LIKE '%include/photo%' ");
358 q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `id` = %d LIMIT 1",
359 dbesc(str_replace('include/photo','photo',$rr['photo'])),
360 dbesc(str_replace('include/photo','photo',$rr['thumb'])),
361 dbesc(str_replace('include/photo','photo',$rr['micro'])),
367 function update_1037() {
369 q("ALTER TABLE `contact` CHANGE `lrdd` `alias` CHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ");
373 function update_1038() {
374 q("ALTER TABLE `item` ADD `plink` CHAR( 255 ) NOT NULL AFTER `target` ");
377 function update_1039() {
378 q("ALTER TABLE `addon` ADD `timestamp` BIGINT NOT NULL DEFAULT '0'");