3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Console;
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;
35 * tool to move cached avatars to the avatar file cache.
37 class MoveToAvatarCache extends \Asika\SimpleConsole\Console
39 protected $helpOptions = ['h', 'help', '?'];
42 * @var $dba Friendica\Database\Database
47 * @var $baseurl Friendica\App\BaseURL
57 * @var IManageConfigValues
61 protected function getHelp()
64 console movetoavatarcache - Move all cached avatars to the file based avatar cache
66 bin/console movetoavatarcache
69 bin/console movetoavatarcache
70 Move all cached avatars to the file based avatar cache
73 -h|--help|-? Show help information
78 public function __construct(\Friendica\Database\Database $dba, BaseURL $baseUrl, L10n $l10n, IManageConfigValues $config, array $argv = null)
80 parent::__construct($argv);
83 $this->baseUrl = $baseUrl;
85 $this->config = $config;
88 protected function doExecute(): int
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.'));
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];
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'])) {
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);
112 $this->storeAvatar($resourceid, $contact, false);
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'])) {
124 $this->out(++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t", false);
125 $this->storeAvatar($photo['resource-id'], $contact, true);
130 private function storeAvatar(string $resourceid, array $contact, bool $quit_on_invalid)
132 $valid = !empty($resourceid);
134 $this->out('1', false);
135 $photo = Photo::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
137 $this->out(' ' . $this->l10n->t('no photo with id %s', $resourceid) . ' ', false);
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);
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);
161 $this->out('4', false);
162 $fields = Avatar::storeAvatarByImage($contact, $image);
164 $fields = ['photo' => '', 'thumb' => '', 'micro' => ''];
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]);
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']);