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\Photo;
33 use Friendica\Model\Post\Category;
34 use Friendica\Model\Tag;
35 use Friendica\Model\UserItem;
36 use Friendica\Model\Verb;
37 use Friendica\Util\Strings;
40 * These database-intensive post update routines are meant to be executed in the background by the cronjob.
42 * If there is a need for a intensive migration after a database structure change, update this file
43 * by adding a new method at the end with the number of the new DB_UPDATE_VERSION.
47 // Needed for the helper function to read from the legacy term table
48 const OBJECT_TYPE_POST = 1;
52 * Calls the post update functions
54 public static function update()
56 if (!self::update1194()) {
59 if (!self::update1206()) {
62 if (!self::update1279()) {
65 if (!self::update1281()) {
68 if (!self::update1297()) {
71 if (!self::update1322()) {
74 if (!self::update1329()) {
77 if (!self::update1341()) {
80 if (!self::update1342()) {
83 if (!self::update1345()) {
86 if (!self::update1346()) {
89 if (!self::update1347()) {
92 if (!self::update1348()) {
95 if (!self::update1349()) {
98 if (!self::update1383()) {
106 * Updates the "global" field in the item table
108 * @return bool "true" when the job is done
109 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
111 private static function update1194()
113 // Was the script completed?
114 if (DI::config()->get("system", "post_update_version") >= 1194) {
118 Logger::log("Start", Logger::DEBUG);
120 $end_id = DI::config()->get("system", "post_update_1194_end");
122 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
124 DI::config()->set("system", "post_update_1194_end", $r[0]["id"]);
125 $end_id = DI::config()->get("system", "post_update_1194_end");
129 Logger::log("End ID: ".$end_id, Logger::DEBUG);
131 $start_id = DI::config()->get("system", "post_update_1194_start");
133 $query1 = "SELECT `item`.`id` FROM `item` ";
135 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
137 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
138 AND `item`.`visible` AND NOT `item`.`private`
139 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
140 AND `item`.`network` IN ('%s', '%s', '%s', '')
141 AND NOT `item`.`global`";
143 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
144 intval($start_id), intval($end_id),
145 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
147 DI::config()->set("system", "post_update_version", 1194);
148 Logger::log("Update is done", Logger::DEBUG);
151 DI::config()->set("system", "post_update_1194_start", $r[0]["id"]);
152 $start_id = DI::config()->get("system", "post_update_1194_start");
155 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
157 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
158 intval($start_id), intval($end_id),
159 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
161 $pos_id = $r[0]["id"];
165 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
167 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
168 intval($start_id), intval($pos_id),
169 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
171 Logger::log("Done", Logger::DEBUG);
175 * update the "last-item" field in the "self" contact
177 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
179 * @return bool "true" when the job is done
180 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
182 private static function update1206()
184 // Was the script completed?
185 if (DI::config()->get("system", "post_update_version") >= 1206) {
189 Logger::log("Start", Logger::DEBUG);
190 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
191 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
193 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
195 if (!DBA::isResult($r)) {
198 foreach ($r as $user) {
199 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
200 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
204 DI::config()->set("system", "post_update_version", 1206);
205 Logger::log("Done", Logger::DEBUG);
210 * update the item related tables
212 * @return bool "true" when the job is done
213 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
214 * @throws \ImagickException
216 private static function update1279()
218 // Was the script completed?
219 if (DI::config()->get("system", "post_update_version") >= 1279) {
223 $id = DI::config()->get("system", "post_update_version_1279_id", 0);
225 Logger::log("Start from item " . $id, Logger::DEBUG);
227 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
228 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
229 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
230 'inform', 'postopts', 'icid']);
234 $condition = ["`id` > ?", $id];
235 $params = ['order' => ['id'], 'limit' => 10000];
236 $items = Item::select($fields, $condition, $params);
238 if (DBA::errorNo() != 0) {
239 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
243 while ($item = Item::fetch($items)) {
246 if (empty($item['author-id'])) {
247 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
248 'photo' => $item['author-avatar'], 'network' => $item['network']];
250 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, null, $default);
253 if (empty($item['owner-id'])) {
254 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
255 'photo' => $item['owner-avatar'], 'network' => $item['network']];
257 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, null, $default);
260 if (empty($item['psid'])) {
261 $item['psid'] = PermissionSet::getIdFromACL(
270 $item['allow_cid'] = null;
271 $item['allow_gid'] = null;
272 $item['deny_cid'] = null;
273 $item['deny_gid'] = null;
275 if ($item['post-type'] == 0) {
276 if (!empty($item['type']) && ($item['type'] == 'note')) {
277 $item['post-type'] = Item::PT_PERSONAL_NOTE;
278 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
279 $item['post-type'] = Item::PT_IMAGE;
280 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
281 $item['post-type'] = Item::PT_PAGE;
285 self::createLanguage($item);
287 if (!empty($item['icid']) && !empty($item['language'])) {
288 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
290 unset($item['language']);
292 Item::update($item, ['id' => $id]);
298 DI::config()->set("system", "post_update_version_1279_id", $id);
300 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
302 if ($start_id == $id) {
303 // Set all deprecated fields to "null" if they contain an empty string
304 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
305 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
306 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
307 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
308 'rendered-hash', 'rendered-html'];
309 foreach ($nullfields as $field) {
310 $fields = [$field => null];
311 $condition = [$field => ''];
312 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
313 // Important: This has to be a "DBA::update", not a "Item::update"
314 DBA::update('item', $fields, $condition);
317 DI::config()->set("system", "post_update_version", 1279);
318 Logger::log("Done", Logger::DEBUG);
325 private static function createLanguage(&$item)
327 if (empty($item['postopts'])) {
331 $opts = explode(',', $item['postopts']);
335 foreach ($opts as $opt) {
336 if (strstr($opt, 'lang=')) {
337 $language = substr($opt, 5);
343 if (empty($language)) {
347 if (!empty($postopts)) {
348 $item['postopts'] = implode(',', $postopts);
350 $item['postopts'] = null;
353 $lang_pairs = explode(':', $language);
357 foreach ($lang_pairs as $pair) {
358 $lang_pair_arr = explode(';', $pair);
359 if (count($lang_pair_arr) == 2) {
360 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
364 $item['language'] = json_encode($lang_arr);
368 * update item-uri data. Prerequisite for the next item structure update.
370 * @return bool "true" when the job is done
371 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
373 private static function update1281()
375 // Was the script completed?
376 if (DI::config()->get("system", "post_update_version") >= 1281) {
380 $id = DI::config()->get("system", "post_update_version_1281_id", 0);
382 Logger::log("Start from item " . $id, Logger::DEBUG);
384 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
388 $condition = ["`id` > ?", $id];
389 $params = ['order' => ['id'], 'limit' => 10000];
390 $items = DBA::select('item', $fields, $condition, $params);
392 if (DBA::errorNo() != 0) {
393 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
397 while ($item = DBA::fetch($items)) {
400 if (empty($item['uri'])) {
403 } elseif (empty($item['uri-id'])) {
404 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
407 if (empty($item['parent-uri'])) {
408 $item['parent-uri-id'] = $item['uri-id'];
409 } elseif (empty($item['parent-uri-id'])) {
410 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
413 // Very old items don't have this field
414 if (empty($item['thr-parent'])) {
415 $item['thr-parent-id'] = $item['parent-uri-id'];
416 } elseif (empty($item['thr-parent-id'])) {
417 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
421 unset($item['guid']);
423 unset($item['parent-uri']);
424 unset($item['thr-parent']);
426 DBA::update('item', $item, ['id' => $id]);
432 DI::config()->set("system", "post_update_version_1281_id", $id);
434 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
436 if ($start_id == $id) {
437 Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
438 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");
440 Logger::log("Updating item-uri in item-content", Logger::DEBUG);
441 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");
443 DI::config()->set("system", "post_update_version", 1281);
444 Logger::log("Done", Logger::DEBUG);
452 * Set the delivery queue count to a negative value for all items preceding the feature.
454 * @return bool "true" when the job is done
455 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
457 private static function update1297()
459 // Was the script completed?
460 if (DI::config()->get('system', 'post_update_version') >= 1297) {
464 if (!DBStructure::existsTable('item-delivery-data')) {
465 DI::config()->set('system', 'post_update_version', 1297);
469 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
470 $max_iid = $max_item_delivery_data['iid'];
472 Logger::info('Start update1297 with max iid: ' . $max_iid);
474 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
476 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
478 if (DBA::errorNo() != 0) {
479 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
483 Logger::info('Processed rows: ' . DBA::affectedRows());
485 DI::config()->set('system', 'post_update_version', 1297);
487 Logger::info('Done');
492 * Remove contact duplicates
494 * @return bool "true" when the job is done
495 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
497 private static function update1322()
499 // Was the script completed?
500 if (DI::config()->get('system', 'post_update_version') >= 1322) {
504 Logger::info('Start');
506 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
507 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
508 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
509 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
510 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
511 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
513 while ($contact = DBA::fetch($contacts)) {
514 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
515 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
518 DBA::close($contact);
519 DI::config()->set('system', 'post_update_version', 1322);
521 Logger::info('Done');
527 * update user-item data with notifications
529 * @return bool "true" when the job is done
530 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
532 private static function update1329()
534 // Was the script completed?
535 if (DI::config()->get('system', 'post_update_version') >= 1329) {
539 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
541 Logger::info('Start', ['item' => $id]);
545 $condition = ["`id` > ?", $id];
546 $params = ['order' => ['id'], 'limit' => 10000];
547 $items = DBA::select('item', ['id'], $condition, $params);
549 if (DBA::errorNo() != 0) {
550 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
554 while ($item = DBA::fetch($items)) {
557 UserItem::setNotification($item['id']);
563 DI::config()->set('system', 'post_update_version_1329_id', $id);
565 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
567 if ($start_id == $id) {
568 DI::config()->set('system', 'post_update_version', 1329);
569 Logger::info('Done');
577 * Fill the "tag" table with tags and mentions from the body
579 * @return bool "true" when the job is done
580 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
582 private static function update1341()
584 // Was the script completed?
585 if (DI::config()->get('system', 'post_update_version') >= 1341) {
589 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
591 Logger::info('Start', ['item' => $id]);
595 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
596 (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
597 ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
599 if (DBA::errorNo() != 0) {
600 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
604 while ($item = DBA::fetch($items)) {
605 Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
606 $id = $item['uri-id'];
608 if ($rows % 1000 == 0) {
609 DI::config()->set('system', 'post_update_version_1341_id', $id);
614 DI::config()->set('system', 'post_update_version_1341_id', $id);
616 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
618 // When there are less than 1,000 items processed this means that we reached the end
619 // The other entries will then be processed with the regular functionality
621 DI::config()->set('system', 'post_update_version', 1341);
622 Logger::info('Done');
630 * Fill the "tag" table with tags and mentions from the "term" table
632 * @return bool "true" when the job is done
633 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
635 private static function update1342()
637 // Was the script completed?
638 if (DI::config()->get('system', 'post_update_version') >= 1342) {
642 if (!DBStructure::existsTable('term')) {
643 DI::config()->set('system', 'post_update_version', 1342);
647 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
649 Logger::info('Start', ['item' => $id]);
653 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
655 INNER JOIN `item` ON `item`.`id` = `term`.`oid`
656 INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
657 WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
658 Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
660 if (DBA::errorNo() != 0) {
661 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
665 while ($term = DBA::fetch($terms)) {
666 if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
667 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
668 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
669 if (!DBA::isResult($contact)) {
670 $ssl_url = str_replace('http://', 'https://', $term['url']);
671 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
672 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
675 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
676 $term['type'] = Tag::IMPLICIT_MENTION;
680 Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
684 if ($rows % 1000 == 0) {
685 DI::config()->set('system', 'post_update_version_1342_id', $id);
690 DI::config()->set('system', 'post_update_version_1342_id', $id);
692 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
694 // When there are less than 1,000 items processed this means that we reached the end
695 // The other entries will then be processed with the regular functionality
697 DI::config()->set('system', 'post_update_version', 1342);
698 Logger::info('Done');
706 * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
708 * @return bool "true" when the job is done
709 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
711 private static function update1345()
713 // Was the script completed?
714 if (DI::config()->get('system', 'post_update_version') >= 1345) {
718 if (!DBStructure::existsTable('item-delivery-data')) {
719 DI::config()->set('system', 'post_update_version', 1345);
723 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
725 Logger::info('Start', ['item' => $id]);
729 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
730 `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
731 FROM `item-delivery-data`
732 INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
733 WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
735 if (DBA::errorNo() != 0) {
736 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
740 while ($delivery = DBA::fetch($deliveries)) {
741 $id = $delivery['iid'];
742 unset($delivery['iid']);
743 DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
746 DBA::close($deliveries);
748 DI::config()->set('system', 'post_update_version_1345_id', $id);
750 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
752 // When there are less than 100 items processed this means that we reached the end
753 // The other entries will then be processed with the regular functionality
755 DI::config()->set('system', 'post_update_version', 1345);
756 Logger::info('Done');
764 * Generates the legacy item.file field string from an item ID.
765 * Includes only file and category terms.
767 * @param int $item_id
771 private static function fileTextFromItemId($item_id)
775 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
776 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
777 foreach ($tags as $tag) {
778 if ($tag['type'] == Category::CATEGORY) {
779 $file_text .= '<' . $tag['term'] . '>';
781 $file_text .= '[' . $tag['term'] . ']';
789 * Fill the "tag" table with tags and mentions from the "term" table
791 * @return bool "true" when the job is done
792 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
794 private static function update1346()
796 // Was the script completed?
797 if (DI::config()->get('system', 'post_update_version') >= 1346) {
801 if (!DBStructure::existsTable('term')) {
802 DI::config()->set('system', 'post_update_version', 1346);
806 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
808 Logger::info('Start', ['item' => $id]);
812 $terms = DBA::select('term', ['oid'],
813 ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
814 ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
816 if (DBA::errorNo() != 0) {
817 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
821 while ($term = DBA::fetch($terms)) {
822 $item = Item::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
823 if (!DBA::isResult($item)) {
827 $file = self::fileTextFromItemId($term['oid']);
829 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
834 if ($rows % 100 == 0) {
835 DI::config()->set('system', 'post_update_version_1346_id', $id);
840 DI::config()->set('system', 'post_update_version_1346_id', $id);
842 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
844 // When there are less than 10 items processed this means that we reached the end
845 // The other entries will then be processed with the regular functionality
847 DI::config()->set('system', 'post_update_version', 1346);
848 Logger::info('Done');
856 * update the "vid" (verb) field in the item table
858 * @return bool "true" when the job is done
859 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
860 * @throws \ImagickException
862 private static function update1347()
864 // Was the script completed?
865 if (DI::config()->get("system", "post_update_version") >= 1347) {
869 $id = DI::config()->get("system", "post_update_version_1347_id", 0);
871 Logger::info('Start', ['item' => $id]);
876 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
877 FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
878 LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
879 WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
881 if (DBA::errorNo() != 0) {
882 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
886 while ($item = DBA::fetch($items)) {
888 $verb = $item['item-verb'];
890 $verb = $item['verb'];
892 if (empty($verb) && is_int($item['activity'])) {
893 $verb = Item::ACTIVITIES[$item['activity']];
899 DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
904 DI::config()->set("system", "post_update_version_1347_id", $id);
906 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
908 if ($start_id == $id) {
909 DI::config()->set("system", "post_update_version", 1347);
910 Logger::info('Done');
918 * update the "gsid" (global server id) field in the contact table
920 * @return bool "true" when the job is done
921 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
922 * @throws \ImagickException
924 private static function update1348()
926 // Was the script completed?
927 if (DI::config()->get("system", "post_update_version") >= 1348) {
931 $id = DI::config()->get("system", "post_update_version_1348_id", 0);
933 Logger::info('Start', ['contact' => $id]);
937 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
938 $params = ['order' => ['id'], 'limit' => 10000];
939 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
941 if (DBA::errorNo() != 0) {
942 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
946 while ($contact = DBA::fetch($contacts)) {
947 $id = $contact['id'];
949 DBA::update('contact',
950 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
951 ['id' => $contact['id']]);
955 DBA::close($contacts);
957 DI::config()->set("system", "post_update_version_1348_id", $id);
959 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
961 if ($start_id == $id) {
962 DI::config()->set("system", "post_update_version", 1348);
963 Logger::info('Done');
971 * update the "gsid" (global server id) field in the apcontact table
973 * @return bool "true" when the job is done
974 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
975 * @throws \ImagickException
977 private static function update1349()
979 // Was the script completed?
980 if (DI::config()->get("system", "post_update_version") >= 1349) {
984 $id = DI::config()->get("system", "post_update_version_1349_id", '');
986 Logger::info('Start', ['apcontact' => $id]);
990 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
991 $params = ['order' => ['url'], 'limit' => 10000];
992 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
994 if (DBA::errorNo() != 0) {
995 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
999 while ($apcontact = DBA::fetch($apcontacts)) {
1000 $id = $apcontact['url'];
1002 DBA::update('apcontact',
1003 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
1004 ['url' => $apcontact['url']]);
1008 DBA::close($apcontacts);
1010 DI::config()->set("system", "post_update_version_1349_id", $id);
1012 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
1014 if ($start_id == $id) {
1015 DI::config()->set("system", "post_update_version", 1349);
1016 Logger::info('Done');
1023 * Remove orphaned photo entries
1025 * @return bool "true" when the job is done
1026 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1027 * @throws \ImagickException
1029 private static function update1383()
1031 // Was the script completed?
1032 if (DI::config()->get("system", "post_update_version") >= 1383) {
1036 Logger::info('Start');
1039 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
1041 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
1042 while ($photo = DBA::fetch($photos)) {
1043 $delete = !in_array($photo['scale'], [4, 5, 6]);
1046 // Check if there is a contact entry with that photo
1047 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
1048 $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
1052 Photo::delete(['id' => $photo['id']]);
1056 DBA::close($photos);
1058 DI::config()->set("system", "post_update_version", 1383);
1059 Logger::info('Done', ['deleted' => $deleted]);