]> git.mxchange.org Git - friendica.git/blob - src/Console/MoveToAvatarCache.php
18948a0e5db0aaf0cd8bd093f068e0f813764b96
[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
32 /**
33  * tool to control the list of ActivityPub relay servers from the CLI
34  *
35  * With this script you can access the relay servers of your node from
36  * the CLI.
37  */
38 class MoveToAvatarCache extends \Asika\SimpleConsole\Console
39 {
40         protected $helpOptions = ['h', 'help', '?'];
41
42         /**
43          * @var $dba Friendica\Database\Database
44          */
45         private $dba;
46
47         /**
48          * @var $baseurl Friendica\App\BaseURL
49          */
50         private $baseurl;
51
52         /**
53          * @var L10n
54          */
55         private $l10n;
56
57         protected function getHelp()
58         {
59                 $help = <<<HELP
60 console movetoavatarcache - Move all cached avatars to the file based avatar cache
61 Synopsis
62         bin/console movetoavatarcache
63
64 Description
65         bin/console movetoavatarcache
66                 Move all cached avatars to the file based avatar cache
67
68 Options
69         -h|--help|-? Show help information
70 HELP;
71                 return $help;
72         }
73
74         public function __construct(\Friendica\Database\Database $dba, BaseURL $baseurl, L10n $l10n, array $argv = null)
75         {
76                 parent::__construct($argv);
77
78                 $this->dba     = $dba;
79                 $this->baseurl = $baseurl;
80                 $this->l10n    = $l10n;
81         }
82
83         protected function doExecute()
84         {
85                 $condition = ["`avatar` != ? AND `photo` LIKE ? AND `uid` = ? AND `uri-id` != ? AND NOT `uri-id` IS NULL",
86                         '', $this->baseurl->get() . '/photo/%', 0, 0];
87
88                 $count    = 0;
89                 $total    = $this->dba->count('contact', $condition);
90                 $contacts = $this->dba->select('contact', ['id', 'avatar', 'photo', 'uri-id', 'url', 'avatar'], $condition, ['order' => ['id']]);
91                 while ($contact = $this->dba->fetch($contacts)) {
92                         echo ++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t";
93                         $resourceid = Photo::ridFromURI($contact['photo']);
94                         if (empty($resourceid)) {
95                                 echo $this->l10n->t('no resource') . "\n";
96                                 continue;
97                         }
98                         echo '1';
99                         $photo = Photo::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
100                         if (empty($photo)) {
101                                 echo $this->l10n->t('no photo') . "\n";
102                                 continue;
103                         }
104
105                         echo '2';
106                         $imgdata = Photo::getImageDataForPhoto($photo);
107                         if (empty($imgdata)) {
108                                 echo $this->l10n->t('no image data') . "\n";
109                                 continue;
110                         }
111                         echo '3';
112                         $image = new Image($imgdata, Images::getMimeTypeByData($imgdata));
113                         if (!$image->isValid()) {
114                                 echo $this->l10n->t('invalid image') . "\n";
115                                 continue;
116                         }
117
118                         echo '4';
119                         $fields = Avatar::storeAvatarByImage($contact, $image);
120                         echo '5';
121                         Contact::update($fields, ['uri-id' => $contact['uri-id']]);
122                         echo '6';
123                         Photo::delete(['resource-id' => $resourceid]);
124                         echo ' ' . $fields['photo'] . "\n";
125                 }
126
127                 return 0;
128         }
129 }