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