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()) {
118 * Set the delivery queue count to a negative value for all items preceding the feature.
120 * @return bool "true" when the job is done
121 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
123 private static function update1297()
125 // Was the script completed?
126 if (DI::config()->get('system', 'post_update_version') >= 1297) {
130 if (!DBStructure::existsTable('item-delivery-data')) {
131 DI::config()->set('system', 'post_update_version', 1297);
135 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
136 $max_iid = $max_item_delivery_data['iid'] ?? 0;
138 Logger::info('Start update1297 with max iid: ' . $max_iid);
140 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
142 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
144 if (DBA::errorNo() != 0) {
145 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
149 Logger::info('Processed rows: ' . DBA::affectedRows());
151 DI::config()->set('system', 'post_update_version', 1297);
153 Logger::info('Done');
158 * Remove contact duplicates
160 * @return bool "true" when the job is done
161 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
163 private static function update1322()
165 // Was the script completed?
166 if (DI::config()->get('system', 'post_update_version') >= 1322) {
170 Logger::info('Start');
172 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
173 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
174 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
175 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
176 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
177 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
179 while ($contact = DBA::fetch($contacts)) {
180 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
181 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
184 DBA::close($contact);
185 DI::config()->set('system', 'post_update_version', 1322);
187 Logger::info('Done');
193 * update user notification data
195 * @return bool "true" when the job is done
196 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
198 private static function update1329()
200 // Was the script completed?
201 if (DI::config()->get('system', 'post_update_version') >= 1329) {
205 if (!DBStructure::existsTable('item')) {
206 DI::config()->set('system', 'post_update_version', 1329);
210 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
212 Logger::info('Start', ['item' => $id]);
216 $condition = ["`id` > ?", $id];
217 $params = ['order' => ['id'], 'limit' => 10000];
218 $items = DBA::select('item', ['id', 'uri-id', 'uid'], $condition, $params);
220 if (DBA::errorNo() != 0) {
221 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
225 while ($item = DBA::fetch($items)) {
228 Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
234 DI::config()->set('system', 'post_update_version_1329_id', $id);
236 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
238 if ($start_id == $id) {
239 DI::config()->set('system', 'post_update_version', 1329);
240 Logger::info('Done');
248 * Fill the "tag" table with tags and mentions from the body
250 * @return bool "true" when the job is done
251 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
253 private static function update1341()
255 // Was the script completed?
256 if (DI::config()->get('system', 'post_update_version') >= 1341) {
260 if (!DBStructure::existsTable('item-content')) {
261 DI::config()->set('system', 'post_update_version', 1342);
265 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
267 Logger::info('Start', ['item' => $id]);
271 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
272 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
273 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
275 if (DBA::errorNo() != 0) {
276 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
280 while ($item = DBA::fetch($items)) {
281 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
282 $id = $item['uri-id'];
284 if ($rows % 1000 == 0) {
285 DI::config()->set('system', 'post_update_version_1341_id', $id);
290 DI::config()->set('system', 'post_update_version_1341_id', $id);
292 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
294 // When there are less than 1,000 items processed this means that we reached the end
295 // The other entries will then be processed with the regular functionality
297 DI::config()->set('system', 'post_update_version', 1341);
298 Logger::info('Done');
306 * Fill the "tag" table with tags and mentions from the "term" table
308 * @return bool "true" when the job is done
309 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
311 private static function update1342()
313 // Was the script completed?
314 if (DI::config()->get('system', 'post_update_version') >= 1342) {
318 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
319 DI::config()->set('system', 'post_update_version', 1342);
323 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
325 Logger::info('Start', ['item' => $id]);
329 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
331 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
332 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
333 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
334 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
336 if (DBA::errorNo() != 0) {
337 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
341 while ($term = DBA::fetch($terms)) {
342 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
343 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
344 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
345 if (!DBA::isResult($contact)) {
346 $ssl_url = str_replace('http://', 'https://', $term['url']);
347 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
348 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
351 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
352 $term['type'] = Tag::IMPLICIT_MENTION;
356 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
360 if ($rows % 1000 == 0) {
361 DI::config()->set('system', 'post_update_version_1342_id', $id);
366 DI::config()->set('system', 'post_update_version_1342_id', $id);
368 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
370 // When there are less than 1,000 items processed this means that we reached the end
371 // The other entries will then be processed with the regular functionality
373 DI::config()->set('system', 'post_update_version', 1342);
374 Logger::info('Done');
382 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
384 * @return bool "true" when the job is done
385 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
387 private static function update1345()
389 // Was the script completed?
390 if (DI::config()->get('system', 'post_update_version') >= 1345) {
394 if (!DBStructure::existsTable('item-delivery-data')) {
395 DI::config()->set('system', 'post_update_version', 1345);
399 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
401 Logger::info('Start', ['item' => $id]);
405 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
406 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
407 FROM `item-delivery-data`
408 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
409 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
411 if (DBA::errorNo() != 0) {
412 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
416 while ($delivery = DBA::fetch($deliveries)) {
417 $id = $delivery['iid'];
418 unset($delivery['iid']);
419 DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
422 DBA::close($deliveries);
424 DI::config()->set('system', 'post_update_version_1345_id', $id);
426 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
428 // When there are less than 100 items processed this means that we reached the end
429 // The other entries will then be processed with the regular functionality
431 DI::config()->set('system', 'post_update_version', 1345);
432 Logger::info('Done');
440 * Generates the legacy item.file field string from an item ID.
441 * Includes only file and category terms.
443 * @param int $item_id
447 private static function fileTextFromItemId($item_id)
451 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
452 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
453 foreach ($tags as $tag) {
454 if ($tag['type'] == Category::CATEGORY) {
455 $file_text .= '<' . $tag['term'] . '>';
457 $file_text .= '[' . $tag['term'] . ']';
465 * Fill the "tag" table with tags and mentions from the "term" table
467 * @return bool "true" when the job is done
468 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
470 private static function update1346()
472 // Was the script completed?
473 if (DI::config()->get('system', 'post_update_version') >= 1346) {
477 if (!DBStructure::existsTable('term')) {
478 DI::config()->set('system', 'post_update_version', 1346);
482 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
484 Logger::info('Start', ['item' => $id]);
488 $terms = DBA::select('term', ['oid'],
489 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
490 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
492 if (DBA::errorNo() != 0) {
493 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
497 while ($term = DBA::fetch($terms)) {
498 $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
499 if (!DBA::isResult($item)) {
503 $file = self::fileTextFromItemId($term['oid']);
505 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
510 if ($rows % 100 == 0) {
511 DI::config()->set('system', 'post_update_version_1346_id', $id);
516 DI::config()->set('system', 'post_update_version_1346_id', $id);
518 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
520 // When there are less than 10 items processed this means that we reached the end
521 // The other entries will then be processed with the regular functionality
523 DI::config()->set('system', 'post_update_version', 1346);
524 Logger::info('Done');
532 * update the "vid" (verb) field in the item table
534 * @return bool "true" when the job is done
535 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
536 * @throws \ImagickException
538 private static function update1347()
540 // Was the script completed?
541 if (DI::config()->get('system', 'post_update_version') >= 1347) {
545 if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
546 DI::config()->set('system', 'post_update_version', 1347);
550 $id = DI::config()->get('system', 'post_update_version_1347_id', 0);
552 Logger::info('Start', ['item' => $id]);
557 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
558 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
559 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
560 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
562 if (DBA::errorNo() != 0) {
563 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
567 while ($item = DBA::fetch($items)) {
569 $verb = $item['item-verb'];
571 $verb = $item['verb'];
573 if (empty($verb) && is_int($item['activity'])) {
574 $verb = Item::ACTIVITIES[$item['activity']];
580 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
585 DI::config()->set('system', 'post_update_version_1347_id', $id);
587 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
589 if ($start_id == $id) {
590 DI::config()->set('system', 'post_update_version', 1347);
591 Logger::info('Done');
599 * update the "gsid" (global server id) field in the contact table
601 * @return bool "true" when the job is done
602 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
603 * @throws \ImagickException
605 private static function update1348()
607 // Was the script completed?
608 if (DI::config()->get('system', 'post_update_version') >= 1348) {
612 $id = DI::config()->get('system', 'post_update_version_1348_id', 0);
614 Logger::info('Start', ['contact' => $id]);
618 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
619 $params = ['order' => ['id'], 'limit' => 10000];
620 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
622 if (DBA::errorNo() != 0) {
623 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
627 while ($contact = DBA::fetch($contacts)) {
628 $id = $contact['id'];
630 DBA::update('contact',
631 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
632 ['id' => $contact['id']]);
636 DBA::close($contacts);
638 DI::config()->set('system', 'post_update_version_1348_id', $id);
640 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
642 if ($start_id == $id) {
643 DI::config()->set('system', 'post_update_version', 1348);
644 Logger::info('Done');
652 * update the "gsid" (global server id) field in the apcontact table
654 * @return bool "true" when the job is done
655 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
656 * @throws \ImagickException
658 private static function update1349()
660 // Was the script completed?
661 if (DI::config()->get('system', 'post_update_version') >= 1349) {
665 $id = DI::config()->get('system', 'post_update_version_1349_id', '');
667 Logger::info('Start', ['apcontact' => $id]);
671 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
672 $params = ['order' => ['url'], 'limit' => 10000];
673 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
675 if (DBA::errorNo() != 0) {
676 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
680 while ($apcontact = DBA::fetch($apcontacts)) {
681 $id = $apcontact['url'];
683 DBA::update('apcontact',
684 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
685 ['url' => $apcontact['url']]);
689 DBA::close($apcontacts);
691 DI::config()->set('system', 'post_update_version_1349_id', $id);
693 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
695 if ($start_id == $id) {
696 DI::config()->set('system', 'post_update_version', 1349);
697 Logger::info('Done');
705 * Remove orphaned photo entries
707 * @return bool "true" when the job is done
708 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
709 * @throws \ImagickException
711 private static function update1383()
713 // Was the script completed?
714 if (DI::config()->get('system', 'post_update_version') >= 1383) {
718 Logger::info('Start');
721 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
723 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
724 while ($photo = DBA::fetch($photos)) {
725 $delete = !in_array($photo['scale'], [4, 5, 6]);
728 // Check if there is a contact entry with that photo
729 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
730 $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
734 Photo::delete(['id' => $photo['id']]);
740 DI::config()->set('system', 'post_update_version', 1383);
741 Logger::info('Done', ['deleted' => $deleted]);
746 * update the "hash" field in the photo table
748 * @return bool "true" when the job is done
749 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
750 * @throws \ImagickException
752 private static function update1384()
754 // Was the script completed?
755 if (DI::config()->get('system', 'post_update_version') >= 1384) {
759 $condition = ["`hash` IS NULL"];
760 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
763 $photos = DBA::select('photo', [], $condition, ['limit' => 100]);
765 if (DBA::errorNo() != 0) {
766 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
770 while ($photo = DBA::fetch($photos)) {
771 $img = Photo::getImageForPhoto($photo);
773 $md5 = md5($img->asString());
777 DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
782 Logger::info('Processed', ['rows' => $rows]);
785 DI::config()->set('system', 'post_update_version', 1384);
786 Logger::info('Done');
794 * update the "external-id" field in the post table
796 * @return bool "true" when the job is done
797 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
798 * @throws \ImagickException
800 private static function update1400()
802 // Was the script completed?
803 if (DI::config()->get('system', 'post_update_version') >= 1400) {
807 if (!DBStructure::existsTable('item')) {
808 DI::config()->set('system', 'post_update_version', 1400);
812 $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
813 Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
816 $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
818 if (DBA::errorNo() != 0) {
819 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
823 while ($item = DBA::fetch($items)) {
824 Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
829 Logger::info('Processed', ['rows' => $rows]);
832 DI::config()->set('system', 'post_update_version', 1400);
833 Logger::info('Done');
841 * update the "uri-id" field in the contact table
843 * @return bool "true" when the job is done
844 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
845 * @throws \ImagickException
847 private static function update1424()
849 // Was the script completed?
850 if (DI::config()->get('system', 'post_update_version') >= 1424) {
854 $condition = ["`uri-id` IS NULL"];
855 Logger::info('Start', ['rest' => DBA::count('contact', $condition)]);
858 $contacts = DBA::select('contact', ['id', 'url'], $condition, ['limit' => 1000]);
860 if (DBA::errorNo() != 0) {
861 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
865 while ($contact = DBA::fetch($contacts)) {
866 DBA::update('contact', ['uri-id' => ItemURI::getIdByURI($contact['url'])], ['id' => $contact['id']]);
869 DBA::close($contacts);
871 Logger::info('Processed', ['rows' => $rows]);
874 DI::config()->set('system', 'post_update_version', 1424);
875 Logger::info('Done');
883 * update the "uri-id" field in the fcontact table
885 * @return bool "true" when the job is done
886 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
887 * @throws \ImagickException
889 private static function update1425()
891 // Was the script completed?
892 if (DI::config()->get('system', 'post_update_version') >= 1425) {
896 $condition = ["`uri-id` IS NULL"];
897 Logger::info('Start', ['rest' => DBA::count('fcontact', $condition)]);
900 $fcontacts = DBA::select('fcontact', ['id', 'url', 'guid'], $condition, ['limit' => 1000]);
902 if (DBA::errorNo() != 0) {
903 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
907 while ($fcontact = DBA::fetch($fcontacts)) {
908 if (!empty($fcontact['guid'])) {
909 $uriid = ItemURI::insert(['uri' => $fcontact['url'], 'guid' => $fcontact['guid']]);
911 $uriid = ItemURI::getIdByURI($fcontact['url']);
913 DBA::update('fcontact', ['uri-id' => $uriid], ['id' => $fcontact['id']]);
916 DBA::close($fcontacts);
918 Logger::info('Processed', ['rows' => $rows]);
921 DI::config()->set('system', 'post_update_version', 1425);
922 Logger::info('Done');
930 * update the "uri-id" field in the apcontact table
932 * @return bool "true" when the job is done
933 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
934 * @throws \ImagickException
936 private static function update1426()
938 // Was the script completed?
939 if (DI::config()->get('system', 'post_update_version') >= 1426) {
943 $condition = ["`uri-id` IS NULL"];
944 Logger::info('Start', ['rest' => DBA::count('apcontact', $condition)]);
947 $apcontacts = DBA::select('apcontact', ['url', 'uuid'], $condition, ['limit' => 1000]);
949 if (DBA::errorNo() != 0) {
950 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
954 while ($apcontact = DBA::fetch($apcontacts)) {
955 if (!empty($apcontact['uuid'])) {
956 $uriid = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
958 $uriid = ItemURI::getIdByURI($apcontact['url']);
960 DBA::update('apcontact', ['uri-id' => $uriid], ['url' => $apcontact['url']]);
963 DBA::close($apcontacts);
965 Logger::info('Processed', ['rows' => $rows]);
968 DI::config()->set('system', 'post_update_version', 1426);
969 Logger::info('Done');
977 * update the "uri-id" field in the event table
979 * @return bool "true" when the job is done
980 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
981 * @throws \ImagickException
983 private static function update1427()
985 // Was the script completed?
986 if (DI::config()->get('system', 'post_update_version') >= 1427) {
990 $condition = ["`uri-id` IS NULL"];
991 Logger::info('Start', ['rest' => DBA::count('event', $condition)]);
994 $events = DBA::select('event', ['id', 'uri', 'guid'], $condition, ['limit' => 1000]);
996 if (DBA::errorNo() != 0) {
997 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1001 while ($event = DBA::fetch($events)) {
1002 if (!empty($event['guid'])) {
1003 $uriid = ItemURI::insert(['uri' => $event['uri'], 'guid' => $event['guid']]);
1005 $uriid = ItemURI::getIdByURI($event['uri']);
1007 DBA::update('event', ['uri-id' => $uriid], ['id' => $event['id']]);
1010 DBA::close($events);
1012 Logger::info('Processed', ['rows' => $rows]);
1015 DI::config()->set('system', 'post_update_version', 1427);
1016 Logger::info('Done');
1024 * Fill the receivers of the post via the raw source
1026 * @return bool "true" when the job is done
1027 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1028 * @throws \ImagickException
1030 private static function update1452()
1032 // Was the script completed?
1033 if (DI::config()->get('system', 'post_update_version') >= 1452) {
1037 $id = DI::config()->get('system', 'post_update_version_1452_id', 0);
1039 Logger::info('Start', ['uri-id' => $id]);
1044 $conversations = DBA::p("SELECT `post-view`.`uri-id`, `conversation`.`source`, `conversation`.`received` FROM `conversation`
1045 INNER JOIN `post-view` ON `post-view`.`uri` = `conversation`.`item-uri`
1046 WHERE NOT `source` IS NULL AND `conversation`.`protocol` = ? AND `uri-id` > ? LIMIT ?",
1047 Conversation::PARCEL_ACTIVITYPUB, $id, 1000);
1049 if (DBA::errorNo() != 0) {
1050 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1054 while ($conversation = DBA::fetch($conversations)) {
1055 $id = $conversation['uri-id'];
1056 $received = $conversation['received'];
1058 $raw = json_decode($conversation['source'], true);
1062 $activity = JsonLD::compact($raw);
1064 $urls = Receiver::getReceiverURL($activity);
1065 Processor::storeReceivers($conversation['uri-id'], $urls);
1067 if (!empty($activity['as:object'])) {
1068 $urls = array_merge($urls, Receiver::getReceiverURL($activity['as:object']));
1069 Processor::storeReceivers($conversation['uri-id'], $urls);
1074 DBA::close($conversations);
1076 DI::config()->set('system', 'post_update_version_1452_id', $id);
1078 Logger::info('Processed', ['rows' => $rows, 'last' => $id, 'last-received' => $received]);
1081 DI::config()->set('system', 'post_update_version', 1452);
1082 Logger::info('Done');