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