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