]> git.mxchange.org Git - friendica.git/blob - src/Console/MoveToAvatarCache.php
Merge remote-tracking branch 'upstream/2022.05-rc' into move-to-avatar-cache
[friendica.git] / src / Console / MoveToAvatarCache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Console;
23
24 use Friendica\App\BaseURL;
25 use Friendica\Contact\Avatar;
26 use Friendica\Core\L10n;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Photo;
29 use Friendica\Util\Images;
30 use Friendica\Object\Image;
31
32 /**
33  * tool to move cached avatars to the avatar file cache.
34  */
35 class MoveToAvatarCache extends \Asika\SimpleConsole\Console
36 {
37         protected $helpOptions = ['h', 'help', '?'];
38
39         /**
40          * @var $dba Friendica\Database\Database
41          */
42         private $dba;
43
44         /**
45          * @var $baseurl Friendica\App\BaseURL
46          */
47         private $baseurl;
48
49         /**
50          * @var L10n
51          */
52         private $l10n;
53
54         protected function getHelp()
55         {
56                 $help = <<<HELP
57 console movetoavatarcache - Move all cached avatars to the file based avatar cache
58 Synopsis
59         bin/console movetoavatarcache
60
61 Description
62         bin/console movetoavatarcache
63                 Move all cached avatars to the file based avatar cache
64
65 Options
66         -h|--help|-? Show help information
67 HELP;
68                 return $help;
69         }
70
71         public function __construct(\Friendica\Database\Database $dba, BaseURL $baseurl, L10n $l10n, array $argv = null)
72         {
73                 parent::__construct($argv);
74
75                 $this->dba     = $dba;
76                 $this->baseurl = $baseurl;
77                 $this->l10n    = $l10n;
78         }
79
80         protected function doExecute()
81         {
82                 $condition = ["`avatar` != ? AND `photo` LIKE ? AND `uid` = ? AND `uri-id` != ? AND NOT `uri-id` IS NULL",
83                         '', $this->baseurl->get() . '/photo/%', 0, 0];
84
85                 $count    = 0;
86                 $total    = $this->dba->count('contact', $condition);
87                 $contacts = $this->dba->select('contact', ['id', 'avatar', 'photo', 'uri-id', 'url', 'avatar'], $condition, ['order' => ['id']]);
88                 while ($contact = $this->dba->fetch($contacts)) {
89                         $this->out(++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t", false);
90                         $resourceid = Photo::ridFromURI($contact['photo']);
91                         if (empty($resourceid)) {
92                                 $this->out($this->l10n->t('no resource in photo %s', $contact['photo']) . ' ', false);
93                         }
94
95                         $this->storeAvatar($resourceid, $contact, false);
96                 }
97
98                 $count  = 0;
99                 $totals = $this->dba->p("SELECT COUNT(DISTINCT(`resource-id`)) AS `total` FROM `photo` WHERE `contact-id` != ? AND `photo-type` = ?;", 0, Photo::CONTACT_AVATAR);
100                 $total  = $this->dba->fetch($totals)['total'] ?? 0;
101                 $photos = $this->dba->p("SELECT `resource-id`, MAX(`contact-id`) AS `contact-id` FROM `photo` WHERE `contact-id` != ? AND `photo-type` = ? GROUP BY `resource-id`;", 0, Photo::CONTACT_AVATAR);
102                 while ($photo = $this->dba->fetch($photos)) {
103                         $contact = Contact::getById($photo['contact-id'], ['id', 'avatar', 'photo', 'uri-id', 'url', 'avatar']);
104                         if (empty($contact)) {
105                                 continue;
106                         }
107                         $this->out(++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t", false);
108                         $this->storeAvatar($photo['resource-id'], $contact, true);
109                 }
110                 return 0;
111         }
112
113         private function storeAvatar(string $resourceid, array $contact, bool $quit_on_invalid)
114         {
115                 $valid = !empty($resourceid);
116                 if ($valid) {
117                         $this->out('1', false);
118                         $photo = Photo::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
119                         if (empty($photo)) {
120                                 $this->out(' ' . $this->l10n->t('no photo with id %s', $resourceid) . ' ', false);
121                                 $valid = false;
122                         }
123                 }
124
125                 if ($valid) {
126                         $this->out('2', false);
127                         $imgdata = Photo::getImageDataForPhoto($photo);
128                         if (empty($imgdata)) {
129                                 $this->out(' ' . $this->l10n->t('no image data for photo with id %s', $resourceid) . ' ', false);
130                                 $valid = false;
131                         }
132                 }
133
134                 if ($valid) {
135                         $this->out('3', false);
136                         $image = new Image($imgdata, Images::getMimeTypeByData($imgdata));
137                         if (!$image->isValid()) {
138                                 $this->out(' ' . $this->l10n->t('invalid image for id %s', $resourceid) . ' ', false);
139                                 $valid = false;
140                         }
141                 }
142
143                 if ($valid) {
144                         $this->out('4', false);
145                         $fields = Avatar::storeAvatarByImage($contact, $image);
146                 } else {
147                         $fields = ['photo' => '', 'thumb' => '', 'micro' => ''];
148                 }
149
150                 if ($quit_on_invalid && $fields['photo'] == '') {
151                         $this->out(' ' . $this->l10n->t('Quit on invalid photo %s', $contact['avatar']));
152                         Photo::delete(['resource-id' => $resourceid]);
153                         return;
154                 }
155
156                 $this->out('5', false);
157                 Contact::update($fields, ['uri-id' => $contact['uri-id']]);
158                 $this->out('6', false);
159                 Photo::delete(['resource-id' => $resourceid]);
160                 $this->out(' ' . $fields['photo']);
161         }
162 }