]> git.mxchange.org Git - friendica.git/blob - src/Database/PostUpdate.php
Merge pull request #9889 from MrPetovan/task/9872-item-ignore
[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                 if (!DBStructure::existsTable('item-content')) {
234                         DI::config()->set('system', 'post_update_version', 1342);
235                         return true;
236                 }
237
238                 $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
239
240                 Logger::info('Start', ['item' => $id]);
241
242                 $rows = 0;
243
244                 $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
245                         (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
246                         ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
247
248                 if (DBA::errorNo() != 0) {
249                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
250                         return false;
251                 }
252
253                 while ($item = DBA::fetch($items)) {
254                         Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
255                         $id = $item['uri-id'];
256                         ++$rows;
257                         if ($rows % 1000 == 0) {
258                                 DI::config()->set('system', 'post_update_version_1341_id', $id);
259                         }
260                 }
261                 DBA::close($items);
262
263                 DI::config()->set('system', 'post_update_version_1341_id', $id);
264
265                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
266
267                 // When there are less than 1,000 items processed this means that we reached the end
268                 // The other entries will then be processed with the regular functionality
269                 if ($rows < 1000) {
270                         DI::config()->set('system', 'post_update_version', 1341);
271                         Logger::info('Done');
272                         return true;
273                 }
274
275                 return false;
276         }
277
278         /**
279          * Fill the "tag" table with tags and mentions from the "term" table
280          *
281          * @return bool "true" when the job is done
282          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
283          */
284         private static function update1342()
285         {
286                 // Was the script completed?
287                 if (DI::config()->get('system', 'post_update_version') >= 1342) {
288                         return true;
289                 }
290
291                 if (!DBStructure::existsTable('term') || !DBStructure::existsTable('item-content')) {
292                         DI::config()->set('system', 'post_update_version', 1342);
293                         return true;
294                 }
295
296                 $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
297
298                 Logger::info('Start', ['item' => $id]);
299
300                 $rows = 0;
301
302                 $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
303                         FROM `term`
304                         INNER JOIN `item` ON `item`.`id` = `term`.`oid`
305                         INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
306                         WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
307                         Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
308
309                 if (DBA::errorNo() != 0) {
310                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
311                         return false;
312                 }
313
314                 while ($term = DBA::fetch($terms)) {
315                         if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
316                 $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
317                 $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
318                 if (!DBA::isResult($contact)) {
319                         $ssl_url = str_replace('http://', 'https://', $term['url']);
320                         $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
321                         $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
322                 }
323
324                 if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
325                         $term['type'] = Tag::IMPLICIT_MENTION;
326                 }
327                         }
328
329                         Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
330
331                         $id = $term['tid'];
332                         ++$rows;
333                         if ($rows % 1000 == 0) {
334                                 DI::config()->set('system', 'post_update_version_1342_id', $id);
335                         }
336                 }
337                 DBA::close($terms);
338
339                 DI::config()->set('system', 'post_update_version_1342_id', $id);
340
341                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
342
343                 // When there are less than 1,000 items processed this means that we reached the end
344                 // The other entries will then be processed with the regular functionality
345                 if ($rows < 1000) {
346                         DI::config()->set('system', 'post_update_version', 1342);
347                         Logger::info('Done');
348                         return true;
349                 }
350
351                 return false;
352         }
353
354         /**
355          * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
356          *
357          * @return bool "true" when the job is done
358          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
359          */
360         private static function update1345()
361         {
362                 // Was the script completed?
363                 if (DI::config()->get('system', 'post_update_version') >= 1345) {
364                         return true;
365                 }
366
367                 if (!DBStructure::existsTable('item-delivery-data')) {
368                         DI::config()->set('system', 'post_update_version', 1345);
369                         return true;
370                 }
371
372                 $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
373
374                 Logger::info('Start', ['item' => $id]);
375
376                 $rows = 0;
377
378                 $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
379                         `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
380                         FROM `item-delivery-data`
381                         INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
382                         WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
383
384                 if (DBA::errorNo() != 0) {
385                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
386                         return false;
387                 }
388
389                 while ($delivery = DBA::fetch($deliveries)) {
390                         $id = $delivery['iid'];
391                         unset($delivery['iid']);
392                         DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
393                         ++$rows;
394                 }
395                 DBA::close($deliveries);
396
397                 DI::config()->set('system', 'post_update_version_1345_id', $id);
398
399                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
400
401                 // When there are less than 100 items processed this means that we reached the end
402                 // The other entries will then be processed with the regular functionality
403                 if ($rows < 100) {
404                         DI::config()->set('system', 'post_update_version', 1345);
405                         Logger::info('Done');
406                         return true;
407                 }
408
409                 return false;
410         }
411
412         /**
413          * Generates the legacy item.file field string from an item ID.
414          * Includes only file and category terms.
415          *
416          * @param int $item_id
417          * @return string
418          * @throws \Exception
419          */
420         private static function fileTextFromItemId($item_id)
421         {
422                 $file_text = '';
423
424                 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
425                 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
426                 foreach ($tags as $tag) {
427                         if ($tag['type'] == Category::CATEGORY) {
428                                 $file_text .= '<' . $tag['term'] . '>';
429                         } else {
430                                 $file_text .= '[' . $tag['term'] . ']';
431                         }
432                 }
433
434                 return $file_text;
435         }
436
437         /**
438          * Fill the "tag" table with tags and mentions from the "term" table
439          *
440          * @return bool "true" when the job is done
441          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
442          */
443         private static function update1346()
444         {
445                 // Was the script completed?
446                 if (DI::config()->get('system', 'post_update_version') >= 1346) {
447                         return true;
448                 }
449
450                 if (!DBStructure::existsTable('term')) {
451                         DI::config()->set('system', 'post_update_version', 1346);
452                         return true;
453                 }
454
455                 $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
456
457                 Logger::info('Start', ['item' => $id]);
458
459                 $rows = 0;
460
461                 $terms = DBA::select('term', ['oid'],
462                         ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
463                         ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
464
465                 if (DBA::errorNo() != 0) {
466                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
467                         return false;
468                 }
469
470                 while ($term = DBA::fetch($terms)) {
471                         $item = Post::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
472                         if (!DBA::isResult($item)) {
473                                 continue;
474                         }
475
476                         $file = self::fileTextFromItemId($term['oid']);
477                         if (!empty($file)) {
478                                 Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
479                         }
480
481                         $id = $term['oid'];
482                         ++$rows;
483                         if ($rows % 100 == 0) {
484                                 DI::config()->set('system', 'post_update_version_1346_id', $id);
485                         }
486                 }
487                 DBA::close($terms);
488
489                 DI::config()->set('system', 'post_update_version_1346_id', $id);
490
491                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
492
493                 // When there are less than 10 items processed this means that we reached the end
494                 // The other entries will then be processed with the regular functionality
495                 if ($rows < 10) {
496                         DI::config()->set('system', 'post_update_version', 1346);
497                         Logger::info('Done');
498                         return true;
499                 }
500
501                 return false;
502         }
503
504         /**
505          * update the "vid" (verb) field in the item table
506          *
507          * @return bool "true" when the job is done
508          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
509          * @throws \ImagickException
510          */
511         private static function update1347()
512         {
513                 // Was the script completed?
514                 if (DI::config()->get("system", "post_update_version") >= 1347) {
515                         return true;
516                 }
517
518                 if (!DBStructure::existsTable('item-activity')) {
519                         DI::config()->set('system', 'post_update_version', 1347);
520                         return true;
521                 }
522
523                 $id = DI::config()->get("system", "post_update_version_1347_id", 0);
524
525                 Logger::info('Start', ['item' => $id]);
526
527                 $start_id = $id;
528                 $rows = 0;
529
530                 $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
531                         FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
532                         LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
533                         WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
534
535                 if (DBA::errorNo() != 0) {
536                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
537                         return false;
538                 }
539
540                 while ($item = DBA::fetch($items)) {
541                         $id = $item['id'];
542                         $verb = $item['item-verb'];
543                         if (empty($verb)) {
544                                 $verb = $item['verb'];
545                         }
546                         if (empty($verb) && is_int($item['activity'])) {
547                                 $verb = Item::ACTIVITIES[$item['activity']];
548                         }
549                         if (empty($verb)) {
550                                 continue;
551                         }
552
553                         DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
554                         ++$rows;
555                 }
556                 DBA::close($items);
557
558                 DI::config()->set("system", "post_update_version_1347_id", $id);
559
560                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
561
562                 if ($start_id == $id) {
563                         DI::config()->set("system", "post_update_version", 1347);
564                         Logger::info('Done');
565                         return true;
566                 }
567
568                 return false;
569         }
570
571         /**
572          * update the "gsid" (global server id) field in the contact table
573          *
574          * @return bool "true" when the job is done
575          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
576          * @throws \ImagickException
577          */
578         private static function update1348()
579         {
580                 // Was the script completed?
581                 if (DI::config()->get("system", "post_update_version") >= 1348) {
582                         return true;
583                 }
584
585                 $id = DI::config()->get("system", "post_update_version_1348_id", 0);
586
587                 Logger::info('Start', ['contact' => $id]);
588
589                 $start_id = $id;
590                 $rows = 0;
591                 $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
592                 $params = ['order' => ['id'], 'limit' => 10000];
593                 $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
594
595                 if (DBA::errorNo() != 0) {
596                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
597                         return false;
598                 }
599
600                 while ($contact = DBA::fetch($contacts)) {
601                         $id = $contact['id'];
602
603                         DBA::update('contact',
604                                 ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
605                                 ['id' => $contact['id']]);
606
607                         ++$rows;
608                 }
609                 DBA::close($contacts);
610
611                 DI::config()->set("system", "post_update_version_1348_id", $id);
612
613                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
614
615                 if ($start_id == $id) {
616                         DI::config()->set("system", "post_update_version", 1348);
617                         Logger::info('Done');
618                         return true;
619                 }
620
621                 return false;
622         }
623
624         /**
625          * update the "gsid" (global server id) field in the apcontact table
626          *
627          * @return bool "true" when the job is done
628          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
629          * @throws \ImagickException
630          */
631         private static function update1349()
632         {
633                 // Was the script completed?
634                 if (DI::config()->get("system", "post_update_version") >= 1349) {
635                         return true;
636                 }
637
638                 $id = DI::config()->get("system", "post_update_version_1349_id", '');
639
640                 Logger::info('Start', ['apcontact' => $id]);
641
642                 $start_id = $id;
643                 $rows = 0;
644                 $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
645                 $params = ['order' => ['url'], 'limit' => 10000];
646                 $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
647
648                 if (DBA::errorNo() != 0) {
649                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
650                         return false;
651                 }
652
653                 while ($apcontact = DBA::fetch($apcontacts)) {
654                         $id = $apcontact['url'];
655
656                         DBA::update('apcontact',
657                                 ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
658                                 ['url' => $apcontact['url']]);
659
660                         ++$rows;
661                 }
662                 DBA::close($apcontacts);
663
664                 DI::config()->set("system", "post_update_version_1349_id", $id);
665
666                 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
667
668                 if ($start_id == $id) {
669                         DI::config()->set("system", "post_update_version", 1349);
670                         Logger::info('Done');
671                         return true;
672                 }
673
674                 return false;
675         }
676
677         /**
678          * Remove orphaned photo entries
679          *
680          * @return bool "true" when the job is done
681          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
682          * @throws \ImagickException
683          */
684         private static function update1383()
685         {
686                 // Was the script completed?
687                 if (DI::config()->get("system", "post_update_version") >= 1383) {
688                         return true;
689                 }
690
691                 Logger::info('Start');
692
693                 $deleted = 0;
694                 $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
695
696                 $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
697                 while ($photo = DBA::fetch($photos)) {
698                         $delete = !in_array($photo['scale'], [4, 5, 6]);
699
700                         if (!$delete) {
701                                 // Check if there is a contact entry with that photo
702                                 $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
703                                         $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
704                         }
705
706                         if ($delete) {
707                                 Photo::delete(['id' => $photo['id']]);
708                                 $deleted++;
709                         }
710                 }
711                 DBA::close($photos);
712
713                 DI::config()->set("system", "post_update_version", 1383);
714                 Logger::info('Done', ['deleted' => $deleted]);
715                 return true;
716         }
717
718         /**
719          * update the "hash" field in the photo table
720          *
721          * @return bool "true" when the job is done
722          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
723          * @throws \ImagickException
724          */
725         private static function update1384()
726         {
727                 // Was the script completed?
728                 if (DI::config()->get("system", "post_update_version") >= 1384) {
729                         return true;
730                 }
731
732                 $condition = ["`hash` IS NULL"];
733                 Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
734
735                 $rows = 0;
736                 $photos = DBA::select('photo', [], $condition, ['limit' => 10000]);
737
738                 if (DBA::errorNo() != 0) {
739                         Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
740                         return false;
741                 }
742
743                 while ($photo = DBA::fetch($photos)) {
744                         $img = Photo::getImageForPhoto($photo);
745                         if (!empty($img)) {
746                                 $md5 = md5($img->asString());
747                         } else {
748                                 $md5 = '';
749                         }
750                         DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
751                         ++$rows;
752                 }
753                 DBA::close($photos);
754
755                 Logger::info('Processed', ['rows' => $rows]);
756
757                 if ($rows <= 100) {
758                         DI::config()->set("system", "post_update_version", 1384);
759                         Logger::info('Done');
760                         return true;
761                 }
762
763                 return false;
764         }
765 }