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