3 * @copyright Copyright (C) 2020, Friendica
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\PermissionSet;
32 use Friendica\Model\Post\Category;
33 use Friendica\Model\Tag;
34 use Friendica\Model\UserItem;
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;
50 * Calls the post update functions
52 public static function update()
54 if (!self::update1194()) {
57 if (!self::update1206()) {
60 if (!self::update1279()) {
63 if (!self::update1281()) {
66 if (!self::update1297()) {
69 if (!self::update1322()) {
72 if (!self::update1329()) {
75 if (!self::update1341()) {
78 if (!self::update1342()) {
81 if (!self::update1345()) {
84 if (!self::update1346()) {
87 if (!self::update1347()) {
90 if (!self::update1348()) {
93 if (!self::update1349()) {
101 * Updates the "global" field in the item table
103 * @return bool "true" when the job is done
104 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
106 private static function update1194()
108 // Was the script completed?
109 if (DI::config()->get("system", "post_update_version") >= 1194) {
113 Logger::log("Start", Logger::DEBUG);
115 $end_id = DI::config()->get("system", "post_update_1194_end");
117 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
119 DI::config()->set("system", "post_update_1194_end", $r[0]["id"]);
120 $end_id = DI::config()->get("system", "post_update_1194_end");
124 Logger::log("End ID: ".$end_id, Logger::DEBUG);
126 $start_id = DI::config()->get("system", "post_update_1194_start");
128 $query1 = "SELECT `item`.`id` FROM `item` ";
130 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
132 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
133 AND `item`.`visible` AND NOT `item`.`private`
134 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
135 AND `item`.`network` IN ('%s', '%s', '%s', '')
136 AND NOT `item`.`global`";
138 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
139 intval($start_id), intval($end_id),
140 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
142 DI::config()->set("system", "post_update_version", 1194);
143 Logger::log("Update is done", Logger::DEBUG);
146 DI::config()->set("system", "post_update_1194_start", $r[0]["id"]);
147 $start_id = DI::config()->get("system", "post_update_1194_start");
150 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
152 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
153 intval($start_id), intval($end_id),
154 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
156 $pos_id = $r[0]["id"];
160 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
162 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
163 intval($start_id), intval($pos_id),
164 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
166 Logger::log("Done", Logger::DEBUG);
170 * update the "last-item" field in the "self" contact
172 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
174 * @return bool "true" when the job is done
175 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
177 private static function update1206()
179 // Was the script completed?
180 if (DI::config()->get("system", "post_update_version") >= 1206) {
184 Logger::log("Start", Logger::DEBUG);
185 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
186 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
188 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
190 if (!DBA::isResult($r)) {
193 foreach ($r as $user) {
194 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
195 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
199 DI::config()->set("system", "post_update_version", 1206);
200 Logger::log("Done", Logger::DEBUG);
205 * update the item related tables
207 * @return bool "true" when the job is done
208 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
209 * @throws \ImagickException
211 private static function update1279()
213 // Was the script completed?
214 if (DI::config()->get("system", "post_update_version") >= 1279) {
218 $id = DI::config()->get("system", "post_update_version_1279_id", 0);
220 Logger::log("Start from item " . $id, Logger::DEBUG);
222 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
223 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
224 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
225 'inform', 'postopts', 'icid']);
229 $condition = ["`id` > ?", $id];
230 $params = ['order' => ['id'], 'limit' => 10000];
231 $items = Item::select($fields, $condition, $params);
233 if (DBA::errorNo() != 0) {
234 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
238 while ($item = Item::fetch($items)) {
241 if (empty($item['author-id'])) {
242 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
243 'photo' => $item['author-avatar'], 'network' => $item['network']];
245 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, null, $default);
248 if (empty($item['owner-id'])) {
249 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
250 'photo' => $item['owner-avatar'], 'network' => $item['network']];
252 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, null, $default);
255 if (empty($item['psid'])) {
256 $item['psid'] = PermissionSet::getIdFromACL(
265 $item['allow_cid'] = null;
266 $item['allow_gid'] = null;
267 $item['deny_cid'] = null;
268 $item['deny_gid'] = null;
270 if ($item['post-type'] == 0) {
271 if (!empty($item['type']) && ($item['type'] == 'note')) {
272 $item['post-type'] = Item::PT_PERSONAL_NOTE;
273 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
274 $item['post-type'] = Item::PT_IMAGE;
275 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
276 $item['post-type'] = Item::PT_PAGE;
280 self::createLanguage($item);
282 if (!empty($item['icid']) && !empty($item['language'])) {
283 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
285 unset($item['language']);
287 Item::update($item, ['id' => $id]);
293 DI::config()->set("system", "post_update_version_1279_id", $id);
295 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
297 if ($start_id == $id) {
298 // Set all deprecated fields to "null" if they contain an empty string
299 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
300 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
301 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
302 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
303 'rendered-hash', 'rendered-html'];
304 foreach ($nullfields as $field) {
305 $fields = [$field => null];
306 $condition = [$field => ''];
307 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
308 // Important: This has to be a "DBA::update", not a "Item::update"
309 DBA::update('item', $fields, $condition);
312 DI::config()->set("system", "post_update_version", 1279);
313 Logger::log("Done", Logger::DEBUG);
320 private static function createLanguage(&$item)
322 if (empty($item['postopts'])) {
326 $opts = explode(',', $item['postopts']);
330 foreach ($opts as $opt) {
331 if (strstr($opt, 'lang=')) {
332 $language = substr($opt, 5);
338 if (empty($language)) {
342 if (!empty($postopts)) {
343 $item['postopts'] = implode(',', $postopts);
345 $item['postopts'] = null;
348 $lang_pairs = explode(':', $language);
352 foreach ($lang_pairs as $pair) {
353 $lang_pair_arr = explode(';', $pair);
354 if (count($lang_pair_arr) == 2) {
355 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
359 $item['language'] = json_encode($lang_arr);
363 * update item-uri data. Prerequisite for the next item structure update.
365 * @return bool "true" when the job is done
366 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
368 private static function update1281()
370 // Was the script completed?
371 if (DI::config()->get("system", "post_update_version") >= 1281) {
375 $id = DI::config()->get("system", "post_update_version_1281_id", 0);
377 Logger::log("Start from item " . $id, Logger::DEBUG);
379 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
383 $condition = ["`id` > ?", $id];
384 $params = ['order' => ['id'], 'limit' => 10000];
385 $items = DBA::select('item', $fields, $condition, $params);
387 if (DBA::errorNo() != 0) {
388 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
392 while ($item = DBA::fetch($items)) {
395 if (empty($item['uri'])) {
398 } elseif (empty($item['uri-id'])) {
399 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
402 if (empty($item['parent-uri'])) {
403 $item['parent-uri-id'] = $item['uri-id'];
404 } elseif (empty($item['parent-uri-id'])) {
405 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
408 // Very old items don't have this field
409 if (empty($item['thr-parent'])) {
410 $item['thr-parent-id'] = $item['parent-uri-id'];
411 } elseif (empty($item['thr-parent-id'])) {
412 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
416 unset($item['guid']);
418 unset($item['parent-uri']);
419 unset($item['thr-parent']);
421 DBA::update('item', $item, ['id' => $id]);
427 DI::config()->set("system", "post_update_version_1281_id", $id);
429 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
431 if ($start_id == $id) {
432 Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
433 DBA::e("UPDATE `item-activity` INNER JOIN `item-uri` ON `item-uri`.`uri` = `item-activity`.`uri` SET `item-activity`.`uri-id` = `item-uri`.`id` WHERE `item-activity`.`uri-id` IS NULL");
435 Logger::log("Updating item-uri in item-content", Logger::DEBUG);
436 DBA::e("UPDATE `item-content` INNER JOIN `item-uri` ON `item-uri`.`uri` = `item-content`.`uri` SET `item-content`.`uri-id` = `item-uri`.`id` WHERE `item-content`.`uri-id` IS NULL");
438 DI::config()->set("system", "post_update_version", 1281);
439 Logger::log("Done", Logger::DEBUG);
447 * Set the delivery queue count to a negative value for all items preceding the feature.
449 * @return bool "true" when the job is done
450 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
452 private static function update1297()
454 // Was the script completed?
455 if (DI::config()->get('system', 'post_update_version') >= 1297) {
459 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
460 $max_iid = $max_item_delivery_data['iid'];
462 Logger::info('Start update1297 with max iid: ' . $max_iid);
464 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
466 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
468 if (DBA::errorNo() != 0) {
469 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
473 Logger::info('Processed rows: ' . DBA::affectedRows());
475 DI::config()->set('system', 'post_update_version', 1297);
477 Logger::info('Done');
482 * Remove contact duplicates
484 * @return bool "true" when the job is done
485 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
487 private static function update1322()
489 // Was the script completed?
490 if (DI::config()->get('system', 'post_update_version') >= 1322) {
494 Logger::info('Start');
496 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
497 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
498 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
499 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
500 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
501 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
503 while ($contact = DBA::fetch($contacts)) {
504 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
505 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
508 DBA::close($contact);
509 DI::config()->set('system', 'post_update_version', 1322);
511 Logger::info('Done');
517 * update user-item data with notifications
519 * @return bool "true" when the job is done
520 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
522 private static function update1329()
524 // Was the script completed?
525 if (DI::config()->get('system', 'post_update_version') >= 1329) {
529 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
531 Logger::info('Start', ['item' => $id]);
535 $condition = ["`id` > ?", $id];
536 $params = ['order' => ['id'], 'limit' => 10000];
537 $items = DBA::select('item', ['id'], $condition, $params);
539 if (DBA::errorNo() != 0) {
540 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
544 while ($item = DBA::fetch($items)) {
547 UserItem::setNotification($item['id']);
553 DI::config()->set('system', 'post_update_version_1329_id', $id);
555 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
557 if ($start_id == $id) {
558 DI::config()->set('system', 'post_update_version', 1329);
559 Logger::info('Done');
567 * Fill the "tag" table with tags and mentions from the body
569 * @return bool "true" when the job is done
570 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
572 private static function update1341()
574 // Was the script completed?
575 if (DI::config()->get('system', 'post_update_version') >= 1341) {
579 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
581 Logger::info('Start', ['item' => $id]);
585 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
586 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
587 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
589 if (DBA::errorNo() != 0) {
590 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
594 while ($item = DBA::fetch($items)) {
595 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
596 $id = $item['uri-id'];
598 if ($rows % 1000 == 0) {
599 DI::config()->set('system', 'post_update_version_1341_id', $id);
604 DI::config()->set('system', 'post_update_version_1341_id', $id);
606 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
608 // When there are less than 1,000 items processed this means that we reached the end
609 // The other entries will then be processed with the regular functionality
611 DI::config()->set('system', 'post_update_version', 1341);
612 Logger::info('Done');
620 * Fill the "tag" table with tags and mentions from the "term" table
622 * @return bool "true" when the job is done
623 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
625 private static function update1342()
627 // Was the script completed?
628 if (DI::config()->get('system', 'post_update_version') >= 1342) {
632 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
634 Logger::info('Start', ['item' => $id]);
638 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
640 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
641 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
642 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
643 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
645 if (DBA::errorNo() != 0) {
646 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
650 while ($term = DBA::fetch($terms)) {
651 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
652 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
653 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
654 if (!DBA::isResult($contact)) {
655 $ssl_url = str_replace('http://', 'https://', $term['url']);
656 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
657 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
660 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
661 $term['type'] = Tag::IMPLICIT_MENTION;
665 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
669 if ($rows % 1000 == 0) {
670 DI::config()->set('system', 'post_update_version_1342_id', $id);
675 DI::config()->set('system', 'post_update_version_1342_id', $id);
677 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
679 // When there are less than 1,000 items processed this means that we reached the end
680 // The other entries will then be processed with the regular functionality
682 DI::config()->set('system', 'post_update_version', 1342);
683 Logger::info('Done');
691 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
693 * @return bool "true" when the job is done
694 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
696 private static function update1345()
698 // Was the script completed?
699 if (DI::config()->get('system', 'post_update_version') >= 1345) {
703 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
705 Logger::info('Start', ['item' => $id]);
709 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
710 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
711 FROM `item-delivery-data`
712 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
713 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
715 if (DBA::errorNo() != 0) {
716 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
720 while ($delivery = DBA::fetch($deliveries)) {
721 $id = $delivery['iid'];
722 unset($delivery['iid']);
723 DBA::insert('post-delivery-data', $delivery, true);
726 DBA::close($deliveries);
728 DI::config()->set('system', 'post_update_version_1345_id', $id);
730 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
732 // When there are less than 100 items processed this means that we reached the end
733 // The other entries will then be processed with the regular functionality
735 DI::config()->set('system', 'post_update_version', 1345);
736 Logger::info('Done');
744 * Generates the legacy item.file field string from an item ID.
745 * Includes only file and category terms.
747 * @param int $item_id
751 private static function fileTextFromItemId($item_id)
755 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
756 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
757 foreach ($tags as $tag) {
758 if ($tag['type'] == Category::CATEGORY) {
759 $file_text .= '<' . $tag['term'] . '>';
761 $file_text .= '[' . $tag['term'] . ']';
769 * Fill the "tag" table with tags and mentions from the "term" table
771 * @return bool "true" when the job is done
772 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
774 private static function update1346()
776 // Was the script completed?
777 if (DI::config()->get('system', 'post_update_version') >= 1346) {
781 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
783 Logger::info('Start', ['item' => $id]);
787 $terms = DBA::select('term', ['oid'],
788 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
789 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
791 if (DBA::errorNo() != 0) {
792 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
796 while ($term = DBA::fetch($terms)) {
797 $item = Item::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
798 if (!DBA::isResult($item)) {
802 $file = self::fileTextFromItemId($term['oid']);
804 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
809 if ($rows % 100 == 0) {
810 DI::config()->set('system', 'post_update_version_1346_id', $id);
815 DI::config()->set('system', 'post_update_version_1346_id', $id);
817 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
819 // When there are less than 10 items processed this means that we reached the end
820 // The other entries will then be processed with the regular functionality
822 DI::config()->set('system', 'post_update_version', 1346);
823 Logger::info('Done');
831 * update the "vid" (verb) field in the item table
833 * @return bool "true" when the job is done
834 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
835 * @throws \ImagickException
837 private static function update1347()
839 // Was the script completed?
840 if (DI::config()->get("system", "post_update_version") >= 1347) {
844 $id = DI::config()->get("system", "post_update_version_1347_id", 0);
846 Logger::info('Start', ['item' => $id]);
851 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
852 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
853 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
854 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
856 if (DBA::errorNo() != 0) {
857 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
861 while ($item = DBA::fetch($items)) {
863 $verb = $item['item-verb'];
865 $verb = $item['verb'];
867 if (empty($verb) && is_int($item['activity'])) {
868 $verb = Item::ACTIVITIES[$item['activity']];
874 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
879 DI::config()->set("system", "post_update_version_1347_id", $id);
881 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
883 if ($start_id == $id) {
884 DI::config()->set("system", "post_update_version", 1347);
885 Logger::info('Done');
893 * update the "gsid" (global server id) field in the contact table
895 * @return bool "true" when the job is done
896 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
897 * @throws \ImagickException
899 private static function update1348()
901 // Was the script completed?
902 if (DI::config()->get("system", "post_update_version") >= 1348) {
906 $id = DI::config()->get("system", "post_update_version_1348_id", 0);
908 Logger::info('Start', ['contact' => $id]);
912 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
913 $params = ['order' => ['id'], 'limit' => 10000];
914 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
916 if (DBA::errorNo() != 0) {
917 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
921 while ($contact = DBA::fetch($contacts)) {
922 $id = $contact['id'];
924 DBA::update('contact',
925 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
926 ['id' => $contact['id']]);
930 DBA::close($contacts);
932 DI::config()->set("system", "post_update_version_1348_id", $id);
934 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
936 if ($start_id == $id) {
937 DI::config()->set("system", "post_update_version", 1348);
938 Logger::info('Done');
946 * update the "gsid" (global server id) field in the apcontact table
948 * @return bool "true" when the job is done
949 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
950 * @throws \ImagickException
952 private static function update1349()
954 // Was the script completed?
955 if (DI::config()->get("system", "post_update_version") >= 1349) {
959 $id = DI::config()->get("system", "post_update_version_1349_id", '');
961 Logger::info('Start', ['apcontact' => $id]);
965 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
966 $params = ['order' => ['url'], 'limit' => 10000];
967 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
969 if (DBA::errorNo() != 0) {
970 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
974 while ($apcontact = DBA::fetch($apcontacts)) {
975 $id = $apcontact['url'];
977 DBA::update('apcontact',
978 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
979 ['url' => $apcontact['url']]);
983 DBA::close($apcontacts);
985 DI::config()->set("system", "post_update_version_1349_id", $id);
987 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
989 if ($start_id == $id) {
990 DI::config()->set("system", "post_update_version", 1349);
991 Logger::info('Done');