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