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;
43 * These database-intensive post update routines are meant to be executed in the background by the cronjob.
45 * If there is a need for a intensive migration after a database structure change, update this file
46 * by adding a new method at the end with the number of the new DB_UPDATE_VERSION.
50 // Needed for the helper function to read from the legacy term table
51 const OBJECT_TYPE_POST = 1;
56 * Calls the post update functions
58 public static function update()
60 if (!self::update1297()) {
63 if (!self::update1322()) {
66 if (!self::update1329()) {
69 if (!self::update1341()) {
72 if (!self::update1342()) {
75 if (!self::update1345()) {
78 if (!self::update1346()) {
81 if (!self::update1347()) {
84 if (!self::update1348()) {
87 if (!self::update1349()) {
90 if (!self::update1383()) {
93 if (!self::update1384()) {
96 if (!self::update1400()) {
99 if (!self::update1424()) {
102 if (!self::update1425()) {
105 if (!self::update1426()) {
108 if (!self::update1427()) {
111 if (!self::update1452()) {
114 if (!self::update1483()) {
117 if (!self::update1484()) {
120 if (!self::update1507()) {
127 * Set the delivery queue count to a negative value for all items preceding the feature.
129 * @return bool "true" when the job is done
130 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
132 private static function update1297()
134 // Was the script completed?
135 if (DI::keyValue()->get('post_update_version') >= 1297) {
139 if (!DBStructure::existsTable('item-delivery-data')) {
140 DI::keyValue()->set('post_update_version', 1297);
144 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
145 $max_iid = $max_item_delivery_data['iid'] ?? 0;
147 Logger::info('Start update1297 with max iid: ' . $max_iid);
149 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
151 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
153 if (DBA::errorNo() != 0) {
154 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
158 Logger::info('Processed rows: ' . DBA::affectedRows());
160 DI::keyValue()->set('post_update_version', 1297);
162 Logger::info('Done');
167 * Remove contact duplicates
169 * @return bool "true" when the job is done
170 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
172 private static function update1322()
174 // Was the script completed?
175 if (DI::keyValue()->get('post_update_version') >= 1322) {
179 Logger::info('Start');
181 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
182 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
183 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
184 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
185 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
186 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
188 while ($contact = DBA::fetch($contacts)) {
189 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
190 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
193 DBA::close($contact);
194 DI::keyValue()->set('post_update_version', 1322);
196 Logger::info('Done');
202 * update user notification data
204 * @return bool "true" when the job is done
205 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
207 private static function update1329()
209 // Was the script completed?
210 if (DI::keyValue()->get('post_update_version') >= 1329) {
214 if (!DBStructure::existsTable('item')) {
215 DI::keyValue()->set('post_update_version', 1329);
219 $id = DI::keyValue()->get('post_update_version_1329_id') ?? 0;
221 Logger::info('Start', ['item' => $id]);
225 $condition = ["`id` > ?", $id];
226 $params = ['order' => ['id'], 'limit' => 10000];
227 $items = DBA::select('item', ['id', 'uri-id', 'uid'], $condition, $params);
229 if (DBA::errorNo() != 0) {
230 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
234 while ($item = DBA::fetch($items)) {
237 Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
243 DI::keyValue()->set('post_update_version_1329_id', $id);
245 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
247 if ($start_id == $id) {
248 DI::keyValue()->set('post_update_version', 1329);
249 Logger::info('Done');
257 * Fill the "tag" table with tags and mentions from the body
259 * @return bool "true" when the job is done
260 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
262 private static function update1341()
264 // Was the script completed?
265 if (DI::keyValue()->get('post_update_version') >= 1341) {
269 if (!DBStructure::existsTable('item-content')) {
270 DI::keyValue()->set('post_update_version', 1342);
274 $id = DI::keyValue()->get('post_update_version_1341_id') ?? 0;
276 Logger::info('Start', ['item' => $id]);
280 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
281 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
282 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
284 if (DBA::errorNo() != 0) {
285 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
289 while ($item = DBA::fetch($items)) {
290 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
291 $id = $item['uri-id'];
293 if ($rows % 1000 == 0) {
294 DI::keyValue()->set('post_update_version_1341_id', $id);
299 DI::keyValue()->set('post_update_version_1341_id', $id);
301 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
303 // When there are less than 1,000 items processed this means that we reached the end
304 // The other entries will then be processed with the regular functionality
306 DI::keyValue()->set('post_update_version', 1341);
307 Logger::info('Done');
315 * Fill the "tag" table with tags and mentions from the "term" table
317 * @return bool "true" when the job is done
318 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
320 private static function update1342()
322 // Was the script completed?
323 if (DI::keyValue()->get('post_update_version') >= 1342) {
327 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
328 DI::keyValue()->set('post_update_version', 1342);
332 $id = DI::keyValue()->get('post_update_version_1342_id') ?? 0;
334 Logger::info('Start', ['item' => $id]);
338 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
340 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
341 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
342 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
343 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
345 if (DBA::errorNo() != 0) {
346 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
350 while ($term = DBA::fetch($terms)) {
351 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
352 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
353 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
354 if (!DBA::isResult($contact)) {
355 $ssl_url = str_replace('http://', 'https://', $term['url']);
356 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
357 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
360 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
361 $term['type'] = Tag::IMPLICIT_MENTION;
365 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url']);
369 if ($rows % 1000 == 0) {
370 DI::keyValue()->set('post_update_version_1342_id', $id);
375 DI::keyValue()->set('post_update_version_1342_id', $id);
377 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
379 // When there are less than 1,000 items processed this means that we reached the end
380 // The other entries will then be processed with the regular functionality
382 DI::keyValue()->set('post_update_version', 1342);
383 Logger::info('Done');
391 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
393 * @return bool "true" when the job is done
394 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
396 private static function update1345()
398 // Was the script completed?
399 if (DI::keyValue()->get('post_update_version') >= 1345) {
403 if (!DBStructure::existsTable('item-delivery-data')) {
404 DI::keyValue()->set('post_update_version', 1345);
408 $id = DI::keyValue()->get('post_update_version_1345_id') ?? 0;
410 Logger::info('Start', ['item' => $id]);
414 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
415 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
416 FROM `item-delivery-data`
417 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
418 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
420 if (DBA::errorNo() != 0) {
421 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
425 while ($delivery = DBA::fetch($deliveries)) {
426 $id = $delivery['iid'];
427 unset($delivery['iid']);
428 DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
431 DBA::close($deliveries);
433 DI::keyValue()->set('post_update_version_1345_id', $id);
435 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
437 // When there are less than 100 items processed this means that we reached the end
438 // The other entries will then be processed with the regular functionality
440 DI::keyValue()->set('post_update_version', 1345);
441 Logger::info('Done');
449 * Generates the legacy item.file field string from an item ID.
450 * Includes only file and category terms.
452 * @param int $item_id
456 private static function fileTextFromItemId($item_id)
460 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
461 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
462 foreach ($tags as $tag) {
463 if ($tag['type'] == Category::CATEGORY) {
464 $file_text .= '<' . $tag['term'] . '>';
466 $file_text .= '[' . $tag['term'] . ']';
474 * Fill the "tag" table with tags and mentions from the "term" table
476 * @return bool "true" when the job is done
477 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
479 private static function update1346()
481 // Was the script completed?
482 if (DI::keyValue()->get('post_update_version') >= 1346) {
486 if (!DBStructure::existsTable('term')) {
487 DI::keyValue()->set('post_update_version', 1346);
491 $id = DI::keyValue()->get('post_update_version_1346_id') ?? 0;
493 Logger::info('Start', ['item' => $id]);
497 $terms = DBA::select('term', ['oid'],
498 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
499 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
501 if (DBA::errorNo() != 0) {
502 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
506 while ($term = DBA::fetch($terms)) {
507 $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
508 if (!DBA::isResult($item)) {
512 $file = self::fileTextFromItemId($term['oid']);
514 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
519 if ($rows % 100 == 0) {
520 DI::keyValue()->set('post_update_version_1346_id', $id);
525 DI::keyValue()->set('post_update_version_1346_id', $id);
527 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
529 // When there are less than 10 items processed this means that we reached the end
530 // The other entries will then be processed with the regular functionality
532 DI::keyValue()->set('post_update_version', 1346);
533 Logger::info('Done');
541 * update the "vid" (verb) field in the item table
543 * @return bool "true" when the job is done
544 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
545 * @throws \ImagickException
547 private static function update1347()
549 // Was the script completed?
550 if (DI::keyValue()->get('post_update_version') >= 1347) {
554 if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
555 DI::keyValue()->set('post_update_version', 1347);
559 $id = DI::keyValue()->get('post_update_version_1347_id') ?? 0;
561 Logger::info('Start', ['item' => $id]);
566 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
567 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
568 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
569 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", Item::GRAVITY_ACTIVITY, $id);
571 if (DBA::errorNo() != 0) {
572 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
576 while ($item = DBA::fetch($items)) {
578 $verb = $item['item-verb'];
580 $verb = $item['verb'];
582 if (empty($verb) && is_int($item['activity'])) {
583 $verb = Item::ACTIVITIES[$item['activity']];
589 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
594 DI::keyValue()->set('post_update_version_1347_id', $id);
596 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
598 if ($start_id == $id) {
599 DI::keyValue()->set('post_update_version', 1347);
600 Logger::info('Done');
608 * update the "gsid" (global server id) field in the contact table
610 * @return bool "true" when the job is done
611 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
612 * @throws \ImagickException
614 private static function update1348()
616 // Was the script completed?
617 if (DI::keyValue()->get('post_update_version') >= 1348) {
621 $id = DI::keyValue()->get('post_update_version_1348_id') ?? 0;
623 Logger::info('Start', ['contact' => $id]);
627 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
628 $params = ['order' => ['id'], 'limit' => 10000];
629 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
631 if (DBA::errorNo() != 0) {
632 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
636 while ($contact = DBA::fetch($contacts)) {
637 $id = $contact['id'];
639 DBA::update('contact',
640 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
641 ['id' => $contact['id']]);
645 DBA::close($contacts);
647 DI::keyValue()->set('post_update_version_1348_id', $id);
649 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
651 if ($start_id == $id) {
652 DI::keyValue()->set('post_update_version', 1348);
653 Logger::info('Done');
661 * update the "gsid" (global server id) field in the apcontact table
663 * @return bool "true" when the job is done
664 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
665 * @throws \ImagickException
667 private static function update1349()
669 // Was the script completed?
670 if (DI::keyValue()->get('post_update_version') >= 1349) {
674 $id = DI::keyValue()->get('post_update_version_1349_id') ?? '';
676 Logger::info('Start', ['apcontact' => $id]);
680 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
681 $params = ['order' => ['url'], 'limit' => 10000];
682 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
684 if (DBA::errorNo() != 0) {
685 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
689 while ($apcontact = DBA::fetch($apcontacts)) {
690 $id = $apcontact['url'];
692 DBA::update('apcontact',
693 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
694 ['url' => $apcontact['url']]);
698 DBA::close($apcontacts);
700 DI::keyValue()->set('post_update_version_1349_id', $id);
702 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
704 if ($start_id == $id) {
705 DI::keyValue()->set('post_update_version', 1349);
706 Logger::info('Done');
714 * Remove orphaned photo entries
716 * @return bool "true" when the job is done
717 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
718 * @throws \ImagickException
720 private static function update1383()
722 // Was the script completed?
723 if (DI::keyValue()->get('post_update_version') >= 1383) {
727 Logger::info('Start');
730 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
732 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
733 while ($photo = DBA::fetch($photos)) {
734 $delete = !in_array($photo['scale'], [4, 5, 6]);
737 // Check if there is a contact entry with that photo
738 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
739 $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
743 Photo::delete(['id' => $photo['id']]);
749 DI::keyValue()->set('post_update_version', 1383);
750 Logger::info('Done', ['deleted' => $deleted]);
755 * update the "hash" field in the photo table
757 * @return bool "true" when the job is done
758 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
759 * @throws \ImagickException
761 private static function update1384()
763 // Was the script completed?
764 if (DI::keyValue()->get('post_update_version') >= 1384) {
768 $condition = ["`hash` IS NULL"];
769 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
772 $photos = DBA::select('photo', [], $condition, ['limit' => 100]);
774 if (DBA::errorNo() != 0) {
775 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
779 while ($photo = DBA::fetch($photos)) {
780 $img = Photo::getImageForPhoto($photo);
782 $md5 = md5($img->asString());
786 DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
791 Logger::info('Processed', ['rows' => $rows]);
794 DI::keyValue()->set('post_update_version', 1384);
795 Logger::info('Done');
803 * update the "external-id" field in the post table
805 * @return bool "true" when the job is done
806 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
807 * @throws \ImagickException
809 private static function update1400()
811 // Was the script completed?
812 if (DI::keyValue()->get('post_update_version') >= 1400) {
816 if (!DBStructure::existsTable('item')) {
817 DI::keyValue()->set('post_update_version', 1400);
821 $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
822 Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
825 $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
827 if (DBA::errorNo() != 0) {
828 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
832 while ($item = DBA::fetch($items)) {
833 Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
838 Logger::info('Processed', ['rows' => $rows]);
841 DI::keyValue()->set('post_update_version', 1400);
842 Logger::info('Done');
850 * update the "uri-id" field in the contact table
852 * @return bool "true" when the job is done
853 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
854 * @throws \ImagickException
856 private static function update1424()
858 // Was the script completed?
859 if (DI::keyValue()->get('post_update_version') >= 1424) {
863 $condition = ["`uri-id` IS NULL"];
864 Logger::info('Start', ['rest' => DBA::count('contact', $condition)]);
867 $contacts = DBA::select('contact', ['id', 'url'], $condition, ['limit' => 1000]);
869 if (DBA::errorNo() != 0) {
870 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
874 while ($contact = DBA::fetch($contacts)) {
875 DBA::update('contact', ['uri-id' => ItemURI::getIdByURI($contact['url'])], ['id' => $contact['id']]);
878 DBA::close($contacts);
880 Logger::info('Processed', ['rows' => $rows]);
883 DI::keyValue()->set('post_update_version', 1424);
884 Logger::info('Done');
892 * update the "uri-id" field in the fcontact table
894 * @return bool "true" when the job is done
895 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
896 * @throws \ImagickException
898 private static function update1425()
900 // Was the script completed?
901 if (DI::keyValue()->get('post_update_version') >= 1425) {
905 if (!DBStructure::existsTable('fcontact')) {
906 DI::keyValue()->set('post_update_version', 1425);
910 $condition = ["`uri-id` IS NULL"];
911 Logger::info('Start', ['rest' => DBA::count('fcontact', $condition)]);
914 $fcontacts = DBA::select('fcontact', ['id', 'url', 'guid'], $condition, ['limit' => 1000]);
916 if (DBA::errorNo() != 0) {
917 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
921 while ($fcontact = DBA::fetch($fcontacts)) {
922 if (!empty($fcontact['guid'])) {
923 $uriid = ItemURI::insert(['uri' => $fcontact['url'], 'guid' => $fcontact['guid']]);
925 $uriid = ItemURI::getIdByURI($fcontact['url']);
927 DBA::update('fcontact', ['uri-id' => $uriid], ['id' => $fcontact['id']]);
930 DBA::close($fcontacts);
932 Logger::info('Processed', ['rows' => $rows]);
935 DI::keyValue()->set('post_update_version', 1425);
936 Logger::info('Done');
944 * update the "uri-id" field in the apcontact table
946 * @return bool "true" when the job is done
947 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
948 * @throws \ImagickException
950 private static function update1426()
952 // Was the script completed?
953 if (DI::keyValue()->get('post_update_version') >= 1426) {
957 $condition = ["`uri-id` IS NULL"];
958 Logger::info('Start', ['rest' => DBA::count('apcontact', $condition)]);
961 $apcontacts = DBA::select('apcontact', ['url', 'uuid'], $condition, ['limit' => 1000]);
963 if (DBA::errorNo() != 0) {
964 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
968 while ($apcontact = DBA::fetch($apcontacts)) {
969 if (!empty($apcontact['uuid'])) {
970 $uriid = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
972 $uriid = ItemURI::getIdByURI($apcontact['url']);
974 DBA::update('apcontact', ['uri-id' => $uriid], ['url' => $apcontact['url']]);
977 DBA::close($apcontacts);
979 Logger::info('Processed', ['rows' => $rows]);
982 DI::keyValue()->set('post_update_version', 1426);
983 Logger::info('Done');
991 * update the "uri-id" field in the event table
993 * @return bool "true" when the job is done
994 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
995 * @throws \ImagickException
997 private static function update1427()
999 // Was the script completed?
1000 if (DI::keyValue()->get('post_update_version') >= 1427) {
1004 $condition = ["`uri-id` IS NULL"];
1005 Logger::info('Start', ['rest' => DBA::count('event', $condition)]);
1008 $events = DBA::select('event', ['id', 'uri', 'guid'], $condition, ['limit' => 1000]);
1010 if (DBA::errorNo() != 0) {
1011 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1015 while ($event = DBA::fetch($events)) {
1016 if (!empty($event['guid'])) {
1017 $uriid = ItemURI::insert(['uri' => $event['uri'], 'guid' => $event['guid']]);
1019 $uriid = ItemURI::getIdByURI($event['uri']);
1021 DBA::update('event', ['uri-id' => $uriid], ['id' => $event['id']]);
1024 DBA::close($events);
1026 Logger::info('Processed', ['rows' => $rows]);
1029 DI::keyValue()->set('post_update_version', 1427);
1030 Logger::info('Done');
1038 * Fill the receivers of the post via the raw source
1040 * @return bool "true" when the job is done
1041 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1042 * @throws \ImagickException
1044 private static function update1452()
1046 // Was the script completed?
1047 if (DI::keyValue()->get('post_update_version') >= 1452) {
1051 if (!DBStructure::existsTable('conversation')) {
1052 DI::keyValue()->set('post_update_version', 1452);
1056 $id = DI::keyValue()->get('post_update_version_1452_id') ?? 0;
1058 Logger::info('Start', ['uri-id' => $id]);
1063 $conversations = DBA::p("SELECT `post-view`.`uri-id`, `conversation`.`source`, `conversation`.`received` FROM `conversation`
1064 INNER JOIN `post-view` ON `post-view`.`uri` = `conversation`.`item-uri`
1065 WHERE NOT `source` IS NULL AND `conversation`.`protocol` = ? AND `uri-id` > ? LIMIT ?",
1066 Conversation::PARCEL_ACTIVITYPUB, $id, 1000);
1068 if (DBA::errorNo() != 0) {
1069 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1073 while ($conversation = DBA::fetch($conversations)) {
1074 $id = $conversation['uri-id'];
1075 $received = $conversation['received'];
1077 $raw = json_decode($conversation['source'], true);
1081 $activity = JsonLD::compact($raw);
1083 $urls = Receiver::getReceiverURL($activity);
1084 Processor::storeReceivers($conversation['uri-id'], $urls);
1086 if (!empty($activity['as:object'])) {
1087 $urls = array_merge($urls, Receiver::getReceiverURL($activity['as:object']));
1088 Processor::storeReceivers($conversation['uri-id'], $urls);
1093 DBA::close($conversations);
1095 DI::keyValue()->set('post_update_version_1452_id', $id);
1097 Logger::info('Processed', ['rows' => $rows, 'last' => $id, 'last-received' => $received]);
1100 DI::keyValue()->set('post_update_version', 1452);
1101 Logger::info('Done');
1109 * Correct the parent.
1110 * This fixes a bug that was introduced in the development of version 2022.09
1112 * @return bool "true" when the job is done
1113 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1114 * @throws \ImagickException
1116 private static function update1483()
1118 // Was the script completed?
1119 if (DI::keyValue()->get('post_update_version') >= 1483) {
1123 Logger::info('Start');
1125 $posts = DBA::select('post-view', ['uri-id'], ['conversation' => './']);
1126 while ($post = DBA::fetch($posts)) {
1127 $parent = Item::getParent($post['uri-id']);
1129 DBA::update('post', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1130 DBA::update('post-user', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1135 DI::keyValue()->set('post_update_version', 1483);
1136 Logger::info('Done');
1141 * Handle duplicate contact entries
1143 * @return bool "true" when the job is done
1144 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1145 * @throws \ImagickException
1147 private static function update1484()
1149 // Was the script completed?
1150 if (DI::keyValue()->get('post_update_version') >= 1484) {
1154 $id = DI::keyValue()->get('post_update_version_1484_id') ?? 0;
1156 Logger::info('Start', ['id' => $id]);
1160 $contacts = DBA::select('contact', ['id', 'uid', 'uri-id', 'url'], ["`id` > ?", $id], ['order' => ['id'], 'limit' => 1000]);
1162 if (DBA::errorNo() != 0) {
1163 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1167 while ($contact = DBA::fetch($contacts)) {
1168 $id = $contact['id'];
1169 if (is_null($contact['uri-id'])) {
1170 $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
1171 DBA::update('contact', ['uri-id' => $contact['uri-id']], ['id' => $contact['id']]);
1173 Contact::setAccountUser($contact['id'], $contact['uid'], $contact['uri-id'], $contact['url']);
1176 DBA::close($contacts);
1178 DI::keyValue()->set('post_update_version_1484_id', $id);
1180 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1183 DI::keyValue()->set('post_update_version', 1484);
1184 Logger::info('Done');
1192 * update the "gsid" (global server id) field in the inbox-status table
1194 * @return bool "true" when the job is done
1195 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1196 * @throws \ImagickException
1198 private static function update1507()
1200 // Was the script completed?
1201 if (DI::keyValue()->get('post_update_version') >= 1507) {
1205 $id = DI::keyValue()->get('post_update_version_1507_id') ?? '';
1207 Logger::info('Start', ['apcontact' => $id]);
1211 $condition = ["`url` > ? AND NOT `gsid` IS NULL", $id];
1212 $params = ['order' => ['url'], 'limit' => 10000];
1213 $apcontacts = DBA::select('apcontact', ['url', 'gsid', 'sharedinbox', 'inbox'], $condition, $params);
1215 if (DBA::errorNo() != 0) {
1216 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1220 while ($apcontact = DBA::fetch($apcontacts)) {
1221 $id = $apcontact['url'];
1223 $inbox = [$apcontact['inbox']];
1224 if (!empty($apcontact['sharedinbox'])) {
1225 $inbox[] = $apcontact['sharedinbox'];
1227 // $condition = DBA::mergeConditions(['url' => $inbox], ["`gsid` IS NULL"]);
1228 $condition = ['url' => $inbox];
1229 DBA::update('inbox-status', ['gsid' => $apcontact['gsid'], 'archive' => GServer::isDefunctById($apcontact['gsid'])], $condition);
1232 DBA::close($apcontacts);
1234 DI::keyValue()->set('post_update_version_1507_id', $id);
1236 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1238 if ($start_id == $id) {
1239 DI::keyValue()->set('post_update_version', 1507);
1240 Logger::info('Done');