]> git.mxchange.org Git - friendica.git/blob - src/Console/MoveToAvatarCache.php
Don't move mail and feed
[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 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()
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 = ["`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                         $this->out(++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t", false);
104                         $resourceid = Photo::ridFromURI($contact['photo']);
105                         if (empty($resourceid)) {
106                                 $this->out($this->l10n->t('no resource in photo %s', $contact['photo']) . ' ', false);
107                         }
108
109                         $this->storeAvatar($resourceid, $contact, false);
110                 }
111
112                 $count  = 0;
113                 $totals = $this->dba->p("SELECT COUNT(DISTINCT(`resource-id`)) AS `total` FROM `photo` WHERE `contact-id` != ? AND `photo-type` = ?;", 0, Photo::CONTACT_AVATAR);
114                 $total  = $this->dba->fetch($totals)['total'] ?? 0;
115                 $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);
116                 while ($photo = $this->dba->fetch($photos)) {
117                         $contact = Contact::getById($photo['contact-id'], $fields);
118                         if (empty($contact) || in_array($contact['network'], [Protocol::MAIL, Protocol::FEED])) {
119                                 continue;
120                         }
121                         $this->out(++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t", false);
122                         $this->storeAvatar($photo['resource-id'], $contact, true);
123                 }
124                 return 0;
125         }
126
127         private function storeAvatar(string $resourceid, array $contact, bool $quit_on_invalid)
128         {
129                 $valid = !empty($resourceid);
130                 if ($valid) {
131                         $this->out('1', false);
132                         $photo = Photo::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
133                         if (empty($photo)) {
134                                 $this->out(' ' . $this->l10n->t('no photo with id %s', $resourceid) . ' ', false);
135                                 $valid = false;
136                         }
137                 }
138
139                 if ($valid) {
140                         $this->out('2', false);
141                         $imgdata = Photo::getImageDataForPhoto($photo);
142                         if (empty($imgdata)) {
143                                 $this->out(' ' . $this->l10n->t('no image data for photo with id %s', $resourceid) . ' ', false);
144                                 $valid = false;
145                         }
146                 }
147
148                 if ($valid) {
149                         $this->out('3', false);
150                         $image = new Image($imgdata, Images::getMimeTypeByData($imgdata));
151                         if (!$image->isValid()) {
152                                 $this->out(' ' . $this->l10n->t('invalid image for id %s', $resourceid) . ' ', false);
153                                 $valid = false;
154                         }
155                 }
156
157                 if ($valid) {
158                         $this->out('4', false);
159                         $fields = Avatar::storeAvatarByImage($contact, $image);
160                 } else {
161                         $fields = ['photo' => '', 'thumb' => '', 'micro' => ''];
162                 }
163
164                 if ($quit_on_invalid && $fields['photo'] == '') {
165                         $this->out(' ' . $this->l10n->t('Quit on invalid photo %s', $contact['avatar']));
166                         Photo::delete(['resource-id' => $resourceid]);
167                         return;
168                 }
169
170                 $this->out('5', false);
171                 Contact::update($fields, ['uri-id' => $contact['uri-id']]);
172                 $this->out('6', false);
173                 Photo::delete(['resource-id' => $resourceid]);
174                 $this->out(' ' . $fields['photo']);
175         }
176 }