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;
33 * tool to move cached avatars to the avatar file cache.
35 class MoveToAvatarCache extends \Asika\SimpleConsole\Console
37 protected $helpOptions = ['h', 'help', '?'];
40 * @var $dba Friendica\Database\Database
45 * @var $baseurl Friendica\App\BaseURL
54 protected function getHelp()
57 console movetoavatarcache - Move all cached avatars to the file based avatar cache
59 bin/console movetoavatarcache
62 bin/console movetoavatarcache
63 Move all cached avatars to the file based avatar cache
66 -h|--help|-? Show help information
71 public function __construct(\Friendica\Database\Database $dba, BaseURL $baseurl, L10n $l10n, array $argv = null)
73 parent::__construct($argv);
76 $this->baseurl = $baseurl;
80 protected function doExecute()
82 $condition = ["`avatar` != ? AND `photo` LIKE ? AND `uid` = ? AND `uri-id` != ? AND NOT `uri-id` IS NULL",
83 '', $this->baseurl->get() . '/photo/%', 0, 0];
86 $total = $this->dba->count('contact', $condition);
87 $contacts = $this->dba->select('contact', ['id', 'avatar', 'photo', 'uri-id', 'url', 'avatar'], $condition, ['order' => ['id']]);
88 while ($contact = $this->dba->fetch($contacts)) {
89 echo ++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t";
90 $resourceid = Photo::ridFromURI($contact['photo']);
91 if (empty($resourceid)) {
92 echo $this->l10n->t('no resource in photo %1', $contact['photo']) . "\n";
96 $photo = Photo::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
98 echo $this->l10n->t('no photo with id %1', $resourceid) . "\n";
103 $imgdata = Photo::getImageDataForPhoto($photo);
104 if (empty($imgdata)) {
105 echo $this->l10n->t('no image data for photo with id %1', $resourceid) . "\n";
109 $image = new Image($imgdata, Images::getMimeTypeByData($imgdata));
110 if (!$image->isValid()) {
111 echo $this->l10n->t('invalid image for id %1', $resourceid) . "\n";
116 $fields = Avatar::storeAvatarByImage($contact, $image);
118 Contact::update($fields, ['uri-id' => $contact['uri-id']]);
120 Photo::delete(['resource-id' => $resourceid]);
121 echo ' ' . $fields['photo'] . "\n";