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-database 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\Config\ValueObject\Cache;
44 use Friendica\Core\Logger;
45 use Friendica\Core\Protocol;
46 use Friendica\Core\Storage\Capability\ICanReadFromStorage;
47 use Friendica\Core\Storage\Type\Database as DatabaseStorage;
48 use Friendica\Core\Update;
49 use Friendica\Core\Worker;
50 use Friendica\Database\Database;
51 use Friendica\Database\DBA;
52 use Friendica\Database\DBStructure;
54 use Friendica\Model\Contact;
55 use Friendica\Model\Item;
56 use Friendica\Model\ItemURI;
57 use Friendica\Model\Notification;
58 use Friendica\Model\Photo;
59 use Friendica\Model\Post;
60 use Friendica\Model\Profile;
61 use Friendica\Model\User;
62 use Friendica\Security\PermissionSet\Repository\PermissionSet;
63 use Friendica\Worker\Delivery;
65 // Post-update script of PR 5751
66 function update_1298()
68 $keys = ['gender', 'marital', 'sexual'];
69 foreach ($keys as $translateKey) {
70 $allData = DBA::select('profile', ['id', $translateKey]);
71 $allLangs = DI::l10n()->getAvailableLanguages();
74 foreach ($allData as $key => $data) {
75 $toTranslate = $data[$translateKey];
76 if ($toTranslate != '') {
77 foreach ($allLangs as $key => $lang) {
81 // First we get the the localizations
82 if (file_exists('view/lang/$lang/strings.php')) {
83 include 'view/lang/$lang/strings.php';
85 if (file_exists('addon/morechoice/lang/$lang/strings.php')) {
86 include 'addon/morechoice/lang/$lang/strings.php';
89 $localizedStrings = $a->strings;
92 $key = array_search($toTranslate, $localizedStrings);
97 // defaulting to empty string
104 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
105 Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
106 'was' => $data[$translateKey]]);
108 Contact::updateSelfFromUserID($data['id']);
109 Profile::publishUpdate($data['id']);
115 Logger::notice($translateKey . ' fix completed', ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
117 return Update::SUCCESS;
120 function update_1309()
122 $queue = DBA::select('queue', ['id', 'cid', 'guid']);
123 while ($entry = DBA::fetch($queue)) {
124 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
125 if (!DBA::isResult($contact)) {
129 $item = Post::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
130 if (!DBA::isResult($item)) {
134 $deliver_options = ['priority' => Worker::PRIORITY_MEDIUM, 'dont_fork' => true];
135 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
136 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
137 DBA::delete('queue', ['id' => $entry['id']]);
139 return Update::SUCCESS;
142 function update_1315()
144 if (DBStructure::existsTable('item-delivery-data')) {
145 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
147 return Update::SUCCESS;
150 function update_1318()
152 DBA::update('profile', ['marital' => 'In a relation'], ['marital' => 'Unavailable']);
153 DBA::update('profile', ['marital' => 'Single'], ['marital' => 'Available']);
155 Worker::add(Worker::PRIORITY_LOW, 'ProfileUpdate');
156 return Update::SUCCESS;
159 function update_1323()
161 $users = DBA::select('user', ['uid']);
162 while ($user = DBA::fetch($users)) {
163 if (Contact::updateSelfFromUserID($user['uid'])) {
164 Profile::publishUpdate($user['uid']);
169 return Update::SUCCESS;
172 function update_1327()
174 $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
175 while ($contact = DBA::fetch($contacts)) {
176 Contact\User::setBlocked($contact['id'], $contact['uid'], $contact['blocked']);
177 Contact\User::setIgnored($contact['id'], $contact['uid'], $contact['readonly']);
179 DBA::close($contacts);
181 return Update::SUCCESS;
184 function update_1330()
186 $currStorage = DI::config()->get('storage', 'class', '');
188 // set the name of the storage instead of the classpath as config
189 if (!empty($currStorage)) {
190 /** @var ICanReadFromStorage $currStorage */
191 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
192 return Update::FAILED;
195 // try to delete the class since it isn't needed. This won't work with config files
196 DI::config()->delete('storage', 'class');
199 // Update attachments and photos
200 if (!DBA::e("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
201 !DBA::e("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
202 return Update::FAILED;
205 return Update::SUCCESS;
208 function update_1332()
210 $condition = ["`is-default` IS NOT NULL"];
211 $profiles = DBA::select('profile', [], $condition);
213 while ($profile = DBA::fetch($profiles)) {
214 Profile::migrate($profile);
216 DBA::close($profiles);
218 DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
220 return Update::SUCCESS;
223 function update_1347()
225 foreach (Item::ACTIVITIES as $index => $activity) {
226 DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
229 return Update::SUCCESS;
232 function pre_update_1348()
234 if (!DBA::exists('contact', ['id' => 0])) {
235 DBA::insert('contact', ['nurl' => '']);
236 $lastid = DBA::lastInsertId();
238 DBA::update('contact', ['id' => 0], ['id' => $lastid]);
242 // The tables "permissionset" and "tag" could or could not exist during the update.
243 // This depends upon the previous version. Depending upon this situation we have to add
244 // the "0" values before adding the foreign keys - or after would be sufficient.
248 if (DBStructure::existsTable('auth_codes') && DBStructure::existsTable('clients')) {
249 DBA::e("DELETE FROM `auth_codes` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
251 if (DBStructure::existsTable('tokens') && DBStructure::existsTable('clients')) {
252 DBA::e("DELETE FROM `tokens` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
254 return Update::SUCCESS;
257 function update_1348()
259 // Insert a permissionset with id=0
260 // Inserting it without an ID and then changing the value to 0 tricks the auto increment
261 if (!DBA::exists('permissionset', ['id' => 0])) {
262 DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
263 $lastid = DBA::lastInsertId();
265 DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
269 if (!DBA::exists('tag', ['id' => 0])) {
270 DBA::insert('tag', ['name' => '']);
271 $lastid = DBA::lastInsertId();
273 DBA::update('tag', ['id' => 0], ['id' => $lastid]);
277 return Update::SUCCESS;
280 function update_1349()
282 if (!DBStructure::existsTable('item-activity')) {
283 return Update::SUCCESS;
287 foreach (Item::ACTIVITIES as $index => $activity) {
288 if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
294 // The update failed - but it cannot be recovered, since the data doesn't match our expectation
295 // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
296 // the postupdate. This is not fatal, but means that it will take some longer time for the system
298 return Update::SUCCESS;
301 if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
302 SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", Item::GRAVITY_ACTIVITY)) {
303 return Update::FAILED;
306 return Update::SUCCESS;
309 function update_1351()
311 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
312 return Update::FAILED;
315 return Update::SUCCESS;
318 function pre_update_1354()
320 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
321 && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
322 && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
323 return Update::FAILED;
325 return Update::SUCCESS;
328 function update_1354()
330 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
331 && DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])) {
332 if (!DBA::e("UPDATE `contact` SET `ffi_keyword_denylist` = `ffi_keyword_blacklist`")) {
333 return Update::FAILED;
336 // When the data had been copied then the main task is done.
337 // Having the old field removed is only beauty but not crucial.
338 // So we don't care if this was successful or not.
339 DBA::e("ALTER TABLE `contact` DROP `ffi_keyword_blacklist`");
341 return Update::SUCCESS;
344 function update_1357()
346 if (!DBA::e("UPDATE `contact` SET `failed` = true WHERE `success_update` < `failure_update` AND `failed` IS NULL")) {
347 return Update::FAILED;
350 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `success_update` > `failure_update` AND `failed` IS NULL")) {
351 return Update::FAILED;
354 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `updated` > `failure_update` AND `failed` IS NULL")) {
355 return Update::FAILED;
358 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `last-item` > `failure_update` AND `failed` IS NULL")) {
359 return Update::FAILED;
362 if (!DBA::e("UPDATE `gserver` SET `failed` = true WHERE `last_contact` < `last_failure` AND `failed` IS NULL")) {
363 return Update::FAILED;
366 if (!DBA::e("UPDATE `gserver` SET `failed` = false WHERE `last_contact` > `last_failure` AND `failed` IS NULL")) {
367 return Update::FAILED;
370 return Update::SUCCESS;
373 function pre_update_1358()
375 if (!DBA::e("DELETE FROM `contact-relation` WHERE NOT `relation-cid` IN (SELECT `id` FROM `contact`) OR NOT `cid` IN (SELECT `id` FROM `contact`)")) {
376 return Update::FAILED;
379 return Update::SUCCESS;
382 function pre_update_1363()
384 Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
385 return Update::SUCCESS;
388 function pre_update_1364()
390 if (!DBA::e("DELETE FROM `2fa_recovery_codes` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
391 return Update::FAILED;
394 if (!DBA::e("DELETE FROM `2fa_app_specific_password` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
395 return Update::FAILED;
398 if (!DBA::e("DELETE FROM `attach` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
399 return Update::FAILED;
402 if (DBStructure::existsTable('clients') && !DBA::e("DELETE FROM `clients` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
403 return Update::FAILED;
406 if (!DBA::e("DELETE FROM `conv` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
407 return Update::FAILED;
410 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
411 return Update::FAILED;
414 if (!DBA::e("DELETE FROM `group` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
415 return Update::FAILED;
418 if (!DBA::e("DELETE FROM `intro` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
419 return Update::FAILED;
422 if (!DBA::e("DELETE FROM `manage` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
423 return Update::FAILED;
426 if (!DBA::e("DELETE FROM `manage` WHERE NOT `mid` IN (SELECT `uid` FROM `user`)")) {
427 return Update::FAILED;
430 if (!DBA::e("DELETE FROM `mail` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
431 return Update::FAILED;
434 if (!DBA::e("DELETE FROM `mailacct` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
435 return Update::FAILED;
438 if (!DBA::e("DELETE FROM `notify` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
439 return Update::FAILED;
442 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
443 return Update::FAILED;
446 if (!DBA::e("DELETE FROM `pconfig` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
447 return Update::FAILED;
450 if (!DBA::e("DELETE FROM `profile` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
451 return Update::FAILED;
454 if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
455 return Update::FAILED;
458 if (!DBA::e("DELETE FROM `profile_field` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
459 return Update::FAILED;
462 if (!DBA::e("DELETE FROM `push_subscriber` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
463 return Update::FAILED;
466 if (!DBA::e("DELETE FROM `register` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
467 return Update::FAILED;
470 if (!DBA::e("DELETE FROM `search` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
471 return Update::FAILED;
474 if (DBStructure::existsTable('tokens') && !DBA::e("DELETE FROM `tokens` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
475 return Update::FAILED;
478 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
479 return Update::FAILED;
482 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
483 return Update::FAILED;
486 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `receiver-uid` IN (SELECT `uid` FROM `user`)")) {
487 return Update::FAILED;
490 if (!DBA::e("DELETE FROM `event` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
491 return Update::FAILED;
494 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
495 return Update::FAILED;
498 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
499 return Update::FAILED;
502 if (!DBA::e("DELETE FROM `intro` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
503 return Update::FAILED;
506 if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
507 return Update::FAILED;
510 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
511 return Update::FAILED;
514 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `gid` IN (SELECT `id` FROM `group`)")) {
515 return Update::FAILED;
518 if (!DBA::e("DELETE FROM `gserver-tag` WHERE NOT `gserver-id` IN (SELECT `id` FROM `gserver`)")) {
519 return Update::FAILED;
522 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
523 return Update::FAILED;
526 return Update::SUCCESS;
529 function pre_update_1365()
531 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `notify-id` IN (SELECT `id` FROM `notify`)")) {
532 return Update::FAILED;
535 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
536 return Update::FAILED;
539 return Update::SUCCESS;
542 function update_1375()
544 if (!DBA::e("UPDATE `item` SET `thr-parent` = `parent-uri`, `thr-parent-id` = `parent-uri-id` WHERE `thr-parent` = ''")) {
545 return Update::FAILED;
548 return Update::SUCCESS;
551 function pre_update_1376()
553 // Insert a user with uid=0
554 DBStructure::checkInitialValues();
556 if (!DBA::e("DELETE FROM `item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
557 return Update::FAILED;
560 if (!DBA::e("DELETE FROM `event` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
561 return Update::FAILED;
564 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
565 return Update::FAILED;
568 if (!DBA::e("DELETE FROM `permissionset` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
569 return Update::FAILED;
572 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
573 return Update::FAILED;
576 if (!DBA::e("DELETE FROM `post-category` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
577 return Update::FAILED;
580 Photo::delete(["NOT `uid` IN (SELECT `uid` FROM `user`)"]);
582 if (!DBA::e("DELETE FROM `contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
583 return Update::FAILED;
586 return Update::SUCCESS;
589 function pre_update_1377()
591 DBStructure::checkInitialValues();
593 if (!DBA::e("DELETE FROM `item` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
594 return Update::FAILED;
597 if (!DBA::e("DELETE FROM `item` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
598 return Update::FAILED;
601 if (!DBA::e("UPDATE `item` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
602 return Update::FAILED;
605 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
606 return Update::FAILED;
609 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
610 return Update::FAILED;
613 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
614 return Update::FAILED;
617 if (!DBA::e("UPDATE `notify` SET `uri-id` = NULL WHERE `uri-id` = 0")) {
618 return Update::FAILED;
621 if (DBStructure::existsTable('diaspora-interaction') && !DBA::e("DELETE FROM `diaspora-interaction` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
622 return Update::FAILED;
625 if (DBStructure::existsTable('item-activity') && !DBA::e("DELETE FROM `item-activity` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
626 return Update::FAILED;
629 if (DBStructure::existsTable('item-content') && !DBA::e("DELETE FROM `item-content` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
630 return Update::FAILED;
633 if (!DBA::e("DELETE FROM `notify` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
634 return Update::FAILED;
637 if (!DBA::e("UPDATE `notify` SET `parent-uri-id` = NULL WHERE `parent-uri-id` = 0")) {
638 return Update::FAILED;
640 if (!DBA::e("DELETE FROM `notify` WHERE `parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
641 return Update::FAILED;
644 if (!DBA::e("UPDATE `notify-threads` SET `master-parent-uri-id` = NULL WHERE `master-parent-uri-id` = 0")) {
645 return Update::FAILED;
648 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
649 return Update::FAILED;
652 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-item` NOT IN (SELECT `id` FROM `item`)")) {
653 return Update::FAILED;
656 return Update::SUCCESS;
659 function update_1380()
661 if (!DBA::e("UPDATE `notify` INNER JOIN `item` ON `item`.`id` = `notify`.`iid` SET `notify`.`uri-id` = `item`.`uri-id` WHERE `notify`.`uri-id` IS NULL AND `notify`.`otype` IN (?, ?)",
662 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
663 return Update::FAILED;
666 if (!DBA::e("UPDATE `notify` INNER JOIN `item` ON `item`.`id` = `notify`.`parent` SET `notify`.`parent-uri-id` = `item`.`uri-id` WHERE `notify`.`parent-uri-id` IS NULL AND `notify`.`otype` IN (?, ?)",
667 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
668 return Update::FAILED;
671 return Update::SUCCESS;
674 function pre_update_1395()
676 if (DBStructure::existsTable('post-user') && !DBStructure::existsColumn('post-user', ['id']) && !DBA::e("DROP TABLE `post-user`")) {
677 return Update::FAILED;
679 return Update::SUCCESS;
682 function update_1395()
684 if (!DBA::e("INSERT INTO `post-user`(`id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid`)
685 SELECT `id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid` FROM `item`
686 ON DUPLICATE KEY UPDATE `contact-id` = `item`.`contact-id`, `unseen` = `item`.`unseen`, `origin` = `item`.`origin`, `psid` = `item`.`psid`")) {
687 return Update::FAILED;
690 if (DBStructure::existsTable('user-item') && !DBA::e("INSERT INTO `post-user`(`uri-id`, `uid`, `hidden`, `notification-type`)
691 SELECT `uri-id`, `user-item`.`uid`, `hidden`,`notification-type` FROM `user-item`
692 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid`
693 ON DUPLICATE KEY UPDATE `hidden` = `user-item`.`hidden`, `notification-type` = `user-item`.`notification-type`")) {
694 return Update::FAILED;
696 return Update::SUCCESS;
699 function update_1396()
701 if (!DBStructure::existsTable('item-content')) {
702 return Update::SUCCESS;
705 if (DBStructure::existsColumn('item-content', ['raw-body'])) {
706 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`, `raw-body`,
707 `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
708 `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
709 SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
710 `item-content`.`body`, `item-content`.`raw-body`, `item-content`.`location`, `item-content`.`coord`,
711 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
712 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
713 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
714 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
715 return Update::FAILED;
718 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`,
719 `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
720 `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
721 SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
722 `item-content`.`body`, `item-content`.`location`, `item-content`.`coord`,
723 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
724 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
725 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
726 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
727 return Update::FAILED;
730 return Update::SUCCESS;
733 function update_1397()
735 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
736 SELECT `uri-id`, `uid`, `notification-type` FROM `post-user` WHERE `notification-type` != 0
737 ON DUPLICATE KEY UPDATE `uri-id` = `post-user`.`uri-id`, `uid` = `post-user`.`uid`, `notification-type` = `post-user`.`notification-type`")) {
738 return Update::FAILED;
741 if (!DBStructure::existsTable('user-item')) {
742 return Update::SUCCESS;
745 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
746 SELECT `uri-id`, `user-item`.`uid`, `notification-type` FROM `user-item`
747 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` WHERE `notification-type` != 0
748 ON DUPLICATE KEY UPDATE `notification-type` = `user-item`.`notification-type`")) {
749 return Update::FAILED;
752 if (!DBStructure::existsTable('thread')) {
753 return Update::SUCCESS;
756 if (!DBA::e("INSERT IGNORE INTO `post-thread-user`(`uri-id`, `uid`, `pinned`, `starred`, `ignored`, `wall`, `pubmail`, `forum_mode`)
757 SELECT `thread`.`uri-id`, `thread`.`uid`, `user-item`.`pinned`, `thread`.`starred`,
758 `thread`.`ignored`, `thread`.`wall`, `thread`.`pubmail`, `thread`.`forum_mode`
759 FROM `thread` LEFT JOIN `user-item` ON `user-item`.`iid` = `thread`.`iid`")) {
760 return Update::FAILED;
763 return Update::SUCCESS;
766 function update_1398()
768 if (!DBStructure::existsTable('thread')) {
769 return Update::SUCCESS;
772 if (!DBA::e("INSERT IGNORE INTO `post-thread` (`uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented`)
773 SELECT `uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented` FROM `thread`")) {
774 return Update::FAILED;
777 if (!DBStructure::existsTable('thread')) {
778 return Update::SUCCESS;
781 if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `thread` ON `thread`.`uid` = `post-thread-user`.`uid` AND `thread`.`uri-id` = `post-thread-user`.`uri-id`
782 SET `post-thread-user`.`mention` = `thread`.`mention`")) {
783 return Update::FAILED;
786 return Update::SUCCESS;
789 function update_1399()
791 if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-user` ON `post-user`.`uid` = `post-thread-user`.`uid` AND `post-user`.`uri-id` = `post-thread-user`.`uri-id`
792 SET `post-thread-user`.`contact-id` = `post-user`.`contact-id`, `post-thread-user`.`unseen` = `post-user`.`unseen`,
793 `post-thread-user`.`hidden` = `post-user`.`hidden`, `post-thread-user`.`origin` = `post-user`.`origin`,
794 `post-thread-user`.`psid` = `post-user`.`psid`, `post-thread-user`.`post-user-id` = `post-user`.`id`")) {
795 return Update::FAILED;
798 return Update::SUCCESS;
801 function update_1400()
803 if (!DBA::e("INSERT IGNORE INTO `post` (`uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`,
804 `created`, `received`, `edited`, `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global`)
805 SELECT `uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `edited`,
806 `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global` FROM `item`")) {
807 return Update::FAILED;
810 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
811 INNER JOIN `event` ON `item`.`event-id` = `event`.`id` AND `event`.`id` != 0
812 SET `post-user`.`event-id` = `item`.`event-id`")) {
813 return Update::FAILED;
816 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
817 SET `post-user`.`wall` = `item`.`wall`, `post-user`.`parent-uri-id` = `item`.`parent-uri-id`,
818 `post-user`.`thr-parent-id` = `item`.`thr-parent-id`,
819 `post-user`.`created` = `item`.`created`, `post-user`.`edited` = `item`.`edited`,
820 `post-user`.`received` = `item`.`received`, `post-user`.`gravity` = `item`.`gravity`,
821 `post-user`.`network` = `item`.`network`, `post-user`.`owner-id` = `item`.`owner-id`,
822 `post-user`.`author-id` = `item`.`author-id`, `post-user`.`causer-id` = `item`.`causer-id`,
823 `post-user`.`post-type` = `item`.`post-type`, `post-user`.`vid` = `item`.`vid`,
824 `post-user`.`private` = `item`.`private`, `post-user`.`global` = `item`.`global`,
825 `post-user`.`visible` = `item`.`visible`, `post-user`.`deleted` = `item`.`deleted`")) {
826 return Update::FAILED;
829 if (!DBA::e("INSERT IGNORE INTO `post-thread-user` (`uri-id`, `owner-id`, `author-id`, `causer-id`, `network`,
830 `created`, `received`, `changed`, `commented`, `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `post-user-id`)
831 SELECT `uri-id`, `owner-id`, `author-id`, `causer-id`, `network`, `created`, `received`, `received`, `received`,
832 `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `id`
833 FROM `post-user` WHERE `gravity` = 0 AND NOT EXISTS(SELECT `uri-id` FROM `post-thread-user` WHERE `post-user-id` = `post-user`.id)")) {
834 return Update::FAILED;
837 if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-thread` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id`
838 SET `post-thread-user`.`owner-id` = `post-thread`.`owner-id`, `post-thread-user`.`author-id` = `post-thread`.`author-id`,
839 `post-thread-user`.`causer-id` = `post-thread`.`causer-id`, `post-thread-user`.`network` = `post-thread`.`network`,
840 `post-thread-user`.`created` = `post-thread`.`created`, `post-thread-user`.`received` = `post-thread`.`received`,
841 `post-thread-user`.`changed` = `post-thread`.`changed`, `post-thread-user`.`commented` = `post-thread`.`commented`")) {
842 return Update::FAILED;
845 return Update::SUCCESS;
848 function pre_update_1403()
850 // Necessary before a primary key change
851 if (DBStructure::existsTable('parsed_url') && !DBA::e("DROP TABLE `parsed_url`")) {
852 return Update::FAILED;
855 return Update::SUCCESS;
858 function update_1404()
860 $tasks = DBA::select('workerqueue', ['id', 'command', 'parameter'], ['command' => ['notifier', 'delivery', 'apdelivery', 'done' => false]]);
861 while ($task = DBA::fetch($tasks)) {
862 $parameters = json_decode($task['parameter'], true);
864 if (is_array($parameters) && count($parameters) && in_array($parameters[0], [Delivery::MAIL, Delivery::SUGGESTION, Delivery::REMOVAL, Delivery::RELOCATION])) {
868 switch (strtolower($task['command'])) {
870 if (count($parameters) == 3) {
873 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
874 if (!DBA::isResult($item)) {
878 $parameters[1] = $item['uri-id'];
879 $parameters[2] = $item['uid'];
882 if (count($parameters) == 4) {
885 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
886 if (!DBA::isResult($item)) {
890 $parameters[1] = $item['uri-id'];
891 $parameters[3] = $item['uid'];
894 if (count($parameters) == 6) {
898 if (empty($parameters[4])) {
902 $item = DBA::selectFirst('item', ['uri-id'], ['id' => $parameters[1]]);
903 if (!DBA::isResult($item)) {
907 $parameters[5] = $item['uri-id'];
912 DBA::update('workerqueue', ['parameter' => json_encode($parameters)], ['id' => $task['id']]);
914 return Update::SUCCESS;
918 function update_1407()
920 if (!DBA::e("UPDATE `post` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
921 return Update::FAILED;
923 if (!DBA::e("UPDATE `post-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
924 return Update::FAILED;
926 if (!DBA::e("UPDATE `post-thread` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
927 return Update::FAILED;
929 if (!DBA::e("UPDATE `post-thread-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
930 return Update::FAILED;
933 return Update::SUCCESS;
936 function update_1413()
938 if (!DBA::e("UPDATE `post-user` SET `post-reason` = `post-type` WHERE `post-type` >= 64 and `post-type` <= 75")) {
939 return Update::FAILED;
943 function update_1419()
945 $mails = DBA::select('mail', ['id', 'from-url', 'uri', 'parent-uri', 'guid'], [], ['order' => ['id']]);
946 while ($mail = DBA::fetch($mails)) {
948 $fields['author-id'] = Contact::getIdForURL($mail['from-url'], 0, false);
949 if (empty($fields['author-id'])) {
953 $fields['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
954 $fields['parent-uri-id'] = ItemURI::getIdByURI($mail['parent-uri']);
956 $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
957 if (!empty($reply)) {
958 $fields['thr-parent'] = $reply['uri'];
959 if (!empty($reply['uri-id'])) {
960 $fields['thr-parent-id'] = $reply['uri-id'];
962 $fields['thr-parent-id'] = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]);
966 DBA::update('mail', $fields, ['id' => $mail['id']]);
968 return Update::SUCCESS;
971 function update_1429()
973 if (!DBA::e("UPDATE `contact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
974 return Update::FAILED;
977 if (!DBA::e("UPDATE `fcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
978 return Update::FAILED;
981 if (!DBA::e("UPDATE `apcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
982 return Update::FAILED;
985 DI::config()->set('system', 'post_update_version', 1423);
987 return Update::SUCCESS;
990 function update_1434()
992 $name = DI::config()->get('storage', 'name');
994 // In case of an empty config, set "Database" as default storage backend
996 DI::config()->set('storage', 'name', DatabaseStorage::getName());
999 // In case of a Using deprecated storage class value, set the right name for it
1000 if (stristr($name, 'Friendica\Model\Storage\\')) {
1001 DI::config()->set('storage', 'name', substr($name, 24));
1004 return Update::SUCCESS;
1007 function update_1438()
1009 DBA::update('photo', ['photo-type' => Photo::USER_AVATAR], ['profile' => true]);
1010 DBA::update('photo', ['photo-type' => Photo::CONTACT_AVATAR], ["NOT `profile` AND NOT `contact-id` IS NULL AND `contact-id` != ?", 0]);
1011 DBA::update('photo', ['photo-type' => Photo::DEFAULT], ["NOT `profile` AND (`contact-id` IS NULL OR `contact-id` = ?) AND `photo-type` IS NULL AND `album` != ?", 0, Photo::CONTACT_PHOTOS]);
1014 function update_1439()
1016 $intros = DBA::select('intro', ['id', 'fid'], ["NOT `fid` IS NULL AND `fid` != ?", 0]);
1017 while ($intro = DBA::fetch($intros)) {
1018 $fcontact = DBA::selectFirst('fcontact', ['url'], ['id' => $intro['fid']]);
1019 if (!empty($fcontact['url'])) {
1020 $id = Contact::getIdForURL($fcontact['url']);
1022 DBA::update('intro',['suggest-cid' => $id], ['id' => $intro['id']]);
1026 DBA::close($intros);
1029 function update_1440()
1031 // Fix wrong public permissionset
1032 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);
1033 DBA::delete('permissionset', ["`id` != ? AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''", PermissionSet::PUBLIC]);
1035 return Update::SUCCESS;
1038 function update_1441()
1040 $languages = DI::l10n()->getAvailableLanguages();
1042 $albums = [Photo::PROFILE_PHOTOS];
1043 foreach ($languages as $language) {
1044 $albums[] = DI::l10n()->withLang($language)->t(Photo::PROFILE_PHOTOS);
1046 $albums = array_unique($albums);
1048 Photo::update(['photo-type' => Photo::USER_AVATAR], ['album' => $albums]);
1050 return Update::SUCCESS;
1053 function update_1442()
1055 // transform blocked intros into ignored intros
1056 DBA::update('intro', ['ignore' => 1, 'blocked' => 0], ['blocked' => 1]);
1058 return Update::SUCCESS;
1062 * A bug in Contact\User::updateByContactUpdate prevented any update to the user-contact table since the rows have been
1063 * created in version 1435. This version fixes this bug but the user-contact rows are outdated, we need to regenerate
1066 function update_1444()
1068 DBA::e('TRUNCATE TABLE `user-contact`');
1070 $contacts = DBA::select('contact', [], ["`uid` != ?", 0]);
1071 while ($contact = DBA::fetch($contacts)) {
1072 Contact\User::insertForContactArray($contact);
1075 return Update::SUCCESS;
1078 function update_1446()
1080 $distributed_cache_driver_source = DI::config()->getCache()->getSource('system', 'distributed_cache_driver');
1081 $cache_driver_source = DI::config()->getCache()->getSource('system', 'cache_driver');
1083 // In case the distributed cache driver is the default value, but the current cache driver isn't default,
1084 // we assume that the distributed cache driver should be the same as the current cache driver
1085 if ($distributed_cache_driver_source === Cache::SOURCE_STATIC && $cache_driver_source > Cache::SOURCE_STATIC) {
1086 DI::config()->set('system', 'distributed_cache_driver', DI::config()->get('system', 'cache_driver'));
1089 return Update::SUCCESS;
1092 function update_1451()
1094 DBA::update('user', ['account-type' => User::ACCOUNT_TYPE_COMMUNITY], ['page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]]);
1095 DBA::update('contact', ['contact-type' => Contact::TYPE_COMMUNITY], ["`forum` OR `prv`"]);
1096 DBA::update('contact', ['manually-approve' => true], ['prv' => true]);
1098 return Update::SUCCESS;
1101 function update_1457()
1103 $pinned = DBA::select('post-thread-user', ['uri-id', 'author-id'], ['pinned' => true]);
1104 while ($post = DBA::fetch($pinned)) {
1105 Post\Collection::add($post['uri-id'], Post\Collection::FEATURED, $post['author-id']);
1107 DBA::close($pinned);
1109 return Update::SUCCESS;
1112 function update_1480()
1114 DBA::update('contact', ['next-update' => DBA::NULL_DATETIME], ['network' => Protocol::FEDERATED]);
1115 DBA::update('post', ['deleted' => false], ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE NOT `deleted`)"]);
1116 return Update::SUCCESS;
1119 function update_1481()
1121 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");
1122 return Update::SUCCESS;
1125 function update_1491()
1127 DBA::update('contact', ['remote_self' => Contact::MIRROR_OWN_POST], ['remote_self' => Contact::MIRROR_FORWARDED]);
1128 return Update::SUCCESS;
1131 function update_1497()
1133 DBA::e("UPDATE `user` SET `last-activity` = DATE(`login_date`) WHERE `last-activity` IS NULL");
1134 return Update::SUCCESS;