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