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