]> git.mxchange.org Git - friendica.git/blob - update.php
Merge pull request #9557 from annando/fix-update
[friendica.git] / update.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  * Automatic post-databse structure change updates
21  *
22  * These functions are responsible for doing critical post update changes to the data (not the structure) in the database.
23  *
24  * Database structure changes are done in static/dbstructure.config.php
25  *
26  * For non-critical database migrations, please add a method in the Database\PostUpdate class
27  *
28  * If there is a need for a post update to a structure change, update this file
29  * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
30  *
31  * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
32  *
33  * Example:
34  * You are currently on version 4711 and you are preparing changes that demand an update script.
35  *
36  * 1. Create a function "update_4712()" here in the update.php
37  * 2. Apply the needed structural changes in static/dbStructure.php
38  * 3. Set DB_UPDATE_VERSION in static/dbstructure.config.php to 4712.
39  *
40  * If you need to run a script before the database update, name the function "pre_update_4712()"
41  */
42
43 use Friendica\Core\Addon;
44 use Friendica\Core\Logger;
45 use Friendica\Core\Update;
46 use Friendica\Core\Worker;
47 use Friendica\Database\Database;
48 use Friendica\Database\DBA;
49 use Friendica\Database\DBStructure;
50 use Friendica\DI;
51 use Friendica\Model\Contact;
52 use Friendica\Model\Item;
53 use Friendica\Model\Photo;
54 use Friendica\Model\User;
55 use Friendica\Model\Storage;
56 use Friendica\Util\DateTimeFormat;
57 use Friendica\Worker\Delivery;
58
59 function update_1179()
60 {
61         if (DI::config()->get('system', 'no_community_page')) {
62                 DI::config()->set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
63         }
64
65         // Update the central item storage with uid=0
66         Worker::add(PRIORITY_LOW, "threadupdate");
67
68         return Update::SUCCESS;
69 }
70
71 function update_1181()
72 {
73
74         // Fill the new fields in the term table.
75         // deactivated, the "term" table is deprecated
76         // Worker::add(PRIORITY_LOW, "TagUpdate");
77
78         return Update::SUCCESS;
79 }
80
81 function update_1189()
82 {
83
84         if (strlen(DI::config()->get('system', 'directory_submit_url')) &&
85                 !strlen(DI::config()->get('system', 'directory'))) {
86                 DI::config()->set('system', 'directory', dirname(DI::config()->get('system', 'directory_submit_url')));
87                 DI::config()->delete('system', 'directory_submit_url');
88         }
89
90         return Update::SUCCESS;
91 }
92
93 function update_1191()
94 {
95         DI::config()->set('system', 'maintenance', 1);
96
97         if (Addon::isEnabled('forumlist')) {
98                 Addon::uninstall('forumlist');
99         }
100
101         // select old formlist addon entries
102         $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
103                 DBA::escape('forumlist')
104         );
105
106         // convert old forumlist addon entries in new config entries
107         if (DBA::isResult($r)) {
108                 foreach ($r as $rr) {
109                         $uid = $rr['uid'];
110                         $family = $rr['cat'];
111                         $key = $rr['k'];
112                         $value = $rr['v'];
113
114                         if ($key === 'randomise') {
115                                 DI::pConfig()->delete($uid, $family, $key);
116                         }
117
118                         if ($key === 'show_on_profile') {
119                                 if ($value) {
120                                         DI::pConfig()->set($uid, 'feature', 'forumlist_profile', $value);
121                                 }
122
123                                 DI::pConfig()->delete($uid, $family, $key);
124                         }
125
126                         if ($key === 'show_on_network') {
127                                 if ($value) {
128                                         DI::pConfig()->set($uid, 'feature', 'forumlist_widget', $value);
129                                 }
130
131                                 DI::pConfig()->delete($uid, $family, $key);
132                         }
133                 }
134         }
135
136         DI::config()->set('system', 'maintenance', 0);
137
138         return Update::SUCCESS;
139 }
140
141 function update_1203()
142 {
143         $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
144                 DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
145                 DBA::escape(User::PAGE_FLAGS_COMMUNITY),
146                 DBA::escape(User::PAGE_FLAGS_PRVGROUP)
147         );
148 }
149
150 function update_1244()
151 {
152         // Sets legacy_password for all legacy hashes
153         DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
154
155         // All legacy hashes are re-hashed using the new secure hashing function
156         $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
157         while ($user = DBA::fetch($stmt)) {
158                 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
159         }
160
161         // Logged in users are forcibly logged out
162         DBA::delete('session', ['1 = 1']);
163
164         return Update::SUCCESS;
165 }
166
167 function update_1245()
168 {
169         $rino = DI::config()->get('system', 'rino_encrypt');
170
171         if (!$rino) {
172                 return Update::SUCCESS;
173         }
174
175         DI::config()->set('system', 'rino_encrypt', 1);
176
177         return Update::SUCCESS;
178 }
179
180 function update_1247()
181 {
182         // Removing hooks with the old name
183         DBA::e("DELETE FROM `hook`
184 WHERE `hook` LIKE 'plugin_%'");
185
186         // Make sure we install the new renamed ones
187         Addon::reload();
188 }
189
190 function update_1260()
191 {
192         DI::config()->set('system', 'maintenance', 1);
193         DI::config()->set(
194                 'system',
195                 'maintenance_reason',
196                 DI::l10n()->t(
197                         '%s: Updating author-id and owner-id in item and thread table. ',
198                         DateTimeFormat::utcNow().' '.date('e')
199                 )
200         );
201
202         $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
203                 WHERE `owner-id` = 0 AND `owner-link` != ''");
204         while ($item = DBA::fetch($items)) {
205                 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
206                         'photo' => $item['owner-avatar'], 'network' => $item['network']];
207                 $cid = Contact::getIdForURL($item['owner-link'], 0, null, $contact);
208                 if (empty($cid)) {
209                         continue;
210                 }
211                 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
212         }
213         DBA::close($items);
214
215         DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
216                 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
217
218         $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
219                 WHERE `author-id` = 0 AND `author-link` != ''");
220         while ($item = DBA::fetch($items)) {
221                 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
222                         'photo' => $item['author-avatar'], 'network' => $item['network']];
223                 $cid = Contact::getIdForURL($item['author-link'], 0, null, $contact);
224                 if (empty($cid)) {
225                         continue;
226                 }
227                 Item::update(['author-id' => $cid], ['id' => $item['id']]);
228         }
229         DBA::close($items);
230
231         DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
232                 SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
233
234         DI::config()->set('system', 'maintenance', 0);
235         return Update::SUCCESS;
236 }
237
238 function update_1261()
239 {
240         // This fixes the results of an issue in the develop branch of 2018-05.
241         DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
242         return Update::SUCCESS;
243 }
244
245 function update_1278()
246 {
247         DI::config()->set('system', 'maintenance', 1);
248         DI::config()->set(
249                 'system',
250                 'maintenance_reason',
251                 DI::l10n()->t(
252                         '%s: Updating post-type.',
253                         DateTimeFormat::utcNow().' '.date('e')
254                 )
255         );
256
257         Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
258         Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
259
260         DI::config()->set('system', 'maintenance', 0);
261
262         return Update::SUCCESS;
263 }
264
265 function update_1288()
266 {
267         // Updates missing `uri-id` values
268
269         DBA::e("UPDATE `item-activity` INNER JOIN `item` ON `item`.`iaid` = `item-activity`.`id` SET `item-activity`.`uri-id` = `item`.`uri-id` WHERE `item-activity`.`uri-id` IS NULL OR `item-activity`.`uri-id` = 0");
270         DBA::e("UPDATE `item-content` INNER JOIN `item` ON `item`.`icid` = `item-content`.`id` SET `item-content`.`uri-id` = `item`.`uri-id` WHERE `item-content`.`uri-id` IS NULL OR `item-content`.`uri-id` = 0");
271
272         return Update::SUCCESS;
273 }
274
275 // Post-update script of PR 5751
276 function update_1298()
277 {
278         $keys = ['gender', 'marital', 'sexual'];
279         foreach ($keys as $translateKey) {
280                 $allData = DBA::select('profile', ['id', $translateKey]);
281                 $allLangs = DI::l10n()->getAvailableLanguages();
282                 $success = 0;
283                 $fail = 0;
284                 foreach ($allData as $key => $data) {
285                         $toTranslate = $data[$translateKey];
286                         if ($toTranslate != '') {
287                                 foreach ($allLangs as $key => $lang) {
288                                         $a = new \stdClass();
289                                         $a->strings = [];
290
291                                         // First we get the the localizations
292                                         if (file_exists("view/lang/$lang/strings.php")) {
293                                                 include "view/lang/$lang/strings.php";
294                                         }
295                                         if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
296                                                 include "addon/morechoice/lang/$lang/strings.php";
297                                         }
298
299                                         $localizedStrings = $a->strings;
300                                         unset($a);
301
302                                         $key = array_search($toTranslate, $localizedStrings);
303                                         if ($key !== false) {
304                                                 break;
305                                         }
306
307                                         // defaulting to empty string
308                                         $key = '';
309                                 }
310
311                                 if ($key == '') {
312                                         $fail++;
313                                 } else {
314                                         DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
315                                         Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
316                                                 'was' => $data[$translateKey]]);
317                                         Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
318                                         Contact::updateSelfFromUserID($data['id']);
319                                         $success++;
320                                 }
321                         }
322                 }
323
324                 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
325         }
326         return Update::SUCCESS;
327 }
328
329 function update_1309()
330 {
331         $queue = DBA::select('queue', ['id', 'cid', 'guid']);
332         while ($entry = DBA::fetch($queue)) {
333                 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
334                 if (!DBA::isResult($contact)) {
335                         continue;
336                 }
337
338                 $item = Item::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
339                 if (!DBA::isResult($item)) {
340                         continue;
341                 }
342
343                 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
344                 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
345                 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
346                 DBA::delete('queue', ['id' => $entry['id']]);
347         }
348         return Update::SUCCESS;
349 }
350
351 function update_1315()
352 {
353         if (DBStructure::existsTable('item-delivery-data')) {
354                 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
355         }
356         return Update::SUCCESS;
357 }
358
359 function update_1318()
360 {
361         DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
362         DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
363
364         Worker::add(PRIORITY_LOW, 'ProfileUpdate');
365         return Update::SUCCESS;
366 }
367
368 function update_1323()
369 {
370         $users = DBA::select('user', ['uid']);
371         while ($user = DBA::fetch($users)) {
372                 Contact::updateSelfFromUserID($user['uid']);
373         }
374         DBA::close($users);
375
376         return Update::SUCCESS;
377 }
378
379 function update_1327()
380 {
381         $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
382         while ($contact = DBA::fetch($contacts)) {
383                 Contact\User::setBlocked($contact['id'], $contact['uid'], $contact['blocked']);
384                 Contact\User::setIgnored($contact['id'], $contact['uid'], $contact['readonly']);
385         }
386         DBA::close($contacts);
387
388         return Update::SUCCESS;
389 }
390
391 function update_1330()
392 {
393         $currStorage = DI::config()->get('storage', 'class', '');
394
395         // set the name of the storage instead of the classpath as config
396         if (!empty($currStorage)) {
397                 /** @var Storage\IStorage $currStorage */
398                 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
399                         return Update::FAILED;
400                 }
401
402                 // try to delete the class since it isn't needed. This won't work with config files
403                 DI::config()->delete('storage', 'class');
404         }
405
406         // Update attachments and photos
407         if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
408             !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
409                 return Update::FAILED;
410         };
411
412         return Update::SUCCESS;
413 }
414
415 function update_1332()
416 {
417         $condition = ["`is-default` IS NOT NULL"];
418         $profiles = DBA::select('profile', [], $condition);
419
420         while ($profile = DBA::fetch($profiles)) {
421                 DI::profileField()->migrateFromLegacyProfile($profile);
422         }
423         DBA::close($profiles);
424
425         DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
426
427         return Update::SUCCESS;
428 }
429
430 function update_1347()
431 {
432         foreach (Item::ACTIVITIES as $index => $activity) {
433                 DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
434         }
435
436         return Update::SUCCESS;
437 }
438
439 function pre_update_1348()
440 {
441         if (!DBA::exists('contact', ['id' => 0])) {
442                 DBA::insert('contact', ['nurl' => '']);
443                 $lastid = DBA::lastInsertId();
444                 if ($lastid != 0) {
445                         DBA::update('contact', ['id' => 0], ['id' => $lastid]);
446                 }
447         }
448
449         // The tables "permissionset" and "tag" could or could not exist during the update.
450         // This depends upon the previous version. Depending upon this situation we have to add
451         // the "0" values before adding the foreign keys - or after would be sufficient.
452
453         update_1348();
454
455         DBA::e("DELETE FROM `auth_codes` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
456         DBA::e("DELETE FROM `tokens` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
457
458         return Update::SUCCESS;
459 }
460
461 function update_1348()
462 {
463         // Insert a permissionset with id=0
464         // Inserting it without an ID and then changing the value to 0 tricks the auto increment
465         if (!DBA::exists('permissionset', ['id' => 0])) {
466                 DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);       
467                 $lastid = DBA::lastInsertId();
468                 if ($lastid != 0) {
469                         DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
470                 }
471         }
472
473         if (!DBA::exists('tag', ['id' => 0])) {
474                 DBA::insert('tag', ['name' => '']);
475                 $lastid = DBA::lastInsertId();
476                 if ($lastid != 0) {
477                         DBA::update('tag', ['id' => 0], ['id' => $lastid]);
478                 }
479         }
480
481         return Update::SUCCESS;
482 }
483
484 function update_1349()
485 {
486         $correct = true;
487         foreach (Item::ACTIVITIES as $index => $activity) {
488                 if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
489                         $correct = false;
490                 }
491         }
492
493         if (!$correct) {
494                 // The update failed - but it cannot be recovered, since the data doesn't match our expectation
495                 // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
496                 // the postupdate. This is not fatal, but means that it will take some longer time for the system
497                 // to fill all data.
498                 return Update::SUCCESS;
499         }
500
501         if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
502                 SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", GRAVITY_ACTIVITY)) {
503                 return Update::FAILED;
504         }
505
506         return Update::SUCCESS;
507 }
508
509 function update_1351()
510 {
511         if (!DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
512                 return Update::FAILED;
513         }
514
515         return Update::SUCCESS;
516 }
517
518 function pre_update_1354()
519 {
520         if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
521                 && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
522                 && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
523                 return Update::FAILED;
524         }
525         return Update::SUCCESS;
526 }
527
528 function update_1354()
529 {
530         if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
531                 && DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])) {
532                 if (!DBA::e("UPDATE `contact` SET `ffi_keyword_denylist` = `ffi_keyword_blacklist`")) {
533                         return Update::FAILED;
534                 }
535
536                 // When the data had been copied then the main task is done.
537                 // Having the old field removed is only beauty but not crucial.
538                 // So we don't care if this was successful or not.
539                 DBA::e("ALTER TABLE `contact` DROP `ffi_keyword_blacklist`");
540         }
541         return Update::SUCCESS;
542 }
543
544 function update_1357()
545 {
546         if (!DBA::e("UPDATE `contact` SET `failed` = true WHERE `success_update` < `failure_update` AND `failed` IS NULL")) {
547                 return Update::FAILED;
548         }
549
550         if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `success_update` > `failure_update` AND `failed` IS NULL")) {
551                 return Update::FAILED;
552         }
553
554         if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `updated` > `failure_update` AND `failed` IS NULL")) {
555                 return Update::FAILED;
556         }
557
558         if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `last-item` > `failure_update` AND `failed` IS NULL")) {
559                 return Update::FAILED;
560         }
561
562         if (!DBA::e("UPDATE `gserver` SET `failed` = true WHERE `last_contact` < `last_failure` AND `failed` IS NULL")) {
563                 return Update::FAILED;
564         }
565
566         if (!DBA::e("UPDATE `gserver` SET `failed` = false WHERE `last_contact` > `last_failure` AND `failed` IS NULL")) {
567                 return Update::FAILED;
568         }
569
570         return Update::SUCCESS;
571 }
572
573 function pre_update_1358()
574 {
575         if (!DBA::e("DELETE FROM `contact-relation` WHERE NOT `relation-cid` IN (SELECT `id` FROM `contact`) OR NOT `cid` IN (SELECT `id` FROM `contact`)")) {
576                 return Update::FAILED;
577         }
578
579         return Update::SUCCESS;
580 }
581
582 function pre_update_1363()
583 {
584         Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
585         return Update::SUCCESS;
586 }
587
588 function pre_update_1364()
589 {
590         if (!DBA::e("DELETE FROM `2fa_recovery_codes` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
591                 return Update::FAILED;
592         }
593
594         if (!DBA::e("DELETE FROM `2fa_app_specific_password` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
595                 return Update::FAILED;
596         }
597
598         if (!DBA::e("DELETE FROM `attach` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
599                 return Update::FAILED;
600         }
601
602         if (!DBA::e("DELETE FROM `clients` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
603                 return Update::FAILED;
604         }
605
606         if (!DBA::e("DELETE FROM `conv` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
607                 return Update::FAILED;
608         }
609
610         if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
611                 return Update::FAILED;
612         }
613
614         if (!DBA::e("DELETE FROM `group` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
615                 return Update::FAILED;
616         }
617
618         if (!DBA::e("DELETE FROM `intro` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
619                 return Update::FAILED;
620         }
621
622         if (!DBA::e("DELETE FROM `manage` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
623                 return Update::FAILED;
624         }
625
626         if (!DBA::e("DELETE FROM `manage` WHERE NOT `mid` IN (SELECT `uid` FROM `user`)")) {
627                 return Update::FAILED;
628         }
629
630         if (!DBA::e("DELETE FROM `mail` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
631                 return Update::FAILED;
632         }
633
634         if (!DBA::e("DELETE FROM `mailacct` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
635                 return Update::FAILED;
636         }
637
638         if (!DBA::e("DELETE FROM `notify` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
639                 return Update::FAILED;
640         }
641
642         if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
643                 return Update::FAILED;
644         }
645
646         if (!DBA::e("DELETE FROM `pconfig` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
647                 return Update::FAILED;
648         }
649
650         if (!DBA::e("DELETE FROM `profile` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
651                 return Update::FAILED;
652         }
653
654         if (!DBA::e("DELETE FROM `profile_check` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
655                 return Update::FAILED;
656         }
657
658         if (!DBA::e("DELETE FROM `profile_field` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
659                 return Update::FAILED;
660         }
661
662         if (!DBA::e("DELETE FROM `push_subscriber` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
663                 return Update::FAILED;
664         }
665
666         if (!DBA::e("DELETE FROM `register` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
667                 return Update::FAILED;
668         }
669
670         if (!DBA::e("DELETE FROM `search` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
671                 return Update::FAILED;
672         }
673
674         if (!DBA::e("DELETE FROM `tokens` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
675                 return Update::FAILED;
676         }
677
678         if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
679                 return Update::FAILED;
680         }
681
682         if (!DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
683                 return Update::FAILED;
684         }
685
686         if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `receiver-uid` IN (SELECT `uid` FROM `user`)")) {
687                 return Update::FAILED;
688         }
689
690         if (!DBA::e("DELETE FROM `event` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
691                 return Update::FAILED;
692         }
693
694         if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
695                 return Update::FAILED;
696         }
697
698         if (!DBA::e("DELETE FROM `group_member` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
699                 return Update::FAILED;
700         }
701
702         if (!DBA::e("DELETE FROM `intro` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
703                 return Update::FAILED;
704         }
705
706         if (!DBA::e("DELETE FROM `participation` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
707                 return Update::FAILED;
708         }
709
710         if (!DBA::e("DELETE FROM `profile_check` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
711                 return Update::FAILED;
712         }
713
714         if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
715                 return Update::FAILED;
716         }
717
718         if (!DBA::e("DELETE FROM `participation` WHERE NOT `fid` IN (SELECT `id` FROM `fcontact`)")) {
719                 return Update::FAILED;
720         }
721
722         if (!DBA::e("DELETE FROM `group_member` WHERE NOT `gid` IN (SELECT `id` FROM `group`)")) {
723                 return Update::FAILED;
724         }
725
726         if (!DBA::e("DELETE FROM `gserver-tag` WHERE NOT `gserver-id` IN (SELECT `id` FROM `gserver`)")) {
727                 return Update::FAILED;
728         }
729
730         if (!DBA::e("DELETE FROM `participation` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
731                 return Update::FAILED;
732         }
733
734         if (!DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
735                 return Update::FAILED;
736         }
737
738         return Update::SUCCESS;
739 }
740
741 function pre_update_1365()
742 {
743         if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `notify-id` IN (SELECT `id` FROM `notify`)")) {
744                 return Update::FAILED;
745         }
746
747         if (!DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
748                 return Update::FAILED;
749         }
750
751         return Update::SUCCESS;
752 }
753
754 function update_1375()
755 {
756         if (!DBA::e("UPDATE `item` SET `thr-parent` = `parent-uri`, `thr-parent-id` = `parent-uri-id` WHERE `thr-parent` = ''")) {
757                 return Update::FAILED;
758         }
759
760         return Update::SUCCESS;
761 }
762
763 function pre_update_1376()
764 {
765         // Insert a user with uid=0
766         DBStructure::checkInitialValues();
767
768         if (!DBA::e("DELETE FROM `item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
769                 return Update::FAILED;
770         }
771
772         if (!DBA::e("DELETE FROM `event` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
773                 return Update::FAILED;
774         }
775
776         if (!DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
777                 return Update::FAILED;
778         }
779
780         if (!DBA::e("DELETE FROM `permissionset` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
781                 return Update::FAILED;
782         }
783
784         if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
785                 return Update::FAILED;
786         }
787
788         if (!DBA::e("DELETE FROM `post-category` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
789                 return Update::FAILED;
790         }
791
792         Photo::delete(["NOT `uid` IN (SELECT `uid` FROM `user`)"]);
793
794         if (!DBA::e("DELETE FROM `contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
795                 return Update::FAILED;
796         }
797
798         return Update::SUCCESS;
799 }
800
801 function pre_update_1377()
802 {
803         DBStructure::checkInitialValues();
804
805         if (!DBA::e("DELETE FROM `item` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
806                 return Update::FAILED;
807         }
808
809         if (!DBA::e("DELETE FROM `item` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
810                 return Update::FAILED;
811         }
812
813         if (!DBA::e("UPDATE `item` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
814                 return Update::FAILED;
815         }
816
817         if (!DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
818                 return Update::FAILED;
819         }
820
821         if (!DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
822                 return Update::FAILED;
823         }
824
825         if (!DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
826                 return Update::FAILED;
827         }
828
829         if (!DBA::e("UPDATE `notify` SET `uri-id` = NULL WHERE `uri-id` = 0")) {
830                 return Update::FAILED;
831         }
832
833         if (!DBA::e("DELETE FROM `notify` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
834                 return Update::FAILED;
835         }
836
837         if (!DBA::e("UPDATE `notify` SET `parent-uri-id` = NULL WHERE `parent-uri-id` = 0")) {
838                 return Update::FAILED;
839         }
840
841         if (!DBA::e("DELETE FROM `notify` WHERE `parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
842                 return Update::FAILED;
843         }
844
845         if (!DBA::e("UPDATE `notify-threads` SET `master-parent-uri-id` = NULL WHERE `master-parent-uri-id` = 0")) {
846                 return Update::FAILED;
847         }
848
849         if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
850                 return Update::FAILED;
851         }
852
853         if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-item` NOT IN (SELECT `id` FROM `item`)")) {
854                 return Update::FAILED;
855         }
856
857         return Update::SUCCESS;
858 }