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