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