3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
20 * Automatic post-databse structure change updates
22 * These functions are responsible for doing critical post update changes to the data (not the structure) in the database.
24 * Database structure changes are done in static/dbstructure.config.php
26 * For non-critical database migrations, please add a method in the Database\PostUpdate class
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.
31 * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
34 * You are currently on version 4711 and you are preparing changes that demand an update script.
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.
40 * If you need to run a script before the database update, name the function "pre_update_4712()"
43 use Friendica\Core\Logger;
44 use Friendica\Core\Storage\Capability\ICanReadFromStorage;
45 use Friendica\Core\Update;
46 use Friendica\Core\Worker;
47 use Friendica\Database\Database;
48 use Friendica\Database\DBA;
49 use Friendica\Database\DBStructure;
51 use Friendica\Model\Contact;
52 use Friendica\Model\Item;
53 use Friendica\Model\ItemURI;
54 use Friendica\Model\Notification;
55 use Friendica\Model\Photo;
56 use Friendica\Model\Post;
57 use Friendica\Model\Profile;
58 use Friendica\Security\PermissionSet\Repository\PermissionSet;
59 use Friendica\Worker\Delivery;
61 // Post-update script of PR 5751
62 function update_1298()
64 $keys = ['gender', 'marital', 'sexual'];
65 foreach ($keys as $translateKey) {
66 $allData = DBA::select('profile', ['id', $translateKey]);
67 $allLangs = DI::l10n()->getAvailableLanguages();
70 foreach ($allData as $key => $data) {
71 $toTranslate = $data[$translateKey];
72 if ($toTranslate != '') {
73 foreach ($allLangs as $key => $lang) {
77 // First we get the the localizations
78 if (file_exists("view/lang/$lang/strings.php")) {
79 include "view/lang/$lang/strings.php";
81 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
82 include "addon/morechoice/lang/$lang/strings.php";
85 $localizedStrings = $a->strings;
88 $key = array_search($toTranslate, $localizedStrings);
93 // defaulting to empty string
100 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
101 Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
102 'was' => $data[$translateKey]]);
104 Contact::updateSelfFromUserID($data['id']);
105 Profile::publishUpdate($data['id']);
111 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
113 return Update::SUCCESS;
116 function update_1309()
118 $queue = DBA::select('queue', ['id', 'cid', 'guid']);
119 while ($entry = DBA::fetch($queue)) {
120 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
121 if (!DBA::isResult($contact)) {
125 $item = Post::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
126 if (!DBA::isResult($item)) {
130 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
131 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
132 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
133 DBA::delete('queue', ['id' => $entry['id']]);
135 return Update::SUCCESS;
138 function update_1315()
140 if (DBStructure::existsTable('item-delivery-data')) {
141 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
143 return Update::SUCCESS;
146 function update_1318()
148 DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
149 DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
151 Worker::add(PRIORITY_LOW, 'ProfileUpdate');
152 return Update::SUCCESS;
155 function update_1323()
157 $users = DBA::select('user', ['uid']);
158 while ($user = DBA::fetch($users)) {
159 if (Contact::updateSelfFromUserID($user['uid'])) {
160 Profile::publishUpdate($user['uid']);
165 return Update::SUCCESS;
168 function update_1327()
170 $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
171 while ($contact = DBA::fetch($contacts)) {
172 Contact\User::setBlocked($contact['id'], $contact['uid'], $contact['blocked']);
173 Contact\User::setIgnored($contact['id'], $contact['uid'], $contact['readonly']);
175 DBA::close($contacts);
177 return Update::SUCCESS;
180 function update_1330()
182 $currStorage = DI::config()->get('storage', 'class', '');
184 // set the name of the storage instead of the classpath as config
185 if (!empty($currStorage)) {
186 /** @var ICanReadFromStorage $currStorage */
187 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
188 return Update::FAILED;
191 // try to delete the class since it isn't needed. This won't work with config files
192 DI::config()->delete('storage', 'class');
195 // Update attachments and photos
196 if (!DBA::e("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
197 !DBA::e("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
198 return Update::FAILED;
201 return Update::SUCCESS;
204 function update_1332()
206 $condition = ["`is-default` IS NOT NULL"];
207 $profiles = DBA::select('profile', [], $condition);
209 while ($profile = DBA::fetch($profiles)) {
210 Profile::migrate($profile);
212 DBA::close($profiles);
214 DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
216 return Update::SUCCESS;
219 function update_1347()
221 foreach (Item::ACTIVITIES as $index => $activity) {
222 DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
225 return Update::SUCCESS;
228 function pre_update_1348()
230 if (!DBA::exists('contact', ['id' => 0])) {
231 DBA::insert('contact', ['nurl' => '']);
232 $lastid = DBA::lastInsertId();
234 DBA::update('contact', ['id' => 0], ['id' => $lastid]);
238 // The tables "permissionset" and "tag" could or could not exist during the update.
239 // This depends upon the previous version. Depending upon this situation we have to add
240 // the "0" values before adding the foreign keys - or after would be sufficient.
244 if (DBStructure::existsTable('auth_codes') && DBStructure::existsTable('clients')) {
245 DBA::e("DELETE FROM `auth_codes` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
247 if (DBStructure::existsTable('tokens') && DBStructure::existsTable('clients')) {
248 DBA::e("DELETE FROM `tokens` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
250 return Update::SUCCESS;
253 function update_1348()
255 // Insert a permissionset with id=0
256 // Inserting it without an ID and then changing the value to 0 tricks the auto increment
257 if (!DBA::exists('permissionset', ['id' => 0])) {
258 DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
259 $lastid = DBA::lastInsertId();
261 DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
265 if (!DBA::exists('tag', ['id' => 0])) {
266 DBA::insert('tag', ['name' => '']);
267 $lastid = DBA::lastInsertId();
269 DBA::update('tag', ['id' => 0], ['id' => $lastid]);
273 return Update::SUCCESS;
276 function update_1349()
278 if (!DBStructure::existsTable('item-activity')) {
279 return Update::SUCCESS;
283 foreach (Item::ACTIVITIES as $index => $activity) {
284 if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
290 // The update failed - but it cannot be recovered, since the data doesn't match our expectation
291 // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
292 // the postupdate. This is not fatal, but means that it will take some longer time for the system
294 return Update::SUCCESS;
297 if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
298 SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", GRAVITY_ACTIVITY)) {
299 return Update::FAILED;
302 return Update::SUCCESS;
305 function update_1351()
307 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
308 return Update::FAILED;
311 return Update::SUCCESS;
314 function pre_update_1354()
316 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
317 && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
318 && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
319 return Update::FAILED;
321 return Update::SUCCESS;
324 function update_1354()
326 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
327 && DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])) {
328 if (!DBA::e("UPDATE `contact` SET `ffi_keyword_denylist` = `ffi_keyword_blacklist`")) {
329 return Update::FAILED;
332 // When the data had been copied then the main task is done.
333 // Having the old field removed is only beauty but not crucial.
334 // So we don't care if this was successful or not.
335 DBA::e("ALTER TABLE `contact` DROP `ffi_keyword_blacklist`");
337 return Update::SUCCESS;
340 function update_1357()
342 if (!DBA::e("UPDATE `contact` SET `failed` = true WHERE `success_update` < `failure_update` AND `failed` IS NULL")) {
343 return Update::FAILED;
346 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `success_update` > `failure_update` AND `failed` IS NULL")) {
347 return Update::FAILED;
350 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `updated` > `failure_update` AND `failed` IS NULL")) {
351 return Update::FAILED;
354 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `last-item` > `failure_update` AND `failed` IS NULL")) {
355 return Update::FAILED;
358 if (!DBA::e("UPDATE `gserver` SET `failed` = true WHERE `last_contact` < `last_failure` AND `failed` IS NULL")) {
359 return Update::FAILED;
362 if (!DBA::e("UPDATE `gserver` SET `failed` = false WHERE `last_contact` > `last_failure` AND `failed` IS NULL")) {
363 return Update::FAILED;
366 return Update::SUCCESS;
369 function pre_update_1358()
371 if (!DBA::e("DELETE FROM `contact-relation` WHERE NOT `relation-cid` IN (SELECT `id` FROM `contact`) OR NOT `cid` IN (SELECT `id` FROM `contact`)")) {
372 return Update::FAILED;
375 return Update::SUCCESS;
378 function pre_update_1363()
380 Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
381 return Update::SUCCESS;
384 function pre_update_1364()
386 if (!DBA::e("DELETE FROM `2fa_recovery_codes` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
387 return Update::FAILED;
390 if (!DBA::e("DELETE FROM `2fa_app_specific_password` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
391 return Update::FAILED;
394 if (!DBA::e("DELETE FROM `attach` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
395 return Update::FAILED;
398 if (DBStructure::existsTable('clients') && !DBA::e("DELETE FROM `clients` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
399 return Update::FAILED;
402 if (!DBA::e("DELETE FROM `conv` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
403 return Update::FAILED;
406 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
407 return Update::FAILED;
410 if (!DBA::e("DELETE FROM `group` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
411 return Update::FAILED;
414 if (!DBA::e("DELETE FROM `intro` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
415 return Update::FAILED;
418 if (!DBA::e("DELETE FROM `manage` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
419 return Update::FAILED;
422 if (!DBA::e("DELETE FROM `manage` WHERE NOT `mid` IN (SELECT `uid` FROM `user`)")) {
423 return Update::FAILED;
426 if (!DBA::e("DELETE FROM `mail` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
427 return Update::FAILED;
430 if (!DBA::e("DELETE FROM `mailacct` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
431 return Update::FAILED;
434 if (!DBA::e("DELETE FROM `notify` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
435 return Update::FAILED;
438 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
439 return Update::FAILED;
442 if (!DBA::e("DELETE FROM `pconfig` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
443 return Update::FAILED;
446 if (!DBA::e("DELETE FROM `profile` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
447 return Update::FAILED;
450 if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
451 return Update::FAILED;
454 if (!DBA::e("DELETE FROM `profile_field` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
455 return Update::FAILED;
458 if (!DBA::e("DELETE FROM `push_subscriber` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
459 return Update::FAILED;
462 if (!DBA::e("DELETE FROM `register` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
463 return Update::FAILED;
466 if (!DBA::e("DELETE FROM `search` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
467 return Update::FAILED;
470 if (DBStructure::existsTable('tokens') && !DBA::e("DELETE FROM `tokens` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
471 return Update::FAILED;
474 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
475 return Update::FAILED;
478 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
479 return Update::FAILED;
482 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `receiver-uid` IN (SELECT `uid` FROM `user`)")) {
483 return Update::FAILED;
486 if (!DBA::e("DELETE FROM `event` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
487 return Update::FAILED;
490 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
491 return Update::FAILED;
494 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
495 return Update::FAILED;
498 if (!DBA::e("DELETE FROM `intro` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
499 return Update::FAILED;
502 if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
503 return Update::FAILED;
506 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
507 return Update::FAILED;
510 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `gid` IN (SELECT `id` FROM `group`)")) {
511 return Update::FAILED;
514 if (!DBA::e("DELETE FROM `gserver-tag` WHERE NOT `gserver-id` IN (SELECT `id` FROM `gserver`)")) {
515 return Update::FAILED;
518 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
519 return Update::FAILED;
522 return Update::SUCCESS;
525 function pre_update_1365()
527 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `notify-id` IN (SELECT `id` FROM `notify`)")) {
528 return Update::FAILED;
531 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
532 return Update::FAILED;
535 return Update::SUCCESS;
538 function update_1375()
540 if (!DBA::e("UPDATE `item` SET `thr-parent` = `parent-uri`, `thr-parent-id` = `parent-uri-id` WHERE `thr-parent` = ''")) {
541 return Update::FAILED;
544 return Update::SUCCESS;
547 function pre_update_1376()
549 // Insert a user with uid=0
550 DBStructure::checkInitialValues();
552 if (!DBA::e("DELETE FROM `item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
553 return Update::FAILED;
556 if (!DBA::e("DELETE FROM `event` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
557 return Update::FAILED;
560 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
561 return Update::FAILED;
564 if (!DBA::e("DELETE FROM `permissionset` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
565 return Update::FAILED;
568 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
569 return Update::FAILED;
572 if (!DBA::e("DELETE FROM `post-category` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
573 return Update::FAILED;
576 Photo::delete(["NOT `uid` IN (SELECT `uid` FROM `user`)"]);
578 if (!DBA::e("DELETE FROM `contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
579 return Update::FAILED;
582 return Update::SUCCESS;
585 function pre_update_1377()
587 DBStructure::checkInitialValues();
589 if (!DBA::e("DELETE FROM `item` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
590 return Update::FAILED;
593 if (!DBA::e("DELETE FROM `item` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
594 return Update::FAILED;
597 if (!DBA::e("UPDATE `item` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
598 return Update::FAILED;
601 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
602 return Update::FAILED;
605 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
606 return Update::FAILED;
609 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
610 return Update::FAILED;
613 if (!DBA::e("UPDATE `notify` SET `uri-id` = NULL WHERE `uri-id` = 0")) {
614 return Update::FAILED;
617 if (DBStructure::existsTable('diaspora-interaction') && !DBA::e("DELETE FROM `diaspora-interaction` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
618 return Update::FAILED;
621 if (DBStructure::existsTable('item-activity') && !DBA::e("DELETE FROM `item-activity` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
622 return Update::FAILED;
625 if (DBStructure::existsTable('item-content') && !DBA::e("DELETE FROM `item-content` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
626 return Update::FAILED;
629 if (!DBA::e("DELETE FROM `notify` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
630 return Update::FAILED;
633 if (!DBA::e("UPDATE `notify` SET `parent-uri-id` = NULL WHERE `parent-uri-id` = 0")) {
634 return Update::FAILED;
636 if (!DBA::e("DELETE FROM `notify` WHERE `parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
637 return Update::FAILED;
640 if (!DBA::e("UPDATE `notify-threads` SET `master-parent-uri-id` = NULL WHERE `master-parent-uri-id` = 0")) {
641 return Update::FAILED;
644 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
645 return Update::FAILED;
648 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-item` NOT IN (SELECT `id` FROM `item`)")) {
649 return Update::FAILED;
652 return Update::SUCCESS;
655 function update_1380()
657 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 (?, ?)",
658 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
659 return Update::FAILED;
662 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 (?, ?)",
663 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
664 return Update::FAILED;
667 return Update::SUCCESS;
670 function pre_update_1395()
672 if (DBStructure::existsTable('post-user') && !DBStructure::existsColumn('post-user', ['id']) && !DBA::e("DROP TABLE `post-user`")) {
673 return Update::FAILED;
675 return Update::SUCCESS;
678 function update_1395()
680 if (!DBA::e("INSERT INTO `post-user`(`id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid`)
681 SELECT `id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid` FROM `item`
682 ON DUPLICATE KEY UPDATE `contact-id` = `item`.`contact-id`, `unseen` = `item`.`unseen`, `origin` = `item`.`origin`, `psid` = `item`.`psid`")) {
683 return Update::FAILED;
686 if (DBStructure::existsTable('user-item') && !DBA::e("INSERT INTO `post-user`(`uri-id`, `uid`, `hidden`, `notification-type`)
687 SELECT `uri-id`, `user-item`.`uid`, `hidden`,`notification-type` FROM `user-item`
688 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid`
689 ON DUPLICATE KEY UPDATE `hidden` = `user-item`.`hidden`, `notification-type` = `user-item`.`notification-type`")) {
690 return Update::FAILED;
692 return Update::SUCCESS;
695 function update_1396()
697 if (!DBStructure::existsTable('item-content')) {
698 return Update::SUCCESS;
701 if (DBStructure::existsColumn('item-content', ['raw-body'])) {
702 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`, `raw-body`,
703 `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
704 `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
705 SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
706 `item-content`.`body`, `item-content`.`raw-body`, `item-content`.`location`, `item-content`.`coord`,
707 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
708 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
709 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
710 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
711 return Update::FAILED;
714 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`,
715 `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
716 `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
717 SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
718 `item-content`.`body`, `item-content`.`location`, `item-content`.`coord`,
719 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
720 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
721 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
722 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
723 return Update::FAILED;
726 return Update::SUCCESS;
729 function update_1397()
731 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
732 SELECT `uri-id`, `uid`, `notification-type` FROM `post-user` WHERE `notification-type` != 0
733 ON DUPLICATE KEY UPDATE `uri-id` = `post-user`.`uri-id`, `uid` = `post-user`.`uid`, `notification-type` = `post-user`.`notification-type`")) {
734 return Update::FAILED;
737 if (!DBStructure::existsTable('user-item')) {
738 return Update::SUCCESS;
741 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
742 SELECT `uri-id`, `user-item`.`uid`, `notification-type` FROM `user-item`
743 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` WHERE `notification-type` != 0
744 ON DUPLICATE KEY UPDATE `notification-type` = `user-item`.`notification-type`")) {
745 return Update::FAILED;
748 if (!DBStructure::existsTable('thread')) {
749 return Update::SUCCESS;
752 if (!DBA::e("INSERT IGNORE INTO `post-thread-user`(`uri-id`, `uid`, `pinned`, `starred`, `ignored`, `wall`, `pubmail`, `forum_mode`)
753 SELECT `thread`.`uri-id`, `thread`.`uid`, `user-item`.`pinned`, `thread`.`starred`,
754 `thread`.`ignored`, `thread`.`wall`, `thread`.`pubmail`, `thread`.`forum_mode`
755 FROM `thread` LEFT JOIN `user-item` ON `user-item`.`iid` = `thread`.`iid`")) {
756 return Update::FAILED;
759 return Update::SUCCESS;
762 function update_1398()
764 if (!DBStructure::existsTable('thread')) {
765 return Update::SUCCESS;
768 if (!DBA::e("INSERT IGNORE INTO `post-thread` (`uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented`)
769 SELECT `uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented` FROM `thread`")) {
770 return Update::FAILED;
773 if (!DBStructure::existsTable('thread')) {
774 return Update::SUCCESS;
777 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`
778 SET `post-thread-user`.`mention` = `thread`.`mention`")) {
779 return Update::FAILED;
782 return Update::SUCCESS;
785 function update_1399()
787 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`
788 SET `post-thread-user`.`contact-id` = `post-user`.`contact-id`, `post-thread-user`.`unseen` = `post-user`.`unseen`,
789 `post-thread-user`.`hidden` = `post-user`.`hidden`, `post-thread-user`.`origin` = `post-user`.`origin`,
790 `post-thread-user`.`psid` = `post-user`.`psid`, `post-thread-user`.`post-user-id` = `post-user`.`id`")) {
791 return Update::FAILED;
794 return Update::SUCCESS;
797 function update_1400()
799 if (!DBA::e("INSERT IGNORE INTO `post` (`uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`,
800 `created`, `received`, `edited`, `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global`)
801 SELECT `uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `edited`,
802 `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global` FROM `item`")) {
803 return Update::FAILED;
806 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
807 INNER JOIN `event` ON `item`.`event-id` = `event`.`id` AND `event`.`id` != 0
808 SET `post-user`.`event-id` = `item`.`event-id`")) {
809 return Update::FAILED;
812 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
813 SET `post-user`.`wall` = `item`.`wall`, `post-user`.`parent-uri-id` = `item`.`parent-uri-id`,
814 `post-user`.`thr-parent-id` = `item`.`thr-parent-id`,
815 `post-user`.`created` = `item`.`created`, `post-user`.`edited` = `item`.`edited`,
816 `post-user`.`received` = `item`.`received`, `post-user`.`gravity` = `item`.`gravity`,
817 `post-user`.`network` = `item`.`network`, `post-user`.`owner-id` = `item`.`owner-id`,
818 `post-user`.`author-id` = `item`.`author-id`, `post-user`.`causer-id` = `item`.`causer-id`,
819 `post-user`.`post-type` = `item`.`post-type`, `post-user`.`vid` = `item`.`vid`,
820 `post-user`.`private` = `item`.`private`, `post-user`.`global` = `item`.`global`,
821 `post-user`.`visible` = `item`.`visible`, `post-user`.`deleted` = `item`.`deleted`")) {
822 return Update::FAILED;
825 if (!DBA::e("INSERT IGNORE INTO `post-thread-user` (`uri-id`, `owner-id`, `author-id`, `causer-id`, `network`,
826 `created`, `received`, `changed`, `commented`, `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `post-user-id`)
827 SELECT `uri-id`, `owner-id`, `author-id`, `causer-id`, `network`, `created`, `received`, `received`, `received`,
828 `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `id`
829 FROM `post-user` WHERE `gravity` = 0 AND NOT EXISTS(SELECT `uri-id` FROM `post-thread-user` WHERE `post-user-id` = `post-user`.id)")) {
830 return Update::FAILED;
833 if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-thread` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id`
834 SET `post-thread-user`.`owner-id` = `post-thread`.`owner-id`, `post-thread-user`.`author-id` = `post-thread`.`author-id`,
835 `post-thread-user`.`causer-id` = `post-thread`.`causer-id`, `post-thread-user`.`network` = `post-thread`.`network`,
836 `post-thread-user`.`created` = `post-thread`.`created`, `post-thread-user`.`received` = `post-thread`.`received`,
837 `post-thread-user`.`changed` = `post-thread`.`changed`, `post-thread-user`.`commented` = `post-thread`.`commented`")) {
838 return Update::FAILED;
841 return Update::SUCCESS;
844 function pre_update_1403()
846 // Necessary before a primary key change
847 if (DBStructure::existsTable('parsed_url') && !DBA::e("DROP TABLE `parsed_url`")) {
848 return Update::FAILED;
851 return Update::SUCCESS;
854 function update_1404()
856 $tasks = DBA::select('workerqueue', ['id', 'command', 'parameter'], ['command' => ['notifier', 'delivery', 'apdelivery', 'done' => false]]);
857 while ($task = DBA::fetch($tasks)) {
858 $parameters = json_decode($task['parameter'], true);
860 if (is_array($parameters) && count($parameters) && in_array($parameters[0], [Delivery::MAIL, Delivery::SUGGESTION, Delivery::REMOVAL, Delivery::RELOCATION])) {
864 switch (strtolower($task['command'])) {
866 if (count($parameters) == 3) {
869 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
870 if (!DBA::isResult($item)) {
874 $parameters[1] = $item['uri-id'];
875 $parameters[2] = $item['uid'];
878 if (count($parameters) == 4) {
881 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
882 if (!DBA::isResult($item)) {
886 $parameters[1] = $item['uri-id'];
887 $parameters[3] = $item['uid'];
890 if (count($parameters) == 6) {
894 if (empty($parameters[4])) {
898 $item = DBA::selectFirst('item', ['uri-id'], ['id' => $parameters[1]]);
899 if (!DBA::isResult($item)) {
903 $parameters[5] = $item['uri-id'];
908 DBA::update('workerqueue', ['parameter' => json_encode($parameters)], ['id' => $task['id']]);
910 return Update::SUCCESS;
914 function update_1407()
916 if (!DBA::e("UPDATE `post` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
917 return Update::FAILED;
919 if (!DBA::e("UPDATE `post-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
920 return Update::FAILED;
922 if (!DBA::e("UPDATE `post-thread` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
923 return Update::FAILED;
925 if (!DBA::e("UPDATE `post-thread-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
926 return Update::FAILED;
929 return Update::SUCCESS;
932 function update_1413()
934 if (!DBA::e("UPDATE `post-user` SET `post-reason` = `post-type` WHERE `post-type` >= 64 and `post-type` <= 75")) {
935 return Update::FAILED;
939 function update_1419()
941 $mails = DBA::select('mail', ['id', 'from-url', 'uri', 'parent-uri', 'guid'], [], ['order' => ['id']]);
942 while ($mail = DBA::fetch($mails)) {
944 $fields['author-id'] = Contact::getIdForURL($mail['from-url'], 0, false);
945 if (empty($fields['author-id'])) {
949 $fields['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
950 $fields['parent-uri-id'] = ItemURI::getIdByURI($mail['parent-uri']);
952 $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
953 if (!empty($reply)) {
954 $fields['thr-parent'] = $reply['uri'];
955 if (!empty($reply['uri-id'])) {
956 $fields['thr-parent-id'] = $reply['uri-id'];
958 $fields['thr-parent-id'] = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]);
962 DBA::update('mail', $fields, ['id' => $mail['id']]);
964 return Update::SUCCESS;
967 function update_1429()
969 if (!DBA::e("UPDATE `contact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
970 return Update::FAILED;
973 if (!DBA::e("UPDATE `fcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
974 return Update::FAILED;
977 if (!DBA::e("UPDATE `apcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
978 return Update::FAILED;
981 DI::config()->set("system", "post_update_version", 1423);
983 return Update::SUCCESS;
986 function update_1434()
988 $name = DI::config()->get('storage', 'name');
990 // in case of an empty config, set "Database" as default storage backend
992 DI::config()->set('storage', 'name', \Friendica\Core\Storage\Type\Database::getName());
995 // In case of a Using deprecated storage class value, set the right name for it
996 if (stristr($name, 'Friendica\Model\Storage\\')) {
997 DI::config()->set('storage', 'name', substr($name, 24));
1000 return Update::SUCCESS;
1003 function update_1438()
1005 DBA::update('photo', ['photo-type' => Photo::USER_AVATAR], ['profile' => true]);
1006 DBA::update('photo', ['photo-type' => Photo::CONTACT_AVATAR], ["NOT `profile` AND NOT `contact-id` IS NULL AND `contact-id` != ?", 0]);
1007 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]);
1010 function update_1439()
1012 $intros = DBA::select('intro', ['id', 'fid'], ["NOT `fid` IS NULL AND `fid` != ?", 0]);
1013 while ($intro = DBA::fetch($intros)) {
1014 $fcontact = DBA::selectFirst('fcontact', ['url'], ['id' => $intro['fid']]);
1015 if (!empty($fcontact['url'])) {
1016 $id = Contact::getIdForURL($fcontact['url']);
1018 DBA::update('intro',['suggest-cid' => $id], ['id' => $intro['id']]);
1022 DBA::close($intros);
1025 function update_1440()
1027 // Fix wrong public permissionset
1028 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);
1029 DBA::delete('permissionset', ["`id` != ? AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''", PermissionSet::PUBLIC]);
1031 return Update::SUCCESS;
1034 function update_1441()
1036 $languages = DI::l10n()->getAvailableLanguages();
1038 $albums = [Photo::PROFILE_PHOTOS];
1039 foreach ($languages as $language) {
1040 $albums[] = DI::l10n()->withLang($language)->t(Photo::PROFILE_PHOTOS);
1042 $albums = array_unique($albums);
1044 Photo::update(['photo-type' => Photo::USER_AVATAR], ['album' => $albums]);
1046 return Update::SUCCESS;
1049 function update_1442()
1051 // transform blocked intros into ignored intros
1052 DBA::update('intro', ['ignore' => 1, 'blocked' => 0], ['blocked' => 1]);
1054 return Update::SUCCESS;
1058 * A bug in Contact\User::updateByContactUpdate prevented any update to the user-contact table since the rows have been
1059 * created in version 1435. This version fixes this bug but the user-contact rows are outdated, we need to regenerate
1062 function update_1444()
1064 DBA::e('TRUNCATE TABLE `user-contact`');
1066 $contacts = DBA::select('contact', [], ["`uid` != ?", 0]);
1067 while ($contact = DBA::fetch($contacts)) {
1068 Contact\User::insertForContactArray($contact);
1071 return Update::SUCCESS;
1074 function update_1446()
1076 $distributed_cache_driver_source = DI::config()->getCache()->getSource('system', 'distributed_cache_driver');
1077 $cache_driver_source = DI::config()->getCache()->getSource('system', 'cache_driver');
1079 // In case the distributed cache driver is the default value, but the current cache driver isn't default,
1080 // we assume that the distributed cache driver should be the same as the current cache driver
1082 $distributed_cache_driver_source === \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC
1083 && $cache_driver_source > \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC
1085 DI::config()->set('system', 'distributed_cache_driver', DI::config()->get('system', 'cache_driver'));
1088 return Update::SUCCESS;