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;
34 * tool to move cached avatars to the avatar file cache.
36 class MoveToAvatarCache extends \Asika\SimpleConsole\Console
38 protected $helpOptions = ['h', 'help', '?'];
41 * @var $dba Friendica\Database\Database
46 * @var $baseurl Friendica\App\BaseURL
56 * @var IManageConfigValues
60 protected function getHelp()
63 console movetoavatarcache - Move all cached avatars to the file based avatar cache
65 bin/console movetoavatarcache
68 bin/console movetoavatarcache
69 Move all cached avatars to the file based avatar cache
72 -h|--help|-? Show help information
77 public function __construct(\Friendica\Database\Database $dba, BaseURL $baseurl, L10n $l10n, IManageConfigValues $config, array $argv = null)
79 parent::__construct($argv);
82 $this->baseurl = $baseurl;
84 $this->config = $config;
87 protected function doExecute()
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.'));
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];
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);
108 $this->storeAvatar($resourceid, $contact, false);
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)) {
120 $this->out(++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t", false);
121 $this->storeAvatar($photo['resource-id'], $contact, true);
126 private function storeAvatar(string $resourceid, array $contact, bool $quit_on_invalid)
128 $valid = !empty($resourceid);
130 $this->out('1', false);
131 $photo = Photo::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
133 $this->out(' ' . $this->l10n->t('no photo with id %s', $resourceid) . ' ', false);
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);
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);
157 $this->out('4', false);
158 $fields = Avatar::storeAvatarByImage($contact, $image);
160 $fields = ['photo' => '', 'thumb' => '', 'micro' => ''];
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]);
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']);