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;
51 * Calls the post update functions
53 public static function update()
55 if (!self::update1194()) {
58 if (!self::update1206()) {
61 if (!self::update1279()) {
64 if (!self::update1281()) {
67 if (!self::update1297()) {
70 if (!self::update1322()) {
73 if (!self::update1329()) {
76 if (!self::update1341()) {
79 if (!self::update1342()) {
82 if (!self::update1345()) {
85 if (!self::update1346()) {
88 if (!self::update1347()) {
91 if (!self::update1348()) {
94 if (!self::update1349()) {
102 * Updates the "global" field in the item table
104 * @return bool "true" when the job is done
105 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
107 private static function update1194()
109 // Was the script completed?
110 if (DI::config()->get("system", "post_update_version") >= 1194) {
114 Logger::log("Start", Logger::DEBUG);
116 $end_id = DI::config()->get("system", "post_update_1194_end");
118 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
120 DI::config()->set("system", "post_update_1194_end", $r[0]["id"]);
121 $end_id = DI::config()->get("system", "post_update_1194_end");
125 Logger::log("End ID: ".$end_id, Logger::DEBUG);
127 $start_id = DI::config()->get("system", "post_update_1194_start");
129 $query1 = "SELECT `item`.`id` FROM `item` ";
131 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
133 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
134 AND `item`.`visible` AND NOT `item`.`private`
135 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
136 AND `item`.`network` IN ('%s', '%s', '%s', '')
137 AND NOT `item`.`global`";
139 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
140 intval($start_id), intval($end_id),
141 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
143 DI::config()->set("system", "post_update_version", 1194);
144 Logger::log("Update is done", Logger::DEBUG);
147 DI::config()->set("system", "post_update_1194_start", $r[0]["id"]);
148 $start_id = DI::config()->get("system", "post_update_1194_start");
151 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
153 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
154 intval($start_id), intval($end_id),
155 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
157 $pos_id = $r[0]["id"];
161 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
163 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
164 intval($start_id), intval($pos_id),
165 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
167 Logger::log("Done", Logger::DEBUG);
171 * update the "last-item" field in the "self" contact
173 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
175 * @return bool "true" when the job is done
176 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
178 private static function update1206()
180 // Was the script completed?
181 if (DI::config()->get("system", "post_update_version") >= 1206) {
185 Logger::log("Start", Logger::DEBUG);
186 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
187 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
189 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
191 if (!DBA::isResult($r)) {
194 foreach ($r as $user) {
195 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
196 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
200 DI::config()->set("system", "post_update_version", 1206);
201 Logger::log("Done", Logger::DEBUG);
206 * update the item related tables
208 * @return bool "true" when the job is done
209 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
210 * @throws \ImagickException
212 private static function update1279()
214 // Was the script completed?
215 if (DI::config()->get("system", "post_update_version") >= 1279) {
219 $id = DI::config()->get("system", "post_update_version_1279_id", 0);
221 Logger::log("Start from item " . $id, Logger::DEBUG);
223 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
224 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
225 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
226 'inform', 'postopts', 'icid']);
230 $condition = ["`id` > ?", $id];
231 $params = ['order' => ['id'], 'limit' => 10000];
232 $items = Item::select($fields, $condition, $params);
234 if (DBA::errorNo() != 0) {
235 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
239 while ($item = Item::fetch($items)) {
242 if (empty($item['author-id'])) {
243 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
244 'photo' => $item['author-avatar'], 'network' => $item['network']];
246 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, null, $default);
249 if (empty($item['owner-id'])) {
250 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
251 'photo' => $item['owner-avatar'], 'network' => $item['network']];
253 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, null, $default);
256 if (empty($item['psid'])) {
257 $item['psid'] = PermissionSet::getIdFromACL(
266 $item['allow_cid'] = null;
267 $item['allow_gid'] = null;
268 $item['deny_cid'] = null;
269 $item['deny_gid'] = null;
271 if ($item['post-type'] == 0) {
272 if (!empty($item['type']) && ($item['type'] == 'note')) {
273 $item['post-type'] = Item::PT_PERSONAL_NOTE;
274 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
275 $item['post-type'] = Item::PT_IMAGE;
276 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
277 $item['post-type'] = Item::PT_PAGE;
281 self::createLanguage($item);
283 if (!empty($item['icid']) && !empty($item['language'])) {
284 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
286 unset($item['language']);
288 Item::update($item, ['id' => $id]);
294 DI::config()->set("system", "post_update_version_1279_id", $id);
296 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
298 if ($start_id == $id) {
299 // Set all deprecated fields to "null" if they contain an empty string
300 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
301 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
302 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
303 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
304 'rendered-hash', 'rendered-html'];
305 foreach ($nullfields as $field) {
306 $fields = [$field => null];
307 $condition = [$field => ''];
308 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
309 // Important: This has to be a "DBA::update", not a "Item::update"
310 DBA::update('item', $fields, $condition);
313 DI::config()->set("system", "post_update_version", 1279);
314 Logger::log("Done", Logger::DEBUG);
321 private static function createLanguage(&$item)
323 if (empty($item['postopts'])) {
327 $opts = explode(',', $item['postopts']);
331 foreach ($opts as $opt) {
332 if (strstr($opt, 'lang=')) {
333 $language = substr($opt, 5);
339 if (empty($language)) {
343 if (!empty($postopts)) {
344 $item['postopts'] = implode(',', $postopts);
346 $item['postopts'] = null;
349 $lang_pairs = explode(':', $language);
353 foreach ($lang_pairs as $pair) {
354 $lang_pair_arr = explode(';', $pair);
355 if (count($lang_pair_arr) == 2) {
356 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
360 $item['language'] = json_encode($lang_arr);
364 * update item-uri data. Prerequisite for the next item structure update.
366 * @return bool "true" when the job is done
367 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
369 private static function update1281()
371 // Was the script completed?
372 if (DI::config()->get("system", "post_update_version") >= 1281) {
376 $id = DI::config()->get("system", "post_update_version_1281_id", 0);
378 Logger::log("Start from item " . $id, Logger::DEBUG);
380 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
384 $condition = ["`id` > ?", $id];
385 $params = ['order' => ['id'], 'limit' => 10000];
386 $items = DBA::select('item', $fields, $condition, $params);
388 if (DBA::errorNo() != 0) {
389 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
393 while ($item = DBA::fetch($items)) {
396 if (empty($item['uri'])) {
399 } elseif (empty($item['uri-id'])) {
400 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
403 if (empty($item['parent-uri'])) {
404 $item['parent-uri-id'] = $item['uri-id'];
405 } elseif (empty($item['parent-uri-id'])) {
406 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
409 // Very old items don't have this field
410 if (empty($item['thr-parent'])) {
411 $item['thr-parent-id'] = $item['parent-uri-id'];
412 } elseif (empty($item['thr-parent-id'])) {
413 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
417 unset($item['guid']);
419 unset($item['parent-uri']);
420 unset($item['thr-parent']);
422 DBA::update('item', $item, ['id' => $id]);
428 DI::config()->set("system", "post_update_version_1281_id", $id);
430 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
432 if ($start_id == $id) {
433 Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
434 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");
436 Logger::log("Updating item-uri in item-content", Logger::DEBUG);
437 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");
439 DI::config()->set("system", "post_update_version", 1281);
440 Logger::log("Done", Logger::DEBUG);
448 * Set the delivery queue count to a negative value for all items preceding the feature.
450 * @return bool "true" when the job is done
451 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
453 private static function update1297()
455 // Was the script completed?
456 if (DI::config()->get('system', 'post_update_version') >= 1297) {
460 if (!DBStructure::existsTable('item-delivery-data')) {
461 DI::config()->set('system', 'post_update_version', 1297);
465 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
466 $max_iid = $max_item_delivery_data['iid'];
468 Logger::info('Start update1297 with max iid: ' . $max_iid);
470 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
472 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
474 if (DBA::errorNo() != 0) {
475 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
479 Logger::info('Processed rows: ' . DBA::affectedRows());
481 DI::config()->set('system', 'post_update_version', 1297);
483 Logger::info('Done');
488 * Remove contact duplicates
490 * @return bool "true" when the job is done
491 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
493 private static function update1322()
495 // Was the script completed?
496 if (DI::config()->get('system', 'post_update_version') >= 1322) {
500 Logger::info('Start');
502 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
503 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
504 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
505 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
506 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
507 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
509 while ($contact = DBA::fetch($contacts)) {
510 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
511 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
514 DBA::close($contact);
515 DI::config()->set('system', 'post_update_version', 1322);
517 Logger::info('Done');
523 * update user-item data with notifications
525 * @return bool "true" when the job is done
526 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
528 private static function update1329()
530 // Was the script completed?
531 if (DI::config()->get('system', 'post_update_version') >= 1329) {
535 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
537 Logger::info('Start', ['item' => $id]);
541 $condition = ["`id` > ?", $id];
542 $params = ['order' => ['id'], 'limit' => 10000];
543 $items = DBA::select('item', ['id'], $condition, $params);
545 if (DBA::errorNo() != 0) {
546 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
550 while ($item = DBA::fetch($items)) {
553 UserItem::setNotification($item['id']);
559 DI::config()->set('system', 'post_update_version_1329_id', $id);
561 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
563 if ($start_id == $id) {
564 DI::config()->set('system', 'post_update_version', 1329);
565 Logger::info('Done');
573 * Fill the "tag" table with tags and mentions from the body
575 * @return bool "true" when the job is done
576 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
578 private static function update1341()
580 // Was the script completed?
581 if (DI::config()->get('system', 'post_update_version') >= 1341) {
585 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
587 Logger::info('Start', ['item' => $id]);
591 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
592 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
593 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
595 if (DBA::errorNo() != 0) {
596 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
600 while ($item = DBA::fetch($items)) {
601 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
602 $id = $item['uri-id'];
604 if ($rows % 1000 == 0) {
605 DI::config()->set('system', 'post_update_version_1341_id', $id);
610 DI::config()->set('system', 'post_update_version_1341_id', $id);
612 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
614 // When there are less than 1,000 items processed this means that we reached the end
615 // The other entries will then be processed with the regular functionality
617 DI::config()->set('system', 'post_update_version', 1341);
618 Logger::info('Done');
626 * Fill the "tag" table with tags and mentions from the "term" table
628 * @return bool "true" when the job is done
629 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
631 private static function update1342()
633 // Was the script completed?
634 if (DI::config()->get('system', 'post_update_version') >= 1342) {
638 if (!DBStructure::existsTable('term')) {
639 DI::config()->set('system', 'post_update_version', 1342);
643 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
645 Logger::info('Start', ['item' => $id]);
649 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
651 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
652 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
653 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
654 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
656 if (DBA::errorNo() != 0) {
657 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
661 while ($term = DBA::fetch($terms)) {
662 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
663 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
664 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
665 if (!DBA::isResult($contact)) {
666 $ssl_url = str_replace('http://', 'https://', $term['url']);
667 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
668 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
671 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
672 $term['type'] = Tag::IMPLICIT_MENTION;
676 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
680 if ($rows % 1000 == 0) {
681 DI::config()->set('system', 'post_update_version_1342_id', $id);
686 DI::config()->set('system', 'post_update_version_1342_id', $id);
688 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
690 // When there are less than 1,000 items processed this means that we reached the end
691 // The other entries will then be processed with the regular functionality
693 DI::config()->set('system', 'post_update_version', 1342);
694 Logger::info('Done');
702 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
704 * @return bool "true" when the job is done
705 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
707 private static function update1345()
709 // Was the script completed?
710 if (DI::config()->get('system', 'post_update_version') >= 1345) {
714 if (!DBStructure::existsTable('item-delivery-data')) {
715 DI::config()->set('system', 'post_update_version', 1345);
719 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
721 Logger::info('Start', ['item' => $id]);
725 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
726 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
727 FROM `item-delivery-data`
728 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
729 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
731 if (DBA::errorNo() != 0) {
732 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
736 while ($delivery = DBA::fetch($deliveries)) {
737 $id = $delivery['iid'];
738 unset($delivery['iid']);
739 DBA::insert('post-delivery-data', $delivery, true);
742 DBA::close($deliveries);
744 DI::config()->set('system', 'post_update_version_1345_id', $id);
746 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
748 // When there are less than 100 items processed this means that we reached the end
749 // The other entries will then be processed with the regular functionality
751 DI::config()->set('system', 'post_update_version', 1345);
752 Logger::info('Done');
760 * Generates the legacy item.file field string from an item ID.
761 * Includes only file and category terms.
763 * @param int $item_id
767 private static function fileTextFromItemId($item_id)
771 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
772 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
773 foreach ($tags as $tag) {
774 if ($tag['type'] == Category::CATEGORY) {
775 $file_text .= '<' . $tag['term'] . '>';
777 $file_text .= '[' . $tag['term'] . ']';
785 * Fill the "tag" table with tags and mentions from the "term" table
787 * @return bool "true" when the job is done
788 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
790 private static function update1346()
792 // Was the script completed?
793 if (DI::config()->get('system', 'post_update_version') >= 1346) {
797 if (!DBStructure::existsTable('term')) {
798 DI::config()->set('system', 'post_update_version', 1346);
802 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
804 Logger::info('Start', ['item' => $id]);
808 $terms = DBA::select('term', ['oid'],
809 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
810 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
812 if (DBA::errorNo() != 0) {
813 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
817 while ($term = DBA::fetch($terms)) {
818 $item = Item::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
819 if (!DBA::isResult($item)) {
823 $file = self::fileTextFromItemId($term['oid']);
825 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
830 if ($rows % 100 == 0) {
831 DI::config()->set('system', 'post_update_version_1346_id', $id);
836 DI::config()->set('system', 'post_update_version_1346_id', $id);
838 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
840 // When there are less than 10 items processed this means that we reached the end
841 // The other entries will then be processed with the regular functionality
843 DI::config()->set('system', 'post_update_version', 1346);
844 Logger::info('Done');
852 * update the "vid" (verb) field in the item table
854 * @return bool "true" when the job is done
855 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
856 * @throws \ImagickException
858 private static function update1347()
860 // Was the script completed?
861 if (DI::config()->get("system", "post_update_version") >= 1347) {
865 $id = DI::config()->get("system", "post_update_version_1347_id", 0);
867 Logger::info('Start', ['item' => $id]);
872 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
873 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
874 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
875 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
877 if (DBA::errorNo() != 0) {
878 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
882 while ($item = DBA::fetch($items)) {
884 $verb = $item['item-verb'];
886 $verb = $item['verb'];
888 if (empty($verb) && is_int($item['activity'])) {
889 $verb = Item::ACTIVITIES[$item['activity']];
895 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
900 DI::config()->set("system", "post_update_version_1347_id", $id);
902 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
904 if ($start_id == $id) {
905 DI::config()->set("system", "post_update_version", 1347);
906 Logger::info('Done');
914 * update the "gsid" (global server id) field in the contact table
916 * @return bool "true" when the job is done
917 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
918 * @throws \ImagickException
920 private static function update1348()
922 // Was the script completed?
923 if (DI::config()->get("system", "post_update_version") >= 1348) {
927 $id = DI::config()->get("system", "post_update_version_1348_id", 0);
929 Logger::info('Start', ['contact' => $id]);
933 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
934 $params = ['order' => ['id'], 'limit' => 10000];
935 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
937 if (DBA::errorNo() != 0) {
938 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
942 while ($contact = DBA::fetch($contacts)) {
943 $id = $contact['id'];
945 DBA::update('contact',
946 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
947 ['id' => $contact['id']]);
951 DBA::close($contacts);
953 DI::config()->set("system", "post_update_version_1348_id", $id);
955 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
957 if ($start_id == $id) {
958 DI::config()->set("system", "post_update_version", 1348);
959 Logger::info('Done');
967 * update the "gsid" (global server id) field in the apcontact table
969 * @return bool "true" when the job is done
970 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
971 * @throws \ImagickException
973 private static function update1349()
975 // Was the script completed?
976 if (DI::config()->get("system", "post_update_version") >= 1349) {
980 $id = DI::config()->get("system", "post_update_version_1349_id", '');
982 Logger::info('Start', ['apcontact' => $id]);
986 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
987 $params = ['order' => ['url'], 'limit' => 10000];
988 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
990 if (DBA::errorNo() != 0) {
991 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
995 while ($apcontact = DBA::fetch($apcontacts)) {
996 $id = $apcontact['url'];
998 DBA::update('apcontact',
999 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
1000 ['url' => $apcontact['url']]);
1004 DBA::close($apcontacts);
1006 DI::config()->set("system", "post_update_version_1349_id", $id);
1008 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1010 if ($start_id == $id) {
1011 DI::config()->set("system", "post_update_version", 1349);
1012 Logger::info('Done');