]> git.mxchange.org Git - friendica.git/blob - src/Database/PostUpdate.php
File and category aren't using "term" anymore
[friendica.git] / src / Database / PostUpdate.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Database;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Item;
29 use Friendica\Model\ItemURI;
30 use Friendica\Model\PermissionSet;
31 use Friendica\Model\Post\Category;
32 use Friendica\Model\Tag;
33 use Friendica\Model\Term;
34 use Friendica\Model\UserItem;
35 use Friendica\Util\Strings;
36
37 /**
38  * These database-intensive post update routines are meant to be executed in the background by the cronjob.
39  *
40  * If there is a need for a intensive migration after a database structure change, update this file
41  * by adding a new method at the end with the number of the new DB_UPDATE_VERSION.
42  */
43 class PostUpdate
44 {
45         /**
46          * Calls the post update functions
47          */
48         public static function update()
49         {
50                 if (!self::update1194()) {
51                         return false;
52                 }
53                 if (!self::update1206()) {
54                         return false;
55                 }
56                 if (!self::update1279()) {
57                         return false;
58                 }
59                 if (!self::update1281()) {
60                         return false;
61                 }
62                 if (!self::update1297()) {
63                         return false;
64                 }
65                 if (!self::update1322()) {
66                         return false;
67                 }
68                 if (!self::update1329()) {
69                         return false;
70                 }
71                 if (!self::update1341()) {
72                         return false;
73                 }
74                 if (!self::update1342()) {
75                         return false;
76                 }
77                 if (!self::update1346()) {
78                         return false;
79                 }
80
81                 return true;
82         }
83
84         /**
85          * Updates the "global" field in the item table
86          *
87          * @return bool "true" when the job is done
88          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
89          */
90         private static function update1194()
91         {
92                 // Was the script completed?
93                 if (DI::config()->get("system", "post_update_version") >= 1194) {
94                         return true;
95                 }
96
97                 Logger::log("Start", Logger::DEBUG);
98
99                 $end_id = DI::config()->get("system", "post_update_1194_end");
100                 if (!$end_id) {
101                         $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
102                         if ($r) {
103                                 DI::config()->set("system", "post_update_1194_end", $r[0]["id"]);
104                                 $end_id = DI::config()->get("system", "post_update_1194_end");
105                         }
106                 }
107
108                 Logger::log("End ID: ".$end_id, Logger::DEBUG);
109
110                 $start_id = DI::config()->get("system", "post_update_1194_start");
111
112                 $query1 = "SELECT `item`.`id` FROM `item` ";
113
114                 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
115
116                 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
117                                 AND `item`.`visible` AND NOT `item`.`private`
118                                 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
119                                 AND `item`.`network` IN ('%s', '%s', '%s', '')
120                                 AND NOT `item`.`global`";
121
122                 $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 1",
123                         intval($start_id), intval($end_id),
124                         DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
125                 if (!$r) {
126                         DI::config()->set("system", "post_update_version", 1194);
127                         Logger::log("Update is done", Logger::DEBUG);
128                         return true;
129                 } else {
130                         DI::config()->set("system", "post_update_1194_start", $r[0]["id"]);
131                         $start_id = DI::config()->get("system", "post_update_1194_start");
132                 }
133
134                 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
135
136                 $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 1000,1",
137                         intval($start_id), intval($end_id),
138                         DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
139                 if ($r) {
140                         $pos_id = $r[0]["id"];
141                 } else {
142                         $pos_id = $end_id;
143                 }
144                 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
145
146                 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
147                         intval($start_id), intval($pos_id),
148                         DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
149
150                 Logger::log("Done", Logger::DEBUG);
151         }
152
153         /**
154          * update the "last-item" field in the "self" contact
155          *
156          * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
157          *
158          * @return bool "true" when the job is done
159          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
160          */
161         private static function update1206()
162         {
163                 // Was the script completed?
164                 if (DI::config()->get("system", "post_update_version") >= 1206) {
165                         return true;
166                 }
167
168                 Logger::log("Start", Logger::DEBUG);
169                 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
170                         (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
171                         FROM `user`
172                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
173
174                 if (!DBA::isResult($r)) {
175                         return false;
176                 }
177                 foreach ($r as $user) {
178                         if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
179                                 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
180                         }
181                 }
182
183                 DI::config()->set("system", "post_update_version", 1206);
184                 Logger::log("Done", Logger::DEBUG);
185                 return true;
186         }
187
188         /**
189          * update the item related tables
190          *
191          * @return bool "true" when the job is done
192          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
193          * @throws \ImagickException
194          */
195         private static function update1279()
196         {
197                 // Was the script completed?
198                 if (DI::config()->get("system", "post_update_version") >= 1279) {
199                         return true;
200                 }
201
202                 $id = DI::config()->get("system", "post_update_version_1279_id", 0);
203
204                 Logger::log("Start from item " . $id, Logger::DEBUG);
205
206                 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
207                         'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
208                         'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
209                         'inform', 'postopts', 'icid']);
210
211                 $start_id = $id;
212                 $rows = 0;
213                 $condition = ["`id` > ?", $id];
214                 $params = ['order' => ['id'], 'limit' => 10000];
215                 $items = Item::select($fields, $condition, $params);
216
217                 if (DBA::errorNo() != 0) {
218                         Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
219                         return false;
220                 }
221
222                 while ($item = Item::fetch($items)) {
223                         $id = $item['id'];
224
225                         if (empty($item['author-id'])) {
226                                 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
227                                         'photo' => $item['author-avatar'], 'network' => $item['network']];
228
229                                 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
230                         }
231
232                         if (empty($item['owner-id'])) {
233                                 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
234                                         'photo' => $item['owner-avatar'], 'network' => $item['network']];
235
236                                 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
237                         }
238
239                         if (empty($item['psid'])) {
240                                 $item['psid'] = PermissionSet::getIdFromACL(
241                                         $item['uid'],
242                                         $item['allow_cid'],
243                                         $item['allow_gid'],
244                                         $item['deny_cid'],
245                                         $item['deny_gid']
246                                 );
247                         }
248
249                         $item['allow_cid'] = null;
250                         $item['allow_gid'] = null;
251                         $item['deny_cid'] = null;
252                         $item['deny_gid'] = null;
253
254                         if ($item['post-type'] == 0) {
255                                 if (!empty($item['type']) && ($item['type'] == 'note')) {
256                                         $item['post-type'] = Item::PT_PERSONAL_NOTE;
257                                 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
258                                         $item['post-type'] = Item::PT_IMAGE;
259                                 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
260                                         $item['post-type'] = Item::PT_PAGE;
261                                 }
262                         }
263
264                         self::createLanguage($item);
265
266                         if (!empty($item['icid']) && !empty($item['language'])) {
267                                 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
268                         }
269                         unset($item['language']);
270
271                         Item::update($item, ['id' => $id]);
272
273                         ++$rows;
274                 }
275                 DBA::close($items);
276
277                 DI::config()->set("system", "post_update_version_1279_id", $id);
278
279                 Logger::log("Processed rows: " . $rows . " - last processed item:  " . $id, Logger::DEBUG);
280
281                 if ($start_id == $id) {
282                         // Set all deprecated fields to "null" if they contain an empty string
283                         $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
284                                 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
285                                 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
286                                 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
287                                 'rendered-hash', 'rendered-html'];
288                         foreach ($nullfields as $field) {
289                                 $fields = [$field => null];
290                                 $condition = [$field => ''];
291                                 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
292                                 // Important: This has to be a "DBA::update", not a "Item::update"
293                                 DBA::update('item', $fields, $condition);
294                         }
295
296                         DI::config()->set("system", "post_update_version", 1279);
297                         Logger::log("Done", Logger::DEBUG);
298                         return true;
299                 }
300
301                 return false;
302         }
303
304         private static function createLanguage(&$item)
305         {
306                 if (empty($item['postopts'])) {
307                         return;
308                 }
309
310                 $opts = explode(',', $item['postopts']);
311
312                 $postopts = [];
313
314                 foreach ($opts as $opt) {
315                         if (strstr($opt, 'lang=')) {
316                                 $language = substr($opt, 5);
317                         } else {
318                                 $postopts[] = $opt;
319                         }
320                 }
321
322                 if (empty($language)) {
323                         return;
324                 }
325
326                 if (!empty($postopts)) {
327                         $item['postopts'] = implode(',', $postopts);
328                 } else {
329                         $item['postopts'] = null;
330                 }
331
332                 $lang_pairs = explode(':', $language);
333
334                 $lang_arr = [];
335
336                 foreach ($lang_pairs as $pair) {
337                         $lang_pair_arr = explode(';', $pair);
338                         if (count($lang_pair_arr) == 2) {
339                                 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
340                         }
341                 }
342
343                 $item['language'] = json_encode($lang_arr);
344         }
345
346         /**
347          * update item-uri data. Prerequisite for the next item structure update.
348          *
349          * @return bool "true" when the job is done
350          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
351          */
352         private static function update1281()
353         {
354                 // Was the script completed?
355                 if (DI::config()->get("system", "post_update_version") >= 1281) {
356                         return true;
357                 }
358
359                 $id = DI::config()->get("system", "post_update_version_1281_id", 0);
360
361                 Logger::log("Start from item " . $id, Logger::DEBUG);
362
363                 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
364
365                 $start_id = $id;
366                 $rows = 0;
367                 $condition = ["`id` > ?", $id];
368                 $params = ['order' => ['id'], 'limit' => 10000];
369                 $items = DBA::select('item', $fields, $condition, $params);
370
371                 if (DBA::errorNo() != 0) {
372                         Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
373                         return false;
374                 }
375
376                 while ($item = DBA::fetch($items)) {
377                         $id = $item['id'];
378
379                         if (empty($item['uri'])) {
380                                 // Should not happen
381                                 continue;
382                         } elseif (empty($item['uri-id'])) {
383                                 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
384                         }
385
386                         if (empty($item['parent-uri'])) {
387                                 $item['parent-uri-id'] = $item['uri-id'];
388                         } elseif (empty($item['parent-uri-id'])) {
389                                 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
390                         }
391
392                         // Very old items don't have this field
393                         if (empty($item['thr-parent'])) {
394                                 $item['thr-parent-id'] = $item['parent-uri-id'];
395                         } elseif (empty($item['thr-parent-id'])) {
396                                 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
397                         }
398
399                         unset($item['id']);
400                         unset($item['guid']);
401                         unset($item['uri']);
402                         unset($item['parent-uri']);
403                         unset($item['thr-parent']);
404
405                         DBA::update('item', $item, ['id' => $id]);
406
407                         ++$rows;
408                 }
409                 DBA::close($items);
410
411                 DI::config()->set("system", "post_update_version_1281_id", $id);
412
413                 Logger::log("Processed rows: " . $rows . " - last processed item:  " . $id, Logger::DEBUG);
414
415                 if ($start_id == $id) {
416                         Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
417                         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");
418
419                         Logger::log("Updating item-uri in item-content", Logger::DEBUG);
420                         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");
421
422                         DI::config()->set("system", "post_update_version", 1281);
423                         Logger::log("Done", Logger::DEBUG);
424                         return true;
425                 }
426
427                 return false;
428         }
429
430         /**
431          * Set the delivery queue count to a negative value for all items preceding the feature.
432          *
433          * @return bool "true" when the job is done
434          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
435          */
436         private static function update1297()
437         {
438                 // Was the script completed?
439                 if (DI::config()->get('system', 'post_update_version') >= 1297) {
440                         return true;
441                 }
442
443                 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
444                 $max_iid = $max_item_delivery_data['iid'];
445
446                 Logger::info('Start update1297 with max iid: ' . $max_iid);
447
448                 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
449
450                 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
451
452                 if (DBA::errorNo() != 0) {
453                         Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
454                         return false;
455                 }
456
457                 Logger::info('Processed rows: ' . DBA::affectedRows());
458
459                 DI::config()->set('system', 'post_update_version', 1297);
460
461                 Logger::info('Done');
462
463                 return true;
464         }
465         /**
466          * Remove contact duplicates
467          *
468          * @return bool "true" when the job is done
469          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
470          */
471         private static function update1322()
472         {
473                 // Was the script completed?
474                 if (DI::config()->get('system', 'post_update_version') >= 1322) {
475                         return true;
476                 }
477
478                 Logger::info('Start');
479
480                 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
481                         WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
482                                 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
483                         AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
484                         Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
485                         Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
486
487                 while ($contact = DBA::fetch($contacts)) {
488                         Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
489                         Contact::removeDuplicates($contact['nurl'], $contact['uid']);
490                 }
491
492                 DBA::close($contact);
493                 DI::config()->set('system', 'post_update_version', 1322);
494
495                 Logger::info('Done');
496
497                 return true;
498         }
499
500         /**
501          * update user-item data with notifications
502          *
503          * @return bool "true" when the job is done
504          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
505          */
506         private static function update1329()
507         {
508                 // Was the script completed?
509                 if (DI::config()->get('system', 'post_update_version') >= 1329) {
510                         return true;
511                 }
512
513                 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
514
515                 Logger::info('Start', ['item' => $id]);
516
517                 $start_id = $id;
518                 $rows = 0;
519                 $condition = ["`id` > ?", $id];
520                 $params = ['order' => ['id'], 'limit' => 10000];
521                 $items = DBA::select('item', ['id'], $condition, $params);
522
523                 if (DBA::errorNo() != 0) {
524                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
525                         return false;
526                 }
527
528                 while ($item = DBA::fetch($items)) {
529                         $id = $item['id'];
530
531                         UserItem::setNotification($item['id']);
532
533                         ++$rows;
534                 }
535                 DBA::close($items);
536
537                 DI::config()->set('system', 'post_update_version_1329_id', $id);
538
539                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
540
541                 if ($start_id == $id) {
542                         DI::config()->set('system', 'post_update_version', 1329);
543                         Logger::info('Done');
544                         return true;
545                 }
546
547                 return false;
548         }
549
550         /**
551          * Fill the "tag" table with tags and mentions from the body
552          *
553          * @return bool "true" when the job is done
554          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
555          */
556         private static function update1341()
557         {
558                 // Was the script completed?
559                 if (DI::config()->get('system', 'post_update_version') >= 1341) {
560                         return true;
561                 }
562
563                 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
564
565                 Logger::info('Start', ['item' => $id]);
566
567                 $start_id = $id;
568                 $rows = 0;
569
570                 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
571                         (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
572                         ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
573
574                 if (DBA::errorNo() != 0) {
575                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
576                         return false;
577                 }
578
579                 while ($item = DBA::fetch($items)) {
580                         Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
581                         $id = $item['uri-id'];
582                         ++$rows;
583                         if ($rows % 1000 == 0) {
584                                 DI::config()->set('system', 'post_update_version_1341_id', $id);
585                         }
586                 }
587                 DBA::close($items);
588
589                 DI::config()->set('system', 'post_update_version_1341_id', $id);
590
591                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
592
593                 // When there are less than 1,000 items processed this means that we reached the end
594                 // The other entries will then be processed with the regular functionality
595                 if ($rows < 1000) {
596                         DI::config()->set('system', 'post_update_version', 1341);
597                         Logger::info('Done');
598                         return true;
599                 }
600
601                 return false;
602         }
603
604         /**
605          * Fill the "tag" table with tags and mentions from the "term" table
606          *
607          * @return bool "true" when the job is done
608          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
609          */
610         private static function update1342()
611         {
612                 // Was the script completed?
613                 if (DI::config()->get('system', 'post_update_version') >= 1342) {
614                         return true;
615                 }
616
617                 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
618
619                 Logger::info('Start', ['item' => $id]);
620
621                 $start_id = $id;
622                 $rows = 0;
623
624                 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
625                         FROM `term`
626                         INNER JOIN `item` ON `item`.`id` = `term`.`oid`
627                         INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
628                         WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
629                         Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
630
631                 if (DBA::errorNo() != 0) {
632                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
633                         return false;
634                 }
635
636                 while ($term = DBA::fetch($terms)) {
637                         if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
638                 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
639                 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
640                 if (!DBA::isResult($contact)) {
641                         $ssl_url = str_replace('http://', 'https://', $term['url']);
642                         $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
643                         $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
644                 }
645
646                 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
647                         $term['type'] = Tag::IMPLICIT_MENTION;
648                 }
649                         }
650
651                         Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
652
653                         $id = $term['tid'];
654                         ++$rows;
655                         if ($rows % 1000 == 0) {
656                                 DI::config()->set('system', 'post_update_version_1342_id', $id);
657                         }
658                 }
659                 DBA::close($terms);
660
661                 DI::config()->set('system', 'post_update_version_1342_id', $id);
662
663                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
664
665                 // When there are less than 1,000 items processed this means that we reached the end
666                 // The other entries will then be processed with the regular functionality
667                 if ($rows < 1000) {
668                         DI::config()->set('system', 'post_update_version', 1342);
669                         Logger::info('Done');
670                         return true;
671                 }
672
673                 return false;
674         }
675
676         /**
677          * Fill the "tag" table with tags and mentions from the "term" table
678          *
679          * @return bool "true" when the job is done
680          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
681          */
682         private static function update1346()
683         {
684                 // Was the script completed?
685                 if (DI::config()->get('system', 'post_update_version') >= 1346) {
686                         return true;
687                 }
688
689                 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
690
691                 Logger::info('Start', ['item' => $id]);
692
693                 $rows = 0;
694
695                 $terms = DBA::select('term', ['oid'],
696                         ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
697                         ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
698
699                 if (DBA::errorNo() != 0) {
700                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
701                         return false;
702                 }
703
704                 while ($term = DBA::fetch($terms)) {
705                         $item = Item::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
706                         if (!DBA::isResult($item)) {
707                                 continue;
708                         }
709
710                         $file = Term::fileTextFromItemId($term['oid']);
711                         if (!empty($file)) {
712                                 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
713                         }
714
715                         $id = $term['oid'];
716                         ++$rows;
717                         if ($rows % 100 == 0) {
718                                 DI::config()->set('system', 'post_update_version_1346_id', $id);
719                         }
720                 }
721                 DBA::close($terms);
722
723                 DI::config()->set('system', 'post_update_version_1346_id', $id);
724
725                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
726
727                 // When there are less than 10 items processed this means that we reached the end
728                 // The other entries will then be processed with the regular functionality
729                 if ($rows < 10) {
730                         DI::config()->set('system', 'post_update_version', 1346);
731                         Logger::info('Done');
732                         return true;
733                 }
734
735                 return false;
736         }
737 }