3 * @copyright Copyright (C) 2010-2021, 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\GServer;
29 use Friendica\Model\Item;
30 use Friendica\Model\ItemURI;
31 use Friendica\Model\Photo;
32 use Friendica\Model\Post;
33 use Friendica\Model\Post\Category;
34 use Friendica\Model\Tag;
35 use Friendica\Model\Verb;
36 use Friendica\Util\Strings;
39 * These database-intensive post update routines are meant to be executed in the background by the cronjob.
41 * If there is a need for a intensive migration after a database structure change, update this file
42 * by adding a new method at the end with the number of the new DB_UPDATE_VERSION.
46 // Needed for the helper function to read from the legacy term table
47 const OBJECT_TYPE_POST = 1;
51 * Calls the post update functions
53 public static function update()
55 if (!self::update1297()) {
58 if (!self::update1322()) {
61 if (!self::update1329()) {
64 if (!self::update1341()) {
67 if (!self::update1342()) {
70 if (!self::update1345()) {
73 if (!self::update1346()) {
76 if (!self::update1347()) {
79 if (!self::update1348()) {
82 if (!self::update1349()) {
85 if (!self::update1383()) {
88 if (!self::update1384()) {
91 if (!self::update1400()) {
94 if (!self::update1424()) {
97 if (!self::update1425()) {
100 if (!self::update1426()) {
107 * Set the delivery queue count to a negative value for all items preceding the feature.
109 * @return bool "true" when the job is done
110 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
112 private static function update1297()
114 // Was the script completed?
115 if (DI::config()->get('system', 'post_update_version') >= 1297) {
119 if (!DBStructure::existsTable('item-delivery-data')) {
120 DI::config()->set('system', 'post_update_version', 1297);
124 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
125 $max_iid = $max_item_delivery_data['iid'];
127 Logger::info('Start update1297 with max iid: ' . $max_iid);
129 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
131 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
133 if (DBA::errorNo() != 0) {
134 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
138 Logger::info('Processed rows: ' . DBA::affectedRows());
140 DI::config()->set('system', 'post_update_version', 1297);
142 Logger::info('Done');
147 * Remove contact duplicates
149 * @return bool "true" when the job is done
150 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
152 private static function update1322()
154 // Was the script completed?
155 if (DI::config()->get('system', 'post_update_version') >= 1322) {
159 Logger::info('Start');
161 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
162 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
163 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
164 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
165 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
166 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
168 while ($contact = DBA::fetch($contacts)) {
169 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
170 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
173 DBA::close($contact);
174 DI::config()->set('system', 'post_update_version', 1322);
176 Logger::info('Done');
182 * update user notification data
184 * @return bool "true" when the job is done
185 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
187 private static function update1329()
189 // Was the script completed?
190 if (DI::config()->get('system', 'post_update_version') >= 1329) {
194 if (!DBStructure::existsTable('item')) {
195 DI::config()->set('system', 'post_update_version', 1329);
199 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
201 Logger::info('Start', ['item' => $id]);
205 $condition = ["`id` > ?", $id];
206 $params = ['order' => ['id'], 'limit' => 10000];
207 $items = DBA::select('item', ['id', 'uri-id', 'uid'], $condition, $params);
209 if (DBA::errorNo() != 0) {
210 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
214 while ($item = DBA::fetch($items)) {
217 Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
223 DI::config()->set('system', 'post_update_version_1329_id', $id);
225 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
227 if ($start_id == $id) {
228 DI::config()->set('system', 'post_update_version', 1329);
229 Logger::info('Done');
237 * Fill the "tag" table with tags and mentions from the body
239 * @return bool "true" when the job is done
240 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
242 private static function update1341()
244 // Was the script completed?
245 if (DI::config()->get('system', 'post_update_version') >= 1341) {
249 if (!DBStructure::existsTable('item-content')) {
250 DI::config()->set('system', 'post_update_version', 1342);
254 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
256 Logger::info('Start', ['item' => $id]);
260 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
261 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
262 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
264 if (DBA::errorNo() != 0) {
265 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
269 while ($item = DBA::fetch($items)) {
270 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
271 $id = $item['uri-id'];
273 if ($rows % 1000 == 0) {
274 DI::config()->set('system', 'post_update_version_1341_id', $id);
279 DI::config()->set('system', 'post_update_version_1341_id', $id);
281 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
283 // When there are less than 1,000 items processed this means that we reached the end
284 // The other entries will then be processed with the regular functionality
286 DI::config()->set('system', 'post_update_version', 1341);
287 Logger::info('Done');
295 * Fill the "tag" table with tags and mentions from the "term" table
297 * @return bool "true" when the job is done
298 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
300 private static function update1342()
302 // Was the script completed?
303 if (DI::config()->get('system', 'post_update_version') >= 1342) {
307 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
308 DI::config()->set('system', 'post_update_version', 1342);
312 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
314 Logger::info('Start', ['item' => $id]);
318 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
320 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
321 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
322 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
323 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
325 if (DBA::errorNo() != 0) {
326 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
330 while ($term = DBA::fetch($terms)) {
331 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
332 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
333 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
334 if (!DBA::isResult($contact)) {
335 $ssl_url = str_replace('http://', 'https://', $term['url']);
336 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
337 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
340 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
341 $term['type'] = Tag::IMPLICIT_MENTION;
345 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
349 if ($rows % 1000 == 0) {
350 DI::config()->set('system', 'post_update_version_1342_id', $id);
355 DI::config()->set('system', 'post_update_version_1342_id', $id);
357 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
359 // When there are less than 1,000 items processed this means that we reached the end
360 // The other entries will then be processed with the regular functionality
362 DI::config()->set('system', 'post_update_version', 1342);
363 Logger::info('Done');
371 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
373 * @return bool "true" when the job is done
374 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
376 private static function update1345()
378 // Was the script completed?
379 if (DI::config()->get('system', 'post_update_version') >= 1345) {
383 if (!DBStructure::existsTable('item-delivery-data')) {
384 DI::config()->set('system', 'post_update_version', 1345);
388 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
390 Logger::info('Start', ['item' => $id]);
394 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
395 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
396 FROM `item-delivery-data`
397 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
398 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
400 if (DBA::errorNo() != 0) {
401 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
405 while ($delivery = DBA::fetch($deliveries)) {
406 $id = $delivery['iid'];
407 unset($delivery['iid']);
408 DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
411 DBA::close($deliveries);
413 DI::config()->set('system', 'post_update_version_1345_id', $id);
415 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
417 // When there are less than 100 items processed this means that we reached the end
418 // The other entries will then be processed with the regular functionality
420 DI::config()->set('system', 'post_update_version', 1345);
421 Logger::info('Done');
429 * Generates the legacy item.file field string from an item ID.
430 * Includes only file and category terms.
432 * @param int $item_id
436 private static function fileTextFromItemId($item_id)
440 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
441 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
442 foreach ($tags as $tag) {
443 if ($tag['type'] == Category::CATEGORY) {
444 $file_text .= '<' . $tag['term'] . '>';
446 $file_text .= '[' . $tag['term'] . ']';
454 * Fill the "tag" table with tags and mentions from the "term" table
456 * @return bool "true" when the job is done
457 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
459 private static function update1346()
461 // Was the script completed?
462 if (DI::config()->get('system', 'post_update_version') >= 1346) {
466 if (!DBStructure::existsTable('term')) {
467 DI::config()->set('system', 'post_update_version', 1346);
471 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
473 Logger::info('Start', ['item' => $id]);
477 $terms = DBA::select('term', ['oid'],
478 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
479 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
481 if (DBA::errorNo() != 0) {
482 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
486 while ($term = DBA::fetch($terms)) {
487 $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
488 if (!DBA::isResult($item)) {
492 $file = self::fileTextFromItemId($term['oid']);
494 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
499 if ($rows % 100 == 0) {
500 DI::config()->set('system', 'post_update_version_1346_id', $id);
505 DI::config()->set('system', 'post_update_version_1346_id', $id);
507 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
509 // When there are less than 10 items processed this means that we reached the end
510 // The other entries will then be processed with the regular functionality
512 DI::config()->set('system', 'post_update_version', 1346);
513 Logger::info('Done');
521 * update the "vid" (verb) field in the item table
523 * @return bool "true" when the job is done
524 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
525 * @throws \ImagickException
527 private static function update1347()
529 // Was the script completed?
530 if (DI::config()->get("system", "post_update_version") >= 1347) {
534 if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
535 DI::config()->set('system', 'post_update_version', 1347);
539 $id = DI::config()->get("system", "post_update_version_1347_id", 0);
541 Logger::info('Start', ['item' => $id]);
546 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
547 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
548 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
549 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
551 if (DBA::errorNo() != 0) {
552 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
556 while ($item = DBA::fetch($items)) {
558 $verb = $item['item-verb'];
560 $verb = $item['verb'];
562 if (empty($verb) && is_int($item['activity'])) {
563 $verb = Item::ACTIVITIES[$item['activity']];
569 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
574 DI::config()->set("system", "post_update_version_1347_id", $id);
576 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
578 if ($start_id == $id) {
579 DI::config()->set("system", "post_update_version", 1347);
580 Logger::info('Done');
588 * update the "gsid" (global server id) field in the contact table
590 * @return bool "true" when the job is done
591 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
592 * @throws \ImagickException
594 private static function update1348()
596 // Was the script completed?
597 if (DI::config()->get("system", "post_update_version") >= 1348) {
601 $id = DI::config()->get("system", "post_update_version_1348_id", 0);
603 Logger::info('Start', ['contact' => $id]);
607 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
608 $params = ['order' => ['id'], 'limit' => 10000];
609 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
611 if (DBA::errorNo() != 0) {
612 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
616 while ($contact = DBA::fetch($contacts)) {
617 $id = $contact['id'];
619 DBA::update('contact',
620 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
621 ['id' => $contact['id']]);
625 DBA::close($contacts);
627 DI::config()->set("system", "post_update_version_1348_id", $id);
629 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
631 if ($start_id == $id) {
632 DI::config()->set("system", "post_update_version", 1348);
633 Logger::info('Done');
641 * update the "gsid" (global server id) field in the apcontact table
643 * @return bool "true" when the job is done
644 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
645 * @throws \ImagickException
647 private static function update1349()
649 // Was the script completed?
650 if (DI::config()->get("system", "post_update_version") >= 1349) {
654 $id = DI::config()->get("system", "post_update_version_1349_id", '');
656 Logger::info('Start', ['apcontact' => $id]);
660 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
661 $params = ['order' => ['url'], 'limit' => 10000];
662 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
664 if (DBA::errorNo() != 0) {
665 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
669 while ($apcontact = DBA::fetch($apcontacts)) {
670 $id = $apcontact['url'];
672 DBA::update('apcontact',
673 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
674 ['url' => $apcontact['url']]);
678 DBA::close($apcontacts);
680 DI::config()->set("system", "post_update_version_1349_id", $id);
682 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
684 if ($start_id == $id) {
685 DI::config()->set("system", "post_update_version", 1349);
686 Logger::info('Done');
694 * Remove orphaned photo entries
696 * @return bool "true" when the job is done
697 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
698 * @throws \ImagickException
700 private static function update1383()
702 // Was the script completed?
703 if (DI::config()->get("system", "post_update_version") >= 1383) {
707 Logger::info('Start');
710 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
712 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
713 while ($photo = DBA::fetch($photos)) {
714 $delete = !in_array($photo['scale'], [4, 5, 6]);
717 // Check if there is a contact entry with that photo
718 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
719 $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
723 Photo::delete(['id' => $photo['id']]);
729 DI::config()->set("system", "post_update_version", 1383);
730 Logger::info('Done', ['deleted' => $deleted]);
735 * update the "hash" field in the photo table
737 * @return bool "true" when the job is done
738 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
739 * @throws \ImagickException
741 private static function update1384()
743 // Was the script completed?
744 if (DI::config()->get("system", "post_update_version") >= 1384) {
748 $condition = ["`hash` IS NULL"];
749 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
752 $photos = DBA::select('photo', [], $condition, ['limit' => 100]);
754 if (DBA::errorNo() != 0) {
755 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
759 while ($photo = DBA::fetch($photos)) {
760 $img = Photo::getImageForPhoto($photo);
762 $md5 = md5($img->asString());
766 DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
771 Logger::info('Processed', ['rows' => $rows]);
774 DI::config()->set("system", "post_update_version", 1384);
775 Logger::info('Done');
783 * update the "external-id" field in the post table
785 * @return bool "true" when the job is done
786 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
787 * @throws \ImagickException
789 private static function update1400()
791 // Was the script completed?
792 if (DI::config()->get("system", "post_update_version") >= 1400) {
796 if (!DBStructure::existsTable('item')) {
797 DI::config()->set("system", "post_update_version", 1400);
801 $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
802 Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
805 $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
807 if (DBA::errorNo() != 0) {
808 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
812 while ($item = DBA::fetch($items)) {
813 Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
818 Logger::info('Processed', ['rows' => $rows]);
821 DI::config()->set("system", "post_update_version", 1400);
822 Logger::info('Done');
830 * update the "uri-id" field in the contact table
832 * @return bool "true" when the job is done
833 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
834 * @throws \ImagickException
836 private static function update1424()
838 // Was the script completed?
839 if (DI::config()->get("system", "post_update_version") >= 1424) {
843 $condition = ["`uri-id` IS NULL"];
844 Logger::info('Start', ['rest' => DBA::count('contact', $condition)]);
847 $contacts = DBA::select('contact', ['id', 'url'], $condition, ['limit' => 1000]);
849 if (DBA::errorNo() != 0) {
850 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
854 while ($contact = DBA::fetch($contacts)) {
855 DBA::update('contact', ['uri-id' => ItemURI::getIdByURI($contact['url'])], ['id' => $contact['id']]);
858 DBA::close($contacts);
860 Logger::info('Processed', ['rows' => $rows]);
863 DI::config()->set("system", "post_update_version", 1424);
864 Logger::info('Done');
872 * update the "uri-id" field in the fcontact table
874 * @return bool "true" when the job is done
875 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
876 * @throws \ImagickException
878 private static function update1425()
880 // Was the script completed?
881 if (DI::config()->get("system", "post_update_version") >= 1425) {
885 $condition = ["`uri-id` IS NULL"];
886 Logger::info('Start', ['rest' => DBA::count('fcontact', $condition)]);
889 $fcontacts = DBA::select('fcontact', ['id', 'url', 'guid'], $condition, ['limit' => 1000]);
891 if (DBA::errorNo() != 0) {
892 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
896 while ($fcontact = DBA::fetch($fcontacts)) {
897 if (!empty($fcontact['guid'])) {
898 $uriid = ItemURI::insert(['uri' => $fcontact['url'], 'guid' => $fcontact['guid']]);
900 $uriid = ItemURI::getIdByURI($fcontact['url']);
902 DBA::update('fcontact', ['uri-id' => $uriid], ['id' => $fcontact['id']]);
905 DBA::close($fcontacts);
907 Logger::info('Processed', ['rows' => $rows]);
910 DI::config()->set("system", "post_update_version", 1425);
911 Logger::info('Done');
919 * update the "uri-id" field in the apcontact table
921 * @return bool "true" when the job is done
922 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
923 * @throws \ImagickException
925 private static function update1426()
927 // Was the script completed?
928 if (DI::config()->get("system", "post_update_version") >= 1426) {
932 $condition = ["`uri-id` IS NULL"];
933 Logger::info('Start', ['rest' => DBA::count('apcontact', $condition)]);
936 $apcontacts = DBA::select('apcontact', ['url', 'uuid'], $condition, ['limit' => 1000]);
938 if (DBA::errorNo() != 0) {
939 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
943 while ($apcontact = DBA::fetch($apcontacts)) {
944 if (!empty($apcontact['uuid'])) {
945 $uriid = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
947 $uriid = ItemURI::getIdByURI($apcontact['url']);
949 DBA::update('apcontact', ['uri-id' => $uriid], ['url' => $apcontact['url']]);
952 DBA::close($apcontacts);
954 Logger::info('Processed', ['rows' => $rows]);
957 DI::config()->set("system", "post_update_version", 1426);
958 Logger::info('Done');