Update copyright
[friendica.git/.git] / src / Database / PostUpdate.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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\GServer;
29 use Friendica\Model\Item;
30 use Friendica\Model\ItemURI;
31 use Friendica\Model\Photo;
32 use Friendica\Model\Post;
33 use Friendica\Model\Post\Category;
34 use Friendica\Model\Tag;
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         // Needed for the helper function to read from the legacy term table
47         const OBJECT_TYPE_POST  = 1;
48         const VERSION = 1400;
49
50         /**
51          * Calls the post update functions
52          */
53         public static function update()
54         {
55                 if (!self::update1297()) {
56                         return false;
57                 }
58                 if (!self::update1322()) {
59                         return false;
60                 }
61                 if (!self::update1329()) {
62                         return false;
63                 }
64                 if (!self::update1341()) {
65                         return false;
66                 }
67                 if (!self::update1342()) {
68                         return false;
69                 }
70                 if (!self::update1345()) {
71                         return false;
72                 }
73                 if (!self::update1346()) {
74                         return false;
75                 }
76                 if (!self::update1347()) {
77                         return false;
78                 }
79                 if (!self::update1348()) {
80                         return false;
81                 }
82                 if (!self::update1349()) {
83                         return false;
84                 }
85                 if (!self::update1383()) {
86                         return false;
87                 }
88                 if (!self::update1384()) {
89                         return false;
90                 }
91                 if (!self::update1400()) {
92                         return false;
93                 }
94                 return true;
95         }
96
97         /**
98          * Set the delivery queue count to a negative value for all items preceding the feature.
99          *
100          * @return bool "true" when the job is done
101          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
102          */
103         private static function update1297()
104         {
105                 // Was the script completed?
106                 if (DI::config()->get('system', 'post_update_version') >= 1297) {
107                         return true;
108                 }
109
110                 if (!DBStructure::existsTable('item-delivery-data')) {
111                         DI::config()->set('system', 'post_update_version', 1297);
112                         return true;
113                 }
114
115                 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
116                 $max_iid = $max_item_delivery_data['iid'];
117
118                 Logger::info('Start update1297 with max iid: ' . $max_iid);
119
120                 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
121
122                 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
123
124                 if (DBA::errorNo() != 0) {
125                         Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
126                         return false;
127                 }
128
129                 Logger::info('Processed rows: ' . DBA::affectedRows());
130
131                 DI::config()->set('system', 'post_update_version', 1297);
132
133                 Logger::info('Done');
134
135                 return true;
136         }
137         /**
138          * Remove contact duplicates
139          *
140          * @return bool "true" when the job is done
141          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
142          */
143         private static function update1322()
144         {
145                 // Was the script completed?
146                 if (DI::config()->get('system', 'post_update_version') >= 1322) {
147                         return true;
148                 }
149
150                 Logger::info('Start');
151
152                 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
153                         WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
154                                 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
155                         AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
156                         Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
157                         Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
158
159                 while ($contact = DBA::fetch($contacts)) {
160                         Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
161                         Contact::removeDuplicates($contact['nurl'], $contact['uid']);
162                 }
163
164                 DBA::close($contact);
165                 DI::config()->set('system', 'post_update_version', 1322);
166
167                 Logger::info('Done');
168
169                 return true;
170         }
171
172         /**
173          * update user notification data
174          *
175          * @return bool "true" when the job is done
176          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
177          */
178         private static function update1329()
179         {
180                 // Was the script completed?
181                 if (DI::config()->get('system', 'post_update_version') >= 1329) {
182                         return true;
183                 }
184
185                 if (!DBStructure::existsTable('item')) {
186                         DI::config()->set('system', 'post_update_version', 1329);
187                         return true;
188                 }
189
190                 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
191
192                 Logger::info('Start', ['item' => $id]);
193
194                 $start_id = $id;
195                 $rows = 0;
196                 $condition = ["`id` > ?", $id];
197                 $params = ['order' => ['id'], 'limit' => 10000];
198                 $items = DBA::select('item', ['id', 'uri-id', 'uid'], $condition, $params);
199
200                 if (DBA::errorNo() != 0) {
201                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
202                         return false;
203                 }
204
205                 while ($item = DBA::fetch($items)) {
206                         $id = $item['id'];
207
208                         Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
209
210                         ++$rows;
211                 }
212                 DBA::close($items);
213
214                 DI::config()->set('system', 'post_update_version_1329_id', $id);
215
216                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
217
218                 if ($start_id == $id) {
219                         DI::config()->set('system', 'post_update_version', 1329);
220                         Logger::info('Done');
221                         return true;
222                 }
223
224                 return false;
225         }
226
227         /**
228          * Fill the "tag" table with tags and mentions from the body
229          *
230          * @return bool "true" when the job is done
231          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
232          */
233         private static function update1341()
234         {
235                 // Was the script completed?
236                 if (DI::config()->get('system', 'post_update_version') >= 1341) {
237                         return true;
238                 }
239
240                 if (!DBStructure::existsTable('item-content')) {
241                         DI::config()->set('system', 'post_update_version', 1342);
242                         return true;
243                 }
244
245                 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
246
247                 Logger::info('Start', ['item' => $id]);
248
249                 $rows = 0;
250
251                 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
252                         (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
253                         ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
254
255                 if (DBA::errorNo() != 0) {
256                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
257                         return false;
258                 }
259
260                 while ($item = DBA::fetch($items)) {
261                         Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
262                         $id = $item['uri-id'];
263                         ++$rows;
264                         if ($rows % 1000 == 0) {
265                                 DI::config()->set('system', 'post_update_version_1341_id', $id);
266                         }
267                 }
268                 DBA::close($items);
269
270                 DI::config()->set('system', 'post_update_version_1341_id', $id);
271
272                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
273
274                 // When there are less than 1,000 items processed this means that we reached the end
275                 // The other entries will then be processed with the regular functionality
276                 if ($rows < 1000) {
277                         DI::config()->set('system', 'post_update_version', 1341);
278                         Logger::info('Done');
279                         return true;
280                 }
281
282                 return false;
283         }
284
285         /**
286          * Fill the "tag" table with tags and mentions from the "term" table
287          *
288          * @return bool "true" when the job is done
289          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
290          */
291         private static function update1342()
292         {
293                 // Was the script completed?
294                 if (DI::config()->get('system', 'post_update_version') >= 1342) {
295                         return true;
296                 }
297
298                 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
299                         DI::config()->set('system', 'post_update_version', 1342);
300                         return true;
301                 }
302
303                 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
304
305                 Logger::info('Start', ['item' => $id]);
306
307                 $rows = 0;
308
309                 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
310                         FROM `term`
311                         INNER JOIN `item` ON `item`.`id` = `term`.`oid`
312                         INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
313                         WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
314                         Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
315
316                 if (DBA::errorNo() != 0) {
317                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
318                         return false;
319                 }
320
321                 while ($term = DBA::fetch($terms)) {
322                         if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
323                 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
324                 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
325                 if (!DBA::isResult($contact)) {
326                         $ssl_url = str_replace('http://', 'https://', $term['url']);
327                         $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
328                         $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
329                 }
330
331                 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
332                         $term['type'] = Tag::IMPLICIT_MENTION;
333                 }
334                         }
335
336                         Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
337
338                         $id = $term['tid'];
339                         ++$rows;
340                         if ($rows % 1000 == 0) {
341                                 DI::config()->set('system', 'post_update_version_1342_id', $id);
342                         }
343                 }
344                 DBA::close($terms);
345
346                 DI::config()->set('system', 'post_update_version_1342_id', $id);
347
348                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
349
350                 // When there are less than 1,000 items processed this means that we reached the end
351                 // The other entries will then be processed with the regular functionality
352                 if ($rows < 1000) {
353                         DI::config()->set('system', 'post_update_version', 1342);
354                         Logger::info('Done');
355                         return true;
356                 }
357
358                 return false;
359         }
360
361         /**
362          * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
363          *
364          * @return bool "true" when the job is done
365          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
366          */
367         private static function update1345()
368         {
369                 // Was the script completed?
370                 if (DI::config()->get('system', 'post_update_version') >= 1345) {
371                         return true;
372                 }
373
374                 if (!DBStructure::existsTable('item-delivery-data')) {
375                         DI::config()->set('system', 'post_update_version', 1345);
376                         return true;
377                 }
378
379                 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
380
381                 Logger::info('Start', ['item' => $id]);
382
383                 $rows = 0;
384
385                 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
386                         `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
387                         FROM `item-delivery-data`
388                         INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
389                         WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
390
391                 if (DBA::errorNo() != 0) {
392                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
393                         return false;
394                 }
395
396                 while ($delivery = DBA::fetch($deliveries)) {
397                         $id = $delivery['iid'];
398                         unset($delivery['iid']);
399                         DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
400                         ++$rows;
401                 }
402                 DBA::close($deliveries);
403
404                 DI::config()->set('system', 'post_update_version_1345_id', $id);
405
406                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
407
408                 // When there are less than 100 items processed this means that we reached the end
409                 // The other entries will then be processed with the regular functionality
410                 if ($rows < 100) {
411                         DI::config()->set('system', 'post_update_version', 1345);
412                         Logger::info('Done');
413                         return true;
414                 }
415
416                 return false;
417         }
418
419         /**
420          * Generates the legacy item.file field string from an item ID.
421          * Includes only file and category terms.
422          *
423          * @param int $item_id
424          * @return string
425          * @throws \Exception
426          */
427         private static function fileTextFromItemId($item_id)
428         {
429                 $file_text = '';
430
431                 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
432                 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
433                 foreach ($tags as $tag) {
434                         if ($tag['type'] == Category::CATEGORY) {
435                                 $file_text .= '<' . $tag['term'] . '>';
436                         } else {
437                                 $file_text .= '[' . $tag['term'] . ']';
438                         }
439                 }
440
441                 return $file_text;
442         }
443
444         /**
445          * Fill the "tag" table with tags and mentions from the "term" table
446          *
447          * @return bool "true" when the job is done
448          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
449          */
450         private static function update1346()
451         {
452                 // Was the script completed?
453                 if (DI::config()->get('system', 'post_update_version') >= 1346) {
454                         return true;
455                 }
456
457                 if (!DBStructure::existsTable('term')) {
458                         DI::config()->set('system', 'post_update_version', 1346);
459                         return true;
460                 }
461
462                 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
463
464                 Logger::info('Start', ['item' => $id]);
465
466                 $rows = 0;
467
468                 $terms = DBA::select('term', ['oid'],
469                         ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
470                         ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
471
472                 if (DBA::errorNo() != 0) {
473                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
474                         return false;
475                 }
476
477                 while ($term = DBA::fetch($terms)) {
478                         $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
479                         if (!DBA::isResult($item)) {
480                                 continue;
481                         }
482
483                         $file = self::fileTextFromItemId($term['oid']);
484                         if (!empty($file)) {
485                                 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
486                         }
487
488                         $id = $term['oid'];
489                         ++$rows;
490                         if ($rows % 100 == 0) {
491                                 DI::config()->set('system', 'post_update_version_1346_id', $id);
492                         }
493                 }
494                 DBA::close($terms);
495
496                 DI::config()->set('system', 'post_update_version_1346_id', $id);
497
498                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
499
500                 // When there are less than 10 items processed this means that we reached the end
501                 // The other entries will then be processed with the regular functionality
502                 if ($rows < 10) {
503                         DI::config()->set('system', 'post_update_version', 1346);
504                         Logger::info('Done');
505                         return true;
506                 }
507
508                 return false;
509         }
510
511         /**
512          * update the "vid" (verb) field in the item table
513          *
514          * @return bool "true" when the job is done
515          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
516          * @throws \ImagickException
517          */
518         private static function update1347()
519         {
520                 // Was the script completed?
521                 if (DI::config()->get("system", "post_update_version") >= 1347) {
522                         return true;
523                 }
524
525                 if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
526                         DI::config()->set('system', 'post_update_version', 1347);
527                         return true;
528                 }
529
530                 $id = DI::config()->get("system", "post_update_version_1347_id", 0);
531
532                 Logger::info('Start', ['item' => $id]);
533
534                 $start_id = $id;
535                 $rows = 0;
536
537                 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
538                         FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
539                         LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
540                         WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
541
542                 if (DBA::errorNo() != 0) {
543                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
544                         return false;
545                 }
546
547                 while ($item = DBA::fetch($items)) {
548                         $id = $item['id'];
549                         $verb = $item['item-verb'];
550                         if (empty($verb)) {
551                                 $verb = $item['verb'];
552                         }
553                         if (empty($verb) && is_int($item['activity'])) {
554                                 $verb = Item::ACTIVITIES[$item['activity']];
555                         }
556                         if (empty($verb)) {
557                                 continue;
558                         }
559
560                         DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
561                         ++$rows;
562                 }
563                 DBA::close($items);
564
565                 DI::config()->set("system", "post_update_version_1347_id", $id);
566
567                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
568
569                 if ($start_id == $id) {
570                         DI::config()->set("system", "post_update_version", 1347);
571                         Logger::info('Done');
572                         return true;
573                 }
574
575                 return false;
576         }
577
578         /**
579          * update the "gsid" (global server id) field in the contact table
580          *
581          * @return bool "true" when the job is done
582          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
583          * @throws \ImagickException
584          */
585         private static function update1348()
586         {
587                 // Was the script completed?
588                 if (DI::config()->get("system", "post_update_version") >= 1348) {
589                         return true;
590                 }
591
592                 $id = DI::config()->get("system", "post_update_version_1348_id", 0);
593
594                 Logger::info('Start', ['contact' => $id]);
595
596                 $start_id = $id;
597                 $rows = 0;
598                 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
599                 $params = ['order' => ['id'], 'limit' => 10000];
600                 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
601
602                 if (DBA::errorNo() != 0) {
603                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
604                         return false;
605                 }
606
607                 while ($contact = DBA::fetch($contacts)) {
608                         $id = $contact['id'];
609
610                         DBA::update('contact',
611                                 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
612                                 ['id' => $contact['id']]);
613
614                         ++$rows;
615                 }
616                 DBA::close($contacts);
617
618                 DI::config()->set("system", "post_update_version_1348_id", $id);
619
620                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
621
622                 if ($start_id == $id) {
623                         DI::config()->set("system", "post_update_version", 1348);
624                         Logger::info('Done');
625                         return true;
626                 }
627
628                 return false;
629         }
630
631         /**
632          * update the "gsid" (global server id) field in the apcontact table
633          *
634          * @return bool "true" when the job is done
635          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
636          * @throws \ImagickException
637          */
638         private static function update1349()
639         {
640                 // Was the script completed?
641                 if (DI::config()->get("system", "post_update_version") >= 1349) {
642                         return true;
643                 }
644
645                 $id = DI::config()->get("system", "post_update_version_1349_id", '');
646
647                 Logger::info('Start', ['apcontact' => $id]);
648
649                 $start_id = $id;
650                 $rows = 0;
651                 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
652                 $params = ['order' => ['url'], 'limit' => 10000];
653                 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
654
655                 if (DBA::errorNo() != 0) {
656                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
657                         return false;
658                 }
659
660                 while ($apcontact = DBA::fetch($apcontacts)) {
661                         $id = $apcontact['url'];
662
663                         DBA::update('apcontact',
664                                 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
665                                 ['url' => $apcontact['url']]);
666
667                         ++$rows;
668                 }
669                 DBA::close($apcontacts);
670
671                 DI::config()->set("system", "post_update_version_1349_id", $id);
672
673                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
674
675                 if ($start_id == $id) {
676                         DI::config()->set("system", "post_update_version", 1349);
677                         Logger::info('Done');
678                         return true;
679                 }
680
681                 return false;
682         }
683
684         /**
685          * Remove orphaned photo entries
686          *
687          * @return bool "true" when the job is done
688          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
689          * @throws \ImagickException
690          */
691         private static function update1383()
692         {
693                 // Was the script completed?
694                 if (DI::config()->get("system", "post_update_version") >= 1383) {
695                         return true;
696                 }
697
698                 Logger::info('Start');
699
700                 $deleted = 0;
701                 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
702
703                 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
704                 while ($photo = DBA::fetch($photos)) {
705                         $delete = !in_array($photo['scale'], [4, 5, 6]);
706
707                         if (!$delete) {
708                                 // Check if there is a contact entry with that photo
709                                 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
710                                         $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
711                         }
712
713                         if ($delete) {
714                                 Photo::delete(['id' => $photo['id']]);
715                                 $deleted++;
716                         }
717                 }
718                 DBA::close($photos);
719
720                 DI::config()->set("system", "post_update_version", 1383);
721                 Logger::info('Done', ['deleted' => $deleted]);
722                 return true;
723         }
724
725         /**
726          * update the "hash" field in the photo table
727          *
728          * @return bool "true" when the job is done
729          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
730          * @throws \ImagickException
731          */
732         private static function update1384()
733         {
734                 // Was the script completed?
735                 if (DI::config()->get("system", "post_update_version") >= 1384) {
736                         return true;
737                 }
738
739                 $condition = ["`hash` IS NULL"];
740                 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
741
742                 $rows = 0;
743                 $photos = DBA::select('photo', [], $condition, ['limit' => 10000]);
744
745                 if (DBA::errorNo() != 0) {
746                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
747                         return false;
748                 }
749
750                 while ($photo = DBA::fetch($photos)) {
751                         $img = Photo::getImageForPhoto($photo);
752                         if (!empty($img)) {
753                                 $md5 = md5($img->asString());
754                         } else {
755                                 $md5 = '';
756                         }
757                         DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
758                         ++$rows;
759                 }
760                 DBA::close($photos);
761
762                 Logger::info('Processed', ['rows' => $rows]);
763
764                 if ($rows <= 100) {
765                         DI::config()->set("system", "post_update_version", 1384);
766                         Logger::info('Done');
767                         return true;
768                 }
769
770                 return false;
771         }
772
773         /**
774          * update the "hash" field in the photo table
775          *
776          * @return bool "true" when the job is done
777          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
778          * @throws \ImagickException
779          */
780         private static function update1400()
781         {
782                 // Was the script completed?
783                 if (DI::config()->get("system", "post_update_version") >= 1400) {
784                         return true;
785                 }
786
787                 if (!DBStructure::existsTable('item')) {
788                         DI::config()->set("system", "post_update_version", 1400);
789                         return true;
790                 }
791
792                 $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
793                 Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
794
795                 $rows = 0;
796                 $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
797
798                 if (DBA::errorNo() != 0) {
799                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
800                         return false;
801                 }
802
803                 while ($item = DBA::fetch($items)) {
804                         Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
805                         ++$rows;
806                 }
807                 DBA::close($items);
808
809                 Logger::info('Processed', ['rows' => $rows]);
810
811                 if ($rows <= 100) {
812                         DI::config()->set("system", "post_update_version", 1400);
813                         Logger::info('Done');
814                         return true;
815                 }
816
817                 return false;
818         }
819 }