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\Profile;
57 use Friendica\Model\Storage;
58 use Friendica\Worker\Delivery;
60 // Post-update script of PR 5751
61 function update_1298()
63 $keys = ['gender', 'marital', 'sexual'];
64 foreach ($keys as $translateKey) {
65 $allData = DBA::select('profile', ['id', $translateKey]);
66 $allLangs = DI::l10n()->getAvailableLanguages();
69 foreach ($allData as $key => $data) {
70 $toTranslate = $data[$translateKey];
71 if ($toTranslate != '') {
72 foreach ($allLangs as $key => $lang) {
76 // First we get the the localizations
77 if (file_exists("view/lang/$lang/strings.php")) {
78 include "view/lang/$lang/strings.php";
80 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
81 include "addon/morechoice/lang/$lang/strings.php";
84 $localizedStrings = $a->strings;
87 $key = array_search($toTranslate, $localizedStrings);
92 // defaulting to empty string
99 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
100 Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
101 'was' => $data[$translateKey]]);
103 Contact::updateSelfFromUserID($data['id']);
104 Profile::publishUpdate($data['id']);
110 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
112 return Update::SUCCESS;
115 function update_1309()
117 $queue = DBA::select('queue', ['id', 'cid', 'guid']);
118 while ($entry = DBA::fetch($queue)) {
119 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
120 if (!DBA::isResult($contact)) {
124 $item = Post::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
125 if (!DBA::isResult($item)) {
129 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
130 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
131 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
132 DBA::delete('queue', ['id' => $entry['id']]);
134 return Update::SUCCESS;
137 function update_1315()
139 if (DBStructure::existsTable('item-delivery-data')) {
140 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
142 return Update::SUCCESS;
145 function update_1318()
147 DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
148 DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
150 Worker::add(PRIORITY_LOW, 'ProfileUpdate');
151 return Update::SUCCESS;
154 function update_1323()
156 $users = DBA::select('user', ['uid']);
157 while ($user = DBA::fetch($users)) {
158 if (Contact::updateSelfFromUserID($user['uid'])) {
159 Profile::publishUpdate($user['uid']);
164 return Update::SUCCESS;
167 function update_1327()
169 $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
170 while ($contact = DBA::fetch($contacts)) {
171 Contact\User::setBlocked($contact['id'], $contact['uid'], $contact['blocked']);
172 Contact\User::setIgnored($contact['id'], $contact['uid'], $contact['readonly']);
174 DBA::close($contacts);
176 return Update::SUCCESS;
179 function update_1330()
181 $currStorage = DI::config()->get('storage', 'class', '');
183 // set the name of the storage instead of the classpath as config
184 if (!empty($currStorage)) {
185 /** @var Storage\IStorage $currStorage */
186 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
187 return Update::FAILED;
190 // try to delete the class since it isn't needed. This won't work with config files
191 DI::config()->delete('storage', 'class');
194 // Update attachments and photos
195 if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
196 !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
197 return Update::FAILED;
200 return Update::SUCCESS;
203 function update_1332()
205 $condition = ["`is-default` IS NOT NULL"];
206 $profiles = DBA::select('profile', [], $condition);
208 while ($profile = DBA::fetch($profiles)) {
209 DI::profileField()->migrateFromLegacyProfile($profile);
211 DBA::close($profiles);
213 DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
215 return Update::SUCCESS;
218 function update_1347()
220 foreach (Item::ACTIVITIES as $index => $activity) {
221 DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
224 return Update::SUCCESS;
227 function pre_update_1348()
229 if (!DBA::exists('contact', ['id' => 0])) {
230 DBA::insert('contact', ['nurl' => '']);
231 $lastid = DBA::lastInsertId();
233 DBA::update('contact', ['id' => 0], ['id' => $lastid]);
237 // The tables "permissionset" and "tag" could or could not exist during the update.
238 // This depends upon the previous version. Depending upon this situation we have to add
239 // the "0" values before adding the foreign keys - or after would be sufficient.
243 if (DBStructure::existsTable('auth_codes') && DBStructure::existsTable('clients')) {
244 DBA::e("DELETE FROM `auth_codes` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
246 if (DBStructure::existsTable('tokens') && DBStructure::existsTable('clients')) {
247 DBA::e("DELETE FROM `tokens` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
249 return Update::SUCCESS;
252 function update_1348()
254 // Insert a permissionset with id=0
255 // Inserting it without an ID and then changing the value to 0 tricks the auto increment
256 if (!DBA::exists('permissionset', ['id' => 0])) {
257 DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
258 $lastid = DBA::lastInsertId();
260 DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
264 if (!DBA::exists('tag', ['id' => 0])) {
265 DBA::insert('tag', ['name' => '']);
266 $lastid = DBA::lastInsertId();
268 DBA::update('tag', ['id' => 0], ['id' => $lastid]);
272 return Update::SUCCESS;
275 function update_1349()
277 if (!DBStructure::existsTable('item-activity')) {
278 return Update::SUCCESS;
282 foreach (Item::ACTIVITIES as $index => $activity) {
283 if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
289 // The update failed - but it cannot be recovered, since the data doesn't match our expectation
290 // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
291 // the postupdate. This is not fatal, but means that it will take some longer time for the system
293 return Update::SUCCESS;
296 if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
297 SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", GRAVITY_ACTIVITY)) {
298 return Update::FAILED;
301 return Update::SUCCESS;
304 function update_1351()
306 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
307 return Update::FAILED;
310 return Update::SUCCESS;
313 function pre_update_1354()
315 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
316 && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
317 && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
318 return Update::FAILED;
320 return Update::SUCCESS;
323 function update_1354()
325 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
326 && DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])) {
327 if (!DBA::e("UPDATE `contact` SET `ffi_keyword_denylist` = `ffi_keyword_blacklist`")) {
328 return Update::FAILED;
331 // When the data had been copied then the main task is done.
332 // Having the old field removed is only beauty but not crucial.
333 // So we don't care if this was successful or not.
334 DBA::e("ALTER TABLE `contact` DROP `ffi_keyword_blacklist`");
336 return Update::SUCCESS;
339 function update_1357()
341 if (!DBA::e("UPDATE `contact` SET `failed` = true WHERE `success_update` < `failure_update` AND `failed` IS NULL")) {
342 return Update::FAILED;
345 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `success_update` > `failure_update` AND `failed` IS NULL")) {
346 return Update::FAILED;
349 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `updated` > `failure_update` AND `failed` IS NULL")) {
350 return Update::FAILED;
353 if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `last-item` > `failure_update` AND `failed` IS NULL")) {
354 return Update::FAILED;
357 if (!DBA::e("UPDATE `gserver` SET `failed` = true WHERE `last_contact` < `last_failure` AND `failed` IS NULL")) {
358 return Update::FAILED;
361 if (!DBA::e("UPDATE `gserver` SET `failed` = false WHERE `last_contact` > `last_failure` AND `failed` IS NULL")) {
362 return Update::FAILED;
365 return Update::SUCCESS;
368 function pre_update_1358()
370 if (!DBA::e("DELETE FROM `contact-relation` WHERE NOT `relation-cid` IN (SELECT `id` FROM `contact`) OR NOT `cid` IN (SELECT `id` FROM `contact`)")) {
371 return Update::FAILED;
374 return Update::SUCCESS;
377 function pre_update_1363()
379 Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
380 return Update::SUCCESS;
383 function pre_update_1364()
385 if (!DBA::e("DELETE FROM `2fa_recovery_codes` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
386 return Update::FAILED;
389 if (!DBA::e("DELETE FROM `2fa_app_specific_password` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
390 return Update::FAILED;
393 if (!DBA::e("DELETE FROM `attach` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
394 return Update::FAILED;
397 if (DBStructure::existsTable('clients') && !DBA::e("DELETE FROM `clients` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
398 return Update::FAILED;
401 if (!DBA::e("DELETE FROM `conv` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
402 return Update::FAILED;
405 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
406 return Update::FAILED;
409 if (!DBA::e("DELETE FROM `group` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
410 return Update::FAILED;
413 if (!DBA::e("DELETE FROM `intro` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
414 return Update::FAILED;
417 if (!DBA::e("DELETE FROM `manage` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
418 return Update::FAILED;
421 if (!DBA::e("DELETE FROM `manage` WHERE NOT `mid` IN (SELECT `uid` FROM `user`)")) {
422 return Update::FAILED;
425 if (!DBA::e("DELETE FROM `mail` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
426 return Update::FAILED;
429 if (!DBA::e("DELETE FROM `mailacct` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
430 return Update::FAILED;
433 if (!DBA::e("DELETE FROM `notify` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
434 return Update::FAILED;
437 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
438 return Update::FAILED;
441 if (!DBA::e("DELETE FROM `pconfig` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
442 return Update::FAILED;
445 if (!DBA::e("DELETE FROM `profile` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
446 return Update::FAILED;
449 if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
450 return Update::FAILED;
453 if (!DBA::e("DELETE FROM `profile_field` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
454 return Update::FAILED;
457 if (!DBA::e("DELETE FROM `push_subscriber` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
458 return Update::FAILED;
461 if (!DBA::e("DELETE FROM `register` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
462 return Update::FAILED;
465 if (!DBA::e("DELETE FROM `search` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
466 return Update::FAILED;
469 if (DBStructure::existsTable('tokens') && !DBA::e("DELETE FROM `tokens` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
470 return Update::FAILED;
473 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
474 return Update::FAILED;
477 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
478 return Update::FAILED;
481 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `receiver-uid` IN (SELECT `uid` FROM `user`)")) {
482 return Update::FAILED;
485 if (!DBA::e("DELETE FROM `event` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
486 return Update::FAILED;
489 if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
490 return Update::FAILED;
493 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
494 return Update::FAILED;
497 if (!DBA::e("DELETE FROM `intro` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
498 return Update::FAILED;
501 if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
502 return Update::FAILED;
505 if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
506 return Update::FAILED;
509 if (!DBA::e("DELETE FROM `group_member` WHERE NOT `gid` IN (SELECT `id` FROM `group`)")) {
510 return Update::FAILED;
513 if (!DBA::e("DELETE FROM `gserver-tag` WHERE NOT `gserver-id` IN (SELECT `id` FROM `gserver`)")) {
514 return Update::FAILED;
517 if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
518 return Update::FAILED;
521 return Update::SUCCESS;
524 function pre_update_1365()
526 if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `notify-id` IN (SELECT `id` FROM `notify`)")) {
527 return Update::FAILED;
530 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
531 return Update::FAILED;
534 return Update::SUCCESS;
537 function update_1375()
539 if (!DBA::e("UPDATE `item` SET `thr-parent` = `parent-uri`, `thr-parent-id` = `parent-uri-id` WHERE `thr-parent` = ''")) {
540 return Update::FAILED;
543 return Update::SUCCESS;
546 function pre_update_1376()
548 // Insert a user with uid=0
549 DBStructure::checkInitialValues();
551 if (!DBA::e("DELETE FROM `item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
552 return Update::FAILED;
555 if (!DBA::e("DELETE FROM `event` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
556 return Update::FAILED;
559 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
560 return Update::FAILED;
563 if (!DBA::e("DELETE FROM `permissionset` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
564 return Update::FAILED;
567 if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
568 return Update::FAILED;
571 if (!DBA::e("DELETE FROM `post-category` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
572 return Update::FAILED;
575 Photo::delete(["NOT `uid` IN (SELECT `uid` FROM `user`)"]);
577 if (!DBA::e("DELETE FROM `contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
578 return Update::FAILED;
581 return Update::SUCCESS;
584 function pre_update_1377()
586 DBStructure::checkInitialValues();
588 if (!DBA::e("DELETE FROM `item` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
589 return Update::FAILED;
592 if (!DBA::e("DELETE FROM `item` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
593 return Update::FAILED;
596 if (!DBA::e("UPDATE `item` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
597 return Update::FAILED;
600 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
601 return Update::FAILED;
604 if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
605 return Update::FAILED;
608 if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
609 return Update::FAILED;
612 if (!DBA::e("UPDATE `notify` SET `uri-id` = NULL WHERE `uri-id` = 0")) {
613 return Update::FAILED;
616 if (DBStructure::existsTable('diaspora-interaction') && !DBA::e("DELETE FROM `diaspora-interaction` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
617 return Update::FAILED;
620 if (DBStructure::existsTable('item-activity') && !DBA::e("DELETE FROM `item-activity` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
621 return Update::FAILED;
624 if (DBStructure::existsTable('item-content') && !DBA::e("DELETE FROM `item-content` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
625 return Update::FAILED;
628 if (!DBA::e("DELETE FROM `notify` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
629 return Update::FAILED;
632 if (!DBA::e("UPDATE `notify` SET `parent-uri-id` = NULL WHERE `parent-uri-id` = 0")) {
633 return Update::FAILED;
635 if (!DBA::e("DELETE FROM `notify` WHERE `parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
636 return Update::FAILED;
639 if (!DBA::e("UPDATE `notify-threads` SET `master-parent-uri-id` = NULL WHERE `master-parent-uri-id` = 0")) {
640 return Update::FAILED;
643 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
644 return Update::FAILED;
647 if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-item` NOT IN (SELECT `id` FROM `item`)")) {
648 return Update::FAILED;
651 return Update::SUCCESS;
654 function update_1380()
656 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 (?, ?)",
657 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
658 return Update::FAILED;
661 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 (?, ?)",
662 Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
663 return Update::FAILED;
666 return Update::SUCCESS;
669 function pre_update_1395()
671 if (DBStructure::existsTable('post-user') && !DBStructure::existsColumn('post-user', ['id']) && !DBA::e("DROP TABLE `post-user`")) {
672 return Update::FAILED;
674 return Update::SUCCESS;
677 function update_1395()
679 if (!DBA::e("INSERT INTO `post-user`(`id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid`)
680 SELECT `id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid` FROM `item`
681 ON DUPLICATE KEY UPDATE `contact-id` = `item`.`contact-id`, `unseen` = `item`.`unseen`, `origin` = `item`.`origin`, `psid` = `item`.`psid`")) {
682 return Update::FAILED;
685 if (DBStructure::existsTable('user-item') && !DBA::e("INSERT INTO `post-user`(`uri-id`, `uid`, `hidden`, `notification-type`)
686 SELECT `uri-id`, `user-item`.`uid`, `hidden`,`notification-type` FROM `user-item`
687 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid`
688 ON DUPLICATE KEY UPDATE `hidden` = `user-item`.`hidden`, `notification-type` = `user-item`.`notification-type`")) {
689 return Update::FAILED;
691 return Update::SUCCESS;
694 function update_1396()
696 if (!DBStructure::existsTable('item-content')) {
697 return Update::SUCCESS;
700 if (DBStructure::existsColumn('item-content', ['raw-body'])) {
701 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`, `raw-body`,
702 `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
703 `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
704 SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
705 `item-content`.`body`, `item-content`.`raw-body`, `item-content`.`location`, `item-content`.`coord`,
706 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
707 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
708 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
709 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
710 return Update::FAILED;
713 if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`,
714 `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
715 `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
716 SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
717 `item-content`.`body`, `item-content`.`location`, `item-content`.`coord`,
718 `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
719 `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
720 `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
721 FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
722 return Update::FAILED;
725 return Update::SUCCESS;
728 function update_1397()
730 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
731 SELECT `uri-id`, `uid`, `notification-type` FROM `post-user` WHERE `notification-type` != 0
732 ON DUPLICATE KEY UPDATE `uri-id` = `post-user`.`uri-id`, `uid` = `post-user`.`uid`, `notification-type` = `post-user`.`notification-type`")) {
733 return Update::FAILED;
736 if (!DBStructure::existsTable('user-item')) {
737 return Update::SUCCESS;
740 if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
741 SELECT `uri-id`, `user-item`.`uid`, `notification-type` FROM `user-item`
742 INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` WHERE `notification-type` != 0
743 ON DUPLICATE KEY UPDATE `notification-type` = `user-item`.`notification-type`")) {
744 return Update::FAILED;
747 if (!DBStructure::existsTable('thread')) {
748 return Update::SUCCESS;
751 if (!DBA::e("INSERT IGNORE INTO `post-thread-user`(`uri-id`, `uid`, `pinned`, `starred`, `ignored`, `wall`, `pubmail`, `forum_mode`)
752 SELECT `thread`.`uri-id`, `thread`.`uid`, `user-item`.`pinned`, `thread`.`starred`,
753 `thread`.`ignored`, `thread`.`wall`, `thread`.`pubmail`, `thread`.`forum_mode`
754 FROM `thread` LEFT JOIN `user-item` ON `user-item`.`iid` = `thread`.`iid`")) {
755 return Update::FAILED;
758 return Update::SUCCESS;
761 function update_1398()
763 if (!DBStructure::existsTable('thread')) {
764 return Update::SUCCESS;
767 if (!DBA::e("INSERT IGNORE INTO `post-thread` (`uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented`)
768 SELECT `uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented` FROM `thread`")) {
769 return Update::FAILED;
772 if (!DBStructure::existsTable('thread')) {
773 return Update::SUCCESS;
776 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`
777 SET `post-thread-user`.`mention` = `thread`.`mention`")) {
778 return Update::FAILED;
781 return Update::SUCCESS;
784 function update_1399()
786 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`
787 SET `post-thread-user`.`contact-id` = `post-user`.`contact-id`, `post-thread-user`.`unseen` = `post-user`.`unseen`,
788 `post-thread-user`.`hidden` = `post-user`.`hidden`, `post-thread-user`.`origin` = `post-user`.`origin`,
789 `post-thread-user`.`psid` = `post-user`.`psid`, `post-thread-user`.`post-user-id` = `post-user`.`id`")) {
790 return Update::FAILED;
793 return Update::SUCCESS;
796 function update_1400()
798 if (!DBA::e("INSERT IGNORE INTO `post` (`uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`,
799 `created`, `received`, `edited`, `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global`)
800 SELECT `uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `edited`,
801 `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global` FROM `item`")) {
802 return Update::FAILED;
805 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
806 INNER JOIN `event` ON `item`.`event-id` = `event`.`id` AND `event`.`id` != 0
807 SET `post-user`.`event-id` = `item`.`event-id`")) {
808 return Update::FAILED;
811 if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
812 SET `post-user`.`wall` = `item`.`wall`, `post-user`.`parent-uri-id` = `item`.`parent-uri-id`,
813 `post-user`.`thr-parent-id` = `item`.`thr-parent-id`,
814 `post-user`.`created` = `item`.`created`, `post-user`.`edited` = `item`.`edited`,
815 `post-user`.`received` = `item`.`received`, `post-user`.`gravity` = `item`.`gravity`,
816 `post-user`.`network` = `item`.`network`, `post-user`.`owner-id` = `item`.`owner-id`,
817 `post-user`.`author-id` = `item`.`author-id`, `post-user`.`causer-id` = `item`.`causer-id`,
818 `post-user`.`post-type` = `item`.`post-type`, `post-user`.`vid` = `item`.`vid`,
819 `post-user`.`private` = `item`.`private`, `post-user`.`global` = `item`.`global`,
820 `post-user`.`visible` = `item`.`visible`, `post-user`.`deleted` = `item`.`deleted`")) {
821 return Update::FAILED;
824 if (!DBA::e("INSERT IGNORE INTO `post-thread-user` (`uri-id`, `owner-id`, `author-id`, `causer-id`, `network`,
825 `created`, `received`, `changed`, `commented`, `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `post-user-id`)
826 SELECT `uri-id`, `owner-id`, `author-id`, `causer-id`, `network`, `created`, `received`, `received`, `received`,
827 `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `id`
828 FROM `post-user` WHERE `gravity` = 0 AND NOT EXISTS(SELECT `uri-id` FROM `post-thread-user` WHERE `post-user-id` = `post-user`.id)")) {
829 return Update::FAILED;
832 if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-thread` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id`
833 SET `post-thread-user`.`owner-id` = `post-thread`.`owner-id`, `post-thread-user`.`author-id` = `post-thread`.`author-id`,
834 `post-thread-user`.`causer-id` = `post-thread`.`causer-id`, `post-thread-user`.`network` = `post-thread`.`network`,
835 `post-thread-user`.`created` = `post-thread`.`created`, `post-thread-user`.`received` = `post-thread`.`received`,
836 `post-thread-user`.`changed` = `post-thread`.`changed`, `post-thread-user`.`commented` = `post-thread`.`commented`")) {
837 return Update::FAILED;
840 return Update::SUCCESS;
843 function pre_update_1403()
845 // Necessary before a primary key change
846 if (DBStructure::existsTable('parsed_url') && !DBA::e("DROP TABLE `parsed_url`")) {
847 return Update::FAILED;
850 return Update::SUCCESS;
853 function update_1404()
855 $tasks = DBA::select('workerqueue', ['id', 'command', 'parameter'], ['command' => ['notifier', 'delivery', 'apdelivery', 'done' => false]]);
856 while ($task = DBA::fetch($tasks)) {
857 $parameters = json_decode($task['parameter'], true);
859 if (is_array($parameters) && count($parameters) && in_array($parameters[0], [Delivery::MAIL, Delivery::SUGGESTION, Delivery::REMOVAL, Delivery::RELOCATION])) {
863 switch (strtolower($task['command'])) {
865 if (count($parameters) == 3) {
868 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
869 if (!DBA::isResult($item)) {
873 $parameters[1] = $item['uri-id'];
874 $parameters[2] = $item['uid'];
877 if (count($parameters) == 4) {
880 $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
881 if (!DBA::isResult($item)) {
885 $parameters[1] = $item['uri-id'];
886 $parameters[3] = $item['uid'];
889 if (count($parameters) == 6) {
893 if (empty($parameters[4])) {
897 $item = DBA::selectFirst('item', ['uri-id'], ['id' => $parameters[1]]);
898 if (!DBA::isResult($item)) {
902 $parameters[5] = $item['uri-id'];
907 DBA::update('workerqueue', ['parameter' => json_encode($parameters)], ['id' => $task['id']]);
909 return Update::SUCCESS;
913 function update_1407()
915 if (!DBA::e("UPDATE `post` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
916 return Update::FAILED;
918 if (!DBA::e("UPDATE `post-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
919 return Update::FAILED;
921 if (!DBA::e("UPDATE `post-thread` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
922 return Update::FAILED;
924 if (!DBA::e("UPDATE `post-thread-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
925 return Update::FAILED;
928 return Update::SUCCESS;
931 function update_1413()
933 if (!DBA::e("UPDATE `post-user` SET `post-reason` = `post-type` WHERE `post-type` >= 64 and `post-type` <= 75")) {
934 return Update::FAILED;
938 function update_1419()
940 $mails = DBA::select('mail', ['id', 'from-url', 'uri', 'parent-uri', 'guid'], [], ['order' => ['id']]);
941 while ($mail = DBA::fetch($mails)) {
943 $fields['author-id'] = Contact::getIdForURL($mail['from-url'], 0, false);
944 if (empty($fields['author-id'])) {
948 $fields['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
949 $fields['parent-uri-id'] = ItemURI::getIdByURI($mail['parent-uri']);
951 $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
952 if (!empty($reply)) {
953 $fields['thr-parent'] = $reply['uri'];
954 if (!empty($reply['uri-id'])) {
955 $fields['thr-parent-id'] = $reply['uri-id'];
957 $fields['thr-parent-id'] = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]);
961 DBA::update('mail', $fields, ['id' => $mail['id']]);
963 return Update::SUCCESS;
966 function update_1429()
968 if (!DBA::e("UPDATE `contact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
969 return Update::FAILED;
972 if (!DBA::e("UPDATE `fcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
973 return Update::FAILED;
976 if (!DBA::e("UPDATE `apcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
977 return Update::FAILED;
980 DI::config()->set("system", "post_update_version", 1423);
982 return Update::SUCCESS;