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