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