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\Post\Counts;
36 use Friendica\Model\Tag;
37 use Friendica\Model\Verb;
38 use Friendica\Protocol\ActivityPub\Processor;
39 use Friendica\Protocol\ActivityPub\Receiver;
40 use Friendica\Util\JsonLD;
41 use Friendica\Util\Strings;
42 use GuzzleHttp\Psr7\Uri;
45 * These database-intensive post update routines are meant to be executed in the background by the cronjob.
47 * If there is a need for a intensive migration after a database structure change, update this file
48 * by adding a new method at the end with the number of the new DB_UPDATE_VERSION.
52 // Needed for the helper function to read from the legacy term table
53 const OBJECT_TYPE_POST = 1;
58 * Calls the post update functions
60 public static function update()
62 if (!self::update1297()) {
65 if (!self::update1322()) {
68 if (!self::update1329()) {
71 if (!self::update1341()) {
74 if (!self::update1342()) {
77 if (!self::update1345()) {
80 if (!self::update1346()) {
83 if (!self::update1347()) {
86 if (!self::update1348()) {
89 if (!self::update1349()) {
92 if (!self::update1383()) {
95 if (!self::update1384()) {
98 if (!self::update1400()) {
101 if (!self::update1424()) {
104 if (!self::update1425()) {
107 if (!self::update1426()) {
110 if (!self::update1427()) {
113 if (!self::update1452()) {
116 if (!self::update1483()) {
119 if (!self::update1484()) {
122 if (!self::update1506()) {
125 if (!self::update1507()) {
128 if (!self::update1543()) {
135 * Set the delivery queue count to a negative value for all items preceding the feature.
137 * @return bool "true" when the job is done
138 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
140 private static function update1297()
142 // Was the script completed?
143 if (DI::keyValue()->get('post_update_version') >= 1297) {
147 if (!DBStructure::existsTable('item-delivery-data')) {
148 DI::keyValue()->set('post_update_version', 1297);
152 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
153 $max_iid = $max_item_delivery_data['iid'] ?? 0;
155 Logger::info('Start update1297 with max iid: ' . $max_iid);
157 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
159 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
161 if (DBA::errorNo() != 0) {
162 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
166 Logger::info('Processed rows: ' . DBA::affectedRows());
168 DI::keyValue()->set('post_update_version', 1297);
170 Logger::info('Done');
175 * Remove contact duplicates
177 * @return bool "true" when the job is done
178 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
180 private static function update1322()
182 // Was the script completed?
183 if (DI::keyValue()->get('post_update_version') >= 1322) {
187 Logger::info('Start');
189 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
190 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
191 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
192 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
193 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
194 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
196 while ($contact = DBA::fetch($contacts)) {
197 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
198 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
201 DBA::close($contact);
202 DI::keyValue()->set('post_update_version', 1322);
204 Logger::info('Done');
210 * update user notification data
212 * @return bool "true" when the job is done
213 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
215 private static function update1329()
217 // Was the script completed?
218 if (DI::keyValue()->get('post_update_version') >= 1329) {
222 if (!DBStructure::existsTable('item')) {
223 DI::keyValue()->set('post_update_version', 1329);
227 $id = DI::keyValue()->get('post_update_version_1329_id') ?? 0;
229 Logger::info('Start', ['item' => $id]);
233 $condition = ["`id` > ?", $id];
234 $params = ['order' => ['id'], 'limit' => 10000];
235 $items = DBA::select('item', ['id', 'uri-id', 'uid'], $condition, $params);
237 if (DBA::errorNo() != 0) {
238 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
242 while ($item = DBA::fetch($items)) {
245 Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
251 DI::keyValue()->set('post_update_version_1329_id', $id);
253 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
255 if ($start_id == $id) {
256 DI::keyValue()->set('post_update_version', 1329);
257 Logger::info('Done');
265 * Fill the "tag" table with tags and mentions from the body
267 * @return bool "true" when the job is done
268 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
270 private static function update1341()
272 // Was the script completed?
273 if (DI::keyValue()->get('post_update_version') >= 1341) {
277 if (!DBStructure::existsTable('item-content')) {
278 DI::keyValue()->set('post_update_version', 1342);
282 $id = DI::keyValue()->get('post_update_version_1341_id') ?? 0;
284 Logger::info('Start', ['item' => $id]);
288 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
289 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
290 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
292 if (DBA::errorNo() != 0) {
293 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
297 while ($item = DBA::fetch($items)) {
298 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
299 $id = $item['uri-id'];
301 if ($rows % 1000 == 0) {
302 DI::keyValue()->set('post_update_version_1341_id', $id);
307 DI::keyValue()->set('post_update_version_1341_id', $id);
309 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
311 // When there are less than 1,000 items processed this means that we reached the end
312 // The other entries will then be processed with the regular functionality
314 DI::keyValue()->set('post_update_version', 1341);
315 Logger::info('Done');
323 * Fill the "tag" table with tags and mentions from the "term" table
325 * @return bool "true" when the job is done
326 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
328 private static function update1342()
330 // Was the script completed?
331 if (DI::keyValue()->get('post_update_version') >= 1342) {
335 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
336 DI::keyValue()->set('post_update_version', 1342);
340 $id = DI::keyValue()->get('post_update_version_1342_id') ?? 0;
342 Logger::info('Start', ['item' => $id]);
346 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
348 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
349 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
350 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
351 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
353 if (DBA::errorNo() != 0) {
354 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
358 while ($term = DBA::fetch($terms)) {
359 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
360 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
361 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
362 if (!DBA::isResult($contact)) {
363 $ssl_url = str_replace('http://', 'https://', $term['url']);
364 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
365 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
368 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
369 $term['type'] = Tag::IMPLICIT_MENTION;
373 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url']);
377 if ($rows % 1000 == 0) {
378 DI::keyValue()->set('post_update_version_1342_id', $id);
383 DI::keyValue()->set('post_update_version_1342_id', $id);
385 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
387 // When there are less than 1,000 items processed this means that we reached the end
388 // The other entries will then be processed with the regular functionality
390 DI::keyValue()->set('post_update_version', 1342);
391 Logger::info('Done');
399 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
401 * @return bool "true" when the job is done
402 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
404 private static function update1345()
406 // Was the script completed?
407 if (DI::keyValue()->get('post_update_version') >= 1345) {
411 if (!DBStructure::existsTable('item-delivery-data')) {
412 DI::keyValue()->set('post_update_version', 1345);
416 $id = DI::keyValue()->get('post_update_version_1345_id') ?? 0;
418 Logger::info('Start', ['item' => $id]);
422 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
423 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
424 FROM `item-delivery-data`
425 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
426 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
428 if (DBA::errorNo() != 0) {
429 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
433 while ($delivery = DBA::fetch($deliveries)) {
434 $id = $delivery['iid'];
435 unset($delivery['iid']);
436 DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
439 DBA::close($deliveries);
441 DI::keyValue()->set('post_update_version_1345_id', $id);
443 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
445 // When there are less than 100 items processed this means that we reached the end
446 // The other entries will then be processed with the regular functionality
448 DI::keyValue()->set('post_update_version', 1345);
449 Logger::info('Done');
457 * Generates the legacy item.file field string from an item ID.
458 * Includes only file and category terms.
460 * @param int $item_id
464 private static function fileTextFromItemId($item_id)
468 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
469 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
470 foreach ($tags as $tag) {
471 if ($tag['type'] == Category::CATEGORY) {
472 $file_text .= '<' . $tag['term'] . '>';
474 $file_text .= '[' . $tag['term'] . ']';
482 * Fill the "tag" table with tags and mentions from the "term" table
484 * @return bool "true" when the job is done
485 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
487 private static function update1346()
489 // Was the script completed?
490 if (DI::keyValue()->get('post_update_version') >= 1346) {
494 if (!DBStructure::existsTable('term')) {
495 DI::keyValue()->set('post_update_version', 1346);
499 $id = DI::keyValue()->get('post_update_version_1346_id') ?? 0;
501 Logger::info('Start', ['item' => $id]);
505 $terms = DBA::select('term', ['oid'],
506 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
507 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
509 if (DBA::errorNo() != 0) {
510 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
514 while ($term = DBA::fetch($terms)) {
515 $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
516 if (!DBA::isResult($item)) {
520 $file = self::fileTextFromItemId($term['oid']);
522 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
527 if ($rows % 100 == 0) {
528 DI::keyValue()->set('post_update_version_1346_id', $id);
533 DI::keyValue()->set('post_update_version_1346_id', $id);
535 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
537 // When there are less than 10 items processed this means that we reached the end
538 // The other entries will then be processed with the regular functionality
540 DI::keyValue()->set('post_update_version', 1346);
541 Logger::info('Done');
549 * update the "vid" (verb) field in the item table
551 * @return bool "true" when the job is done
552 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
553 * @throws \ImagickException
555 private static function update1347()
557 // Was the script completed?
558 if (DI::keyValue()->get('post_update_version') >= 1347) {
562 if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
563 DI::keyValue()->set('post_update_version', 1347);
567 $id = DI::keyValue()->get('post_update_version_1347_id') ?? 0;
569 Logger::info('Start', ['item' => $id]);
574 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
575 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
576 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
577 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", Item::GRAVITY_ACTIVITY, $id);
579 if (DBA::errorNo() != 0) {
580 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
584 while ($item = DBA::fetch($items)) {
586 $verb = $item['item-verb'];
588 $verb = $item['verb'];
590 if (empty($verb) && is_int($item['activity'])) {
591 $verb = Item::ACTIVITIES[$item['activity']];
597 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
602 DI::keyValue()->set('post_update_version_1347_id', $id);
604 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
606 if ($start_id == $id) {
607 DI::keyValue()->set('post_update_version', 1347);
608 Logger::info('Done');
616 * update the "gsid" (global server id) field in the contact table
618 * @return bool "true" when the job is done
619 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
620 * @throws \ImagickException
622 private static function update1348()
624 // Was the script completed?
625 if (DI::keyValue()->get('post_update_version') >= 1348) {
629 $id = DI::keyValue()->get('post_update_version_1348_id') ?? 0;
631 Logger::info('Start', ['contact' => $id]);
635 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
636 $params = ['order' => ['id'], 'limit' => 10000];
637 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
639 if (DBA::errorNo() != 0) {
640 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
644 while ($contact = DBA::fetch($contacts)) {
645 $id = $contact['id'];
647 DBA::update('contact',
648 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
649 ['id' => $contact['id']]);
653 DBA::close($contacts);
655 DI::keyValue()->set('post_update_version_1348_id', $id);
657 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
659 if ($start_id == $id) {
660 DI::keyValue()->set('post_update_version', 1348);
661 Logger::info('Done');
669 * update the "gsid" (global server id) field in the apcontact table
671 * @return bool "true" when the job is done
672 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
673 * @throws \ImagickException
675 private static function update1349()
677 // Was the script completed?
678 if (DI::keyValue()->get('post_update_version') >= 1349) {
682 $id = DI::keyValue()->get('post_update_version_1349_id') ?? '';
684 Logger::info('Start', ['apcontact' => $id]);
688 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
689 $params = ['order' => ['url'], 'limit' => 10000];
690 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
692 if (DBA::errorNo() != 0) {
693 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
697 while ($apcontact = DBA::fetch($apcontacts)) {
698 $id = $apcontact['url'];
700 DBA::update('apcontact',
701 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
702 ['url' => $apcontact['url']]);
706 DBA::close($apcontacts);
708 DI::keyValue()->set('post_update_version_1349_id', $id);
710 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
712 if ($start_id == $id) {
713 DI::keyValue()->set('post_update_version', 1349);
714 Logger::info('Done');
722 * Remove orphaned photo entries
724 * @return bool "true" when the job is done
725 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
726 * @throws \ImagickException
728 private static function update1383()
730 // Was the script completed?
731 if (DI::keyValue()->get('post_update_version') >= 1383) {
735 Logger::info('Start');
738 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
740 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
741 while ($photo = DBA::fetch($photos)) {
742 $delete = !in_array($photo['scale'], [4, 5, 6]);
745 // Check if there is a contact entry with that photo
746 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
747 $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
751 Photo::delete(['id' => $photo['id']]);
757 DI::keyValue()->set('post_update_version', 1383);
758 Logger::info('Done', ['deleted' => $deleted]);
763 * update the "hash" field in the photo table
765 * @return bool "true" when the job is done
766 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
767 * @throws \ImagickException
769 private static function update1384()
771 // Was the script completed?
772 if (DI::keyValue()->get('post_update_version') >= 1384) {
776 $condition = ["`hash` IS NULL"];
777 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
780 $photos = DBA::select('photo', [], $condition, ['limit' => 100]);
782 if (DBA::errorNo() != 0) {
783 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
787 while ($photo = DBA::fetch($photos)) {
788 $img = Photo::getImageForPhoto($photo);
790 $md5 = md5($img->asString());
794 DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
799 Logger::info('Processed', ['rows' => $rows]);
802 DI::keyValue()->set('post_update_version', 1384);
803 Logger::info('Done');
811 * update the "external-id" field in the post table
813 * @return bool "true" when the job is done
814 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
815 * @throws \ImagickException
817 private static function update1400()
819 // Was the script completed?
820 if (DI::keyValue()->get('post_update_version') >= 1400) {
824 if (!DBStructure::existsTable('item')) {
825 DI::keyValue()->set('post_update_version', 1400);
829 $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
830 Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
833 $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
835 if (DBA::errorNo() != 0) {
836 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
840 while ($item = DBA::fetch($items)) {
841 Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
846 Logger::info('Processed', ['rows' => $rows]);
849 DI::keyValue()->set('post_update_version', 1400);
850 Logger::info('Done');
858 * update the "uri-id" field in the contact table
860 * @return bool "true" when the job is done
861 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
862 * @throws \ImagickException
864 private static function update1424()
866 // Was the script completed?
867 if (DI::keyValue()->get('post_update_version') >= 1424) {
871 $condition = ["`uri-id` IS NULL"];
872 Logger::info('Start', ['rest' => DBA::count('contact', $condition)]);
875 $contacts = DBA::select('contact', ['id', 'url'], $condition, ['limit' => 1000]);
877 if (DBA::errorNo() != 0) {
878 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
882 while ($contact = DBA::fetch($contacts)) {
883 DBA::update('contact', ['uri-id' => ItemURI::getIdByURI($contact['url'])], ['id' => $contact['id']]);
886 DBA::close($contacts);
888 Logger::info('Processed', ['rows' => $rows]);
891 DI::keyValue()->set('post_update_version', 1424);
892 Logger::info('Done');
900 * update the "uri-id" field in the fcontact table
902 * @return bool "true" when the job is done
903 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
904 * @throws \ImagickException
906 private static function update1425()
908 // Was the script completed?
909 if (DI::keyValue()->get('post_update_version') >= 1425) {
913 if (!DBStructure::existsTable('fcontact')) {
914 DI::keyValue()->set('post_update_version', 1425);
918 $condition = ["`uri-id` IS NULL"];
919 Logger::info('Start', ['rest' => DBA::count('fcontact', $condition)]);
922 $fcontacts = DBA::select('fcontact', ['id', 'url', 'guid'], $condition, ['limit' => 1000]);
924 if (DBA::errorNo() != 0) {
925 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
929 while ($fcontact = DBA::fetch($fcontacts)) {
930 if (!empty($fcontact['guid'])) {
931 $uriid = ItemURI::insert(['uri' => $fcontact['url'], 'guid' => $fcontact['guid']]);
933 $uriid = ItemURI::getIdByURI($fcontact['url']);
935 DBA::update('fcontact', ['uri-id' => $uriid], ['id' => $fcontact['id']]);
938 DBA::close($fcontacts);
940 Logger::info('Processed', ['rows' => $rows]);
943 DI::keyValue()->set('post_update_version', 1425);
944 Logger::info('Done');
952 * update the "uri-id" field in the apcontact table
954 * @return bool "true" when the job is done
955 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
956 * @throws \ImagickException
958 private static function update1426()
960 // Was the script completed?
961 if (DI::keyValue()->get('post_update_version') >= 1426) {
965 $condition = ["`uri-id` IS NULL"];
966 Logger::info('Start', ['rest' => DBA::count('apcontact', $condition)]);
969 $apcontacts = DBA::select('apcontact', ['url', 'uuid'], $condition, ['limit' => 1000]);
971 if (DBA::errorNo() != 0) {
972 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
976 while ($apcontact = DBA::fetch($apcontacts)) {
977 if (!empty($apcontact['uuid'])) {
978 $uriid = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
980 $uriid = ItemURI::getIdByURI($apcontact['url']);
982 DBA::update('apcontact', ['uri-id' => $uriid], ['url' => $apcontact['url']]);
985 DBA::close($apcontacts);
987 Logger::info('Processed', ['rows' => $rows]);
990 DI::keyValue()->set('post_update_version', 1426);
991 Logger::info('Done');
999 * update the "uri-id" field in the event table
1001 * @return bool "true" when the job is done
1002 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1003 * @throws \ImagickException
1005 private static function update1427()
1007 // Was the script completed?
1008 if (DI::keyValue()->get('post_update_version') >= 1427) {
1012 $condition = ["`uri-id` IS NULL"];
1013 Logger::info('Start', ['rest' => DBA::count('event', $condition)]);
1016 $events = DBA::select('event', ['id', 'uri', 'guid'], $condition, ['limit' => 1000]);
1018 if (DBA::errorNo() != 0) {
1019 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1023 while ($event = DBA::fetch($events)) {
1024 if (!empty($event['guid'])) {
1025 $uriid = ItemURI::insert(['uri' => $event['uri'], 'guid' => $event['guid']]);
1027 $uriid = ItemURI::getIdByURI($event['uri']);
1029 DBA::update('event', ['uri-id' => $uriid], ['id' => $event['id']]);
1032 DBA::close($events);
1034 Logger::info('Processed', ['rows' => $rows]);
1037 DI::keyValue()->set('post_update_version', 1427);
1038 Logger::info('Done');
1046 * Fill the receivers of the post via the raw source
1048 * @return bool "true" when the job is done
1049 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1050 * @throws \ImagickException
1052 private static function update1452()
1054 // Was the script completed?
1055 if (DI::keyValue()->get('post_update_version') >= 1452) {
1059 if (!DBStructure::existsTable('conversation')) {
1060 DI::keyValue()->set('post_update_version', 1452);
1064 $id = DI::keyValue()->get('post_update_version_1452_id') ?? 0;
1066 Logger::info('Start', ['uri-id' => $id]);
1071 $conversations = DBA::p("SELECT `post-view`.`uri-id`, `conversation`.`source`, `conversation`.`received` FROM `conversation`
1072 INNER JOIN `post-view` ON `post-view`.`uri` = `conversation`.`item-uri`
1073 WHERE NOT `source` IS NULL AND `conversation`.`protocol` = ? AND `uri-id` > ? LIMIT ?",
1074 Conversation::PARCEL_ACTIVITYPUB, $id, 1000);
1076 if (DBA::errorNo() != 0) {
1077 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1081 while ($conversation = DBA::fetch($conversations)) {
1082 $id = $conversation['uri-id'];
1083 $received = $conversation['received'];
1085 $raw = json_decode($conversation['source'], true);
1089 $activity = JsonLD::compact($raw);
1091 $urls = Receiver::getReceiverURL($activity);
1092 Processor::storeReceivers($conversation['uri-id'], $urls);
1094 if (!empty($activity['as:object'])) {
1095 $urls = array_merge($urls, Receiver::getReceiverURL($activity['as:object']));
1096 Processor::storeReceivers($conversation['uri-id'], $urls);
1101 DBA::close($conversations);
1103 DI::keyValue()->set('post_update_version_1452_id', $id);
1105 Logger::info('Processed', ['rows' => $rows, 'last' => $id, 'last-received' => $received]);
1108 DI::keyValue()->set('post_update_version', 1452);
1109 Logger::info('Done');
1117 * Correct the parent.
1118 * This fixes a bug that was introduced in the development of version 2022.09
1120 * @return bool "true" when the job is done
1121 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1122 * @throws \ImagickException
1124 private static function update1483()
1126 // Was the script completed?
1127 if (DI::keyValue()->get('post_update_version') >= 1483) {
1131 Logger::info('Start');
1133 $posts = DBA::select('post-view', ['uri-id'], ['conversation' => './']);
1134 while ($post = DBA::fetch($posts)) {
1135 $parent = Item::getParent($post['uri-id']);
1137 DBA::update('post', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1138 DBA::update('post-user', ['parent-uri-id' => $parent], ['uri-id' => $post['uri-id']]);
1143 DI::keyValue()->set('post_update_version', 1483);
1144 Logger::info('Done');
1149 * Handle duplicate contact entries
1151 * @return bool "true" when the job is done
1152 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1153 * @throws \ImagickException
1155 private static function update1484()
1157 // Was the script completed?
1158 if (DI::keyValue()->get('post_update_version') >= 1484) {
1162 $id = DI::keyValue()->get('post_update_version_1484_id') ?? 0;
1164 Logger::info('Start', ['id' => $id]);
1168 $contacts = DBA::select('contact', ['id', 'uid', 'uri-id', 'url'], ["`id` > ?", $id], ['order' => ['id'], 'limit' => 1000]);
1170 if (DBA::errorNo() != 0) {
1171 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1175 while ($contact = DBA::fetch($contacts)) {
1176 $id = $contact['id'];
1177 if (is_null($contact['uri-id'])) {
1178 $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
1179 DBA::update('contact', ['uri-id' => $contact['uri-id']], ['id' => $contact['id']]);
1181 Contact::setAccountUser($contact['id'], $contact['uid'], $contact['uri-id'], $contact['url']);
1184 DBA::close($contacts);
1186 DI::keyValue()->set('post_update_version_1484_id', $id);
1188 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1191 DI::keyValue()->set('post_update_version', 1484);
1192 Logger::info('Done');
1200 * update the "gsid" (global server id) field in the contact table
1202 * @return bool "true" when the job is done
1203 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1204 * @throws \ImagickException
1206 private static function update1506()
1208 // Was the script completed?
1209 if (DI::keyValue()->get('post_update_version') >= 1506) {
1213 $id = DI::keyValue()->get('post_update_version_1506_id') ?? 0;
1215 Logger::info('Start', ['contact' => $id]);
1219 $condition = ["`id` > ? AND `gsid` IS NULL AND `network` = ?", $id, Protocol::DIASPORA];
1220 $params = ['order' => ['id'], 'limit' => 10000];
1221 $contacts = DBA::select('contact', ['id', 'url'], $condition, $params);
1223 if (DBA::errorNo() != 0) {
1224 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1228 while ($contact = DBA::fetch($contacts)) {
1229 $id = $contact['id'];
1231 $parts = parse_url($contact['url']);
1232 unset($parts['path']);
1233 $server = (string)Uri::fromParts($parts);
1235 DBA::update('contact',
1236 ['gsid' => GServer::getID($server, true), 'baseurl' => GServer::cleanURL($server)],
1237 ['id' => $contact['id']]);
1241 DBA::close($contacts);
1243 DI::keyValue()->set('post_update_version_1506_id', $id);
1245 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1247 if ($start_id == $id) {
1248 DI::keyValue()->set('post_update_version', 1506);
1249 Logger::info('Done');
1257 * update the "gsid" (global server id) field in the inbox-status table
1259 * @return bool "true" when the job is done
1260 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1261 * @throws \ImagickException
1263 private static function update1507()
1265 // Was the script completed?
1266 if (DI::keyValue()->get('post_update_version') >= 1507) {
1270 $id = DI::keyValue()->get('post_update_version_1507_id') ?? '';
1272 Logger::info('Start', ['apcontact' => $id]);
1276 $condition = ["`url` > ? AND NOT `gsid` IS NULL", $id];
1277 $params = ['order' => ['url'], 'limit' => 10000];
1278 $apcontacts = DBA::select('apcontact', ['url', 'gsid', 'sharedinbox', 'inbox'], $condition, $params);
1280 if (DBA::errorNo() != 0) {
1281 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1285 while ($apcontact = DBA::fetch($apcontacts)) {
1286 $id = $apcontact['url'];
1288 $inbox = [$apcontact['inbox']];
1289 if (!empty($apcontact['sharedinbox'])) {
1290 $inbox[] = $apcontact['sharedinbox'];
1292 $condition = DBA::mergeConditions(['url' => $inbox], ["`gsid` IS NULL"]);
1293 DBA::update('inbox-status', ['gsid' => $apcontact['gsid'], 'archive' => GServer::isDefunctById($apcontact['gsid'])], $condition);
1296 DBA::close($apcontacts);
1298 DI::keyValue()->set('post_update_version_1507_id', $id);
1300 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1302 if ($start_id == $id) {
1303 DI::keyValue()->set('post_update_version', 1507);
1304 Logger::info('Done');
1312 * Create "post-counts" entries for old entries.
1314 * @return bool "true" when the job is done
1315 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1316 * @throws \ImagickException
1318 private static function update1543()
1320 // Was the script completed?
1321 if (DI::keyValue()->get('post_update_version') >= 1543) {
1325 $id = DI::keyValue()->get('post_update_version_1543_id') ?? 0;
1327 Logger::info('Start', ['uri-id' => $id]);
1331 $posts = Post::selectPosts(['uri-id', 'parent-uri-id'], ["`uri-id` > ? AND `gravity` IN (?, ?)", $id, Item::GRAVITY_COMMENT, Item::GRAVITY_PARENT], ['order' => ['uri-id'], 'limit' => 1000]);
1333 if (DBA::errorNo() != 0) {
1334 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1338 while ($post = Post::fetch($posts)) {
1339 $id = $post['uri-id'];
1340 Counts::updateForPost($post['uri-id'], $post['parent-uri-id']);
1345 DI::keyValue()->set('post_update_version_1543_id', $id);
1347 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1350 DI::keyValue()->set('post_update_version', 1543);
1351 Logger::info('Done');