3 * @copyright Copyright (C) 2010-2023, 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/>.
22 namespace Friendica\Database;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Conversation;
29 use Friendica\Model\GServer;
30 use Friendica\Model\Item;
31 use Friendica\Model\ItemURI;
32 use Friendica\Model\Photo;
33 use Friendica\Model\Post;
34 use Friendica\Model\Post\Category;
35 use Friendica\Model\Tag;
36 use Friendica\Model\Verb;
37 use Friendica\Protocol\ActivityPub\Processor;
38 use Friendica\Protocol\ActivityPub\Receiver;
39 use Friendica\Util\JsonLD;
40 use Friendica\Util\Strings;
41 use GuzzleHttp\Psr7\Uri;
44 * These database-intensive post update routines are meant to be executed in the background by the cronjob.
46 * If there is a need for a intensive migration after a database structure change, update this file
47 * by adding a new method at the end with the number of the new DB_UPDATE_VERSION.
51 // Needed for the helper function to read from the legacy term table
52 const OBJECT_TYPE_POST = 1;
57 * Calls the post update functions
59 public static function update()
61 if (!self::update1297()) {
64 if (!self::update1322()) {
67 if (!self::update1329()) {
70 if (!self::update1341()) {
73 if (!self::update1342()) {
76 if (!self::update1345()) {
79 if (!self::update1346()) {
82 if (!self::update1347()) {
85 if (!self::update1348()) {
88 if (!self::update1349()) {
91 if (!self::update1383()) {
94 if (!self::update1384()) {
97 if (!self::update1400()) {
100 if (!self::update1424()) {
103 if (!self::update1425()) {
106 if (!self::update1426()) {
109 if (!self::update1427()) {
112 if (!self::update1452()) {
115 if (!self::update1483()) {
118 if (!self::update1484()) {
121 if (!self::update1506()) {
124 if (!self::update1507()) {
131 * Set the delivery queue count to a negative value for all items preceding the feature.
133 * @return bool "true" when the job is done
134 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
136 private static function update1297()
138 // Was the script completed?
139 if (DI::keyValue()->get('post_update_version') >= 1297) {
143 if (!DBStructure::existsTable('item-delivery-data')) {
144 DI::keyValue()->set('post_update_version', 1297);
148 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
149 $max_iid = $max_item_delivery_data['iid'] ?? 0;
151 Logger::info('Start update1297 with max iid: ' . $max_iid);
153 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
155 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
157 if (DBA::errorNo() != 0) {
158 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
162 Logger::info('Processed rows: ' . DBA::affectedRows());
164 DI::keyValue()->set('post_update_version', 1297);
166 Logger::info('Done');
171 * Remove contact duplicates
173 * @return bool "true" when the job is done
174 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
176 private static function update1322()
178 // Was the script completed?
179 if (DI::keyValue()->get('post_update_version') >= 1322) {
183 Logger::info('Start');
185 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
186 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
187 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
188 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
189 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
190 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
192 while ($contact = DBA::fetch($contacts)) {
193 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
194 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
197 DBA::close($contact);
198 DI::keyValue()->set('post_update_version', 1322);
200 Logger::info('Done');
206 * update user notification data
208 * @return bool "true" when the job is done
209 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
211 private static function update1329()
213 // Was the script completed?
214 if (DI::keyValue()->get('post_update_version') >= 1329) {
218 if (!DBStructure::existsTable('item')) {
219 DI::keyValue()->set('post_update_version', 1329);
223 $id = DI::keyValue()->get('post_update_version_1329_id') ?? 0;
225 Logger::info('Start', ['item' => $id]);
229 $condition = ["`id` > ?", $id];
230 $params = ['order' => ['id'], 'limit' => 10000];
231 $items = DBA::select('item', ['id', 'uri-id', 'uid'], $condition, $params);
233 if (DBA::errorNo() != 0) {
234 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
238 while ($item = DBA::fetch($items)) {
241 Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
247 DI::keyValue()->set('post_update_version_1329_id', $id);
249 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
251 if ($start_id == $id) {
252 DI::keyValue()->set('post_update_version', 1329);
253 Logger::info('Done');
261 * Fill the "tag" table with tags and mentions from the body
263 * @return bool "true" when the job is done
264 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
266 private static function update1341()
268 // Was the script completed?
269 if (DI::keyValue()->get('post_update_version') >= 1341) {
273 if (!DBStructure::existsTable('item-content')) {
274 DI::keyValue()->set('post_update_version', 1342);
278 $id = DI::keyValue()->get('post_update_version_1341_id') ?? 0;
280 Logger::info('Start', ['item' => $id]);
284 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
285 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
286 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
288 if (DBA::errorNo() != 0) {
289 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
293 while ($item = DBA::fetch($items)) {
294 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
295 $id = $item['uri-id'];
297 if ($rows % 1000 == 0) {
298 DI::keyValue()->set('post_update_version_1341_id', $id);
303 DI::keyValue()->set('post_update_version_1341_id', $id);
305 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
307 // When there are less than 1,000 items processed this means that we reached the end
308 // The other entries will then be processed with the regular functionality
310 DI::keyValue()->set('post_update_version', 1341);
311 Logger::info('Done');
319 * Fill the "tag" table with tags and mentions from the "term" table
321 * @return bool "true" when the job is done
322 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
324 private static function update1342()
326 // Was the script completed?
327 if (DI::keyValue()->get('post_update_version') >= 1342) {
331 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
332 DI::keyValue()->set('post_update_version', 1342);
336 $id = DI::keyValue()->get('post_update_version_1342_id') ?? 0;
338 Logger::info('Start', ['item' => $id]);
342 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
344 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
345 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
346 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
347 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
349 if (DBA::errorNo() != 0) {
350 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
354 while ($term = DBA::fetch($terms)) {
355 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
356 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
357 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
358 if (!DBA::isResult($contact)) {
359 $ssl_url = str_replace('http://', 'https://', $term['url']);
360 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
361 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
364 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
365 $term['type'] = Tag::IMPLICIT_MENTION;
369 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url']);
373 if ($rows % 1000 == 0) {
374 DI::keyValue()->set('post_update_version_1342_id', $id);
379 DI::keyValue()->set('post_update_version_1342_id', $id);
381 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
383 // When there are less than 1,000 items processed this means that we reached the end
384 // The other entries will then be processed with the regular functionality
386 DI::keyValue()->set('post_update_version', 1342);
387 Logger::info('Done');
395 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
397 * @return bool "true" when the job is done
398 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
400 private static function update1345()
402 // Was the script completed?
403 if (DI::keyValue()->get('post_update_version') >= 1345) {
407 if (!DBStructure::existsTable('item-delivery-data')) {
408 DI::keyValue()->set('post_update_version', 1345);
412 $id = DI::keyValue()->get('post_update_version_1345_id') ?? 0;
414 Logger::info('Start', ['item' => $id]);
418 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
419 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
420 FROM `item-delivery-data`
421 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
422 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
424 if (DBA::errorNo() != 0) {
425 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
429 while ($delivery = DBA::fetch($deliveries)) {
430 $id = $delivery['iid'];
431 unset($delivery['iid']);
432 DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
435 DBA::close($deliveries);
437 DI::keyValue()->set('post_update_version_1345_id', $id);
439 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
441 // When there are less than 100 items processed this means that we reached the end
442 // The other entries will then be processed with the regular functionality
444 DI::keyValue()->set('post_update_version', 1345);
445 Logger::info('Done');
453 * Generates the legacy item.file field string from an item ID.
454 * Includes only file and category terms.
456 * @param int $item_id
460 private static function fileTextFromItemId($item_id)
464 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
465 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
466 foreach ($tags as $tag) {
467 if ($tag['type'] == Category::CATEGORY) {
468 $file_text .= '<' . $tag['term'] . '>';
470 $file_text .= '[' . $tag['term'] . ']';
478 * Fill the "tag" table with tags and mentions from the "term" table
480 * @return bool "true" when the job is done
481 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
483 private static function update1346()
485 // Was the script completed?
486 if (DI::keyValue()->get('post_update_version') >= 1346) {
490 if (!DBStructure::existsTable('term')) {
491 DI::keyValue()->set('post_update_version', 1346);
495 $id = DI::keyValue()->get('post_update_version_1346_id') ?? 0;
497 Logger::info('Start', ['item' => $id]);
501 $terms = DBA::select('term', ['oid'],
502 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
503 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
505 if (DBA::errorNo() != 0) {
506 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
510 while ($term = DBA::fetch($terms)) {
511 $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
512 if (!DBA::isResult($item)) {
516 $file = self::fileTextFromItemId($term['oid']);
518 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
523 if ($rows % 100 == 0) {
524 DI::keyValue()->set('post_update_version_1346_id', $id);
529 DI::keyValue()->set('post_update_version_1346_id', $id);
531 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
533 // When there are less than 10 items processed this means that we reached the end
534 // The other entries will then be processed with the regular functionality
536 DI::keyValue()->set('post_update_version', 1346);
537 Logger::info('Done');
545 * update the "vid" (verb) field in the item table
547 * @return bool "true" when the job is done
548 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
549 * @throws \ImagickException
551 private static function update1347()
553 // Was the script completed?
554 if (DI::keyValue()->get('post_update_version') >= 1347) {
558 if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
559 DI::keyValue()->set('post_update_version', 1347);
563 $id = DI::keyValue()->get('post_update_version_1347_id') ?? 0;
565 Logger::info('Start', ['item' => $id]);
570 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
571 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
572 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
573 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", Item::GRAVITY_ACTIVITY, $id);
575 if (DBA::errorNo() != 0) {
576 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
580 while ($item = DBA::fetch($items)) {
582 $verb = $item['item-verb'];
584 $verb = $item['verb'];
586 if (empty($verb) && is_int($item['activity'])) {
587 $verb = Item::ACTIVITIES[$item['activity']];
593 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
598 DI::keyValue()->set('post_update_version_1347_id', $id);
600 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
602 if ($start_id == $id) {
603 DI::keyValue()->set('post_update_version', 1347);
604 Logger::info('Done');
612 * update the "gsid" (global server id) field in the contact table
614 * @return bool "true" when the job is done
615 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
616 * @throws \ImagickException
618 private static function update1348()
620 // Was the script completed?
621 if (DI::keyValue()->get('post_update_version') >= 1348) {
625 $id = DI::keyValue()->get('post_update_version_1348_id') ?? 0;
627 Logger::info('Start', ['contact' => $id]);
631 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
632 $params = ['order' => ['id'], 'limit' => 10000];
633 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
635 if (DBA::errorNo() != 0) {
636 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
640 while ($contact = DBA::fetch($contacts)) {
641 $id = $contact['id'];
643 DBA::update('contact',
644 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
645 ['id' => $contact['id']]);
649 DBA::close($contacts);
651 DI::keyValue()->set('post_update_version_1348_id', $id);
653 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
655 if ($start_id == $id) {
656 DI::keyValue()->set('post_update_version', 1348);
657 Logger::info('Done');
665 * update the "gsid" (global server id) field in the apcontact table
667 * @return bool "true" when the job is done
668 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
669 * @throws \ImagickException
671 private static function update1349()
673 // Was the script completed?
674 if (DI::keyValue()->get('post_update_version') >= 1349) {
678 $id = DI::keyValue()->get('post_update_version_1349_id') ?? '';
680 Logger::info('Start', ['apcontact' => $id]);
684 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
685 $params = ['order' => ['url'], 'limit' => 10000];
686 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
688 if (DBA::errorNo() != 0) {
689 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
693 while ($apcontact = DBA::fetch($apcontacts)) {
694 $id = $apcontact['url'];
696 DBA::update('apcontact',
697 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
698 ['url' => $apcontact['url']]);
702 DBA::close($apcontacts);
704 DI::keyValue()->set('post_update_version_1349_id', $id);
706 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
708 if ($start_id == $id) {
709 DI::keyValue()->set('post_update_version', 1349);
710 Logger::info('Done');
718 * Remove orphaned photo entries
720 * @return bool "true" when the job is done
721 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
722 * @throws \ImagickException
724 private static function update1383()
726 // Was the script completed?
727 if (DI::keyValue()->get('post_update_version') >= 1383) {
731 Logger::info('Start');
734 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
736 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
737 while ($photo = DBA::fetch($photos)) {
738 $delete = !in_array($photo['scale'], [4, 5, 6]);
741 // Check if there is a contact entry with that photo
742 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
743 $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
747 Photo::delete(['id' => $photo['id']]);
753 DI::keyValue()->set('post_update_version', 1383);
754 Logger::info('Done', ['deleted' => $deleted]);
759 * update the "hash" field in the photo table
761 * @return bool "true" when the job is done
762 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
763 * @throws \ImagickException
765 private static function update1384()
767 // Was the script completed?
768 if (DI::keyValue()->get('post_update_version') >= 1384) {
772 $condition = ["`hash` IS NULL"];
773 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
776 $photos = DBA::select('photo', [], $condition, ['limit' => 100]);
778 if (DBA::errorNo() != 0) {
779 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
783 while ($photo = DBA::fetch($photos)) {
784 $img = Photo::getImageForPhoto($photo);
786 $md5 = md5($img->asString());
790 DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
795 Logger::info('Processed', ['rows' => $rows]);
798 DI::keyValue()->set('post_update_version', 1384);
799 Logger::info('Done');
807 * update the "external-id" field in the post table
809 * @return bool "true" when the job is done
810 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
811 * @throws \ImagickException
813 private static function update1400()
815 // Was the script completed?
816 if (DI::keyValue()->get('post_update_version') >= 1400) {
820 if (!DBStructure::existsTable('item')) {
821 DI::keyValue()->set('post_update_version', 1400);
825 $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
826 Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
829 $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
831 if (DBA::errorNo() != 0) {
832 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
836 while ($item = DBA::fetch($items)) {
837 Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
842 Logger::info('Processed', ['rows' => $rows]);
845 DI::keyValue()->set('post_update_version', 1400);
846 Logger::info('Done');
854 * update the "uri-id" field in the contact table
856 * @return bool "true" when the job is done
857 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
858 * @throws \ImagickException
860 private static function update1424()
862 // Was the script completed?
863 if (DI::keyValue()->get('post_update_version') >= 1424) {
867 $condition = ["`uri-id` IS NULL"];
868 Logger::info('Start', ['rest' => DBA::count('contact', $condition)]);
871 $contacts = DBA::select('contact', ['id', 'url'], $condition, ['limit' => 1000]);
873 if (DBA::errorNo() != 0) {
874 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
878 while ($contact = DBA::fetch($contacts)) {
879 DBA::update('contact', ['uri-id' => ItemURI::getIdByURI($contact['url'])], ['id' => $contact['id']]);
882 DBA::close($contacts);
884 Logger::info('Processed', ['rows' => $rows]);
887 DI::keyValue()->set('post_update_version', 1424);
888 Logger::info('Done');
896 * update the "uri-id" field in the fcontact table
898 * @return bool "true" when the job is done
899 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
900 * @throws \ImagickException
902 private static function update1425()
904 // Was the script completed?
905 if (DI::keyValue()->get('post_update_version') >= 1425) {
909 if (!DBStructure::existsTable('fcontact')) {
910 DI::keyValue()->set('post_update_version', 1425);
914 $condition = ["`uri-id` IS NULL"];
915 Logger::info('Start', ['rest' => DBA::count('fcontact', $condition)]);
918 $fcontacts = DBA::select('fcontact', ['id', 'url', 'guid'], $condition, ['limit' => 1000]);
920 if (DBA::errorNo() != 0) {
921 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
925 while ($fcontact = DBA::fetch($fcontacts)) {
926 if (!empty($fcontact['guid'])) {
927 $uriid = ItemURI::insert(['uri' => $fcontact['url'], 'guid' => $fcontact['guid']]);
929 $uriid = ItemURI::getIdByURI($fcontact['url']);
931 DBA::update('fcontact', ['uri-id' => $uriid], ['id' => $fcontact['id']]);
934 DBA::close($fcontacts);
936 Logger::info('Processed', ['rows' => $rows]);
939 DI::keyValue()->set('post_update_version', 1425);
940 Logger::info('Done');
948 * update the "uri-id" field in the apcontact table
950 * @return bool "true" when the job is done
951 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
952 * @throws \ImagickException
954 private static function update1426()
956 // Was the script completed?
957 if (DI::keyValue()->get('post_update_version') >= 1426) {
961 $condition = ["`uri-id` IS NULL"];
962 Logger::info('Start', ['rest' => DBA::count('apcontact', $condition)]);
965 $apcontacts = DBA::select('apcontact', ['url', 'uuid'], $condition, ['limit' => 1000]);
967 if (DBA::errorNo() != 0) {
968 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
972 while ($apcontact = DBA::fetch($apcontacts)) {
973 if (!empty($apcontact['uuid'])) {
974 $uriid = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
976 $uriid = ItemURI::getIdByURI($apcontact['url']);
978 DBA::update('apcontact', ['uri-id' => $uriid], ['url' => $apcontact['url']]);
981 DBA::close($apcontacts);
983 Logger::info('Processed', ['rows' => $rows]);
986 DI::keyValue()->set('post_update_version', 1426);
987 Logger::info('Done');
995 * update the "uri-id" field in the event table
997 * @return bool "true" when the job is done
998 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
999 * @throws \ImagickException
1001 private static function update1427()
1003 // Was the script completed?
1004 if (DI::keyValue()->get('post_update_version') >= 1427) {
1008 $condition = ["`uri-id` IS NULL"];
1009 Logger::info('Start', ['rest' => DBA::count('event', $condition)]);
1012 $events = DBA::select('event', ['id', 'uri', 'guid'], $condition, ['limit' => 1000]);
1014 if (DBA::errorNo() != 0) {
1015 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1019 while ($event = DBA::fetch($events)) {
1020 if (!empty($event['guid'])) {
1021 $uriid = ItemURI::insert(['uri' => $event['uri'], 'guid' => $event['guid']]);
1023 $uriid = ItemURI::getIdByURI($event['uri']);
1025 DBA::update('event', ['uri-id' => $uriid], ['id' => $event['id']]);
1028 DBA::close($events);
1030 Logger::info('Processed', ['rows' => $rows]);
1033 DI::keyValue()->set('post_update_version', 1427);
1034 Logger::info('Done');
1042 * Fill the receivers of the post via the raw source
1044 * @return bool "true" when the job is done
1045 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1046 * @throws \ImagickException
1048 private static function update1452()
1050 // Was the script completed?
1051 if (DI::keyValue()->get('post_update_version') >= 1452) {
1055 if (!DBStructure::existsTable('conversation')) {
1056 DI::keyValue()->set('post_update_version', 1452);
1060 $id = DI::keyValue()->get('post_update_version_1452_id') ?? 0;
1062 Logger::info('Start', ['uri-id' => $id]);
1067 $conversations = DBA::p("SELECT `post-view`.`uri-id`, `conversation`.`source`, `conversation`.`received` FROM `conversation`
1068 INNER JOIN `post-view` ON `post-view`.`uri` = `conversation`.`item-uri`
1069 WHERE NOT `source` IS NULL AND `conversation`.`protocol` = ? AND `uri-id` > ? LIMIT ?",
1070 Conversation::PARCEL_ACTIVITYPUB, $id, 1000);
1072 if (DBA::errorNo() != 0) {
1073 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1077 while ($conversation = DBA::fetch($conversations)) {
1078 $id = $conversation['uri-id'];
1079 $received = $conversation['received'];
1081 $raw = json_decode($conversation['source'], true);
1085 $activity = JsonLD::compact($raw);
1087 $urls = Receiver::getReceiverURL($activity);
1088 Processor::storeReceivers($conversation['uri-id'], $urls);
1090 if (!empty($activity['as:object'])) {
1091 $urls = array_merge($urls, Receiver::getReceiverURL($activity['as:object']));
1092 Processor::storeReceivers($conversation['uri-id'], $urls);
1097 DBA::close($conversations);
1099 DI::keyValue()->set('post_update_version_1452_id', $id);
1101 Logger::info('Processed', ['rows' => $rows, 'last' => $id, 'last-received' => $received]);
1104 DI::keyValue()->set('post_update_version', 1452);
1105 Logger::info('Done');
1113 * Correct the parent.
1114 * This fixes a bug that was introduced in the development of version 2022.09
1116 * @return bool "true" when the job is done
1117 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1118 * @throws \ImagickException
1120 private static function update1483()
1122 // Was the script completed?
1123 if (DI::keyValue()->get('post_update_version') >= 1483) {
1127 Logger::info('Start');
1129 $posts = DBA::select('post-view', ['uri-id'], ['conversation' => './']);
1130 while ($post = DBA::fetch($posts)) {
1131 $parent = Item::getParent($post['uri-id']);
1133 DBA::update('post', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1134 DBA::update('post-user', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1139 DI::keyValue()->set('post_update_version', 1483);
1140 Logger::info('Done');
1145 * Handle duplicate contact entries
1147 * @return bool "true" when the job is done
1148 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1149 * @throws \ImagickException
1151 private static function update1484()
1153 // Was the script completed?
1154 if (DI::keyValue()->get('post_update_version') >= 1484) {
1158 $id = DI::keyValue()->get('post_update_version_1484_id') ?? 0;
1160 Logger::info('Start', ['id' => $id]);
1164 $contacts = DBA::select('contact', ['id', 'uid', 'uri-id', 'url'], ["`id` > ?", $id], ['order' => ['id'], 'limit' => 1000]);
1166 if (DBA::errorNo() != 0) {
1167 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1171 while ($contact = DBA::fetch($contacts)) {
1172 $id = $contact['id'];
1173 if (is_null($contact['uri-id'])) {
1174 $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
1175 DBA::update('contact', ['uri-id' => $contact['uri-id']], ['id' => $contact['id']]);
1177 Contact::setAccountUser($contact['id'], $contact['uid'], $contact['uri-id'], $contact['url']);
1180 DBA::close($contacts);
1182 DI::keyValue()->set('post_update_version_1484_id', $id);
1184 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1187 DI::keyValue()->set('post_update_version', 1484);
1188 Logger::info('Done');
1196 * update the "gsid" (global server id) field in the contact table
1198 * @return bool "true" when the job is done
1199 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1200 * @throws \ImagickException
1202 private static function update1506()
1204 // Was the script completed?
1205 if (DI::keyValue()->get('post_update_version') >= 1506) {
1209 $id = DI::keyValue()->get('post_update_version_1506_id') ?? 0;
1211 Logger::info('Start', ['contact' => $id]);
1215 $condition = ["`id` > ? AND `gsid` IS NULL AND `network` = ?", $id, Protocol::DIASPORA];
1216 $params = ['order' => ['id'], 'limit' => 10000];
1217 $contacts = DBA::select('contact', ['id', 'url'], $condition, $params);
1219 if (DBA::errorNo() != 0) {
1220 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1224 while ($contact = DBA::fetch($contacts)) {
1225 $id = $contact['id'];
1227 $parts = parse_url($contact['url']);
1228 unset($parts['path']);
1229 $server = (string)Uri::fromParts($parts);
1231 DBA::update('contact',
1232 ['gsid' => GServer::getID($server, true), 'baseurl' => GServer::cleanURL($server)],
1233 ['id' => $contact['id']]);
1237 DBA::close($contacts);
1239 DI::keyValue()->set('post_update_version_1506_id', $id);
1241 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1243 if ($start_id == $id) {
1244 DI::keyValue()->set('post_update_version', 1506);
1245 Logger::info('Done');
1253 * update the "gsid" (global server id) field in the inbox-status table
1255 * @return bool "true" when the job is done
1256 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1257 * @throws \ImagickException
1259 private static function update1507()
1261 // Was the script completed?
1262 if (DI::keyValue()->get('post_update_version') >= 1507) {
1266 $id = DI::keyValue()->get('post_update_version_1507_id') ?? '';
1268 Logger::info('Start', ['apcontact' => $id]);
1272 $condition = ["`url` > ? AND NOT `gsid` IS NULL", $id];
1273 $params = ['order' => ['url'], 'limit' => 10000];
1274 $apcontacts = DBA::select('apcontact', ['url', 'gsid', 'sharedinbox', 'inbox'], $condition, $params);
1276 if (DBA::errorNo() != 0) {
1277 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1281 while ($apcontact = DBA::fetch($apcontacts)) {
1282 $id = $apcontact['url'];
1284 $inbox = [$apcontact['inbox']];
1285 if (!empty($apcontact['sharedinbox'])) {
1286 $inbox[] = $apcontact['sharedinbox'];
1288 $condition = DBA::mergeConditions(['url' => $inbox], ["`gsid` IS NULL"]);
1289 DBA::update('inbox-status', ['gsid' => $apcontact['gsid'], 'archive' => GServer::isDefunctById($apcontact['gsid'])], $condition);
1292 DBA::close($apcontacts);
1294 DI::keyValue()->set('post_update_version_1507_id', $id);
1296 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1298 if ($start_id == $id) {
1299 DI::keyValue()->set('post_update_version', 1507);
1300 Logger::info('Done');