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()) {
121 * Set the delivery queue count to a negative value for all items preceding the feature.
123 * @return bool "true" when the job is done
124 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
126 private static function update1297()
128 // Was the script completed?
129 if (DI::config()->get('system', 'post_update_version') >= 1297) {
133 if (!DBStructure::existsTable('item-delivery-data')) {
134 DI::config()->set('system', 'post_update_version', 1297);
138 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
139 $max_iid = $max_item_delivery_data['iid'] ?? 0;
141 Logger::info('Start update1297 with max iid: ' . $max_iid);
143 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
145 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
147 if (DBA::errorNo() != 0) {
148 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
152 Logger::info('Processed rows: ' . DBA::affectedRows());
154 DI::config()->set('system', 'post_update_version', 1297);
156 Logger::info('Done');
161 * Remove contact duplicates
163 * @return bool "true" when the job is done
164 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
166 private static function update1322()
168 // Was the script completed?
169 if (DI::config()->get('system', 'post_update_version') >= 1322) {
173 Logger::info('Start');
175 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
176 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
177 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
178 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
179 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
180 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
182 while ($contact = DBA::fetch($contacts)) {
183 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
184 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
187 DBA::close($contact);
188 DI::config()->set('system', 'post_update_version', 1322);
190 Logger::info('Done');
196 * update user notification data
198 * @return bool "true" when the job is done
199 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
201 private static function update1329()
203 // Was the script completed?
204 if (DI::config()->get('system', 'post_update_version') >= 1329) {
208 if (!DBStructure::existsTable('item')) {
209 DI::config()->set('system', 'post_update_version', 1329);
213 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
215 Logger::info('Start', ['item' => $id]);
219 $condition = ["`id` > ?", $id];
220 $params = ['order' => ['id'], 'limit' => 10000];
221 $items = DBA::select('item', ['id', 'uri-id', 'uid'], $condition, $params);
223 if (DBA::errorNo() != 0) {
224 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
228 while ($item = DBA::fetch($items)) {
231 Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
237 DI::config()->set('system', 'post_update_version_1329_id', $id);
239 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
241 if ($start_id == $id) {
242 DI::config()->set('system', 'post_update_version', 1329);
243 Logger::info('Done');
251 * Fill the "tag" table with tags and mentions from the body
253 * @return bool "true" when the job is done
254 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
256 private static function update1341()
258 // Was the script completed?
259 if (DI::config()->get('system', 'post_update_version') >= 1341) {
263 if (!DBStructure::existsTable('item-content')) {
264 DI::config()->set('system', 'post_update_version', 1342);
268 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
270 Logger::info('Start', ['item' => $id]);
274 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
275 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
276 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
278 if (DBA::errorNo() != 0) {
279 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
283 while ($item = DBA::fetch($items)) {
284 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
285 $id = $item['uri-id'];
287 if ($rows % 1000 == 0) {
288 DI::config()->set('system', 'post_update_version_1341_id', $id);
293 DI::config()->set('system', 'post_update_version_1341_id', $id);
295 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
297 // When there are less than 1,000 items processed this means that we reached the end
298 // The other entries will then be processed with the regular functionality
300 DI::config()->set('system', 'post_update_version', 1341);
301 Logger::info('Done');
309 * Fill the "tag" table with tags and mentions from the "term" table
311 * @return bool "true" when the job is done
312 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
314 private static function update1342()
316 // Was the script completed?
317 if (DI::config()->get('system', 'post_update_version') >= 1342) {
321 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
322 DI::config()->set('system', 'post_update_version', 1342);
326 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
328 Logger::info('Start', ['item' => $id]);
332 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
334 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
335 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
336 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
337 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
339 if (DBA::errorNo() != 0) {
340 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
344 while ($term = DBA::fetch($terms)) {
345 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
346 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
347 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
348 if (!DBA::isResult($contact)) {
349 $ssl_url = str_replace('http://', 'https://', $term['url']);
350 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
351 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
354 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
355 $term['type'] = Tag::IMPLICIT_MENTION;
359 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
363 if ($rows % 1000 == 0) {
364 DI::config()->set('system', 'post_update_version_1342_id', $id);
369 DI::config()->set('system', 'post_update_version_1342_id', $id);
371 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
373 // When there are less than 1,000 items processed this means that we reached the end
374 // The other entries will then be processed with the regular functionality
376 DI::config()->set('system', 'post_update_version', 1342);
377 Logger::info('Done');
385 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
387 * @return bool "true" when the job is done
388 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
390 private static function update1345()
392 // Was the script completed?
393 if (DI::config()->get('system', 'post_update_version') >= 1345) {
397 if (!DBStructure::existsTable('item-delivery-data')) {
398 DI::config()->set('system', 'post_update_version', 1345);
402 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
404 Logger::info('Start', ['item' => $id]);
408 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
409 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
410 FROM `item-delivery-data`
411 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
412 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
414 if (DBA::errorNo() != 0) {
415 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
419 while ($delivery = DBA::fetch($deliveries)) {
420 $id = $delivery['iid'];
421 unset($delivery['iid']);
422 DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
425 DBA::close($deliveries);
427 DI::config()->set('system', 'post_update_version_1345_id', $id);
429 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
431 // When there are less than 100 items processed this means that we reached the end
432 // The other entries will then be processed with the regular functionality
434 DI::config()->set('system', 'post_update_version', 1345);
435 Logger::info('Done');
443 * Generates the legacy item.file field string from an item ID.
444 * Includes only file and category terms.
446 * @param int $item_id
450 private static function fileTextFromItemId($item_id)
454 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
455 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
456 foreach ($tags as $tag) {
457 if ($tag['type'] == Category::CATEGORY) {
458 $file_text .= '<' . $tag['term'] . '>';
460 $file_text .= '[' . $tag['term'] . ']';
468 * Fill the "tag" table with tags and mentions from the "term" table
470 * @return bool "true" when the job is done
471 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
473 private static function update1346()
475 // Was the script completed?
476 if (DI::config()->get('system', 'post_update_version') >= 1346) {
480 if (!DBStructure::existsTable('term')) {
481 DI::config()->set('system', 'post_update_version', 1346);
485 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
487 Logger::info('Start', ['item' => $id]);
491 $terms = DBA::select('term', ['oid'],
492 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
493 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
495 if (DBA::errorNo() != 0) {
496 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
500 while ($term = DBA::fetch($terms)) {
501 $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
502 if (!DBA::isResult($item)) {
506 $file = self::fileTextFromItemId($term['oid']);
508 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
513 if ($rows % 100 == 0) {
514 DI::config()->set('system', 'post_update_version_1346_id', $id);
519 DI::config()->set('system', 'post_update_version_1346_id', $id);
521 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
523 // When there are less than 10 items processed this means that we reached the end
524 // The other entries will then be processed with the regular functionality
526 DI::config()->set('system', 'post_update_version', 1346);
527 Logger::info('Done');
535 * update the "vid" (verb) field in the item table
537 * @return bool "true" when the job is done
538 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
539 * @throws \ImagickException
541 private static function update1347()
543 // Was the script completed?
544 if (DI::config()->get('system', 'post_update_version') >= 1347) {
548 if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
549 DI::config()->set('system', 'post_update_version', 1347);
553 $id = DI::config()->get('system', 'post_update_version_1347_id', 0);
555 Logger::info('Start', ['item' => $id]);
560 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
561 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
562 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
563 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
565 if (DBA::errorNo() != 0) {
566 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
570 while ($item = DBA::fetch($items)) {
572 $verb = $item['item-verb'];
574 $verb = $item['verb'];
576 if (empty($verb) && is_int($item['activity'])) {
577 $verb = Item::ACTIVITIES[$item['activity']];
583 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
588 DI::config()->set('system', 'post_update_version_1347_id', $id);
590 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
592 if ($start_id == $id) {
593 DI::config()->set('system', 'post_update_version', 1347);
594 Logger::info('Done');
602 * update the "gsid" (global server id) field in the contact table
604 * @return bool "true" when the job is done
605 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
606 * @throws \ImagickException
608 private static function update1348()
610 // Was the script completed?
611 if (DI::config()->get('system', 'post_update_version') >= 1348) {
615 $id = DI::config()->get('system', 'post_update_version_1348_id', 0);
617 Logger::info('Start', ['contact' => $id]);
621 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
622 $params = ['order' => ['id'], 'limit' => 10000];
623 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
625 if (DBA::errorNo() != 0) {
626 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
630 while ($contact = DBA::fetch($contacts)) {
631 $id = $contact['id'];
633 DBA::update('contact',
634 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
635 ['id' => $contact['id']]);
639 DBA::close($contacts);
641 DI::config()->set('system', 'post_update_version_1348_id', $id);
643 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
645 if ($start_id == $id) {
646 DI::config()->set('system', 'post_update_version', 1348);
647 Logger::info('Done');
655 * update the "gsid" (global server id) field in the apcontact table
657 * @return bool "true" when the job is done
658 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
659 * @throws \ImagickException
661 private static function update1349()
663 // Was the script completed?
664 if (DI::config()->get('system', 'post_update_version') >= 1349) {
668 $id = DI::config()->get('system', 'post_update_version_1349_id', '');
670 Logger::info('Start', ['apcontact' => $id]);
674 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
675 $params = ['order' => ['url'], 'limit' => 10000];
676 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
678 if (DBA::errorNo() != 0) {
679 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
683 while ($apcontact = DBA::fetch($apcontacts)) {
684 $id = $apcontact['url'];
686 DBA::update('apcontact',
687 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
688 ['url' => $apcontact['url']]);
692 DBA::close($apcontacts);
694 DI::config()->set('system', 'post_update_version_1349_id', $id);
696 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
698 if ($start_id == $id) {
699 DI::config()->set('system', 'post_update_version', 1349);
700 Logger::info('Done');
708 * Remove orphaned photo entries
710 * @return bool "true" when the job is done
711 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
712 * @throws \ImagickException
714 private static function update1383()
716 // Was the script completed?
717 if (DI::config()->get('system', 'post_update_version') >= 1383) {
721 Logger::info('Start');
724 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
726 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
727 while ($photo = DBA::fetch($photos)) {
728 $delete = !in_array($photo['scale'], [4, 5, 6]);
731 // Check if there is a contact entry with that photo
732 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
733 $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
737 Photo::delete(['id' => $photo['id']]);
743 DI::config()->set('system', 'post_update_version', 1383);
744 Logger::info('Done', ['deleted' => $deleted]);
749 * update the "hash" field in the photo table
751 * @return bool "true" when the job is done
752 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
753 * @throws \ImagickException
755 private static function update1384()
757 // Was the script completed?
758 if (DI::config()->get('system', 'post_update_version') >= 1384) {
762 $condition = ["`hash` IS NULL"];
763 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
766 $photos = DBA::select('photo', [], $condition, ['limit' => 100]);
768 if (DBA::errorNo() != 0) {
769 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
773 while ($photo = DBA::fetch($photos)) {
774 $img = Photo::getImageForPhoto($photo);
776 $md5 = md5($img->asString());
780 DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
785 Logger::info('Processed', ['rows' => $rows]);
788 DI::config()->set('system', 'post_update_version', 1384);
789 Logger::info('Done');
797 * update the "external-id" field in the post table
799 * @return bool "true" when the job is done
800 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
801 * @throws \ImagickException
803 private static function update1400()
805 // Was the script completed?
806 if (DI::config()->get('system', 'post_update_version') >= 1400) {
810 if (!DBStructure::existsTable('item')) {
811 DI::config()->set('system', 'post_update_version', 1400);
815 $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
816 Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
819 $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
821 if (DBA::errorNo() != 0) {
822 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
826 while ($item = DBA::fetch($items)) {
827 Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
832 Logger::info('Processed', ['rows' => $rows]);
835 DI::config()->set('system', 'post_update_version', 1400);
836 Logger::info('Done');
844 * update the "uri-id" field in the contact table
846 * @return bool "true" when the job is done
847 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
848 * @throws \ImagickException
850 private static function update1424()
852 // Was the script completed?
853 if (DI::config()->get('system', 'post_update_version') >= 1424) {
857 $condition = ["`uri-id` IS NULL"];
858 Logger::info('Start', ['rest' => DBA::count('contact', $condition)]);
861 $contacts = DBA::select('contact', ['id', 'url'], $condition, ['limit' => 1000]);
863 if (DBA::errorNo() != 0) {
864 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
868 while ($contact = DBA::fetch($contacts)) {
869 DBA::update('contact', ['uri-id' => ItemURI::getIdByURI($contact['url'])], ['id' => $contact['id']]);
872 DBA::close($contacts);
874 Logger::info('Processed', ['rows' => $rows]);
877 DI::config()->set('system', 'post_update_version', 1424);
878 Logger::info('Done');
886 * update the "uri-id" field in the fcontact table
888 * @return bool "true" when the job is done
889 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
890 * @throws \ImagickException
892 private static function update1425()
894 // Was the script completed?
895 if (DI::config()->get('system', 'post_update_version') >= 1425) {
899 $condition = ["`uri-id` IS NULL"];
900 Logger::info('Start', ['rest' => DBA::count('fcontact', $condition)]);
903 $fcontacts = DBA::select('fcontact', ['id', 'url', 'guid'], $condition, ['limit' => 1000]);
905 if (DBA::errorNo() != 0) {
906 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
910 while ($fcontact = DBA::fetch($fcontacts)) {
911 if (!empty($fcontact['guid'])) {
912 $uriid = ItemURI::insert(['uri' => $fcontact['url'], 'guid' => $fcontact['guid']]);
914 $uriid = ItemURI::getIdByURI($fcontact['url']);
916 DBA::update('fcontact', ['uri-id' => $uriid], ['id' => $fcontact['id']]);
919 DBA::close($fcontacts);
921 Logger::info('Processed', ['rows' => $rows]);
924 DI::config()->set('system', 'post_update_version', 1425);
925 Logger::info('Done');
933 * update the "uri-id" field in the apcontact table
935 * @return bool "true" when the job is done
936 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
937 * @throws \ImagickException
939 private static function update1426()
941 // Was the script completed?
942 if (DI::config()->get('system', 'post_update_version') >= 1426) {
946 $condition = ["`uri-id` IS NULL"];
947 Logger::info('Start', ['rest' => DBA::count('apcontact', $condition)]);
950 $apcontacts = DBA::select('apcontact', ['url', 'uuid'], $condition, ['limit' => 1000]);
952 if (DBA::errorNo() != 0) {
953 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
957 while ($apcontact = DBA::fetch($apcontacts)) {
958 if (!empty($apcontact['uuid'])) {
959 $uriid = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
961 $uriid = ItemURI::getIdByURI($apcontact['url']);
963 DBA::update('apcontact', ['uri-id' => $uriid], ['url' => $apcontact['url']]);
966 DBA::close($apcontacts);
968 Logger::info('Processed', ['rows' => $rows]);
971 DI::config()->set('system', 'post_update_version', 1426);
972 Logger::info('Done');
980 * update the "uri-id" field in the event table
982 * @return bool "true" when the job is done
983 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
984 * @throws \ImagickException
986 private static function update1427()
988 // Was the script completed?
989 if (DI::config()->get('system', 'post_update_version') >= 1427) {
993 $condition = ["`uri-id` IS NULL"];
994 Logger::info('Start', ['rest' => DBA::count('event', $condition)]);
997 $events = DBA::select('event', ['id', 'uri', 'guid'], $condition, ['limit' => 1000]);
999 if (DBA::errorNo() != 0) {
1000 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1004 while ($event = DBA::fetch($events)) {
1005 if (!empty($event['guid'])) {
1006 $uriid = ItemURI::insert(['uri' => $event['uri'], 'guid' => $event['guid']]);
1008 $uriid = ItemURI::getIdByURI($event['uri']);
1010 DBA::update('event', ['uri-id' => $uriid], ['id' => $event['id']]);
1013 DBA::close($events);
1015 Logger::info('Processed', ['rows' => $rows]);
1018 DI::config()->set('system', 'post_update_version', 1427);
1019 Logger::info('Done');
1027 * Fill the receivers of the post via the raw source
1029 * @return bool "true" when the job is done
1030 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1031 * @throws \ImagickException
1033 private static function update1452()
1035 // Was the script completed?
1036 if (DI::config()->get('system', 'post_update_version') >= 1452) {
1040 $id = DI::config()->get('system', 'post_update_version_1452_id', 0);
1042 Logger::info('Start', ['uri-id' => $id]);
1047 $conversations = DBA::p("SELECT `post-view`.`uri-id`, `conversation`.`source`, `conversation`.`received` FROM `conversation`
1048 INNER JOIN `post-view` ON `post-view`.`uri` = `conversation`.`item-uri`
1049 WHERE NOT `source` IS NULL AND `conversation`.`protocol` = ? AND `uri-id` > ? LIMIT ?",
1050 Conversation::PARCEL_ACTIVITYPUB, $id, 1000);
1052 if (DBA::errorNo() != 0) {
1053 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1057 while ($conversation = DBA::fetch($conversations)) {
1058 $id = $conversation['uri-id'];
1059 $received = $conversation['received'];
1061 $raw = json_decode($conversation['source'], true);
1065 $activity = JsonLD::compact($raw);
1067 $urls = Receiver::getReceiverURL($activity);
1068 Processor::storeReceivers($conversation['uri-id'], $urls);
1070 if (!empty($activity['as:object'])) {
1071 $urls = array_merge($urls, Receiver::getReceiverURL($activity['as:object']));
1072 Processor::storeReceivers($conversation['uri-id'], $urls);
1077 DBA::close($conversations);
1079 DI::config()->set('system', 'post_update_version_1452_id', $id);
1081 Logger::info('Processed', ['rows' => $rows, 'last' => $id, 'last-received' => $received]);
1084 DI::config()->set('system', 'post_update_version', 1452);
1085 Logger::info('Done');
1093 * Correct the parent.
1094 * This fixes a bug that was introduced in the development of version 2022.09
1096 * @return bool "true" when the job is done
1097 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1098 * @throws \ImagickException
1100 private static function update1483()
1102 // Was the script completed?
1103 if (DI::config()->get('system', 'post_update_version') >= 1483) {
1107 Logger::info('Start');
1109 $posts = DBA::select('post-view', ['uri-id'], ['conversation' => './']);
1110 while ($post = DBA::fetch($posts)) {
1111 $parent = Item::getParent($post['uri-id']);
1113 DBA::update('post', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1114 DBA::update('post-user', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1119 DI::config()->set('system', 'post_update_version', 1483);
1120 Logger::info('Done');