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\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;
52 * Calls the post update functions
54 public static function update()
56 if (!self::update1297()) {
59 if (!self::update1322()) {
62 if (!self::update1329()) {
65 if (!self::update1341()) {
68 if (!self::update1342()) {
71 if (!self::update1345()) {
74 if (!self::update1346()) {
77 if (!self::update1347()) {
80 if (!self::update1348()) {
83 if (!self::update1349()) {
86 if (!self::update1383()) {
89 if (!self::update1384()) {
92 if (!self::update1400()) {
95 if (!self::update1424()) {
98 if (!self::update1425()) {
101 if (!self::update1426()) {
104 if (!self::update1427()) {
111 * Set the delivery queue count to a negative value for all items preceding the feature.
113 * @return bool "true" when the job is done
114 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
116 private static function update1297()
118 // Was the script completed?
119 if (DI::config()->get('system', 'post_update_version') >= 1297) {
123 if (!DBStructure::existsTable('item-delivery-data')) {
124 DI::config()->set('system', 'post_update_version', 1297);
128 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
129 $max_iid = $max_item_delivery_data['iid'];
131 Logger::info('Start update1297 with max iid: ' . $max_iid);
133 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
135 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
137 if (DBA::errorNo() != 0) {
138 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
142 Logger::info('Processed rows: ' . DBA::affectedRows());
144 DI::config()->set('system', 'post_update_version', 1297);
146 Logger::info('Done');
151 * Remove contact duplicates
153 * @return bool "true" when the job is done
154 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
156 private static function update1322()
158 // Was the script completed?
159 if (DI::config()->get('system', 'post_update_version') >= 1322) {
163 Logger::info('Start');
165 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
166 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
167 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
168 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
169 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
170 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
172 while ($contact = DBA::fetch($contacts)) {
173 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
174 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
177 DBA::close($contact);
178 DI::config()->set('system', 'post_update_version', 1322);
180 Logger::info('Done');
186 * update user notification data
188 * @return bool "true" when the job is done
189 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
191 private static function update1329()
193 // Was the script completed?
194 if (DI::config()->get('system', 'post_update_version') >= 1329) {
198 if (!DBStructure::existsTable('item')) {
199 DI::config()->set('system', 'post_update_version', 1329);
203 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
205 Logger::info('Start', ['item' => $id]);
209 $condition = ["`id` > ?", $id];
210 $params = ['order' => ['id'], 'limit' => 10000];
211 $items = DBA::select('item', ['id', 'uri-id', 'uid'], $condition, $params);
213 if (DBA::errorNo() != 0) {
214 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
218 while ($item = DBA::fetch($items)) {
221 Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
227 DI::config()->set('system', 'post_update_version_1329_id', $id);
229 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
231 if ($start_id == $id) {
232 DI::config()->set('system', 'post_update_version', 1329);
233 Logger::info('Done');
241 * Fill the "tag" table with tags and mentions from the body
243 * @return bool "true" when the job is done
244 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
246 private static function update1341()
248 // Was the script completed?
249 if (DI::config()->get('system', 'post_update_version') >= 1341) {
253 if (!DBStructure::existsTable('item-content')) {
254 DI::config()->set('system', 'post_update_version', 1342);
258 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
260 Logger::info('Start', ['item' => $id]);
264 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
265 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
266 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
268 if (DBA::errorNo() != 0) {
269 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
273 while ($item = DBA::fetch($items)) {
274 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
275 $id = $item['uri-id'];
277 if ($rows % 1000 == 0) {
278 DI::config()->set('system', 'post_update_version_1341_id', $id);
283 DI::config()->set('system', 'post_update_version_1341_id', $id);
285 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
287 // When there are less than 1,000 items processed this means that we reached the end
288 // The other entries will then be processed with the regular functionality
290 DI::config()->set('system', 'post_update_version', 1341);
291 Logger::info('Done');
299 * Fill the "tag" table with tags and mentions from the "term" table
301 * @return bool "true" when the job is done
302 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
304 private static function update1342()
306 // Was the script completed?
307 if (DI::config()->get('system', 'post_update_version') >= 1342) {
311 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
312 DI::config()->set('system', 'post_update_version', 1342);
316 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
318 Logger::info('Start', ['item' => $id]);
322 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
324 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
325 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
326 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
327 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
329 if (DBA::errorNo() != 0) {
330 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
334 while ($term = DBA::fetch($terms)) {
335 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
336 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
337 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
338 if (!DBA::isResult($contact)) {
339 $ssl_url = str_replace('http://', 'https://', $term['url']);
340 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
341 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
344 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
345 $term['type'] = Tag::IMPLICIT_MENTION;
349 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
353 if ($rows % 1000 == 0) {
354 DI::config()->set('system', 'post_update_version_1342_id', $id);
359 DI::config()->set('system', 'post_update_version_1342_id', $id);
361 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
363 // When there are less than 1,000 items processed this means that we reached the end
364 // The other entries will then be processed with the regular functionality
366 DI::config()->set('system', 'post_update_version', 1342);
367 Logger::info('Done');
375 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
377 * @return bool "true" when the job is done
378 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
380 private static function update1345()
382 // Was the script completed?
383 if (DI::config()->get('system', 'post_update_version') >= 1345) {
387 if (!DBStructure::existsTable('item-delivery-data')) {
388 DI::config()->set('system', 'post_update_version', 1345);
392 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
394 Logger::info('Start', ['item' => $id]);
398 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
399 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
400 FROM `item-delivery-data`
401 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
402 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
404 if (DBA::errorNo() != 0) {
405 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
409 while ($delivery = DBA::fetch($deliveries)) {
410 $id = $delivery['iid'];
411 unset($delivery['iid']);
412 DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
415 DBA::close($deliveries);
417 DI::config()->set('system', 'post_update_version_1345_id', $id);
419 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
421 // When there are less than 100 items processed this means that we reached the end
422 // The other entries will then be processed with the regular functionality
424 DI::config()->set('system', 'post_update_version', 1345);
425 Logger::info('Done');
433 * Generates the legacy item.file field string from an item ID.
434 * Includes only file and category terms.
436 * @param int $item_id
440 private static function fileTextFromItemId($item_id)
444 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
445 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
446 foreach ($tags as $tag) {
447 if ($tag['type'] == Category::CATEGORY) {
448 $file_text .= '<' . $tag['term'] . '>';
450 $file_text .= '[' . $tag['term'] . ']';
458 * Fill the "tag" table with tags and mentions from the "term" table
460 * @return bool "true" when the job is done
461 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
463 private static function update1346()
465 // Was the script completed?
466 if (DI::config()->get('system', 'post_update_version') >= 1346) {
470 if (!DBStructure::existsTable('term')) {
471 DI::config()->set('system', 'post_update_version', 1346);
475 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
477 Logger::info('Start', ['item' => $id]);
481 $terms = DBA::select('term', ['oid'],
482 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
483 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
485 if (DBA::errorNo() != 0) {
486 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
490 while ($term = DBA::fetch($terms)) {
491 $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
492 if (!DBA::isResult($item)) {
496 $file = self::fileTextFromItemId($term['oid']);
498 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
503 if ($rows % 100 == 0) {
504 DI::config()->set('system', 'post_update_version_1346_id', $id);
509 DI::config()->set('system', 'post_update_version_1346_id', $id);
511 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
513 // When there are less than 10 items processed this means that we reached the end
514 // The other entries will then be processed with the regular functionality
516 DI::config()->set('system', 'post_update_version', 1346);
517 Logger::info('Done');
525 * update the "vid" (verb) field in the item table
527 * @return bool "true" when the job is done
528 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
529 * @throws \ImagickException
531 private static function update1347()
533 // Was the script completed?
534 if (DI::config()->get("system", "post_update_version") >= 1347) {
538 if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
539 DI::config()->set('system', 'post_update_version', 1347);
543 $id = DI::config()->get("system", "post_update_version_1347_id", 0);
545 Logger::info('Start', ['item' => $id]);
550 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
551 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
552 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
553 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
555 if (DBA::errorNo() != 0) {
556 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
560 while ($item = DBA::fetch($items)) {
562 $verb = $item['item-verb'];
564 $verb = $item['verb'];
566 if (empty($verb) && is_int($item['activity'])) {
567 $verb = Item::ACTIVITIES[$item['activity']];
573 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
578 DI::config()->set("system", "post_update_version_1347_id", $id);
580 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
582 if ($start_id == $id) {
583 DI::config()->set("system", "post_update_version", 1347);
584 Logger::info('Done');
592 * update the "gsid" (global server id) field in the contact table
594 * @return bool "true" when the job is done
595 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
596 * @throws \ImagickException
598 private static function update1348()
600 // Was the script completed?
601 if (DI::config()->get("system", "post_update_version") >= 1348) {
605 $id = DI::config()->get("system", "post_update_version_1348_id", 0);
607 Logger::info('Start', ['contact' => $id]);
611 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
612 $params = ['order' => ['id'], 'limit' => 10000];
613 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
615 if (DBA::errorNo() != 0) {
616 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
620 while ($contact = DBA::fetch($contacts)) {
621 $id = $contact['id'];
623 DBA::update('contact',
624 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
625 ['id' => $contact['id']]);
629 DBA::close($contacts);
631 DI::config()->set("system", "post_update_version_1348_id", $id);
633 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
635 if ($start_id == $id) {
636 DI::config()->set("system", "post_update_version", 1348);
637 Logger::info('Done');
645 * update the "gsid" (global server id) field in the apcontact table
647 * @return bool "true" when the job is done
648 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
649 * @throws \ImagickException
651 private static function update1349()
653 // Was the script completed?
654 if (DI::config()->get("system", "post_update_version") >= 1349) {
658 $id = DI::config()->get("system", "post_update_version_1349_id", '');
660 Logger::info('Start', ['apcontact' => $id]);
664 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
665 $params = ['order' => ['url'], 'limit' => 10000];
666 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
668 if (DBA::errorNo() != 0) {
669 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
673 while ($apcontact = DBA::fetch($apcontacts)) {
674 $id = $apcontact['url'];
676 DBA::update('apcontact',
677 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
678 ['url' => $apcontact['url']]);
682 DBA::close($apcontacts);
684 DI::config()->set("system", "post_update_version_1349_id", $id);
686 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
688 if ($start_id == $id) {
689 DI::config()->set("system", "post_update_version", 1349);
690 Logger::info('Done');
698 * Remove orphaned photo entries
700 * @return bool "true" when the job is done
701 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
702 * @throws \ImagickException
704 private static function update1383()
706 // Was the script completed?
707 if (DI::config()->get("system", "post_update_version") >= 1383) {
711 Logger::info('Start');
714 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
716 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
717 while ($photo = DBA::fetch($photos)) {
718 $delete = !in_array($photo['scale'], [4, 5, 6]);
721 // Check if there is a contact entry with that photo
722 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
723 $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
727 Photo::delete(['id' => $photo['id']]);
733 DI::config()->set("system", "post_update_version", 1383);
734 Logger::info('Done', ['deleted' => $deleted]);
739 * update the "hash" field in the photo table
741 * @return bool "true" when the job is done
742 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
743 * @throws \ImagickException
745 private static function update1384()
747 // Was the script completed?
748 if (DI::config()->get("system", "post_update_version") >= 1384) {
752 $condition = ["`hash` IS NULL"];
753 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
756 $photos = DBA::select('photo', [], $condition, ['limit' => 100]);
758 if (DBA::errorNo() != 0) {
759 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
763 while ($photo = DBA::fetch($photos)) {
764 $img = Photo::getImageForPhoto($photo);
766 $md5 = md5($img->asString());
770 DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
775 Logger::info('Processed', ['rows' => $rows]);
778 DI::config()->set("system", "post_update_version", 1384);
779 Logger::info('Done');
787 * update the "external-id" field in the post table
789 * @return bool "true" when the job is done
790 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
791 * @throws \ImagickException
793 private static function update1400()
795 // Was the script completed?
796 if (DI::config()->get("system", "post_update_version") >= 1400) {
800 if (!DBStructure::existsTable('item')) {
801 DI::config()->set("system", "post_update_version", 1400);
805 $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
806 Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
809 $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
811 if (DBA::errorNo() != 0) {
812 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
816 while ($item = DBA::fetch($items)) {
817 Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
822 Logger::info('Processed', ['rows' => $rows]);
825 DI::config()->set("system", "post_update_version", 1400);
826 Logger::info('Done');
834 * update the "uri-id" field in the contact table
836 * @return bool "true" when the job is done
837 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
838 * @throws \ImagickException
840 private static function update1424()
842 // Was the script completed?
843 if (DI::config()->get("system", "post_update_version") >= 1424) {
847 $condition = ["`uri-id` IS NULL"];
848 Logger::info('Start', ['rest' => DBA::count('contact', $condition)]);
851 $contacts = DBA::select('contact', ['id', 'url'], $condition, ['limit' => 1000]);
853 if (DBA::errorNo() != 0) {
854 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
858 while ($contact = DBA::fetch($contacts)) {
859 DBA::update('contact', ['uri-id' => ItemURI::getIdByURI($contact['url'])], ['id' => $contact['id']]);
862 DBA::close($contacts);
864 Logger::info('Processed', ['rows' => $rows]);
867 DI::config()->set("system", "post_update_version", 1424);
868 Logger::info('Done');
876 * update the "uri-id" field in the fcontact table
878 * @return bool "true" when the job is done
879 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
880 * @throws \ImagickException
882 private static function update1425()
884 // Was the script completed?
885 if (DI::config()->get("system", "post_update_version") >= 1425) {
889 $condition = ["`uri-id` IS NULL"];
890 Logger::info('Start', ['rest' => DBA::count('fcontact', $condition)]);
893 $fcontacts = DBA::select('fcontact', ['id', 'url', 'guid'], $condition, ['limit' => 1000]);
895 if (DBA::errorNo() != 0) {
896 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
900 while ($fcontact = DBA::fetch($fcontacts)) {
901 if (!empty($fcontact['guid'])) {
902 $uriid = ItemURI::insert(['uri' => $fcontact['url'], 'guid' => $fcontact['guid']]);
904 $uriid = ItemURI::getIdByURI($fcontact['url']);
906 DBA::update('fcontact', ['uri-id' => $uriid], ['id' => $fcontact['id']]);
909 DBA::close($fcontacts);
911 Logger::info('Processed', ['rows' => $rows]);
914 DI::config()->set("system", "post_update_version", 1425);
915 Logger::info('Done');
923 * update the "uri-id" field in the apcontact table
925 * @return bool "true" when the job is done
926 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
927 * @throws \ImagickException
929 private static function update1426()
931 // Was the script completed?
932 if (DI::config()->get("system", "post_update_version") >= 1426) {
936 $condition = ["`uri-id` IS NULL"];
937 Logger::info('Start', ['rest' => DBA::count('apcontact', $condition)]);
940 $apcontacts = DBA::select('apcontact', ['url', 'uuid'], $condition, ['limit' => 1000]);
942 if (DBA::errorNo() != 0) {
943 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
947 while ($apcontact = DBA::fetch($apcontacts)) {
948 if (!empty($apcontact['uuid'])) {
949 $uriid = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
951 $uriid = ItemURI::getIdByURI($apcontact['url']);
953 DBA::update('apcontact', ['uri-id' => $uriid], ['url' => $apcontact['url']]);
956 DBA::close($apcontacts);
958 Logger::info('Processed', ['rows' => $rows]);
961 DI::config()->set("system", "post_update_version", 1426);
962 Logger::info('Done');
970 * update the "uri-id" field in the event table
972 * @return bool "true" when the job is done
973 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
974 * @throws \ImagickException
976 private static function update1427()
978 // Was the script completed?
979 if (DI::config()->get("system", "post_update_version") >= 1427) {
983 $condition = ["`uri-id` IS NULL"];
984 Logger::info('Start', ['rest' => DBA::count('event', $condition)]);
987 $events = DBA::select('event', ['id', 'uri', 'guid'], $condition, ['limit' => 1000]);
989 if (DBA::errorNo() != 0) {
990 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
994 while ($event = DBA::fetch($events)) {
995 if (!empty($event['guid'])) {
996 $uriid = ItemURI::insert(['uri' => $event['uri'], 'guid' => $event['guid']]);
998 $uriid = ItemURI::getIdByURI($event['uri']);
1000 DBA::update('event', ['uri-id' => $uriid], ['id' => $event['id']]);
1003 DBA::close($events);
1005 Logger::info('Processed', ['rows' => $rows]);
1008 DI::config()->set("system", "post_update_version", 1427);
1009 Logger::info('Done');