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