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()) {
96 if (!self::update1350()) {
104 * Updates the "global" field in the item table
106 * @return bool "true" when the job is done
107 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
109 private static function update1194()
111 // Was the script completed?
112 if (DI::config()->get("system", "post_update_version") >= 1194) {
116 Logger::log("Start", Logger::DEBUG);
118 $end_id = DI::config()->get("system", "post_update_1194_end");
120 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
122 DI::config()->set("system", "post_update_1194_end", $r[0]["id"]);
123 $end_id = DI::config()->get("system", "post_update_1194_end");
127 Logger::log("End ID: ".$end_id, Logger::DEBUG);
129 $start_id = DI::config()->get("system", "post_update_1194_start");
131 $query1 = "SELECT `item`.`id` FROM `item` ";
133 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
135 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
136 AND `item`.`visible` AND NOT `item`.`private`
137 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
138 AND `item`.`network` IN ('%s', '%s', '%s', '')
139 AND NOT `item`.`global`";
141 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
142 intval($start_id), intval($end_id),
143 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
145 DI::config()->set("system", "post_update_version", 1194);
146 Logger::log("Update is done", Logger::DEBUG);
149 DI::config()->set("system", "post_update_1194_start", $r[0]["id"]);
150 $start_id = DI::config()->get("system", "post_update_1194_start");
153 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
155 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
156 intval($start_id), intval($end_id),
157 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
159 $pos_id = $r[0]["id"];
163 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
165 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
166 intval($start_id), intval($pos_id),
167 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
169 Logger::log("Done", Logger::DEBUG);
173 * update the "last-item" field in the "self" contact
175 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
177 * @return bool "true" when the job is done
178 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
180 private static function update1206()
182 // Was the script completed?
183 if (DI::config()->get("system", "post_update_version") >= 1206) {
187 Logger::log("Start", Logger::DEBUG);
188 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
189 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
191 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
193 if (!DBA::isResult($r)) {
196 foreach ($r as $user) {
197 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
198 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
202 DI::config()->set("system", "post_update_version", 1206);
203 Logger::log("Done", Logger::DEBUG);
208 * update the item related tables
210 * @return bool "true" when the job is done
211 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
212 * @throws \ImagickException
214 private static function update1279()
216 // Was the script completed?
217 if (DI::config()->get("system", "post_update_version") >= 1279) {
221 $id = DI::config()->get("system", "post_update_version_1279_id", 0);
223 Logger::log("Start from item " . $id, Logger::DEBUG);
225 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
226 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
227 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
228 'inform', 'postopts', 'icid']);
232 $condition = ["`id` > ?", $id];
233 $params = ['order' => ['id'], 'limit' => 10000];
234 $items = Item::select($fields, $condition, $params);
236 if (DBA::errorNo() != 0) {
237 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
241 while ($item = Item::fetch($items)) {
244 if (empty($item['author-id'])) {
245 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
246 'photo' => $item['author-avatar'], 'network' => $item['network']];
248 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
251 if (empty($item['owner-id'])) {
252 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
253 'photo' => $item['owner-avatar'], 'network' => $item['network']];
255 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
258 if (empty($item['psid'])) {
259 $item['psid'] = PermissionSet::getIdFromACL(
268 $item['allow_cid'] = null;
269 $item['allow_gid'] = null;
270 $item['deny_cid'] = null;
271 $item['deny_gid'] = null;
273 if ($item['post-type'] == 0) {
274 if (!empty($item['type']) && ($item['type'] == 'note')) {
275 $item['post-type'] = Item::PT_PERSONAL_NOTE;
276 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
277 $item['post-type'] = Item::PT_IMAGE;
278 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
279 $item['post-type'] = Item::PT_PAGE;
283 self::createLanguage($item);
285 if (!empty($item['icid']) && !empty($item['language'])) {
286 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
288 unset($item['language']);
290 Item::update($item, ['id' => $id]);
296 DI::config()->set("system", "post_update_version_1279_id", $id);
298 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
300 if ($start_id == $id) {
301 // Set all deprecated fields to "null" if they contain an empty string
302 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
303 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
304 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
305 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
306 'rendered-hash', 'rendered-html'];
307 foreach ($nullfields as $field) {
308 $fields = [$field => null];
309 $condition = [$field => ''];
310 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
311 // Important: This has to be a "DBA::update", not a "Item::update"
312 DBA::update('item', $fields, $condition);
315 DI::config()->set("system", "post_update_version", 1279);
316 Logger::log("Done", Logger::DEBUG);
323 private static function createLanguage(&$item)
325 if (empty($item['postopts'])) {
329 $opts = explode(',', $item['postopts']);
333 foreach ($opts as $opt) {
334 if (strstr($opt, 'lang=')) {
335 $language = substr($opt, 5);
341 if (empty($language)) {
345 if (!empty($postopts)) {
346 $item['postopts'] = implode(',', $postopts);
348 $item['postopts'] = null;
351 $lang_pairs = explode(':', $language);
355 foreach ($lang_pairs as $pair) {
356 $lang_pair_arr = explode(';', $pair);
357 if (count($lang_pair_arr) == 2) {
358 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
362 $item['language'] = json_encode($lang_arr);
366 * update item-uri data. Prerequisite for the next item structure update.
368 * @return bool "true" when the job is done
369 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
371 private static function update1281()
373 // Was the script completed?
374 if (DI::config()->get("system", "post_update_version") >= 1281) {
378 $id = DI::config()->get("system", "post_update_version_1281_id", 0);
380 Logger::log("Start from item " . $id, Logger::DEBUG);
382 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
386 $condition = ["`id` > ?", $id];
387 $params = ['order' => ['id'], 'limit' => 10000];
388 $items = DBA::select('item', $fields, $condition, $params);
390 if (DBA::errorNo() != 0) {
391 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
395 while ($item = DBA::fetch($items)) {
398 if (empty($item['uri'])) {
401 } elseif (empty($item['uri-id'])) {
402 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
405 if (empty($item['parent-uri'])) {
406 $item['parent-uri-id'] = $item['uri-id'];
407 } elseif (empty($item['parent-uri-id'])) {
408 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
411 // Very old items don't have this field
412 if (empty($item['thr-parent'])) {
413 $item['thr-parent-id'] = $item['parent-uri-id'];
414 } elseif (empty($item['thr-parent-id'])) {
415 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
419 unset($item['guid']);
421 unset($item['parent-uri']);
422 unset($item['thr-parent']);
424 DBA::update('item', $item, ['id' => $id]);
430 DI::config()->set("system", "post_update_version_1281_id", $id);
432 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
434 if ($start_id == $id) {
435 Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
436 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");
438 Logger::log("Updating item-uri in item-content", Logger::DEBUG);
439 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");
441 DI::config()->set("system", "post_update_version", 1281);
442 Logger::log("Done", Logger::DEBUG);
450 * Set the delivery queue count to a negative value for all items preceding the feature.
452 * @return bool "true" when the job is done
453 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
455 private static function update1297()
457 // Was the script completed?
458 if (DI::config()->get('system', 'post_update_version') >= 1297) {
462 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
463 $max_iid = $max_item_delivery_data['iid'];
465 Logger::info('Start update1297 with max iid: ' . $max_iid);
467 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
469 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
471 if (DBA::errorNo() != 0) {
472 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
476 Logger::info('Processed rows: ' . DBA::affectedRows());
478 DI::config()->set('system', 'post_update_version', 1297);
480 Logger::info('Done');
485 * Remove contact duplicates
487 * @return bool "true" when the job is done
488 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
490 private static function update1322()
492 // Was the script completed?
493 if (DI::config()->get('system', 'post_update_version') >= 1322) {
497 Logger::info('Start');
499 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
500 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
501 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
502 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
503 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
504 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
506 while ($contact = DBA::fetch($contacts)) {
507 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
508 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
511 DBA::close($contact);
512 DI::config()->set('system', 'post_update_version', 1322);
514 Logger::info('Done');
520 * update user-item data with notifications
522 * @return bool "true" when the job is done
523 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
525 private static function update1329()
527 // Was the script completed?
528 if (DI::config()->get('system', 'post_update_version') >= 1329) {
532 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
534 Logger::info('Start', ['item' => $id]);
538 $condition = ["`id` > ?", $id];
539 $params = ['order' => ['id'], 'limit' => 10000];
540 $items = DBA::select('item', ['id'], $condition, $params);
542 if (DBA::errorNo() != 0) {
543 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
547 while ($item = DBA::fetch($items)) {
550 UserItem::setNotification($item['id']);
556 DI::config()->set('system', 'post_update_version_1329_id', $id);
558 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
560 if ($start_id == $id) {
561 DI::config()->set('system', 'post_update_version', 1329);
562 Logger::info('Done');
570 * Fill the "tag" table with tags and mentions from the body
572 * @return bool "true" when the job is done
573 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
575 private static function update1341()
577 // Was the script completed?
578 if (DI::config()->get('system', 'post_update_version') >= 1341) {
582 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
584 Logger::info('Start', ['item' => $id]);
588 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
589 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
590 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
592 if (DBA::errorNo() != 0) {
593 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
597 while ($item = DBA::fetch($items)) {
598 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
599 $id = $item['uri-id'];
601 if ($rows % 1000 == 0) {
602 DI::config()->set('system', 'post_update_version_1341_id', $id);
607 DI::config()->set('system', 'post_update_version_1341_id', $id);
609 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
611 // When there are less than 1,000 items processed this means that we reached the end
612 // The other entries will then be processed with the regular functionality
614 DI::config()->set('system', 'post_update_version', 1341);
615 Logger::info('Done');
623 * Fill the "tag" table with tags and mentions from the "term" table
625 * @return bool "true" when the job is done
626 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
628 private static function update1342()
630 // Was the script completed?
631 if (DI::config()->get('system', 'post_update_version') >= 1342) {
635 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
637 Logger::info('Start', ['item' => $id]);
641 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
643 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
644 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
645 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
646 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
648 if (DBA::errorNo() != 0) {
649 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
653 while ($term = DBA::fetch($terms)) {
654 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
655 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
656 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
657 if (!DBA::isResult($contact)) {
658 $ssl_url = str_replace('http://', 'https://', $term['url']);
659 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
660 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
663 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
664 $term['type'] = Tag::IMPLICIT_MENTION;
668 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
672 if ($rows % 1000 == 0) {
673 DI::config()->set('system', 'post_update_version_1342_id', $id);
678 DI::config()->set('system', 'post_update_version_1342_id', $id);
680 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
682 // When there are less than 1,000 items processed this means that we reached the end
683 // The other entries will then be processed with the regular functionality
685 DI::config()->set('system', 'post_update_version', 1342);
686 Logger::info('Done');
694 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
696 * @return bool "true" when the job is done
697 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
699 private static function update1345()
701 // Was the script completed?
702 if (DI::config()->get('system', 'post_update_version') >= 1345) {
706 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
708 Logger::info('Start', ['item' => $id]);
712 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
713 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
714 FROM `item-delivery-data`
715 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
716 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
718 if (DBA::errorNo() != 0) {
719 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
723 while ($delivery = DBA::fetch($deliveries)) {
724 $id = $delivery['iid'];
725 unset($delivery['iid']);
726 DBA::insert('post-delivery-data', $delivery, true);
729 DBA::close($deliveries);
731 DI::config()->set('system', 'post_update_version_1345_id', $id);
733 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
735 // When there are less than 100 items processed this means that we reached the end
736 // The other entries will then be processed with the regular functionality
738 DI::config()->set('system', 'post_update_version', 1345);
739 Logger::info('Done');
747 * Generates the legacy item.file field string from an item ID.
748 * Includes only file and category terms.
750 * @param int $item_id
754 private static function fileTextFromItemId($item_id)
758 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
759 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
760 foreach ($tags as $tag) {
761 if ($tag['type'] == Category::CATEGORY) {
762 $file_text .= '<' . $tag['term'] . '>';
764 $file_text .= '[' . $tag['term'] . ']';
772 * Fill the "tag" table with tags and mentions from the "term" table
774 * @return bool "true" when the job is done
775 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
777 private static function update1346()
779 // Was the script completed?
780 if (DI::config()->get('system', 'post_update_version') >= 1346) {
784 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
786 Logger::info('Start', ['item' => $id]);
790 $terms = DBA::select('term', ['oid'],
791 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
792 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
794 if (DBA::errorNo() != 0) {
795 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
799 while ($term = DBA::fetch($terms)) {
800 $item = Item::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
801 if (!DBA::isResult($item)) {
805 $file = self::fileTextFromItemId($term['oid']);
807 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
812 if ($rows % 100 == 0) {
813 DI::config()->set('system', 'post_update_version_1346_id', $id);
818 DI::config()->set('system', 'post_update_version_1346_id', $id);
820 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
822 // When there are less than 10 items processed this means that we reached the end
823 // The other entries will then be processed with the regular functionality
825 DI::config()->set('system', 'post_update_version', 1346);
826 Logger::info('Done');
834 * update the "vid" (verb) field in the item table
836 * @return bool "true" when the job is done
837 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
838 * @throws \ImagickException
840 private static function update1347()
842 // Was the script completed?
843 if (DI::config()->get("system", "post_update_version") >= 1347) {
847 $id = DI::config()->get("system", "post_update_version_1347_id", 0);
849 Logger::info('Start', ['item' => $id]);
854 $items = DBA::p("SELECT `item`.`id`, `item`.`vid`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
855 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
856 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
857 WHERE `item`.`id` >= ? ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
859 if (DBA::errorNo() != 0) {
860 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
864 while ($item = DBA::fetch($items)) {
866 $verb = $item['item-verb'];
868 $verb = $item['verb'];
870 if (empty($verb) && is_int($item['activity'])) {
871 $verb = Item::ACTIVITIES[$item['activity']];
877 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
882 DI::config()->set("system", "post_update_version_1347_id", $id);
884 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
886 if ($start_id == $id) {
887 DI::config()->set("system", "post_update_version", 1347);
888 Logger::info('Done');
896 * update the "gsid" (global server id) field in the contact table
898 * @return bool "true" when the job is done
899 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
900 * @throws \ImagickException
902 private static function update1348()
904 // Was the script completed?
905 if (DI::config()->get("system", "post_update_version") >= 1348) {
909 $id = DI::config()->get("system", "post_update_version_1348_id", 0);
911 Logger::info('Start', ['contact' => $id]);
915 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
916 $params = ['order' => ['id'], 'limit' => 10000];
917 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
919 if (DBA::errorNo() != 0) {
920 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
924 while ($contact = DBA::fetch($contacts)) {
925 $id = $contact['id'];
927 DBA::update('contact',
928 ['gsid' => GServer::getID($contact['baseurl']), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
929 ['id' => $contact['id']]);
933 DBA::close($contacts);
935 DI::config()->set("system", "post_update_version_1348_id", $id);
937 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
939 if ($start_id == $id) {
940 DI::config()->set("system", "post_update_version", 1348);
941 Logger::info('Done');
949 * update the "gsid" (global server id) field in the apcontact table
951 * @return bool "true" when the job is done
952 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
953 * @throws \ImagickException
955 private static function update1349()
957 // Was the script completed?
958 if (DI::config()->get("system", "post_update_version") >= 1349) {
962 $id = DI::config()->get("system", "post_update_version_1349_id", 0);
964 Logger::info('Start', ['apcontact' => $id]);
968 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
969 $params = ['order' => ['url'], 'limit' => 10000];
970 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
972 if (DBA::errorNo() != 0) {
973 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
977 while ($apcontact = DBA::fetch($apcontacts)) {
978 $id = $apcontact['url'];
980 DBA::update('apcontact',
981 ['gsid' => GServer::getID($apcontact['baseurl']), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
982 ['url' => $apcontact['url']]);
986 DBA::close($apcontacts);
988 DI::config()->set("system", "post_update_version_1349_id", $id);
990 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
992 if ($start_id == $id) {
993 DI::config()->set("system", "post_update_version", 1349);
994 Logger::info('Done');
1002 * update the "gsid" (global server id) field in the gcontact table
1004 * @return bool "true" when the job is done
1005 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1006 * @throws \ImagickException
1008 private static function update1350()
1010 // Was the script completed?
1011 if (DI::config()->get("system", "post_update_version") >= 1350) {
1015 $id = DI::config()->get("system", "post_update_version_1350_id", 0);
1017 Logger::info('Start', ['gcontact' => $id]);
1021 $condition = ["`id` > ? AND `gsid` IS NULL AND `server_url` != '' AND NOT `server_url` IS NULL", $id];
1022 $params = ['order' => ['id'], 'limit' => 10000];
1023 $gcontacts = DBA::select('gcontact', ['id', 'server_url'], $condition, $params);
1025 if (DBA::errorNo() != 0) {
1026 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
1030 while ($gcontact = DBA::fetch($gcontacts)) {
1031 $id = $gcontact['id'];
1033 DBA::update('gcontact',
1034 ['gsid' => GServer::getID($gcontact['server_url']), 'server_url' => GServer::cleanURL($gcontact['server_url'])],
1035 ['id' => $gcontact['id']]);
1039 DBA::close($gcontacts);
1041 DI::config()->set("system", "post_update_version_1350_id", $id);
1043 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1045 if ($start_id == $id) {
1046 DI::config()->set("system", "post_update_version", 1350);
1047 Logger::info('Done');