Merge remote-tracking branch 'upstream/develop' into diaspora-item
[friendica.git/.git] / update.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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-database 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\Config\ValueObject\Cache;
44 use Friendica\Core\Logger;
45 use Friendica\Core\Protocol;
46 use Friendica\Core\Storage\Capability\ICanReadFromStorage;
47 use Friendica\Core\Storage\Type\Database as DatabaseStorage;
48 use Friendica\Core\Update;
49 use Friendica\Core\Worker;
50 use Friendica\Database\Database;
51 use Friendica\Database\DBA;
52 use Friendica\Database\DBStructure;
53 use Friendica\DI;
54 use Friendica\Model\Contact;
55 use Friendica\Model\Item;
56 use Friendica\Model\ItemURI;
57 use Friendica\Model\Notification;
58 use Friendica\Model\Photo;
59 use Friendica\Model\Post;
60 use Friendica\Model\Profile;
61 use Friendica\Model\User;
62 use Friendica\Protocol\Delivery;
63 use Friendica\Security\PermissionSet\Repository\PermissionSet;
64
65 // Post-update script of PR 5751
66 function update_1298()
67 {
68         $keys = ['gender', 'marital', 'sexual'];
69         foreach ($keys as $translateKey) {
70                 $allData = DBA::select('profile', ['id', $translateKey]);
71                 $allLangs = DI::l10n()->getAvailableLanguages();
72                 $success = 0;
73                 $fail = 0;
74                 foreach ($allData as $key => $data) {
75                         $toTranslate = $data[$translateKey];
76                         if ($toTranslate != '') {
77                                 foreach ($allLangs as $key => $lang) {
78                                         $a = new \stdClass();
79                                         $a->strings = [];
80
81                                         // First we get the the localizations
82                                         if (file_exists('view/lang/$lang/strings.php')) {
83                                                 include 'view/lang/$lang/strings.php';
84                                         }
85                                         if (file_exists('addon/morechoice/lang/$lang/strings.php')) {
86                                                 include 'addon/morechoice/lang/$lang/strings.php';
87                                         }
88
89                                         $localizedStrings = $a->strings;
90                                         unset($a);
91
92                                         $key = array_search($toTranslate, $localizedStrings);
93                                         if ($key !== false) {
94                                                 break;
95                                         }
96
97                                         // defaulting to empty string
98                                         $key = '';
99                                 }
100
101                                 if ($key == '') {
102                                         $fail++;
103                                 } else {
104                                         DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
105                                         Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
106                                                 'was' => $data[$translateKey]]);
107
108                                         Contact::updateSelfFromUserID($data['id']);
109                                         Profile::publishUpdate($data['id']);
110                                         $success++;
111                                 }
112                         }
113                 }
114
115                 Logger::notice($translateKey . ' fix completed', ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
116         }
117         return Update::SUCCESS;
118 }
119
120 function update_1309()
121 {
122         $queue = DBA::select('queue', ['id', 'cid', 'guid']);
123         while ($entry = DBA::fetch($queue)) {
124                 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
125                 if (!DBA::isResult($contact)) {
126                         continue;
127                 }
128
129                 $item = Post::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
130                 if (!DBA::isResult($item)) {
131                         continue;
132                 }
133
134                 $deliver_options = ['priority' => Worker::PRIORITY_MEDIUM, 'dont_fork' => true];
135                 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
136                 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
137                 DBA::delete('queue', ['id' => $entry['id']]);
138         }
139         return Update::SUCCESS;
140 }
141
142 function update_1315()
143 {
144         if (DBStructure::existsTable('item-delivery-data')) {
145                 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
146         }
147         return Update::SUCCESS;
148 }
149
150 function update_1318()
151 {
152         DBA::update('profile', ['marital' => 'In a relation'], ['marital' => 'Unavailable']);
153         DBA::update('profile', ['marital' => 'Single'], ['marital' => 'Available']);
154
155         Worker::add(Worker::PRIORITY_LOW, 'ProfileUpdate');
156         return Update::SUCCESS;
157 }
158
159 function update_1323()
160 {
161         $users = DBA::select('user', ['uid']);
162         while ($user = DBA::fetch($users)) {
163                 if (Contact::updateSelfFromUserID($user['uid'])) {
164                         Profile::publishUpdate($user['uid']);
165                 }
166         }
167         DBA::close($users);
168
169         return Update::SUCCESS;
170 }
171
172 function update_1327()
173 {
174         $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
175         while ($contact = DBA::fetch($contacts)) {
176                 Contact\User::setBlocked($contact['id'], $contact['uid'], $contact['blocked']);
177                 Contact\User::setIgnored($contact['id'], $contact['uid'], $contact['readonly']);
178         }
179         DBA::close($contacts);
180
181         return Update::SUCCESS;
182 }
183
184 function update_1330()
185 {
186         $currStorage = DI::config()->get('storage', 'class', '');
187
188         // set the name of the storage instead of the classpath as config
189         if (!empty($currStorage)) {
190                 /** @var ICanReadFromStorage $currStorage */
191                 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
192                         return Update::FAILED;
193                 }
194
195                 // try to delete the class since it isn't needed. This won't work with config files
196                 DI::config()->delete('storage', 'class');
197         }
198
199         // Update attachments and photos
200         if (!DBA::e("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
201             !DBA::e("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
202                 return Update::FAILED;
203         };
204
205         return Update::SUCCESS;
206 }
207
208 function update_1332()
209 {
210         $condition = ["`is-default` IS NOT NULL"];
211         $profiles = DBA::select('profile', [], $condition);
212
213         while ($profile = DBA::fetch($profiles)) {
214                 Profile::migrate($profile);
215         }
216         DBA::close($profiles);
217
218         DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
219
220         return Update::SUCCESS;
221 }
222
223 function update_1347()
224 {
225         foreach (Item::ACTIVITIES as $index => $activity) {
226                 DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
227         }
228
229         return Update::SUCCESS;
230 }
231
232 function pre_update_1348()
233 {
234         if (!DBA::exists('contact', ['id' => 0])) {
235                 DBA::insert('contact', ['nurl' => '']);
236                 $lastid = DBA::lastInsertId();
237                 if ($lastid != 0) {
238                         DBA::update('contact', ['id' => 0], ['id' => $lastid]);
239                 }
240         }
241
242         // The tables "permissionset" and "tag" could or could not exist during the update.
243         // This depends upon the previous version. Depending upon this situation we have to add
244         // the "0" values before adding the foreign keys - or after would be sufficient.
245
246         update_1348();
247
248         if (DBStructure::existsTable('auth_codes') && DBStructure::existsTable('clients')) {
249                 DBA::e("DELETE FROM `auth_codes` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
250         }
251         if (DBStructure::existsTable('tokens') && DBStructure::existsTable('clients')) {
252                 DBA::e("DELETE FROM `tokens` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
253         }
254         return Update::SUCCESS;
255 }
256
257 function update_1348()
258 {
259         // Insert a permissionset with id=0
260         // Inserting it without an ID and then changing the value to 0 tricks the auto increment
261         if (!DBA::exists('permissionset', ['id' => 0])) {
262                 DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
263                 $lastid = DBA::lastInsertId();
264                 if ($lastid != 0) {
265                         DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
266                 }
267         }
268
269         if (!DBA::exists('tag', ['id' => 0])) {
270                 DBA::insert('tag', ['name' => '']);
271                 $lastid = DBA::lastInsertId();
272                 if ($lastid != 0) {
273                         DBA::update('tag', ['id' => 0], ['id' => $lastid]);
274                 }
275         }
276
277         return Update::SUCCESS;
278 }
279
280 function update_1349()
281 {
282         if (!DBStructure::existsTable('item-activity')) {
283                 return Update::SUCCESS;
284         }
285
286         $correct = true;
287         foreach (Item::ACTIVITIES as $index => $activity) {
288                 if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
289                         $correct = false;
290                 }
291         }
292
293         if (!$correct) {
294                 // The update failed - but it cannot be recovered, since the data doesn't match our expectation
295                 // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
296                 // the postupdate. This is not fatal, but means that it will take some longer time for the system
297                 // to fill all data.
298                 return Update::SUCCESS;
299         }
300
301         if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
302                 SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", Item::GRAVITY_ACTIVITY)) {
303                 return Update::FAILED;
304         }
305
306         return Update::SUCCESS;
307 }
308
309 function update_1351()
310 {
311         if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
312                 return Update::FAILED;
313         }
314
315         return Update::SUCCESS;
316 }
317
318 function pre_update_1354()
319 {
320         if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
321                 && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
322                 && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
323                 return Update::FAILED;
324         }
325         return Update::SUCCESS;
326 }
327
328 function update_1354()
329 {
330         if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
331                 && DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])) {
332                 if (!DBA::e("UPDATE `contact` SET `ffi_keyword_denylist` = `ffi_keyword_blacklist`")) {
333                         return Update::FAILED;
334                 }
335
336                 // When the data had been copied then the main task is done.
337                 // Having the old field removed is only beauty but not crucial.
338                 // So we don't care if this was successful or not.
339                 DBA::e("ALTER TABLE `contact` DROP `ffi_keyword_blacklist`");
340         }
341         return Update::SUCCESS;
342 }
343
344 function update_1357()
345 {
346         if (!DBA::e("UPDATE `contact` SET `failed` = true WHERE `success_update` < `failure_update` AND `failed` IS NULL")) {
347                 return Update::FAILED;
348         }
349
350         if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `success_update` > `failure_update` AND `failed` IS NULL")) {
351                 return Update::FAILED;
352         }
353
354         if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `updated` > `failure_update` AND `failed` IS NULL")) {
355                 return Update::FAILED;
356         }
357
358         if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `last-item` > `failure_update` AND `failed` IS NULL")) {
359                 return Update::FAILED;
360         }
361
362         if (!DBA::e("UPDATE `gserver` SET `failed` = true WHERE `last_contact` < `last_failure` AND `failed` IS NULL")) {
363                 return Update::FAILED;
364         }
365
366         if (!DBA::e("UPDATE `gserver` SET `failed` = false WHERE `last_contact` > `last_failure` AND `failed` IS NULL")) {
367                 return Update::FAILED;
368         }
369
370         return Update::SUCCESS;
371 }
372
373 function pre_update_1358()
374 {
375         if (!DBA::e("DELETE FROM `contact-relation` WHERE NOT `relation-cid` IN (SELECT `id` FROM `contact`) OR NOT `cid` IN (SELECT `id` FROM `contact`)")) {
376                 return Update::FAILED;
377         }
378
379         return Update::SUCCESS;
380 }
381
382 function pre_update_1363()
383 {
384         Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
385         return Update::SUCCESS;
386 }
387
388 function pre_update_1364()
389 {
390         if (!DBA::e("DELETE FROM `2fa_recovery_codes` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
391                 return Update::FAILED;
392         }
393
394         if (!DBA::e("DELETE FROM `2fa_app_specific_password` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
395                 return Update::FAILED;
396         }
397
398         if (!DBA::e("DELETE FROM `attach` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
399                 return Update::FAILED;
400         }
401
402         if (DBStructure::existsTable('clients') && !DBA::e("DELETE FROM `clients` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
403                 return Update::FAILED;
404         }
405
406         if (!DBA::e("DELETE FROM `conv` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
407                 return Update::FAILED;
408         }
409
410         if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
411                 return Update::FAILED;
412         }
413
414         if (!DBA::e("DELETE FROM `group` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
415                 return Update::FAILED;
416         }
417
418         if (!DBA::e("DELETE FROM `intro` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
419                 return Update::FAILED;
420         }
421
422         if (!DBA::e("DELETE FROM `manage` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
423                 return Update::FAILED;
424         }
425
426         if (!DBA::e("DELETE FROM `manage` WHERE NOT `mid` IN (SELECT `uid` FROM `user`)")) {
427                 return Update::FAILED;
428         }
429
430         if (!DBA::e("DELETE FROM `mail` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
431                 return Update::FAILED;
432         }
433
434         if (!DBA::e("DELETE FROM `mailacct` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
435                 return Update::FAILED;
436         }
437
438         if (!DBA::e("DELETE FROM `notify` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
439                 return Update::FAILED;
440         }
441
442         if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
443                 return Update::FAILED;
444         }
445
446         if (!DBA::e("DELETE FROM `pconfig` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
447                 return Update::FAILED;
448         }
449
450         if (!DBA::e("DELETE FROM `profile` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
451                 return Update::FAILED;
452         }
453
454         if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
455                 return Update::FAILED;
456         }
457
458         if (!DBA::e("DELETE FROM `profile_field` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
459                 return Update::FAILED;
460         }
461
462         if (!DBA::e("DELETE FROM `push_subscriber` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
463                 return Update::FAILED;
464         }
465
466         if (!DBA::e("DELETE FROM `register` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
467                 return Update::FAILED;
468         }
469
470         if (!DBA::e("DELETE FROM `search` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
471                 return Update::FAILED;
472         }
473
474         if (DBStructure::existsTable('tokens') && !DBA::e("DELETE FROM `tokens` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
475                 return Update::FAILED;
476         }
477
478         if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
479                 return Update::FAILED;
480         }
481
482         if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
483                 return Update::FAILED;
484         }
485
486         if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `receiver-uid` IN (SELECT `uid` FROM `user`)")) {
487                 return Update::FAILED;
488         }
489
490         if (!DBA::e("DELETE FROM `event` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
491                 return Update::FAILED;
492         }
493
494         if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
495                 return Update::FAILED;
496         }
497
498         if (!DBA::e("DELETE FROM `group_member` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
499                 return Update::FAILED;
500         }
501
502         if (!DBA::e("DELETE FROM `intro` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
503                 return Update::FAILED;
504         }
505
506         if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
507                 return Update::FAILED;
508         }
509
510         if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
511                 return Update::FAILED;
512         }
513
514         if (!DBA::e("DELETE FROM `group_member` WHERE NOT `gid` IN (SELECT `id` FROM `group`)")) {
515                 return Update::FAILED;
516         }
517
518         if (!DBA::e("DELETE FROM `gserver-tag` WHERE NOT `gserver-id` IN (SELECT `id` FROM `gserver`)")) {
519                 return Update::FAILED;
520         }
521
522         if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
523                 return Update::FAILED;
524         }
525
526         return Update::SUCCESS;
527 }
528
529 function pre_update_1365()
530 {
531         if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `notify-id` IN (SELECT `id` FROM `notify`)")) {
532                 return Update::FAILED;
533         }
534
535         if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
536                 return Update::FAILED;
537         }
538
539         return Update::SUCCESS;
540 }
541
542 function update_1375()
543 {
544         if (!DBA::e("UPDATE `item` SET `thr-parent` = `parent-uri`, `thr-parent-id` = `parent-uri-id` WHERE `thr-parent` = ''")) {
545                 return Update::FAILED;
546         }
547
548         return Update::SUCCESS;
549 }
550
551 function pre_update_1376()
552 {
553         // Insert a user with uid=0
554         DBStructure::checkInitialValues();
555
556         if (!DBA::e("DELETE FROM `item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
557                 return Update::FAILED;
558         }
559
560         if (!DBA::e("DELETE FROM `event` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
561                 return Update::FAILED;
562         }
563
564         if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
565                 return Update::FAILED;
566         }
567
568         if (!DBA::e("DELETE FROM `permissionset` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
569                 return Update::FAILED;
570         }
571
572         if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
573                 return Update::FAILED;
574         }
575
576         if (!DBA::e("DELETE FROM `post-category` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
577                 return Update::FAILED;
578         }
579
580         Photo::delete(["NOT `uid` IN (SELECT `uid` FROM `user`)"]);
581
582         if (!DBA::e("DELETE FROM `contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
583                 return Update::FAILED;
584         }
585
586         return Update::SUCCESS;
587 }
588
589 function pre_update_1377()
590 {
591         DBStructure::checkInitialValues();
592
593         if (!DBA::e("DELETE FROM `item` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
594                 return Update::FAILED;
595         }
596
597         if (!DBA::e("DELETE FROM `item` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
598                 return Update::FAILED;
599         }
600
601         if (!DBA::e("UPDATE `item` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
602                 return Update::FAILED;
603         }
604
605         if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
606                 return Update::FAILED;
607         }
608
609         if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
610                 return Update::FAILED;
611         }
612
613         if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
614                 return Update::FAILED;
615         }
616
617         if (!DBA::e("UPDATE `notify` SET `uri-id` = NULL WHERE `uri-id` = 0")) {
618                 return Update::FAILED;
619         }
620
621         if (DBStructure::existsTable('diaspora-interaction') && !DBA::e("DELETE FROM `diaspora-interaction` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
622                 return Update::FAILED;
623         }
624
625         if (DBStructure::existsTable('item-activity') && !DBA::e("DELETE FROM `item-activity` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
626                 return Update::FAILED;
627         }
628
629         if (DBStructure::existsTable('item-content') && !DBA::e("DELETE FROM `item-content` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
630                 return Update::FAILED;
631         }
632
633         if (!DBA::e("DELETE FROM `notify` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
634                 return Update::FAILED;
635         }
636
637         if (!DBA::e("UPDATE `notify` SET `parent-uri-id` = NULL WHERE `parent-uri-id` = 0")) {
638                 return Update::FAILED;
639         }
640         if (!DBA::e("DELETE FROM `notify` WHERE `parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
641                 return Update::FAILED;
642         }
643
644         if (!DBA::e("UPDATE `notify-threads` SET `master-parent-uri-id` = NULL WHERE `master-parent-uri-id` = 0")) {
645                 return Update::FAILED;
646         }
647
648         if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
649                 return Update::FAILED;
650         }
651
652         if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-item` NOT IN (SELECT `id` FROM `item`)")) {
653                 return Update::FAILED;
654         }
655
656         return Update::SUCCESS;
657 }
658
659 function update_1380()
660 {
661         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 (?, ?)",
662                 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
663                 return Update::FAILED;
664         }
665
666         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 (?, ?)",
667                 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
668                 return Update::FAILED;
669         }
670
671         return Update::SUCCESS;
672 }
673
674 function pre_update_1395()
675 {
676         if (DBStructure::existsTable('post-user') && !DBStructure::existsColumn('post-user', ['id']) && !DBA::e("DROP TABLE `post-user`")) {
677                 return Update::FAILED;
678         }
679         return Update::SUCCESS;
680 }
681
682 function update_1395()
683 {
684         if (!DBA::e("INSERT INTO `post-user`(`id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid`)
685                 SELECT `id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid` FROM `item`
686                 ON DUPLICATE KEY UPDATE `contact-id` = `item`.`contact-id`, `unseen` = `item`.`unseen`, `origin` = `item`.`origin`, `psid` = `item`.`psid`")) {
687                 return Update::FAILED;
688         }
689
690         if (DBStructure::existsTable('user-item') && !DBA::e("INSERT INTO `post-user`(`uri-id`, `uid`, `hidden`, `notification-type`)
691                 SELECT `uri-id`, `user-item`.`uid`, `hidden`,`notification-type` FROM `user-item`
692                         INNER JOIN `item` ON `item`.`id` = `user-item`.`iid`
693                 ON DUPLICATE KEY UPDATE `hidden` = `user-item`.`hidden`, `notification-type` = `user-item`.`notification-type`")) {
694                 return Update::FAILED;
695         }
696         return Update::SUCCESS;
697 }
698
699 function update_1396()
700 {
701         if (!DBStructure::existsTable('item-content')) {
702                 return Update::SUCCESS;
703         }
704
705         if (DBStructure::existsColumn('item-content', ['raw-body'])) {
706                 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`, `raw-body`,
707                         `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
708                         `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
709                         SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
710                                 `item-content`.`body`, `item-content`.`raw-body`, `item-content`.`location`, `item-content`.`coord`,
711                                 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
712                                 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
713                                 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
714                                 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
715                         return Update::FAILED;
716                 }
717         } else {
718                 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`,
719                         `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
720                         `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
721                         SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
722                                 `item-content`.`body`, `item-content`.`location`, `item-content`.`coord`,
723                                 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
724                                 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
725                                 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
726                                 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
727                         return Update::FAILED;
728                 }
729         }
730         return Update::SUCCESS;
731 }
732
733 function update_1397()
734 {
735         if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
736                 SELECT `uri-id`, `uid`, `notification-type` FROM `post-user` WHERE `notification-type` != 0
737                 ON DUPLICATE KEY UPDATE `uri-id` = `post-user`.`uri-id`, `uid` = `post-user`.`uid`, `notification-type` = `post-user`.`notification-type`")) {
738                 return Update::FAILED;
739         }
740
741         if (!DBStructure::existsTable('user-item')) {
742                 return Update::SUCCESS;
743         }
744
745         if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
746                 SELECT `uri-id`, `user-item`.`uid`, `notification-type` FROM `user-item`
747                         INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` WHERE `notification-type` != 0
748                 ON DUPLICATE KEY UPDATE `notification-type` = `user-item`.`notification-type`")) {
749                 return Update::FAILED;
750         }
751
752         if (!DBStructure::existsTable('thread')) {
753                 return Update::SUCCESS;
754         }
755
756         if (!DBA::e("INSERT IGNORE INTO `post-thread-user`(`uri-id`, `uid`, `pinned`, `starred`, `ignored`, `wall`, `pubmail`, `forum_mode`)
757                 SELECT `thread`.`uri-id`, `thread`.`uid`, `user-item`.`pinned`, `thread`.`starred`,
758                         `thread`.`ignored`, `thread`.`wall`, `thread`.`pubmail`, `thread`.`forum_mode`
759                 FROM `thread` LEFT JOIN `user-item` ON `user-item`.`iid` = `thread`.`iid`")) {
760                 return Update::FAILED;
761         }
762
763         return Update::SUCCESS;
764 }
765
766 function update_1398()
767 {
768         if (!DBStructure::existsTable('thread')) {
769                 return Update::SUCCESS;
770         }
771
772         if (!DBA::e("INSERT IGNORE INTO `post-thread` (`uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented`)
773                 SELECT `uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented` FROM `thread`")) {
774                         return Update::FAILED;
775         }
776
777         if (!DBStructure::existsTable('thread')) {
778                 return Update::SUCCESS;
779         }
780
781         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`
782                 SET `post-thread-user`.`mention` = `thread`.`mention`")) {
783                         return Update::FAILED;
784         }
785
786         return Update::SUCCESS;
787 }
788
789 function update_1399()
790 {
791         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`
792                 SET `post-thread-user`.`contact-id` = `post-user`.`contact-id`, `post-thread-user`.`unseen` = `post-user`.`unseen`,
793                 `post-thread-user`.`hidden` = `post-user`.`hidden`, `post-thread-user`.`origin` = `post-user`.`origin`,
794                 `post-thread-user`.`psid` = `post-user`.`psid`, `post-thread-user`.`post-user-id` = `post-user`.`id`")) {
795                         return Update::FAILED;
796         }
797
798         return Update::SUCCESS;
799 }
800
801 function update_1400()
802 {
803         if (!DBA::e("INSERT IGNORE INTO `post` (`uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`,
804                 `created`, `received`, `edited`, `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global`)
805                 SELECT `uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `edited`,
806                         `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global` FROM `item`")) {
807                         return Update::FAILED;
808         }
809
810         if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
811                 INNER JOIN `event` ON `item`.`event-id` = `event`.`id` AND `event`.`id` != 0
812                 SET `post-user`.`event-id` = `item`.`event-id`")) {
813                 return Update::FAILED;
814         }
815
816         if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
817                 SET `post-user`.`wall` = `item`.`wall`, `post-user`.`parent-uri-id` = `item`.`parent-uri-id`,
818                 `post-user`.`thr-parent-id` = `item`.`thr-parent-id`,
819                 `post-user`.`created` = `item`.`created`, `post-user`.`edited` = `item`.`edited`,
820                 `post-user`.`received` = `item`.`received`, `post-user`.`gravity` = `item`.`gravity`,
821                 `post-user`.`network` = `item`.`network`, `post-user`.`owner-id` = `item`.`owner-id`,
822                 `post-user`.`author-id` = `item`.`author-id`, `post-user`.`causer-id` = `item`.`causer-id`,
823                 `post-user`.`post-type` = `item`.`post-type`, `post-user`.`vid` = `item`.`vid`,
824                 `post-user`.`private` = `item`.`private`, `post-user`.`global` = `item`.`global`,
825                 `post-user`.`visible` = `item`.`visible`, `post-user`.`deleted` = `item`.`deleted`")) {
826                 return Update::FAILED;
827         }
828
829         if (!DBA::e("INSERT IGNORE INTO `post-thread-user` (`uri-id`, `owner-id`, `author-id`, `causer-id`, `network`,
830                 `created`, `received`, `changed`, `commented`, `uid`,  `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `post-user-id`)
831                 SELECT `uri-id`, `owner-id`, `author-id`, `causer-id`, `network`, `created`, `received`, `received`, `received`,
832                         `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `id`
833                 FROM `post-user` WHERE `gravity` = 0 AND NOT EXISTS(SELECT `uri-id` FROM `post-thread-user` WHERE `post-user-id` = `post-user`.id)")) {
834                 return Update::FAILED;
835         }
836
837         if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-thread` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id`
838                 SET `post-thread-user`.`owner-id` = `post-thread`.`owner-id`, `post-thread-user`.`author-id` = `post-thread`.`author-id`,
839                 `post-thread-user`.`causer-id` = `post-thread`.`causer-id`, `post-thread-user`.`network` = `post-thread`.`network`,
840                 `post-thread-user`.`created` = `post-thread`.`created`, `post-thread-user`.`received` = `post-thread`.`received`,
841                 `post-thread-user`.`changed` = `post-thread`.`changed`, `post-thread-user`.`commented` = `post-thread`.`commented`")) {
842                 return Update::FAILED;
843         }
844
845         return Update::SUCCESS;
846 }
847
848 function pre_update_1403()
849 {
850         // Necessary before a primary key change
851         if (DBStructure::existsTable('parsed_url') && !DBA::e("DROP TABLE `parsed_url`")) {
852                 return Update::FAILED;
853         }
854
855         return Update::SUCCESS;
856 }
857
858 function update_1404()
859 {
860         $tasks = DBA::select('workerqueue', ['id', 'command', 'parameter'], ['command' => ['notifier', 'delivery', 'apdelivery', 'done' => false]]);
861         while ($task = DBA::fetch($tasks)) {
862                 $parameters = json_decode($task['parameter'], true);
863
864                 if (is_array($parameters) && count($parameters) && in_array($parameters[0], [Delivery::MAIL, Delivery::SUGGESTION, Delivery::REMOVAL, Delivery::RELOCATION])) {
865                         continue;
866                 }
867
868                 switch (strtolower($task['command'])) {
869                         case 'notifier':
870                                 if (count($parameters) == 3) {
871                                         continue 2;
872                                 }
873                                 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
874                                 if (!DBA::isResult($item)) {
875                                         continue 2;
876                                 }
877
878                                 $parameters[1] = $item['uri-id'];
879                                 $parameters[2] = $item['uid'];
880                                 break;
881                         case 'delivery':
882                                 if (count($parameters) == 4) {
883                                         continue 2;
884                                 }
885                                 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
886                                 if (!DBA::isResult($item)) {
887                                         continue 2;
888                                 }
889
890                                 $parameters[1] = $item['uri-id'];
891                                 $parameters[3] = $item['uid'];
892                                 break;
893                         case 'apdelivery':
894                                 if (count($parameters) == 6) {
895                                         continue 2;
896                                 }
897
898                                 if (empty($parameters[4])) {
899                                         $parameters[4] = [];
900                                 }
901
902                                 $item = DBA::selectFirst('item', ['uri-id'], ['id' => $parameters[1]]);
903                                 if (!DBA::isResult($item)) {
904                                         continue 2;
905                                 }
906
907                                 $parameters[5] = $item['uri-id'];
908                                 break;
909                         default:
910                                 continue 2;
911                 }
912                 DBA::update('workerqueue', ['parameter' => json_encode($parameters)], ['id' => $task['id']]);
913
914                 return Update::SUCCESS;
915         }
916 }
917
918 function update_1407()
919 {
920         if (!DBA::e("UPDATE `post` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
921                 return Update::FAILED;
922         }
923         if (!DBA::e("UPDATE `post-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
924                 return Update::FAILED;
925         }
926         if (!DBA::e("UPDATE `post-thread` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
927                 return Update::FAILED;
928         }
929         if (!DBA::e("UPDATE `post-thread-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
930                 return Update::FAILED;
931         }
932
933         return Update::SUCCESS;
934 }
935
936 function update_1413()
937 {
938         if (!DBA::e("UPDATE `post-user` SET `post-reason` = `post-type` WHERE `post-type` >= 64 and `post-type` <= 75")) {
939                 return Update::FAILED;
940         }
941 }
942
943 function update_1419()
944 {
945         $mails = DBA::select('mail', ['id', 'from-url', 'uri', 'parent-uri', 'guid'], [], ['order' => ['id']]);
946         while ($mail = DBA::fetch($mails)) {
947                 $fields = [];
948                 $fields['author-id'] = Contact::getIdForURL($mail['from-url'], 0, false);
949                 if (empty($fields['author-id'])) {
950                         continue;
951                 }
952
953                 $fields['uri-id']        = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
954                 $fields['parent-uri-id'] = ItemURI::getIdByURI($mail['parent-uri']);
955
956                 $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
957                 if (!empty($reply)) {
958                         $fields['thr-parent'] = $reply['uri'];
959                         if (!empty($reply['uri-id'])) {
960                                 $fields['thr-parent-id'] = $reply['uri-id'];
961                         } else {
962                                 $fields['thr-parent-id'] = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]);
963                         }
964                 }
965
966                 DBA::update('mail', $fields, ['id' => $mail['id']]);
967         }
968         return Update::SUCCESS;
969 }
970
971 function update_1429()
972 {
973         if (!DBA::e("UPDATE `contact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
974                 return Update::FAILED;
975         }
976
977         if (DBStructure::existsTable('fcontact') && !DBA::e("UPDATE `fcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
978                 return Update::FAILED;
979         }
980
981         if (!DBA::e("UPDATE `apcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
982                 return Update::FAILED;
983         }
984
985         DI::keyValue()->set('post_update_version', 1423);
986
987         return Update::SUCCESS;
988 }
989
990 function update_1434()
991 {
992         $name = DI::config()->get('storage', 'name');
993
994         // In case of an empty config, set "Database" as default storage backend
995         if (empty($name)) {
996                 DI::config()->set('storage', 'name', DatabaseStorage::getName());
997         }
998
999         // In case of a Using deprecated storage class value, set the right name for it
1000         if (stristr($name, 'Friendica\Model\Storage\\')) {
1001                 DI::config()->set('storage', 'name', substr($name, 24));
1002         }
1003
1004         return Update::SUCCESS;
1005 }
1006
1007 function update_1438()
1008 {
1009         DBA::update('photo', ['photo-type' => Photo::USER_AVATAR], ['profile' => true]);
1010         DBA::update('photo', ['photo-type' => Photo::CONTACT_AVATAR], ["NOT `profile` AND NOT `contact-id` IS NULL AND `contact-id` != ?", 0]);
1011         DBA::update('photo', ['photo-type' => Photo::DEFAULT], ["NOT `profile` AND (`contact-id` IS NULL OR `contact-id` = ?) AND `photo-type` IS NULL AND `album` != ?", 0, Photo::CONTACT_PHOTOS]);
1012 }
1013
1014 function update_1439()
1015 {
1016         if (!DBStructure::existsTable('fcontact')) {
1017                 return Update::SUCCESS;
1018         }
1019
1020         $intros = DBA::select('intro', ['id', 'fid'], ["NOT `fid` IS NULL AND `fid` != ?", 0]);
1021         while ($intro = DBA::fetch($intros)) {
1022                 $fcontact = DBA::selectFirst('fcontact', ['url'], ['id' => $intro['fid']]);
1023                 if (!empty($fcontact['url'])) {
1024                         $id = Contact::getIdForURL($fcontact['url']);
1025                         if (!empty($id)) {
1026                                 DBA::update('intro',['suggest-cid' => $id], ['id' => $intro['id']]);
1027                         }
1028                 }
1029         }
1030         DBA::close($intros);
1031
1032         return Update::SUCCESS;
1033 }
1034
1035 function update_1440()
1036 {
1037         // Fix wrong public permissionset
1038         DBA::p("UPDATE `profile_field` SET `psid` = ? WHERE psid IN (SELECT `id` FROM `permissionset` WHERE `id` != ? AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '')", PermissionSet::PUBLIC, PermissionSet::PUBLIC);
1039         DBA::delete('permissionset', ["`id` != ? AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''", PermissionSet::PUBLIC]);
1040
1041         return Update::SUCCESS;
1042 }
1043
1044 function update_1441()
1045 {
1046         $languages = DI::l10n()->getAvailableLanguages();
1047
1048         $albums = [Photo::PROFILE_PHOTOS];
1049         foreach ($languages as $language) {
1050                 $albums[] = DI::l10n()->withLang($language)->t(Photo::PROFILE_PHOTOS);
1051         }
1052         $albums = array_unique($albums);
1053
1054         Photo::update(['photo-type' => Photo::USER_AVATAR], ['album' => $albums]);
1055
1056         return Update::SUCCESS;
1057 }
1058
1059 function update_1442()
1060 {
1061         // transform blocked intros into ignored intros
1062         DBA::update('intro', ['ignore' => 1, 'blocked' => 0], ['blocked' => 1]);
1063
1064         return Update::SUCCESS;
1065 }
1066
1067 /**
1068  * A bug in Contact\User::updateByContactUpdate prevented any update to the user-contact table since the rows have been
1069  * created in version 1435. This version fixes this bug but the user-contact rows are outdated, we need to regenerate
1070  * them.
1071  */
1072 function update_1444()
1073 {
1074         DBA::e('TRUNCATE TABLE `user-contact`');
1075
1076         $contacts = DBA::select('contact', [], ["`uid` != ?", 0]);
1077         while ($contact = DBA::fetch($contacts)) {
1078                 Contact\User::insertForContactArray($contact);
1079         }
1080
1081         return Update::SUCCESS;
1082 }
1083
1084 function update_1446()
1085 {
1086         $distributed_cache_driver_source = DI::config()->getCache()->getSource('system', 'distributed_cache_driver');
1087         $cache_driver_source = DI::config()->getCache()->getSource('system', 'cache_driver');
1088
1089         // In case the distributed cache driver is the default value, but the current cache driver isn't default,
1090         // we assume that the distributed cache driver should be the same as the current cache driver
1091         if ($distributed_cache_driver_source === Cache::SOURCE_STATIC && $cache_driver_source > Cache::SOURCE_STATIC) {
1092                 DI::config()->set('system', 'distributed_cache_driver', DI::config()->get('system', 'cache_driver'));
1093         }
1094
1095         return Update::SUCCESS;
1096 }
1097
1098 function update_1451()
1099 {
1100         DBA::update('user', ['account-type' => User::ACCOUNT_TYPE_COMMUNITY], ['page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]]);
1101         DBA::update('contact', ['contact-type' => Contact::TYPE_COMMUNITY], ["`forum` OR `prv`"]);
1102         DBA::update('contact', ['manually-approve' => true], ['prv' => true]);
1103
1104         return Update::SUCCESS;
1105 }
1106
1107 function update_1457()
1108 {
1109         $pinned = DBA::select('post-thread-user', ['uri-id', 'author-id'], ['pinned' => true]);
1110         while ($post = DBA::fetch($pinned)) {
1111                 Post\Collection::add($post['uri-id'], Post\Collection::FEATURED, $post['author-id']);
1112         }
1113         DBA::close($pinned);
1114
1115         return Update::SUCCESS;
1116 }
1117
1118 function update_1480()
1119 {
1120         DBA::update('contact', ['next-update' => DBA::NULL_DATETIME], ['network' => Protocol::FEDERATED]);
1121         DBA::update('post', ['deleted' => false], ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE NOT `deleted`)"]);
1122         return Update::SUCCESS;
1123 }
1124
1125 function update_1481()
1126 {
1127         DBA::e("UPDATE `post-collection` INNER JOIN `post` ON `post`.`uri-id` = `post-collection`.`uri-id` SET `post-collection`.`author-id` = `post`.`author-id` WHERE `post-collection`.`author-id` IS null");
1128         return Update::SUCCESS;
1129 }
1130
1131 function update_1491()
1132 {
1133         DBA::update('contact', ['remote_self' => Contact::MIRROR_OWN_POST], ['remote_self' => Contact::MIRROR_FORWARDED]);
1134         return Update::SUCCESS;
1135 }
1136
1137 function update_1497()
1138 {
1139         DBA::e("UPDATE `user` SET `last-activity` = DATE(`login_date`) WHERE `last-activity` IS NULL");
1140         return Update::SUCCESS;
1141 }
1142
1143 function update_1502()
1144 {
1145         DBA::e("UPDATE `pconfig` SET `cat` = 'calendar' WHERE `k` = 'first_day_of_week'");
1146         return Update::SUCCESS;
1147 }
1148
1149 function update_1505()
1150 {
1151         if (!DBStructure::existsTable('config')) {
1152                 return Update::SUCCESS;
1153         }
1154
1155         $conditions = [
1156                 "((`cat`  = ?) AND ((`k` LIKE ?) OR (`k` = ?) OR (`k` LIKE ?) OR (`k` = ?))) OR " .
1157                 "((`cat` != ?) AND  (`k` LIKE ?)) OR " .
1158                 "((`cat`  = ?) AND  (`k` LIKE ?))",
1159                 "system",
1160                 "post_update_%",
1161                 "worker_last_cleaned",
1162                 "last%",
1163                 "worker_daemon_mode",
1164                 "system",
1165                 "last_%",
1166                 "database",
1167                 "update_%",
1168         ];
1169
1170         $postUpdateEntries = DBA::selectToArray('config', ['cat', 'k', 'v'], $conditions);
1171
1172         foreach ($postUpdateEntries as $postUpdateEntry) {
1173                 if ($postUpdateEntry['cat'] === 'system') {
1174                         DI::keyValue()->set($postUpdateEntry['k'], $postUpdateEntry['v']);
1175                 } else {
1176                         DI::keyValue()->set(sprintf('%s_%s', $postUpdateEntry['cat'], $postUpdateEntry['k']), $postUpdateEntry['v']);
1177                 }
1178         }
1179
1180         return DBA::delete('config', $conditions) ? Update::SUCCESS : Update::FAILED;
1181 }
1182
1183 function update_1508()
1184 {
1185         $config = DBA::selectToArray('config');
1186
1187         $newConfig = DI::config()->beginTransaction();
1188
1189         foreach ($config as $entry) {
1190                 $newConfig->set($entry['cat'], $entry['k'], $entry['v']);
1191         }
1192
1193         $newConfig->commit();
1194
1195         return Update::SUCCESS;
1196 }
1197
1198 function update_1509()
1199 {
1200         $addons = DBA::selectToArray('addon');
1201
1202         $newConfig = DI::config()->beginTransaction();
1203
1204         foreach ($addons as $addon) {
1205                 $newConfig->set('addons', $addon['name'], [
1206                         'last_update' => $addon['timestamp'],
1207                         'admin' => (bool)$addon['plugin_admin'],
1208                 ]);
1209         }
1210
1211         $newConfig->commit();
1212
1213         return Update::SUCCESS;
1214 }