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\Model\User;
59 use Friendica\Security\PermissionSet\Repository\PermissionSet;
60 use Friendica\Worker\Delivery;
62 // Post-update script of PR 5751
63 function update_1298()
65 $keys = ['gender', 'marital', 'sexual'];
66 foreach ($keys as $translateKey) {
67 $allData = DBA::select('profile', ['id', $translateKey]);
68 $allLangs = DI::l10n()->getAvailableLanguages();
71 foreach ($allData as $key => $data) {
72 $toTranslate = $data[$translateKey];
73 if ($toTranslate != '') {
74 foreach ($allLangs as $key => $lang) {
78 // First we get the the localizations
79 if (file_exists("view/lang/$lang/strings.php")) {
80 include "view/lang/$lang/strings.php";
82 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
83 include "addon/morechoice/lang/$lang/strings.php";
86 $localizedStrings = $a->strings;
89 $key = array_search($toTranslate, $localizedStrings);
94 // defaulting to empty string
101 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
102 Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
103 'was' => $data[$translateKey]]);
105 Contact::updateSelfFromUserID($data['id']);
106 Profile::publishUpdate($data['id']);
112 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
114 return Update::SUCCESS;
117 function update_1309()
119 $queue = DBA::select('queue', ['id', 'cid', 'guid']);
120 while ($entry = DBA::fetch($queue)) {
121 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
122 if (!DBA::isResult($contact)) {
126 $item = Post::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
127 if (!DBA::isResult($item)) {
131 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
132 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
133 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
134 DBA::delete('queue', ['id' => $entry['id']]);
136 return Update::SUCCESS;
139 function update_1315()
141 if (DBStructure::existsTable('item-delivery-data')) {
142 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
144 return Update::SUCCESS;
147 function update_1318()
149 DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
150 DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
152 Worker::add(PRIORITY_LOW, 'ProfileUpdate');
153 return Update::SUCCESS;
156 function update_1323()
158 $users = DBA::select('user', ['uid']);
159 while ($user = DBA::fetch($users)) {
160 if (Contact::updateSelfFromUserID($user['uid'])) {
161 Profile::publishUpdate($user['uid']);
166 return Update::SUCCESS;
169 function update_1327()
171 $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
172 while ($contact = DBA::fetch($contacts)) {
173 Contact\User::setBlocked($contact['id'], $contact['uid'], $contact['blocked']);
174 Contact\User::setIgnored($contact['id'], $contact['uid'], $contact['readonly']);
176 DBA::close($contacts);
178 return Update::SUCCESS;
181 function update_1330()
183 $currStorage = DI::config()->get('storage', 'class', '');
185 // set the name of the storage instead of the classpath as config
186 if (!empty($currStorage)) {
187 /** @var ICanReadFromStorage $currStorage */
188 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
189 return Update::FAILED;
192 // try to delete the class since it isn't needed. This won't work with config files
193 DI::config()->delete('storage', 'class');
196 // Update attachments and photos
197 if (!DBA::e("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
198 !DBA::e("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
199 return Update::FAILED;
202 return Update::SUCCESS;
205 function update_1332()
207 $condition = ["`is-default` IS NOT NULL"];
208 $profiles = DBA::select('profile', [], $condition);
210 while ($profile = DBA::fetch($profiles)) {
211 Profile::migrate($profile);
213 DBA::close($profiles);
215 DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
217 return Update::SUCCESS;
220 function update_1347()
222 foreach (Item::ACTIVITIES as $index => $activity) {
223 DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
226 return Update::SUCCESS;
229 function pre_update_1348()
231 if (!DBA::exists('contact', ['id' => 0])) {
232 DBA::insert('contact', ['nurl' => '']);
233 $lastid = DBA::lastInsertId();
235 DBA::update('contact', ['id' => 0], ['id' => $lastid]);
239 // The tables "permissionset" and "tag" could or could not exist during the update.
240 // This depends upon the previous version. Depending upon this situation we have to add
241 // the "0" values before adding the foreign keys - or after would be sufficient.
245 if (DBStructure::existsTable('auth_codes') && DBStructure::existsTable('clients')) {
246 DBA::e("DELETE FROM `auth_codes` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
248 if (DBStructure::existsTable('tokens') && DBStructure::existsTable('clients')) {
249 DBA::e("DELETE FROM `tokens` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
251 return Update::SUCCESS;
254 function update_1348()
256 // Insert a permissionset with id=0
257 // Inserting it without an ID and then changing the value to 0 tricks the auto increment
258 if (!DBA::exists('permissionset', ['id' => 0])) {
259 DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
260 $lastid = DBA::lastInsertId();
262 DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
266 if (!DBA::exists('tag', ['id' => 0])) {
267 DBA::insert('tag', ['name' => '']);
268 $lastid = DBA::lastInsertId();
270 DBA::update('tag', ['id' => 0], ['id' => $lastid]);
274 return Update::SUCCESS;
277 function update_1349()
279 if (!DBStructure::existsTable('item-activity')) {
280 return Update::SUCCESS;
284 foreach (Item::ACTIVITIES as $index => $activity) {
285 if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
291 // The update failed - but it cannot be recovered, since the data doesn't match our expectation
292 // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
293 // the postupdate. This is not fatal, but means that it will take some longer time for the system
295 return Update::SUCCESS;
298 if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
299 SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", GRAVITY_ACTIVITY)) {
300 return Update::FAILED;
303 return Update::SUCCESS;
306 function update_1351()
308 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
309 return Update::FAILED;
312 return Update::SUCCESS;
315 function pre_update_1354()
317 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
318 && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
319 && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
320 return Update::FAILED;
322 return Update::SUCCESS;
325 function update_1354()
327 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
328 && DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])) {
329 if (!DBA::e("UPDATE `contact` SET `ffi_keyword_denylist` = `ffi_keyword_blacklist`")) {
330 return Update::FAILED;
333 // When the data had been copied then the main task is done.
334 // Having the old field removed is only beauty but not crucial.
335 // So we don't care if this was successful or not.
336 DBA::e("ALTER TABLE `contact` DROP `ffi_keyword_blacklist`");
338 return Update::SUCCESS;
341 function update_1357()
343 if (!DBA::e("UPDATE `contact` SET `failed` = true WHERE `success_update` < `failure_update` AND `failed` IS NULL")) {
344 return Update::FAILED;
347 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `success_update` > `failure_update` AND `failed` IS NULL")) {
348 return Update::FAILED;
351 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `updated` > `failure_update` AND `failed` IS NULL")) {
352 return Update::FAILED;
355 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `last-item` > `failure_update` AND `failed` IS NULL")) {
356 return Update::FAILED;
359 if (!DBA::e("UPDATE `gserver` SET `failed` = true WHERE `last_contact` < `last_failure` AND `failed` IS NULL")) {
360 return Update::FAILED;
363 if (!DBA::e("UPDATE `gserver` SET `failed` = false WHERE `last_contact` > `last_failure` AND `failed` IS NULL")) {
364 return Update::FAILED;
367 return Update::SUCCESS;
370 function pre_update_1358()
372 if (!DBA::e("DELETE FROM `contact-relation` WHERE NOT `relation-cid` IN (SELECT `id` FROM `contact`) OR NOT `cid` IN (SELECT `id` FROM `contact`)")) {
373 return Update::FAILED;
376 return Update::SUCCESS;
379 function pre_update_1363()
381 Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
382 return Update::SUCCESS;
385 function pre_update_1364()
387 if (!DBA::e("DELETE FROM `2fa_recovery_codes` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
388 return Update::FAILED;
391 if (!DBA::e("DELETE FROM `2fa_app_specific_password` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
392 return Update::FAILED;
395 if (!DBA::e("DELETE FROM `attach` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
396 return Update::FAILED;
399 if (DBStructure::existsTable('clients') && !DBA::e("DELETE FROM `clients` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
400 return Update::FAILED;
403 if (!DBA::e("DELETE FROM `conv` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
404 return Update::FAILED;
407 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
408 return Update::FAILED;
411 if (!DBA::e("DELETE FROM `group` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
412 return Update::FAILED;
415 if (!DBA::e("DELETE FROM `intro` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
416 return Update::FAILED;
419 if (!DBA::e("DELETE FROM `manage` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
420 return Update::FAILED;
423 if (!DBA::e("DELETE FROM `manage` WHERE NOT `mid` IN (SELECT `uid` FROM `user`)")) {
424 return Update::FAILED;
427 if (!DBA::e("DELETE FROM `mail` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
428 return Update::FAILED;
431 if (!DBA::e("DELETE FROM `mailacct` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
432 return Update::FAILED;
435 if (!DBA::e("DELETE FROM `notify` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
436 return Update::FAILED;
439 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
440 return Update::FAILED;
443 if (!DBA::e("DELETE FROM `pconfig` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
444 return Update::FAILED;
447 if (!DBA::e("DELETE FROM `profile` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
448 return Update::FAILED;
451 if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
452 return Update::FAILED;
455 if (!DBA::e("DELETE FROM `profile_field` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
456 return Update::FAILED;
459 if (!DBA::e("DELETE FROM `push_subscriber` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
460 return Update::FAILED;
463 if (!DBA::e("DELETE FROM `register` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
464 return Update::FAILED;
467 if (!DBA::e("DELETE FROM `search` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
468 return Update::FAILED;
471 if (DBStructure::existsTable('tokens') && !DBA::e("DELETE FROM `tokens` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
472 return Update::FAILED;
475 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
476 return Update::FAILED;
479 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
480 return Update::FAILED;
483 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `receiver-uid` IN (SELECT `uid` FROM `user`)")) {
484 return Update::FAILED;
487 if (!DBA::e("DELETE FROM `event` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
488 return Update::FAILED;
491 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
492 return Update::FAILED;
495 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
496 return Update::FAILED;
499 if (!DBA::e("DELETE FROM `intro` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
500 return Update::FAILED;
503 if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
504 return Update::FAILED;
507 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
508 return Update::FAILED;
511 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `gid` IN (SELECT `id` FROM `group`)")) {
512 return Update::FAILED;
515 if (!DBA::e("DELETE FROM `gserver-tag` WHERE NOT `gserver-id` IN (SELECT `id` FROM `gserver`)")) {
516 return Update::FAILED;
519 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
520 return Update::FAILED;
523 return Update::SUCCESS;
526 function pre_update_1365()
528 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `notify-id` IN (SELECT `id` FROM `notify`)")) {
529 return Update::FAILED;
532 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
533 return Update::FAILED;
536 return Update::SUCCESS;
539 function update_1375()
541 if (!DBA::e("UPDATE `item` SET `thr-parent` = `parent-uri`, `thr-parent-id` = `parent-uri-id` WHERE `thr-parent` = ''")) {
542 return Update::FAILED;
545 return Update::SUCCESS;
548 function pre_update_1376()
550 // Insert a user with uid=0
551 DBStructure::checkInitialValues();
553 if (!DBA::e("DELETE FROM `item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
554 return Update::FAILED;
557 if (!DBA::e("DELETE FROM `event` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
558 return Update::FAILED;
561 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
562 return Update::FAILED;
565 if (!DBA::e("DELETE FROM `permissionset` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
566 return Update::FAILED;
569 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
570 return Update::FAILED;
573 if (!DBA::e("DELETE FROM `post-category` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
574 return Update::FAILED;
577 Photo::delete(["NOT `uid` IN (SELECT `uid` FROM `user`)"]);
579 if (!DBA::e("DELETE FROM `contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
580 return Update::FAILED;
583 return Update::SUCCESS;
586 function pre_update_1377()
588 DBStructure::checkInitialValues();
590 if (!DBA::e("DELETE FROM `item` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
591 return Update::FAILED;
594 if (!DBA::e("DELETE FROM `item` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
595 return Update::FAILED;
598 if (!DBA::e("UPDATE `item` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
599 return Update::FAILED;
602 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
603 return Update::FAILED;
606 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
607 return Update::FAILED;
610 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
611 return Update::FAILED;
614 if (!DBA::e("UPDATE `notify` SET `uri-id` = NULL WHERE `uri-id` = 0")) {
615 return Update::FAILED;
618 if (DBStructure::existsTable('diaspora-interaction') && !DBA::e("DELETE FROM `diaspora-interaction` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
619 return Update::FAILED;
622 if (DBStructure::existsTable('item-activity') && !DBA::e("DELETE FROM `item-activity` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
623 return Update::FAILED;
626 if (DBStructure::existsTable('item-content') && !DBA::e("DELETE FROM `item-content` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
627 return Update::FAILED;
630 if (!DBA::e("DELETE FROM `notify` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
631 return Update::FAILED;
634 if (!DBA::e("UPDATE `notify` SET `parent-uri-id` = NULL WHERE `parent-uri-id` = 0")) {
635 return Update::FAILED;
637 if (!DBA::e("DELETE FROM `notify` WHERE `parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
638 return Update::FAILED;
641 if (!DBA::e("UPDATE `notify-threads` SET `master-parent-uri-id` = NULL WHERE `master-parent-uri-id` = 0")) {
642 return Update::FAILED;
645 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
646 return Update::FAILED;
649 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-item` NOT IN (SELECT `id` FROM `item`)")) {
650 return Update::FAILED;
653 return Update::SUCCESS;
656 function update_1380()
658 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 (?, ?)",
659 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
660 return Update::FAILED;
663 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 (?, ?)",
664 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
665 return Update::FAILED;
668 return Update::SUCCESS;
671 function pre_update_1395()
673 if (DBStructure::existsTable('post-user') && !DBStructure::existsColumn('post-user', ['id']) && !DBA::e("DROP TABLE `post-user`")) {
674 return Update::FAILED;
676 return Update::SUCCESS;
679 function update_1395()
681 if (!DBA::e("INSERT INTO `post-user`(`id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid`)
682 SELECT `id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid` FROM `item`
683 ON DUPLICATE KEY UPDATE `contact-id` = `item`.`contact-id`, `unseen` = `item`.`unseen`, `origin` = `item`.`origin`, `psid` = `item`.`psid`")) {
684 return Update::FAILED;
687 if (DBStructure::existsTable('user-item') && !DBA::e("INSERT INTO `post-user`(`uri-id`, `uid`, `hidden`, `notification-type`)
688 SELECT `uri-id`, `user-item`.`uid`, `hidden`,`notification-type` FROM `user-item`
689 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid`
690 ON DUPLICATE KEY UPDATE `hidden` = `user-item`.`hidden`, `notification-type` = `user-item`.`notification-type`")) {
691 return Update::FAILED;
693 return Update::SUCCESS;
696 function update_1396()
698 if (!DBStructure::existsTable('item-content')) {
699 return Update::SUCCESS;
702 if (DBStructure::existsColumn('item-content', ['raw-body'])) {
703 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`, `raw-body`,
704 `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
705 `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
706 SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
707 `item-content`.`body`, `item-content`.`raw-body`, `item-content`.`location`, `item-content`.`coord`,
708 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
709 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
710 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
711 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
712 return Update::FAILED;
715 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`,
716 `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
717 `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
718 SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
719 `item-content`.`body`, `item-content`.`location`, `item-content`.`coord`,
720 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
721 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
722 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
723 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
724 return Update::FAILED;
727 return Update::SUCCESS;
730 function update_1397()
732 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
733 SELECT `uri-id`, `uid`, `notification-type` FROM `post-user` WHERE `notification-type` != 0
734 ON DUPLICATE KEY UPDATE `uri-id` = `post-user`.`uri-id`, `uid` = `post-user`.`uid`, `notification-type` = `post-user`.`notification-type`")) {
735 return Update::FAILED;
738 if (!DBStructure::existsTable('user-item')) {
739 return Update::SUCCESS;
742 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
743 SELECT `uri-id`, `user-item`.`uid`, `notification-type` FROM `user-item`
744 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` WHERE `notification-type` != 0
745 ON DUPLICATE KEY UPDATE `notification-type` = `user-item`.`notification-type`")) {
746 return Update::FAILED;
749 if (!DBStructure::existsTable('thread')) {
750 return Update::SUCCESS;
753 if (!DBA::e("INSERT IGNORE INTO `post-thread-user`(`uri-id`, `uid`, `pinned`, `starred`, `ignored`, `wall`, `pubmail`, `forum_mode`)
754 SELECT `thread`.`uri-id`, `thread`.`uid`, `user-item`.`pinned`, `thread`.`starred`,
755 `thread`.`ignored`, `thread`.`wall`, `thread`.`pubmail`, `thread`.`forum_mode`
756 FROM `thread` LEFT JOIN `user-item` ON `user-item`.`iid` = `thread`.`iid`")) {
757 return Update::FAILED;
760 return Update::SUCCESS;
763 function update_1398()
765 if (!DBStructure::existsTable('thread')) {
766 return Update::SUCCESS;
769 if (!DBA::e("INSERT IGNORE INTO `post-thread` (`uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented`)
770 SELECT `uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented` FROM `thread`")) {
771 return Update::FAILED;
774 if (!DBStructure::existsTable('thread')) {
775 return Update::SUCCESS;
778 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`
779 SET `post-thread-user`.`mention` = `thread`.`mention`")) {
780 return Update::FAILED;
783 return Update::SUCCESS;
786 function update_1399()
788 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`
789 SET `post-thread-user`.`contact-id` = `post-user`.`contact-id`, `post-thread-user`.`unseen` = `post-user`.`unseen`,
790 `post-thread-user`.`hidden` = `post-user`.`hidden`, `post-thread-user`.`origin` = `post-user`.`origin`,
791 `post-thread-user`.`psid` = `post-user`.`psid`, `post-thread-user`.`post-user-id` = `post-user`.`id`")) {
792 return Update::FAILED;
795 return Update::SUCCESS;
798 function update_1400()
800 if (!DBA::e("INSERT IGNORE INTO `post` (`uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`,
801 `created`, `received`, `edited`, `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global`)
802 SELECT `uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `edited`,
803 `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global` FROM `item`")) {
804 return Update::FAILED;
807 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
808 INNER JOIN `event` ON `item`.`event-id` = `event`.`id` AND `event`.`id` != 0
809 SET `post-user`.`event-id` = `item`.`event-id`")) {
810 return Update::FAILED;
813 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
814 SET `post-user`.`wall` = `item`.`wall`, `post-user`.`parent-uri-id` = `item`.`parent-uri-id`,
815 `post-user`.`thr-parent-id` = `item`.`thr-parent-id`,
816 `post-user`.`created` = `item`.`created`, `post-user`.`edited` = `item`.`edited`,
817 `post-user`.`received` = `item`.`received`, `post-user`.`gravity` = `item`.`gravity`,
818 `post-user`.`network` = `item`.`network`, `post-user`.`owner-id` = `item`.`owner-id`,
819 `post-user`.`author-id` = `item`.`author-id`, `post-user`.`causer-id` = `item`.`causer-id`,
820 `post-user`.`post-type` = `item`.`post-type`, `post-user`.`vid` = `item`.`vid`,
821 `post-user`.`private` = `item`.`private`, `post-user`.`global` = `item`.`global`,
822 `post-user`.`visible` = `item`.`visible`, `post-user`.`deleted` = `item`.`deleted`")) {
823 return Update::FAILED;
826 if (!DBA::e("INSERT IGNORE INTO `post-thread-user` (`uri-id`, `owner-id`, `author-id`, `causer-id`, `network`,
827 `created`, `received`, `changed`, `commented`, `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `post-user-id`)
828 SELECT `uri-id`, `owner-id`, `author-id`, `causer-id`, `network`, `created`, `received`, `received`, `received`,
829 `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `id`
830 FROM `post-user` WHERE `gravity` = 0 AND NOT EXISTS(SELECT `uri-id` FROM `post-thread-user` WHERE `post-user-id` = `post-user`.id)")) {
831 return Update::FAILED;
834 if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-thread` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id`
835 SET `post-thread-user`.`owner-id` = `post-thread`.`owner-id`, `post-thread-user`.`author-id` = `post-thread`.`author-id`,
836 `post-thread-user`.`causer-id` = `post-thread`.`causer-id`, `post-thread-user`.`network` = `post-thread`.`network`,
837 `post-thread-user`.`created` = `post-thread`.`created`, `post-thread-user`.`received` = `post-thread`.`received`,
838 `post-thread-user`.`changed` = `post-thread`.`changed`, `post-thread-user`.`commented` = `post-thread`.`commented`")) {
839 return Update::FAILED;
842 return Update::SUCCESS;
845 function pre_update_1403()
847 // Necessary before a primary key change
848 if (DBStructure::existsTable('parsed_url') && !DBA::e("DROP TABLE `parsed_url`")) {
849 return Update::FAILED;
852 return Update::SUCCESS;
855 function update_1404()
857 $tasks = DBA::select('workerqueue', ['id', 'command', 'parameter'], ['command' => ['notifier', 'delivery', 'apdelivery', 'done' => false]]);
858 while ($task = DBA::fetch($tasks)) {
859 $parameters = json_decode($task['parameter'], true);
861 if (is_array($parameters) && count($parameters) && in_array($parameters[0], [Delivery::MAIL, Delivery::SUGGESTION, Delivery::REMOVAL, Delivery::RELOCATION])) {
865 switch (strtolower($task['command'])) {
867 if (count($parameters) == 3) {
870 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
871 if (!DBA::isResult($item)) {
875 $parameters[1] = $item['uri-id'];
876 $parameters[2] = $item['uid'];
879 if (count($parameters) == 4) {
882 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
883 if (!DBA::isResult($item)) {
887 $parameters[1] = $item['uri-id'];
888 $parameters[3] = $item['uid'];
891 if (count($parameters) == 6) {
895 if (empty($parameters[4])) {
899 $item = DBA::selectFirst('item', ['uri-id'], ['id' => $parameters[1]]);
900 if (!DBA::isResult($item)) {
904 $parameters[5] = $item['uri-id'];
909 DBA::update('workerqueue', ['parameter' => json_encode($parameters)], ['id' => $task['id']]);
911 return Update::SUCCESS;
915 function update_1407()
917 if (!DBA::e("UPDATE `post` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
918 return Update::FAILED;
920 if (!DBA::e("UPDATE `post-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
921 return Update::FAILED;
923 if (!DBA::e("UPDATE `post-thread` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
924 return Update::FAILED;
926 if (!DBA::e("UPDATE `post-thread-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
927 return Update::FAILED;
930 return Update::SUCCESS;
933 function update_1413()
935 if (!DBA::e("UPDATE `post-user` SET `post-reason` = `post-type` WHERE `post-type` >= 64 and `post-type` <= 75")) {
936 return Update::FAILED;
940 function update_1419()
942 $mails = DBA::select('mail', ['id', 'from-url', 'uri', 'parent-uri', 'guid'], [], ['order' => ['id']]);
943 while ($mail = DBA::fetch($mails)) {
945 $fields['author-id'] = Contact::getIdForURL($mail['from-url'], 0, false);
946 if (empty($fields['author-id'])) {
950 $fields['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
951 $fields['parent-uri-id'] = ItemURI::getIdByURI($mail['parent-uri']);
953 $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
954 if (!empty($reply)) {
955 $fields['thr-parent'] = $reply['uri'];
956 if (!empty($reply['uri-id'])) {
957 $fields['thr-parent-id'] = $reply['uri-id'];
959 $fields['thr-parent-id'] = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]);
963 DBA::update('mail', $fields, ['id' => $mail['id']]);
965 return Update::SUCCESS;
968 function update_1429()
970 if (!DBA::e("UPDATE `contact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
971 return Update::FAILED;
974 if (!DBA::e("UPDATE `fcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
975 return Update::FAILED;
978 if (!DBA::e("UPDATE `apcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
979 return Update::FAILED;
982 DI::config()->set("system", "post_update_version", 1423);
984 return Update::SUCCESS;
987 function update_1434()
989 $name = DI::config()->get('storage', 'name');
991 // in case of an empty config, set "Database" as default storage backend
993 DI::config()->set('storage', 'name', \Friendica\Core\Storage\Type\Database::getName());
996 // In case of a Using deprecated storage class value, set the right name for it
997 if (stristr($name, 'Friendica\Model\Storage\\')) {
998 DI::config()->set('storage', 'name', substr($name, 24));
1001 return Update::SUCCESS;
1004 function update_1438()
1006 DBA::update('photo', ['photo-type' => Photo::USER_AVATAR], ['profile' => true]);
1007 DBA::update('photo', ['photo-type' => Photo::CONTACT_AVATAR], ["NOT `profile` AND NOT `contact-id` IS NULL AND `contact-id` != ?", 0]);
1008 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]);
1011 function update_1439()
1013 $intros = DBA::select('intro', ['id', 'fid'], ["NOT `fid` IS NULL AND `fid` != ?", 0]);
1014 while ($intro = DBA::fetch($intros)) {
1015 $fcontact = DBA::selectFirst('fcontact', ['url'], ['id' => $intro['fid']]);
1016 if (!empty($fcontact['url'])) {
1017 $id = Contact::getIdForURL($fcontact['url']);
1019 DBA::update('intro',['suggest-cid' => $id], ['id' => $intro['id']]);
1023 DBA::close($intros);
1026 function update_1440()
1028 // Fix wrong public permissionset
1029 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);
1030 DBA::delete('permissionset', ["`id` != ? AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''", PermissionSet::PUBLIC]);
1032 return Update::SUCCESS;
1035 function update_1441()
1037 $languages = DI::l10n()->getAvailableLanguages();
1039 $albums = [Photo::PROFILE_PHOTOS];
1040 foreach ($languages as $language) {
1041 $albums[] = DI::l10n()->withLang($language)->t(Photo::PROFILE_PHOTOS);
1043 $albums = array_unique($albums);
1045 Photo::update(['photo-type' => Photo::USER_AVATAR], ['album' => $albums]);
1047 return Update::SUCCESS;
1050 function update_1442()
1052 // transform blocked intros into ignored intros
1053 DBA::update('intro', ['ignore' => 1, 'blocked' => 0], ['blocked' => 1]);
1055 return Update::SUCCESS;
1059 * A bug in Contact\User::updateByContactUpdate prevented any update to the user-contact table since the rows have been
1060 * created in version 1435. This version fixes this bug but the user-contact rows are outdated, we need to regenerate
1063 function update_1444()
1065 DBA::e('TRUNCATE TABLE `user-contact`');
1067 $contacts = DBA::select('contact', [], ["`uid` != ?", 0]);
1068 while ($contact = DBA::fetch($contacts)) {
1069 Contact\User::insertForContactArray($contact);
1072 return Update::SUCCESS;
1075 function update_1446()
1077 $distributed_cache_driver_source = DI::config()->getCache()->getSource('system', 'distributed_cache_driver');
1078 $cache_driver_source = DI::config()->getCache()->getSource('system', 'cache_driver');
1080 // In case the distributed cache driver is the default value, but the current cache driver isn't default,
1081 // we assume that the distributed cache driver should be the same as the current cache driver
1083 $distributed_cache_driver_source === \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC
1084 && $cache_driver_source > \Friendica\Core\Config\ValueObject\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;