3 * @copyright Copyright (C) 2020, Friendica
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\Addon;
44 use Friendica\Core\Logger;
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\Notify;
54 use Friendica\Model\Photo;
55 use Friendica\Model\Post;
56 use Friendica\Model\Storage;
57 use Friendica\Worker\Delivery;
59 // Post-update script of PR 5751
60 function update_1298()
62 $keys = ['gender', 'marital', 'sexual'];
63 foreach ($keys as $translateKey) {
64 $allData = DBA::select('profile', ['id', $translateKey]);
65 $allLangs = DI::l10n()->getAvailableLanguages();
68 foreach ($allData as $key => $data) {
69 $toTranslate = $data[$translateKey];
70 if ($toTranslate != '') {
71 foreach ($allLangs as $key => $lang) {
75 // First we get the the localizations
76 if (file_exists("view/lang/$lang/strings.php")) {
77 include "view/lang/$lang/strings.php";
79 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
80 include "addon/morechoice/lang/$lang/strings.php";
83 $localizedStrings = $a->strings;
86 $key = array_search($toTranslate, $localizedStrings);
91 // defaulting to empty string
98 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
99 Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
100 'was' => $data[$translateKey]]);
101 Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
102 Contact::updateSelfFromUserID($data['id']);
108 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
110 return Update::SUCCESS;
113 function update_1309()
115 $queue = DBA::select('queue', ['id', 'cid', 'guid']);
116 while ($entry = DBA::fetch($queue)) {
117 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
118 if (!DBA::isResult($contact)) {
122 $item = Post::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
123 if (!DBA::isResult($item)) {
127 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
128 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
129 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
130 DBA::delete('queue', ['id' => $entry['id']]);
132 return Update::SUCCESS;
135 function update_1315()
137 if (DBStructure::existsTable('item-delivery-data')) {
138 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
140 return Update::SUCCESS;
143 function update_1318()
145 DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
146 DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
148 Worker::add(PRIORITY_LOW, 'ProfileUpdate');
149 return Update::SUCCESS;
152 function update_1323()
154 $users = DBA::select('user', ['uid']);
155 while ($user = DBA::fetch($users)) {
156 Contact::updateSelfFromUserID($user['uid']);
160 return Update::SUCCESS;
163 function update_1327()
165 $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
166 while ($contact = DBA::fetch($contacts)) {
167 Contact\User::setBlocked($contact['id'], $contact['uid'], $contact['blocked']);
168 Contact\User::setIgnored($contact['id'], $contact['uid'], $contact['readonly']);
170 DBA::close($contacts);
172 return Update::SUCCESS;
175 function update_1330()
177 $currStorage = DI::config()->get('storage', 'class', '');
179 // set the name of the storage instead of the classpath as config
180 if (!empty($currStorage)) {
181 /** @var Storage\IStorage $currStorage */
182 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
183 return Update::FAILED;
186 // try to delete the class since it isn't needed. This won't work with config files
187 DI::config()->delete('storage', 'class');
190 // Update attachments and photos
191 if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
192 !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
193 return Update::FAILED;
196 return Update::SUCCESS;
199 function update_1332()
201 $condition = ["`is-default` IS NOT NULL"];
202 $profiles = DBA::select('profile', [], $condition);
204 while ($profile = DBA::fetch($profiles)) {
205 DI::profileField()->migrateFromLegacyProfile($profile);
207 DBA::close($profiles);
209 DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
211 return Update::SUCCESS;
214 function update_1347()
216 foreach (Item::ACTIVITIES as $index => $activity) {
217 DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
220 return Update::SUCCESS;
223 function pre_update_1348()
225 if (!DBA::exists('contact', ['id' => 0])) {
226 DBA::insert('contact', ['nurl' => '']);
227 $lastid = DBA::lastInsertId();
229 DBA::update('contact', ['id' => 0], ['id' => $lastid]);
233 // The tables "permissionset" and "tag" could or could not exist during the update.
234 // This depends upon the previous version. Depending upon this situation we have to add
235 // the "0" values before adding the foreign keys - or after would be sufficient.
239 DBA::e("DELETE FROM `auth_codes` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
240 DBA::e("DELETE FROM `tokens` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
242 return Update::SUCCESS;
245 function update_1348()
247 // Insert a permissionset with id=0
248 // Inserting it without an ID and then changing the value to 0 tricks the auto increment
249 if (!DBA::exists('permissionset', ['id' => 0])) {
250 DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
251 $lastid = DBA::lastInsertId();
253 DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
257 if (!DBA::exists('tag', ['id' => 0])) {
258 DBA::insert('tag', ['name' => '']);
259 $lastid = DBA::lastInsertId();
261 DBA::update('tag', ['id' => 0], ['id' => $lastid]);
265 return Update::SUCCESS;
268 function update_1349()
271 foreach (Item::ACTIVITIES as $index => $activity) {
272 if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
278 // The update failed - but it cannot be recovered, since the data doesn't match our expectation
279 // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
280 // the postupdate. This is not fatal, but means that it will take some longer time for the system
282 return Update::SUCCESS;
285 if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
286 SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", GRAVITY_ACTIVITY)) {
287 return Update::FAILED;
290 return Update::SUCCESS;
293 function update_1351()
295 if (!DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
296 return Update::FAILED;
299 return Update::SUCCESS;
302 function pre_update_1354()
304 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
305 && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
306 && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
307 return Update::FAILED;
309 return Update::SUCCESS;
312 function update_1354()
314 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
315 && DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])) {
316 if (!DBA::e("UPDATE `contact` SET `ffi_keyword_denylist` = `ffi_keyword_blacklist`")) {
317 return Update::FAILED;
320 // When the data had been copied then the main task is done.
321 // Having the old field removed is only beauty but not crucial.
322 // So we don't care if this was successful or not.
323 DBA::e("ALTER TABLE `contact` DROP `ffi_keyword_blacklist`");
325 return Update::SUCCESS;
328 function update_1357()
330 if (!DBA::e("UPDATE `contact` SET `failed` = true WHERE `success_update` < `failure_update` AND `failed` IS NULL")) {
331 return Update::FAILED;
334 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `success_update` > `failure_update` AND `failed` IS NULL")) {
335 return Update::FAILED;
338 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `updated` > `failure_update` AND `failed` IS NULL")) {
339 return Update::FAILED;
342 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `last-item` > `failure_update` AND `failed` IS NULL")) {
343 return Update::FAILED;
346 if (!DBA::e("UPDATE `gserver` SET `failed` = true WHERE `last_contact` < `last_failure` AND `failed` IS NULL")) {
347 return Update::FAILED;
350 if (!DBA::e("UPDATE `gserver` SET `failed` = false WHERE `last_contact` > `last_failure` AND `failed` IS NULL")) {
351 return Update::FAILED;
354 return Update::SUCCESS;
357 function pre_update_1358()
359 if (!DBA::e("DELETE FROM `contact-relation` WHERE NOT `relation-cid` IN (SELECT `id` FROM `contact`) OR NOT `cid` IN (SELECT `id` FROM `contact`)")) {
360 return Update::FAILED;
363 return Update::SUCCESS;
366 function pre_update_1363()
368 Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
369 return Update::SUCCESS;
372 function pre_update_1364()
374 if (!DBA::e("DELETE FROM `2fa_recovery_codes` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
375 return Update::FAILED;
378 if (!DBA::e("DELETE FROM `2fa_app_specific_password` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
379 return Update::FAILED;
382 if (!DBA::e("DELETE FROM `attach` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
383 return Update::FAILED;
386 if (!DBA::e("DELETE FROM `clients` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
387 return Update::FAILED;
390 if (!DBA::e("DELETE FROM `conv` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
391 return Update::FAILED;
394 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
395 return Update::FAILED;
398 if (!DBA::e("DELETE FROM `group` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
399 return Update::FAILED;
402 if (!DBA::e("DELETE FROM `intro` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
403 return Update::FAILED;
406 if (!DBA::e("DELETE FROM `manage` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
407 return Update::FAILED;
410 if (!DBA::e("DELETE FROM `manage` WHERE NOT `mid` IN (SELECT `uid` FROM `user`)")) {
411 return Update::FAILED;
414 if (!DBA::e("DELETE FROM `mail` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
415 return Update::FAILED;
418 if (!DBA::e("DELETE FROM `mailacct` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
419 return Update::FAILED;
422 if (!DBA::e("DELETE FROM `notify` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
423 return Update::FAILED;
426 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
427 return Update::FAILED;
430 if (!DBA::e("DELETE FROM `pconfig` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
431 return Update::FAILED;
434 if (!DBA::e("DELETE FROM `profile` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
435 return Update::FAILED;
438 if (!DBA::e("DELETE FROM `profile_check` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
439 return Update::FAILED;
442 if (!DBA::e("DELETE FROM `profile_field` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
443 return Update::FAILED;
446 if (!DBA::e("DELETE FROM `push_subscriber` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
447 return Update::FAILED;
450 if (!DBA::e("DELETE FROM `register` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
451 return Update::FAILED;
454 if (!DBA::e("DELETE FROM `search` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
455 return Update::FAILED;
458 if (!DBA::e("DELETE FROM `tokens` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
459 return Update::FAILED;
462 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
463 return Update::FAILED;
466 if (!DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
467 return Update::FAILED;
470 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `receiver-uid` IN (SELECT `uid` FROM `user`)")) {
471 return Update::FAILED;
474 if (!DBA::e("DELETE FROM `event` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
475 return Update::FAILED;
478 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
479 return Update::FAILED;
482 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
483 return Update::FAILED;
486 if (!DBA::e("DELETE FROM `intro` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
487 return Update::FAILED;
490 if (!DBA::e("DELETE FROM `participation` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
491 return Update::FAILED;
494 if (!DBA::e("DELETE FROM `profile_check` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
495 return Update::FAILED;
498 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
499 return Update::FAILED;
502 if (!DBA::e("DELETE FROM `participation` WHERE NOT `fid` IN (SELECT `id` FROM `fcontact`)")) {
503 return Update::FAILED;
506 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `gid` IN (SELECT `id` FROM `group`)")) {
507 return Update::FAILED;
510 if (!DBA::e("DELETE FROM `gserver-tag` WHERE NOT `gserver-id` IN (SELECT `id` FROM `gserver`)")) {
511 return Update::FAILED;
514 if (!DBA::e("DELETE FROM `participation` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
515 return Update::FAILED;
518 if (!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 (!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 (!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 (!DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
602 return Update::FAILED;
605 if (!DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
606 return Update::FAILED;
609 if (!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 Notify\ObjectType::ITEM, Notify\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 Notify\ObjectType::ITEM, Notify\ObjectType::PERSON)) {
664 return Update::FAILED;
667 return Update::SUCCESS;