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 if (!DBStructure::existsTable('item-delivery-data')) {
460 DI::config()->set('system', 'post_update_version', 1297);
464 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
465 $max_iid = $max_item_delivery_data['iid'];
467 Logger::info('Start update1297 with max iid: ' . $max_iid);
469 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
471 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
473 if (DBA::errorNo() != 0) {
474 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
478 Logger::info('Processed rows: ' . DBA::affectedRows());
480 DI::config()->set('system', 'post_update_version', 1297);
482 Logger::info('Done');
487 * Remove contact duplicates
489 * @return bool "true" when the job is done
490 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
492 private static function update1322()
494 // Was the script completed?
495 if (DI::config()->get('system', 'post_update_version') >= 1322) {
499 Logger::info('Start');
501 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
502 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
503 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
504 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
505 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
506 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
508 while ($contact = DBA::fetch($contacts)) {
509 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
510 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
513 DBA::close($contact);
514 DI::config()->set('system', 'post_update_version', 1322);
516 Logger::info('Done');
522 * update user-item data with notifications
524 * @return bool "true" when the job is done
525 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
527 private static function update1329()
529 // Was the script completed?
530 if (DI::config()->get('system', 'post_update_version') >= 1329) {
534 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
536 Logger::info('Start', ['item' => $id]);
540 $condition = ["`id` > ?", $id];
541 $params = ['order' => ['id'], 'limit' => 10000];
542 $items = DBA::select('item', ['id'], $condition, $params);
544 if (DBA::errorNo() != 0) {
545 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
549 while ($item = DBA::fetch($items)) {
552 UserItem::setNotification($item['id']);
558 DI::config()->set('system', 'post_update_version_1329_id', $id);
560 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
562 if ($start_id == $id) {
563 DI::config()->set('system', 'post_update_version', 1329);
564 Logger::info('Done');
572 * Fill the "tag" table with tags and mentions from the body
574 * @return bool "true" when the job is done
575 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
577 private static function update1341()
579 // Was the script completed?
580 if (DI::config()->get('system', 'post_update_version') >= 1341) {
584 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
586 Logger::info('Start', ['item' => $id]);
590 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
591 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
592 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
594 if (DBA::errorNo() != 0) {
595 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
599 while ($item = DBA::fetch($items)) {
600 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
601 $id = $item['uri-id'];
603 if ($rows % 1000 == 0) {
604 DI::config()->set('system', 'post_update_version_1341_id', $id);
609 DI::config()->set('system', 'post_update_version_1341_id', $id);
611 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
613 // When there are less than 1,000 items processed this means that we reached the end
614 // The other entries will then be processed with the regular functionality
616 DI::config()->set('system', 'post_update_version', 1341);
617 Logger::info('Done');
625 * Fill the "tag" table with tags and mentions from the "term" table
627 * @return bool "true" when the job is done
628 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
630 private static function update1342()
632 // Was the script completed?
633 if (DI::config()->get('system', 'post_update_version') >= 1342) {
637 if (!DBStructure::existsTable('term')) {
638 DI::config()->set('system', 'post_update_version', 1342);
642 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
644 Logger::info('Start', ['item' => $id]);
648 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
650 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
651 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
652 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
653 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
655 if (DBA::errorNo() != 0) {
656 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
660 while ($term = DBA::fetch($terms)) {
661 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
662 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
663 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
664 if (!DBA::isResult($contact)) {
665 $ssl_url = str_replace('http://', 'https://', $term['url']);
666 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
667 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
670 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
671 $term['type'] = Tag::IMPLICIT_MENTION;
675 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
679 if ($rows % 1000 == 0) {
680 DI::config()->set('system', 'post_update_version_1342_id', $id);
685 DI::config()->set('system', 'post_update_version_1342_id', $id);
687 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
689 // When there are less than 1,000 items processed this means that we reached the end
690 // The other entries will then be processed with the regular functionality
692 DI::config()->set('system', 'post_update_version', 1342);
693 Logger::info('Done');
701 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
703 * @return bool "true" when the job is done
704 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
706 private static function update1345()
708 // Was the script completed?
709 if (DI::config()->get('system', 'post_update_version') >= 1345) {
713 if (!DBStructure::existsTable('item-delivery-data')) {
714 DI::config()->set('system', 'post_update_version', 1345);
718 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
720 Logger::info('Start', ['item' => $id]);
724 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
725 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
726 FROM `item-delivery-data`
727 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
728 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
730 if (DBA::errorNo() != 0) {
731 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
735 while ($delivery = DBA::fetch($deliveries)) {
736 $id = $delivery['iid'];
737 unset($delivery['iid']);
738 DBA::insert('post-delivery-data', $delivery, true);
741 DBA::close($deliveries);
743 DI::config()->set('system', 'post_update_version_1345_id', $id);
745 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
747 // When there are less than 100 items processed this means that we reached the end
748 // The other entries will then be processed with the regular functionality
750 DI::config()->set('system', 'post_update_version', 1345);
751 Logger::info('Done');
759 * Generates the legacy item.file field string from an item ID.
760 * Includes only file and category terms.
762 * @param int $item_id
766 private static function fileTextFromItemId($item_id)
770 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
771 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
772 foreach ($tags as $tag) {
773 if ($tag['type'] == Category::CATEGORY) {
774 $file_text .= '<' . $tag['term'] . '>';
776 $file_text .= '[' . $tag['term'] . ']';
784 * Fill the "tag" table with tags and mentions from the "term" table
786 * @return bool "true" when the job is done
787 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
789 private static function update1346()
791 // Was the script completed?
792 if (DI::config()->get('system', 'post_update_version') >= 1346) {
796 if (!DBStructure::existsTable('term')) {
797 DI::config()->set('system', 'post_update_version', 1346);
801 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
803 Logger::info('Start', ['item' => $id]);
807 $terms = DBA::select('term', ['oid'],
808 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
809 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
811 if (DBA::errorNo() != 0) {
812 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
816 while ($term = DBA::fetch($terms)) {
817 $item = Item::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
818 if (!DBA::isResult($item)) {
822 $file = self::fileTextFromItemId($term['oid']);
824 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
829 if ($rows % 100 == 0) {
830 DI::config()->set('system', 'post_update_version_1346_id', $id);
835 DI::config()->set('system', 'post_update_version_1346_id', $id);
837 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
839 // When there are less than 10 items processed this means that we reached the end
840 // The other entries will then be processed with the regular functionality
842 DI::config()->set('system', 'post_update_version', 1346);
843 Logger::info('Done');
851 * update the "vid" (verb) field in the item table
853 * @return bool "true" when the job is done
854 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
855 * @throws \ImagickException
857 private static function update1347()
859 // Was the script completed?
860 if (DI::config()->get("system", "post_update_version") >= 1347) {
864 $id = DI::config()->get("system", "post_update_version_1347_id", 0);
866 Logger::info('Start', ['item' => $id]);
871 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
872 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
873 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
874 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
876 if (DBA::errorNo() != 0) {
877 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
881 while ($item = DBA::fetch($items)) {
883 $verb = $item['item-verb'];
885 $verb = $item['verb'];
887 if (empty($verb) && is_int($item['activity'])) {
888 $verb = Item::ACTIVITIES[$item['activity']];
894 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
899 DI::config()->set("system", "post_update_version_1347_id", $id);
901 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
903 if ($start_id == $id) {
904 DI::config()->set("system", "post_update_version", 1347);
905 Logger::info('Done');
913 * update the "gsid" (global server id) field in the contact table
915 * @return bool "true" when the job is done
916 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
917 * @throws \ImagickException
919 private static function update1348()
921 // Was the script completed?
922 if (DI::config()->get("system", "post_update_version") >= 1348) {
926 $id = DI::config()->get("system", "post_update_version_1348_id", 0);
928 Logger::info('Start', ['contact' => $id]);
932 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
933 $params = ['order' => ['id'], 'limit' => 10000];
934 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
936 if (DBA::errorNo() != 0) {
937 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
941 while ($contact = DBA::fetch($contacts)) {
942 $id = $contact['id'];
944 DBA::update('contact',
945 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
946 ['id' => $contact['id']]);
950 DBA::close($contacts);
952 DI::config()->set("system", "post_update_version_1348_id", $id);
954 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
956 if ($start_id == $id) {
957 DI::config()->set("system", "post_update_version", 1348);
958 Logger::info('Done');
966 * update the "gsid" (global server id) field in the apcontact table
968 * @return bool "true" when the job is done
969 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
970 * @throws \ImagickException
972 private static function update1349()
974 // Was the script completed?
975 if (DI::config()->get("system", "post_update_version") >= 1349) {
979 $id = DI::config()->get("system", "post_update_version_1349_id", '');
981 Logger::info('Start', ['apcontact' => $id]);
985 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
986 $params = ['order' => ['url'], 'limit' => 10000];
987 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
989 if (DBA::errorNo() != 0) {
990 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
994 while ($apcontact = DBA::fetch($apcontacts)) {
995 $id = $apcontact['url'];
997 DBA::update('apcontact',
998 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
999 ['url' => $apcontact['url']]);
1003 DBA::close($apcontacts);
1005 DI::config()->set("system", "post_update_version_1349_id", $id);
1007 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1009 if ($start_id == $id) {
1010 DI::config()->set("system", "post_update_version", 1349);
1011 Logger::info('Done');