3 * @copyright Copyright (C) 2010-2022, 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()) {
124 * Set the delivery queue count to a negative value for all items preceding the feature.
126 * @return bool "true" when the job is done
127 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
129 private static function update1297()
131 // Was the script completed?
132 if (DI::config()->get('system', 'post_update_version') >= 1297) {
136 if (!DBStructure::existsTable('item-delivery-data')) {
137 DI::config()->set('system', 'post_update_version', 1297);
141 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
142 $max_iid = $max_item_delivery_data['iid'] ?? 0;
144 Logger::info('Start update1297 with max iid: ' . $max_iid);
146 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
148 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
150 if (DBA::errorNo() != 0) {
151 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
155 Logger::info('Processed rows: ' . DBA::affectedRows());
157 DI::config()->set('system', 'post_update_version', 1297);
159 Logger::info('Done');
164 * Remove contact duplicates
166 * @return bool "true" when the job is done
167 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
169 private static function update1322()
171 // Was the script completed?
172 if (DI::config()->get('system', 'post_update_version') >= 1322) {
176 Logger::info('Start');
178 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
179 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
180 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
181 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
182 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
183 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
185 while ($contact = DBA::fetch($contacts)) {
186 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
187 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
190 DBA::close($contact);
191 DI::config()->set('system', 'post_update_version', 1322);
193 Logger::info('Done');
199 * update user notification data
201 * @return bool "true" when the job is done
202 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
204 private static function update1329()
206 // Was the script completed?
207 if (DI::config()->get('system', 'post_update_version') >= 1329) {
211 if (!DBStructure::existsTable('item')) {
212 DI::config()->set('system', 'post_update_version', 1329);
216 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
218 Logger::info('Start', ['item' => $id]);
222 $condition = ["`id` > ?", $id];
223 $params = ['order' => ['id'], 'limit' => 10000];
224 $items = DBA::select('item', ['id', 'uri-id', 'uid'], $condition, $params);
226 if (DBA::errorNo() != 0) {
227 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
231 while ($item = DBA::fetch($items)) {
234 Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
240 DI::config()->set('system', 'post_update_version_1329_id', $id);
242 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
244 if ($start_id == $id) {
245 DI::config()->set('system', 'post_update_version', 1329);
246 Logger::info('Done');
254 * Fill the "tag" table with tags and mentions from the body
256 * @return bool "true" when the job is done
257 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
259 private static function update1341()
261 // Was the script completed?
262 if (DI::config()->get('system', 'post_update_version') >= 1341) {
266 if (!DBStructure::existsTable('item-content')) {
267 DI::config()->set('system', 'post_update_version', 1342);
271 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
273 Logger::info('Start', ['item' => $id]);
277 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
278 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
279 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
281 if (DBA::errorNo() != 0) {
282 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
286 while ($item = DBA::fetch($items)) {
287 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
288 $id = $item['uri-id'];
290 if ($rows % 1000 == 0) {
291 DI::config()->set('system', 'post_update_version_1341_id', $id);
296 DI::config()->set('system', 'post_update_version_1341_id', $id);
298 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
300 // When there are less than 1,000 items processed this means that we reached the end
301 // The other entries will then be processed with the regular functionality
303 DI::config()->set('system', 'post_update_version', 1341);
304 Logger::info('Done');
312 * Fill the "tag" table with tags and mentions from the "term" table
314 * @return bool "true" when the job is done
315 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
317 private static function update1342()
319 // Was the script completed?
320 if (DI::config()->get('system', 'post_update_version') >= 1342) {
324 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
325 DI::config()->set('system', 'post_update_version', 1342);
329 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
331 Logger::info('Start', ['item' => $id]);
335 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
337 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
338 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
339 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
340 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
342 if (DBA::errorNo() != 0) {
343 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
347 while ($term = DBA::fetch($terms)) {
348 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
349 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
350 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
351 if (!DBA::isResult($contact)) {
352 $ssl_url = str_replace('http://', 'https://', $term['url']);
353 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
354 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
357 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
358 $term['type'] = Tag::IMPLICIT_MENTION;
362 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url']);
366 if ($rows % 1000 == 0) {
367 DI::config()->set('system', 'post_update_version_1342_id', $id);
372 DI::config()->set('system', 'post_update_version_1342_id', $id);
374 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
376 // When there are less than 1,000 items processed this means that we reached the end
377 // The other entries will then be processed with the regular functionality
379 DI::config()->set('system', 'post_update_version', 1342);
380 Logger::info('Done');
388 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
390 * @return bool "true" when the job is done
391 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
393 private static function update1345()
395 // Was the script completed?
396 if (DI::config()->get('system', 'post_update_version') >= 1345) {
400 if (!DBStructure::existsTable('item-delivery-data')) {
401 DI::config()->set('system', 'post_update_version', 1345);
405 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
407 Logger::info('Start', ['item' => $id]);
411 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
412 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
413 FROM `item-delivery-data`
414 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
415 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
417 if (DBA::errorNo() != 0) {
418 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
422 while ($delivery = DBA::fetch($deliveries)) {
423 $id = $delivery['iid'];
424 unset($delivery['iid']);
425 DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
428 DBA::close($deliveries);
430 DI::config()->set('system', 'post_update_version_1345_id', $id);
432 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
434 // When there are less than 100 items processed this means that we reached the end
435 // The other entries will then be processed with the regular functionality
437 DI::config()->set('system', 'post_update_version', 1345);
438 Logger::info('Done');
446 * Generates the legacy item.file field string from an item ID.
447 * Includes only file and category terms.
449 * @param int $item_id
453 private static function fileTextFromItemId($item_id)
457 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
458 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
459 foreach ($tags as $tag) {
460 if ($tag['type'] == Category::CATEGORY) {
461 $file_text .= '<' . $tag['term'] . '>';
463 $file_text .= '[' . $tag['term'] . ']';
471 * Fill the "tag" table with tags and mentions from the "term" table
473 * @return bool "true" when the job is done
474 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
476 private static function update1346()
478 // Was the script completed?
479 if (DI::config()->get('system', 'post_update_version') >= 1346) {
483 if (!DBStructure::existsTable('term')) {
484 DI::config()->set('system', 'post_update_version', 1346);
488 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
490 Logger::info('Start', ['item' => $id]);
494 $terms = DBA::select('term', ['oid'],
495 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
496 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
498 if (DBA::errorNo() != 0) {
499 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
503 while ($term = DBA::fetch($terms)) {
504 $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
505 if (!DBA::isResult($item)) {
509 $file = self::fileTextFromItemId($term['oid']);
511 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
516 if ($rows % 100 == 0) {
517 DI::config()->set('system', 'post_update_version_1346_id', $id);
522 DI::config()->set('system', 'post_update_version_1346_id', $id);
524 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
526 // When there are less than 10 items processed this means that we reached the end
527 // The other entries will then be processed with the regular functionality
529 DI::config()->set('system', 'post_update_version', 1346);
530 Logger::info('Done');
538 * update the "vid" (verb) field in the item table
540 * @return bool "true" when the job is done
541 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
542 * @throws \ImagickException
544 private static function update1347()
546 // Was the script completed?
547 if (DI::config()->get('system', 'post_update_version') >= 1347) {
551 if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
552 DI::config()->set('system', 'post_update_version', 1347);
556 $id = DI::config()->get('system', 'post_update_version_1347_id', 0);
558 Logger::info('Start', ['item' => $id]);
563 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
564 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
565 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
566 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", Item::GRAVITY_ACTIVITY, $id);
568 if (DBA::errorNo() != 0) {
569 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
573 while ($item = DBA::fetch($items)) {
575 $verb = $item['item-verb'];
577 $verb = $item['verb'];
579 if (empty($verb) && is_int($item['activity'])) {
580 $verb = Item::ACTIVITIES[$item['activity']];
586 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
591 DI::config()->set('system', 'post_update_version_1347_id', $id);
593 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
595 if ($start_id == $id) {
596 DI::config()->set('system', 'post_update_version', 1347);
597 Logger::info('Done');
605 * update the "gsid" (global server id) field in the contact table
607 * @return bool "true" when the job is done
608 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
609 * @throws \ImagickException
611 private static function update1348()
613 // Was the script completed?
614 if (DI::config()->get('system', 'post_update_version') >= 1348) {
618 $id = DI::config()->get('system', 'post_update_version_1348_id', 0);
620 Logger::info('Start', ['contact' => $id]);
624 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
625 $params = ['order' => ['id'], 'limit' => 10000];
626 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
628 if (DBA::errorNo() != 0) {
629 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
633 while ($contact = DBA::fetch($contacts)) {
634 $id = $contact['id'];
636 DBA::update('contact',
637 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
638 ['id' => $contact['id']]);
642 DBA::close($contacts);
644 DI::config()->set('system', 'post_update_version_1348_id', $id);
646 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
648 if ($start_id == $id) {
649 DI::config()->set('system', 'post_update_version', 1348);
650 Logger::info('Done');
658 * update the "gsid" (global server id) field in the apcontact table
660 * @return bool "true" when the job is done
661 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
662 * @throws \ImagickException
664 private static function update1349()
666 // Was the script completed?
667 if (DI::config()->get('system', 'post_update_version') >= 1349) {
671 $id = DI::config()->get('system', 'post_update_version_1349_id', '');
673 Logger::info('Start', ['apcontact' => $id]);
677 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
678 $params = ['order' => ['url'], 'limit' => 10000];
679 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
681 if (DBA::errorNo() != 0) {
682 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
686 while ($apcontact = DBA::fetch($apcontacts)) {
687 $id = $apcontact['url'];
689 DBA::update('apcontact',
690 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
691 ['url' => $apcontact['url']]);
695 DBA::close($apcontacts);
697 DI::config()->set('system', 'post_update_version_1349_id', $id);
699 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
701 if ($start_id == $id) {
702 DI::config()->set('system', 'post_update_version', 1349);
703 Logger::info('Done');
711 * Remove orphaned photo entries
713 * @return bool "true" when the job is done
714 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
715 * @throws \ImagickException
717 private static function update1383()
719 // Was the script completed?
720 if (DI::config()->get('system', 'post_update_version') >= 1383) {
724 Logger::info('Start');
727 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
729 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
730 while ($photo = DBA::fetch($photos)) {
731 $delete = !in_array($photo['scale'], [4, 5, 6]);
734 // Check if there is a contact entry with that photo
735 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
736 $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
740 Photo::delete(['id' => $photo['id']]);
746 DI::config()->set('system', 'post_update_version', 1383);
747 Logger::info('Done', ['deleted' => $deleted]);
752 * update the "hash" field in the photo table
754 * @return bool "true" when the job is done
755 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
756 * @throws \ImagickException
758 private static function update1384()
760 // Was the script completed?
761 if (DI::config()->get('system', 'post_update_version') >= 1384) {
765 $condition = ["`hash` IS NULL"];
766 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
769 $photos = DBA::select('photo', [], $condition, ['limit' => 100]);
771 if (DBA::errorNo() != 0) {
772 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
776 while ($photo = DBA::fetch($photos)) {
777 $img = Photo::getImageForPhoto($photo);
779 $md5 = md5($img->asString());
783 DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
788 Logger::info('Processed', ['rows' => $rows]);
791 DI::config()->set('system', 'post_update_version', 1384);
792 Logger::info('Done');
800 * update the "external-id" field in the post table
802 * @return bool "true" when the job is done
803 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
804 * @throws \ImagickException
806 private static function update1400()
808 // Was the script completed?
809 if (DI::config()->get('system', 'post_update_version') >= 1400) {
813 if (!DBStructure::existsTable('item')) {
814 DI::config()->set('system', 'post_update_version', 1400);
818 $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
819 Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
822 $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
824 if (DBA::errorNo() != 0) {
825 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
829 while ($item = DBA::fetch($items)) {
830 Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
835 Logger::info('Processed', ['rows' => $rows]);
838 DI::config()->set('system', 'post_update_version', 1400);
839 Logger::info('Done');
847 * update the "uri-id" field in the contact table
849 * @return bool "true" when the job is done
850 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
851 * @throws \ImagickException
853 private static function update1424()
855 // Was the script completed?
856 if (DI::config()->get('system', 'post_update_version') >= 1424) {
860 $condition = ["`uri-id` IS NULL"];
861 Logger::info('Start', ['rest' => DBA::count('contact', $condition)]);
864 $contacts = DBA::select('contact', ['id', 'url'], $condition, ['limit' => 1000]);
866 if (DBA::errorNo() != 0) {
867 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
871 while ($contact = DBA::fetch($contacts)) {
872 DBA::update('contact', ['uri-id' => ItemURI::getIdByURI($contact['url'])], ['id' => $contact['id']]);
875 DBA::close($contacts);
877 Logger::info('Processed', ['rows' => $rows]);
880 DI::config()->set('system', 'post_update_version', 1424);
881 Logger::info('Done');
889 * update the "uri-id" field in the fcontact table
891 * @return bool "true" when the job is done
892 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
893 * @throws \ImagickException
895 private static function update1425()
897 // Was the script completed?
898 if (DI::config()->get('system', 'post_update_version') >= 1425) {
902 $condition = ["`uri-id` IS NULL"];
903 Logger::info('Start', ['rest' => DBA::count('fcontact', $condition)]);
906 $fcontacts = DBA::select('fcontact', ['id', 'url', 'guid'], $condition, ['limit' => 1000]);
908 if (DBA::errorNo() != 0) {
909 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
913 while ($fcontact = DBA::fetch($fcontacts)) {
914 if (!empty($fcontact['guid'])) {
915 $uriid = ItemURI::insert(['uri' => $fcontact['url'], 'guid' => $fcontact['guid']]);
917 $uriid = ItemURI::getIdByURI($fcontact['url']);
919 DBA::update('fcontact', ['uri-id' => $uriid], ['id' => $fcontact['id']]);
922 DBA::close($fcontacts);
924 Logger::info('Processed', ['rows' => $rows]);
927 DI::config()->set('system', 'post_update_version', 1425);
928 Logger::info('Done');
936 * update the "uri-id" field in the apcontact table
938 * @return bool "true" when the job is done
939 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
940 * @throws \ImagickException
942 private static function update1426()
944 // Was the script completed?
945 if (DI::config()->get('system', 'post_update_version') >= 1426) {
949 $condition = ["`uri-id` IS NULL"];
950 Logger::info('Start', ['rest' => DBA::count('apcontact', $condition)]);
953 $apcontacts = DBA::select('apcontact', ['url', 'uuid'], $condition, ['limit' => 1000]);
955 if (DBA::errorNo() != 0) {
956 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
960 while ($apcontact = DBA::fetch($apcontacts)) {
961 if (!empty($apcontact['uuid'])) {
962 $uriid = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
964 $uriid = ItemURI::getIdByURI($apcontact['url']);
966 DBA::update('apcontact', ['uri-id' => $uriid], ['url' => $apcontact['url']]);
969 DBA::close($apcontacts);
971 Logger::info('Processed', ['rows' => $rows]);
974 DI::config()->set('system', 'post_update_version', 1426);
975 Logger::info('Done');
983 * update the "uri-id" field in the event table
985 * @return bool "true" when the job is done
986 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
987 * @throws \ImagickException
989 private static function update1427()
991 // Was the script completed?
992 if (DI::config()->get('system', 'post_update_version') >= 1427) {
996 $condition = ["`uri-id` IS NULL"];
997 Logger::info('Start', ['rest' => DBA::count('event', $condition)]);
1000 $events = DBA::select('event', ['id', 'uri', 'guid'], $condition, ['limit' => 1000]);
1002 if (DBA::errorNo() != 0) {
1003 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1007 while ($event = DBA::fetch($events)) {
1008 if (!empty($event['guid'])) {
1009 $uriid = ItemURI::insert(['uri' => $event['uri'], 'guid' => $event['guid']]);
1011 $uriid = ItemURI::getIdByURI($event['uri']);
1013 DBA::update('event', ['uri-id' => $uriid], ['id' => $event['id']]);
1016 DBA::close($events);
1018 Logger::info('Processed', ['rows' => $rows]);
1021 DI::config()->set('system', 'post_update_version', 1427);
1022 Logger::info('Done');
1030 * Fill the receivers of the post via the raw source
1032 * @return bool "true" when the job is done
1033 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1034 * @throws \ImagickException
1036 private static function update1452()
1038 // Was the script completed?
1039 if (DI::config()->get('system', 'post_update_version') >= 1452) {
1043 if (!DBStructure::existsTable('conversation')) {
1044 DI::config()->set('system', 'post_update_version', 1452);
1048 $id = DI::config()->get('system', 'post_update_version_1452_id', 0);
1050 Logger::info('Start', ['uri-id' => $id]);
1055 $conversations = DBA::p("SELECT `post-view`.`uri-id`, `conversation`.`source`, `conversation`.`received` FROM `conversation`
1056 INNER JOIN `post-view` ON `post-view`.`uri` = `conversation`.`item-uri`
1057 WHERE NOT `source` IS NULL AND `conversation`.`protocol` = ? AND `uri-id` > ? LIMIT ?",
1058 Conversation::PARCEL_ACTIVITYPUB, $id, 1000);
1060 if (DBA::errorNo() != 0) {
1061 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1065 while ($conversation = DBA::fetch($conversations)) {
1066 $id = $conversation['uri-id'];
1067 $received = $conversation['received'];
1069 $raw = json_decode($conversation['source'], true);
1073 $activity = JsonLD::compact($raw);
1075 $urls = Receiver::getReceiverURL($activity);
1076 Processor::storeReceivers($conversation['uri-id'], $urls);
1078 if (!empty($activity['as:object'])) {
1079 $urls = array_merge($urls, Receiver::getReceiverURL($activity['as:object']));
1080 Processor::storeReceivers($conversation['uri-id'], $urls);
1085 DBA::close($conversations);
1087 DI::config()->set('system', 'post_update_version_1452_id', $id);
1089 Logger::info('Processed', ['rows' => $rows, 'last' => $id, 'last-received' => $received]);
1092 DI::config()->set('system', 'post_update_version', 1452);
1093 Logger::info('Done');
1101 * Correct the parent.
1102 * This fixes a bug that was introduced in the development of version 2022.09
1104 * @return bool "true" when the job is done
1105 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1106 * @throws \ImagickException
1108 private static function update1483()
1110 // Was the script completed?
1111 if (DI::config()->get('system', 'post_update_version') >= 1483) {
1115 Logger::info('Start');
1117 $posts = DBA::select('post-view', ['uri-id'], ['conversation' => './']);
1118 while ($post = DBA::fetch($posts)) {
1119 $parent = Item::getParent($post['uri-id']);
1121 DBA::update('post', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1122 DBA::update('post-user', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1127 DI::config()->set('system', 'post_update_version', 1483);
1128 Logger::info('Done');
1133 * Handle duplicate contact entries
1135 * @return bool "true" when the job is done
1136 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1137 * @throws \ImagickException
1139 private static function update1484()
1141 // Was the script completed?
1142 if (DI::config()->get('system', 'post_update_version') >= 1484) {
1146 $id = DI::config()->get('system', 'post_update_version_1484_id', 0);
1148 Logger::info('Start', ['id' => $id]);
1152 $contacts = DBA::select('contact', ['id', 'uid', 'uri-id', 'url'], ["`id` > ?", $id], ['order' => ['id'], 'limit' => 1000]);
1154 if (DBA::errorNo() != 0) {
1155 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1159 while ($contact = DBA::fetch($contacts)) {
1160 $id = $contact['id'];
1161 if (is_null($contact['uri-id'])) {
1162 $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
1163 DBA::update('contact', ['uri-id' => $contact['uri-id']], ['id' => $contact['id']]);
1165 Contact::setAccountUser($contact['id'], $contact['uid'], $contact['uri-id'], $contact['url']);
1168 DBA::close($contacts);
1170 DI::config()->set('system', 'post_update_version_1484_id', $id);
1172 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1175 DI::config()->set('system', 'post_update_version', 1484);
1176 Logger::info('Done');