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