3 * @copyright Copyright (C) 2010-2021, 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\Update;
45 use Friendica\Core\Worker;
46 use Friendica\Database\Database;
47 use Friendica\Database\DBA;
48 use Friendica\Database\DBStructure;
50 use Friendica\Model\Contact;
51 use Friendica\Model\Item;
52 use Friendica\Model\ItemURI;
53 use Friendica\Model\Notification;
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()
270 if (!DBStructure::existsTable('item-activity')) {
271 return Update::SUCCESS;
275 foreach (Item::ACTIVITIES as $index => $activity) {
276 if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
282 // The update failed - but it cannot be recovered, since the data doesn't match our expectation
283 // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
284 // the postupdate. This is not fatal, but means that it will take some longer time for the system
286 return Update::SUCCESS;
289 if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
290 SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", GRAVITY_ACTIVITY)) {
291 return Update::FAILED;
294 return Update::SUCCESS;
297 function update_1351()
299 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
300 return Update::FAILED;
303 return Update::SUCCESS;
306 function pre_update_1354()
308 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
309 && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
310 && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
311 return Update::FAILED;
313 return Update::SUCCESS;
316 function update_1354()
318 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
319 && DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])) {
320 if (!DBA::e("UPDATE `contact` SET `ffi_keyword_denylist` = `ffi_keyword_blacklist`")) {
321 return Update::FAILED;
324 // When the data had been copied then the main task is done.
325 // Having the old field removed is only beauty but not crucial.
326 // So we don't care if this was successful or not.
327 DBA::e("ALTER TABLE `contact` DROP `ffi_keyword_blacklist`");
329 return Update::SUCCESS;
332 function update_1357()
334 if (!DBA::e("UPDATE `contact` SET `failed` = true WHERE `success_update` < `failure_update` AND `failed` IS NULL")) {
335 return Update::FAILED;
338 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `success_update` > `failure_update` AND `failed` IS NULL")) {
339 return Update::FAILED;
342 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `updated` > `failure_update` AND `failed` IS NULL")) {
343 return Update::FAILED;
346 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `last-item` > `failure_update` AND `failed` IS NULL")) {
347 return Update::FAILED;
350 if (!DBA::e("UPDATE `gserver` SET `failed` = true WHERE `last_contact` < `last_failure` AND `failed` IS NULL")) {
351 return Update::FAILED;
354 if (!DBA::e("UPDATE `gserver` SET `failed` = false WHERE `last_contact` > `last_failure` AND `failed` IS NULL")) {
355 return Update::FAILED;
358 return Update::SUCCESS;
361 function pre_update_1358()
363 if (!DBA::e("DELETE FROM `contact-relation` WHERE NOT `relation-cid` IN (SELECT `id` FROM `contact`) OR NOT `cid` IN (SELECT `id` FROM `contact`)")) {
364 return Update::FAILED;
367 return Update::SUCCESS;
370 function pre_update_1363()
372 Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
373 return Update::SUCCESS;
376 function pre_update_1364()
378 if (!DBA::e("DELETE FROM `2fa_recovery_codes` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
379 return Update::FAILED;
382 if (!DBA::e("DELETE FROM `2fa_app_specific_password` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
383 return Update::FAILED;
386 if (!DBA::e("DELETE FROM `attach` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
387 return Update::FAILED;
390 if (!DBA::e("DELETE FROM `clients` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
391 return Update::FAILED;
394 if (!DBA::e("DELETE FROM `conv` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
395 return Update::FAILED;
398 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
399 return Update::FAILED;
402 if (!DBA::e("DELETE FROM `group` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
403 return Update::FAILED;
406 if (!DBA::e("DELETE FROM `intro` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
407 return Update::FAILED;
410 if (!DBA::e("DELETE FROM `manage` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
411 return Update::FAILED;
414 if (!DBA::e("DELETE FROM `manage` WHERE NOT `mid` IN (SELECT `uid` FROM `user`)")) {
415 return Update::FAILED;
418 if (!DBA::e("DELETE FROM `mail` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
419 return Update::FAILED;
422 if (!DBA::e("DELETE FROM `mailacct` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
423 return Update::FAILED;
426 if (!DBA::e("DELETE FROM `notify` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
427 return Update::FAILED;
430 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
431 return Update::FAILED;
434 if (!DBA::e("DELETE FROM `pconfig` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
435 return Update::FAILED;
438 if (!DBA::e("DELETE FROM `profile` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
439 return Update::FAILED;
442 if (!DBA::e("DELETE FROM `profile_check` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
443 return Update::FAILED;
446 if (!DBA::e("DELETE FROM `profile_field` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
447 return Update::FAILED;
450 if (!DBA::e("DELETE FROM `push_subscriber` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
451 return Update::FAILED;
454 if (!DBA::e("DELETE FROM `register` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
455 return Update::FAILED;
458 if (!DBA::e("DELETE FROM `search` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
459 return Update::FAILED;
462 if (!DBA::e("DELETE FROM `tokens` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
463 return Update::FAILED;
466 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
467 return Update::FAILED;
470 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
471 return Update::FAILED;
474 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `receiver-uid` IN (SELECT `uid` FROM `user`)")) {
475 return Update::FAILED;
478 if (!DBA::e("DELETE FROM `event` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
479 return Update::FAILED;
482 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
483 return Update::FAILED;
486 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
487 return Update::FAILED;
490 if (!DBA::e("DELETE FROM `intro` WHERE NOT `contact-id` 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 `group_member` WHERE NOT `gid` IN (SELECT `id` FROM `group`)")) {
503 return Update::FAILED;
506 if (!DBA::e("DELETE FROM `gserver-tag` WHERE NOT `gserver-id` IN (SELECT `id` FROM `gserver`)")) {
507 return Update::FAILED;
510 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
511 return Update::FAILED;
514 return Update::SUCCESS;
517 function pre_update_1365()
519 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `notify-id` IN (SELECT `id` FROM `notify`)")) {
520 return Update::FAILED;
523 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
524 return Update::FAILED;
527 return Update::SUCCESS;
530 function update_1375()
532 if (!DBA::e("UPDATE `item` SET `thr-parent` = `parent-uri`, `thr-parent-id` = `parent-uri-id` WHERE `thr-parent` = ''")) {
533 return Update::FAILED;
536 return Update::SUCCESS;
539 function pre_update_1376()
541 // Insert a user with uid=0
542 DBStructure::checkInitialValues();
544 if (!DBA::e("DELETE FROM `item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
545 return Update::FAILED;
548 if (!DBA::e("DELETE FROM `event` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
549 return Update::FAILED;
552 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
553 return Update::FAILED;
556 if (!DBA::e("DELETE FROM `permissionset` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
557 return Update::FAILED;
560 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
561 return Update::FAILED;
564 if (!DBA::e("DELETE FROM `post-category` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
565 return Update::FAILED;
568 Photo::delete(["NOT `uid` IN (SELECT `uid` FROM `user`)"]);
570 if (!DBA::e("DELETE FROM `contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
571 return Update::FAILED;
574 return Update::SUCCESS;
577 function pre_update_1377()
579 DBStructure::checkInitialValues();
581 if (!DBA::e("DELETE FROM `item` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
582 return Update::FAILED;
585 if (!DBA::e("DELETE FROM `item` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
586 return Update::FAILED;
589 if (!DBA::e("UPDATE `item` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
590 return Update::FAILED;
593 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
594 return Update::FAILED;
597 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
598 return Update::FAILED;
601 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
602 return Update::FAILED;
605 if (!DBA::e("UPDATE `notify` SET `uri-id` = NULL WHERE `uri-id` = 0")) {
606 return Update::FAILED;
609 if (DBStructure::existsTable('diaspora-interaction') && !DBA::e("DELETE FROM `diaspora-interaction` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
610 return Update::FAILED;
613 if (DBStructure::existsTable('item-activity') && !DBA::e("DELETE FROM `item-activity` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
614 return Update::FAILED;
617 if (DBStructure::existsTable('item-content') && !DBA::e("DELETE FROM `item-content` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
618 return Update::FAILED;
621 if (!DBA::e("DELETE FROM `notify` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
622 return Update::FAILED;
625 if (!DBA::e("UPDATE `notify` SET `parent-uri-id` = NULL WHERE `parent-uri-id` = 0")) {
626 return Update::FAILED;
628 if (!DBA::e("DELETE FROM `notify` WHERE `parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
629 return Update::FAILED;
632 if (!DBA::e("UPDATE `notify-threads` SET `master-parent-uri-id` = NULL WHERE `master-parent-uri-id` = 0")) {
633 return Update::FAILED;
636 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
637 return Update::FAILED;
640 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-item` NOT IN (SELECT `id` FROM `item`)")) {
641 return Update::FAILED;
644 return Update::SUCCESS;
647 function update_1380()
649 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 (?, ?)",
650 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
651 return Update::FAILED;
654 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 (?, ?)",
655 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
656 return Update::FAILED;
659 return Update::SUCCESS;
662 function pre_update_1395()
664 if (DBStructure::existsTable('post-user') && !DBStructure::existsColumn('post-user', ['id']) && !DBA::e("DROP TABLE `post-user`")) {
665 return Update::FAILED;
667 return Update::SUCCESS;
670 function update_1395()
672 if (!DBA::e("INSERT INTO `post-user`(`id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid`)
673 SELECT `id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid` FROM `item`
674 ON DUPLICATE KEY UPDATE `contact-id` = `item`.`contact-id`, `unseen` = `item`.`unseen`, `origin` = `item`.`origin`, `psid` = `item`.`psid`")) {
675 return Update::FAILED;
678 if (DBStructure::existsTable('user-item') && !DBA::e("INSERT INTO `post-user`(`uri-id`, `uid`, `hidden`, `notification-type`)
679 SELECT `uri-id`, `user-item`.`uid`, `hidden`,`notification-type` FROM `user-item`
680 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid`
681 ON DUPLICATE KEY UPDATE `hidden` = `user-item`.`hidden`, `notification-type` = `user-item`.`notification-type`")) {
682 return Update::FAILED;
684 return Update::SUCCESS;
687 function update_1396()
689 if (!DBStructure::existsTable('item-content')) {
690 return Update::SUCCESS;
693 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`, `raw-body`,
694 `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
695 `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
696 SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
697 `item-content`.`body`, `item-content`.`raw-body`, `item-content`.`location`, `item-content`.`coord`,
698 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
699 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
700 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
701 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
702 return Update::FAILED;
704 return Update::SUCCESS;
707 function update_1397()
709 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
710 SELECT `uri-id`, `uid`, `notification-type` FROM `post-user` WHERE `notification-type` != 0
711 ON DUPLICATE KEY UPDATE `uri-id` = `post-user`.`uri-id`, `uid` = `post-user`.`uid`, `notification-type` = `post-user`.`notification-type`")) {
712 return Update::FAILED;
715 if (!DBStructure::existsTable('user-item')) {
716 return Update::SUCCESS;
719 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
720 SELECT `uri-id`, `user-item`.`uid`, `notification-type` FROM `user-item`
721 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` WHERE `notification-type` != 0
722 ON DUPLICATE KEY UPDATE `notification-type` = `user-item`.`notification-type`")) {
723 return Update::FAILED;
726 if (!DBStructure::existsTable('thread')) {
727 return Update::SUCCESS;
730 if (!DBA::e("INSERT IGNORE INTO `post-thread-user`(`uri-id`, `uid`, `pinned`, `starred`, `ignored`, `wall`, `pubmail`, `forum_mode`)
731 SELECT `thread`.`uri-id`, `thread`.`uid`, `user-item`.`pinned`, `thread`.`starred`,
732 `thread`.`ignored`, `thread`.`wall`, `thread`.`pubmail`, `thread`.`forum_mode`
733 FROM `thread` LEFT JOIN `user-item` ON `user-item`.`iid` = `thread`.`iid`")) {
734 return Update::FAILED;
737 return Update::SUCCESS;
740 function update_1398()
742 if (!DBStructure::existsTable('thread')) {
743 return Update::SUCCESS;
746 if (!DBA::e("INSERT IGNORE INTO `post-thread` (`uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented`)
747 SELECT `uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented` FROM `thread`")) {
748 return Update::FAILED;
751 if (!DBStructure::existsTable('thread')) {
752 return Update::SUCCESS;
755 if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `thread` ON `thread`.`uid` = `post-thread-user`.`uid` AND `thread`.`uri-id` = `post-thread-user`.`uri-id`
756 SET `post-thread-user`.`mention` = `thread`.`mention`")) {
757 return Update::FAILED;
760 return Update::SUCCESS;
763 function update_1399()
765 if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-user` ON `post-user`.`uid` = `post-thread-user`.`uid` AND `post-user`.`uri-id` = `post-thread-user`.`uri-id`
766 SET `post-thread-user`.`contact-id` = `post-user`.`contact-id`, `post-thread-user`.`unseen` = `post-user`.`unseen`,
767 `post-thread-user`.`hidden` = `post-user`.`hidden`, `post-thread-user`.`origin` = `post-user`.`origin`,
768 `post-thread-user`.`psid` = `post-user`.`psid`, `post-thread-user`.`post-user-id` = `post-user`.`id`")) {
769 return Update::FAILED;
772 return Update::SUCCESS;
775 function update_1400()
777 if (!DBA::e("INSERT IGNORE INTO `post` (`uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`,
778 `created`, `received`, `edited`, `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global`)
779 SELECT `uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `edited`,
780 `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global` FROM `item`")) {
781 return Update::FAILED;
784 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
785 INNER JOIN `event` ON `item`.`event-id` = `event`.`id` AND `event`.`id` != 0
786 SET `post-user`.`event-id` = `item`.`event-id`")) {
787 return Update::FAILED;
790 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
791 SET `post-user`.`wall` = `item`.`wall`, `post-user`.`parent-uri-id` = `item`.`parent-uri-id`,
792 `post-user`.`thr-parent-id` = `item`.`thr-parent-id`,
793 `post-user`.`created` = `item`.`created`, `post-user`.`edited` = `item`.`edited`,
794 `post-user`.`received` = `item`.`received`, `post-user`.`gravity` = `item`.`gravity`,
795 `post-user`.`network` = `item`.`network`, `post-user`.`owner-id` = `item`.`owner-id`,
796 `post-user`.`author-id` = `item`.`author-id`, `post-user`.`causer-id` = `item`.`causer-id`,
797 `post-user`.`post-type` = `item`.`post-type`, `post-user`.`vid` = `item`.`vid`,
798 `post-user`.`private` = `item`.`private`, `post-user`.`global` = `item`.`global`,
799 `post-user`.`visible` = `item`.`visible`, `post-user`.`deleted` = `item`.`deleted`")) {
800 return Update::FAILED;
803 if (!DBA::e("INSERT IGNORE INTO `post-thread-user` (`uri-id`, `owner-id`, `author-id`, `causer-id`, `network`,
804 `created`, `received`, `changed`, `commented`, `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `post-user-id`)
805 SELECT `uri-id`, `owner-id`, `author-id`, `causer-id`, `network`, `created`, `received`, `received`, `received`,
806 `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `id`
807 FROM `post-user` WHERE `gravity` = 0 AND NOT EXISTS(SELECT `uri-id` FROM `post-thread-user` WHERE `post-user-id` = `post-user`.id)")) {
808 return Update::FAILED;
811 if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-thread` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id`
812 SET `post-thread-user`.`owner-id` = `post-thread`.`owner-id`, `post-thread-user`.`author-id` = `post-thread`.`author-id`,
813 `post-thread-user`.`causer-id` = `post-thread`.`causer-id`, `post-thread-user`.`network` = `post-thread`.`network`,
814 `post-thread-user`.`created` = `post-thread`.`created`, `post-thread-user`.`received` = `post-thread`.`received`,
815 `post-thread-user`.`changed` = `post-thread`.`changed`, `post-thread-user`.`commented` = `post-thread`.`commented`")) {
816 return Update::FAILED;
819 return Update::SUCCESS;
822 function pre_update_1403()
824 // Necessary before a primary key change
825 if (!DBA::e("DROP TABLE `parsed_url`")) {
826 return Update::FAILED;
829 return Update::SUCCESS;
832 function update_1404()
834 $tasks = DBA::select('workerqueue', ['id', 'command', 'parameter'], ['command' => ['notifier', 'delivery', 'apdelivery', 'done' => false]]);
835 while ($task = DBA::fetch($tasks)) {
836 $parameters = json_decode($task['parameter'], true);
838 if (is_array($parameters) && count($parameters) && in_array($parameters[0], [Delivery::MAIL, Delivery::SUGGESTION, Delivery::REMOVAL, Delivery::RELOCATION])) {
842 switch (strtolower($task['command'])) {
844 if (count($parameters) == 3) {
847 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
848 if (!DBA::isResult($item)) {
852 $parameters[1] = $item['uri-id'];
853 $parameters[2] = $item['uid'];
856 if (count($parameters) == 4) {
859 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
860 if (!DBA::isResult($item)) {
864 $parameters[1] = $item['uri-id'];
865 $parameters[3] = $item['uid'];
868 if (count($parameters) == 6) {
872 if (empty($parameters[4])) {
876 $item = DBA::selectFirst('item', ['uri-id'], ['id' => $parameters[1]]);
877 if (!DBA::isResult($item)) {
881 $parameters[5] = $item['uri-id'];
886 DBA::update('workerqueue', ['parameter' => json_encode($parameters)], ['id' => $task['id']]);
888 return Update::SUCCESS;
892 function update_1407()
894 if (!DBA::e("UPDATE `post` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
895 return Update::FAILED;
897 if (!DBA::e("UPDATE `post-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
898 return Update::FAILED;
900 if (!DBA::e("UPDATE `post-thread` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
901 return Update::FAILED;
903 if (!DBA::e("UPDATE `post-thread-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
904 return Update::FAILED;
907 return Update::SUCCESS;
910 function update_1413()
912 if (!DBA::e("UPDATE `post-user` SET `post-reason` = `post-type` WHERE `post-type` >= 64 and `post-type` <= 75")) {
913 return Update::FAILED;
917 function update_1419()
919 $mails = DBA::select('mail', ['id', 'from-url', 'uri', 'parent-uri', 'guid'], [], ['order' => ['id']]);
920 while ($mail = DBA::fetch($mails)) {
922 $fields['author-id'] = Contact::getIdForURL($mail['from-url'], 0, false);
923 if (empty($fields['author-id'])) {
927 $fields['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
928 $fields['parent-uri-id'] = ItemURI::getIdByURI($mail['parent-uri']);
930 $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
931 if (!empty($reply)) {
932 $fields['thr-parent'] = $reply['uri'];
933 if (!empty($reply['uri-id'])) {
934 $fields['thr-parent-id'] = $reply['uri-id'];
936 $fields['thr-parent-id'] = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]);
940 DBA::update('mail', $fields, ['id' => $mail['id']]);
942 return Update::SUCCESS;