]> git.mxchange.org Git - friendica.git/blobdiff - src/Console/MoveToAvatarCache.php
Merge pull request #11568 from annando/add-timestamp
[friendica.git] / src / Console / MoveToAvatarCache.php
index 3852f0e979ba68d0e661f1d01ab165c801ab401f..a10997620b93251789179f54e5318021d9645fe0 100644 (file)
@@ -28,6 +28,8 @@ use Friendica\Model\Contact;
 use Friendica\Model\Photo;
 use Friendica\Util\Images;
 use Friendica\Object\Image;
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Protocol;
 
 /**
  * tool to move cached avatars to the avatar file cache.
@@ -51,6 +53,11 @@ class MoveToAvatarCache extends \Asika\SimpleConsole\Console
         */
        private $l10n;
 
+       /**
+        * @var IManageConfigValues
+        */
+       private $config;
+
        protected function getHelp()
        {
                $help = <<<HELP
@@ -68,59 +75,102 @@ HELP;
                return $help;
        }
 
-       public function __construct(\Friendica\Database\Database $dba, BaseURL $baseurl, L10n $l10n, array $argv = null)
+       public function __construct(\Friendica\Database\Database $dba, BaseURL $baseurl, L10n $l10n, IManageConfigValues $config, array $argv = null)
        {
                parent::__construct($argv);
 
                $this->dba     = $dba;
                $this->baseurl = $baseurl;
                $this->l10n    = $l10n;
+               $this->config = $config;
        }
 
        protected function doExecute()
        {
-               $condition = ["`avatar` != ? AND `photo` LIKE ? AND `uid` = ? AND `uri-id` != ? AND NOT `uri-id` IS NULL",
-                       '', $this->baseurl->get() . '/photo/%', 0, 0];
+               if (!$this->config->get('system', 'avatar_cache')) {
+                       $this->err($this->l10n->t('The avatar cache needs to be enabled to use this command.'));
+                       return 2;
+               }
+
+               $fields = ['id', 'avatar', 'photo', 'thumb', 'micro', 'uri-id', 'url', 'avatar', 'network'];
+               $condition = ["`avatar` != ? AND `photo` LIKE ? AND `uid` = ? AND `uri-id` != ? AND NOT `uri-id` IS NULL AND NOT `network` IN (?, ?)",
+                       '', $this->baseurl->get() . '/photo/%', 0, 0, Protocol::MAIL, Protocol::FEED];
 
                $count    = 0;
                $total    = $this->dba->count('contact', $condition);
-               $contacts = $this->dba->select('contact', ['id', 'avatar', 'photo', 'uri-id', 'url', 'avatar'], $condition, ['order' => ['id']]);
+               $contacts = $this->dba->select('contact', $fields, $condition, ['order' => ['id']]);
                while ($contact = $this->dba->fetch($contacts)) {
-                       echo ++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t";
+                       $this->out(++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t", false);
                        $resourceid = Photo::ridFromURI($contact['photo']);
                        if (empty($resourceid)) {
-                               echo $this->l10n->t('no resource') . "\n";
+                               $this->out($this->l10n->t('no resource in photo %s', $contact['photo']) . ' ', false);
+                       }
+
+                       $this->storeAvatar($resourceid, $contact, false);
+               }
+
+               $count  = 0;
+               $totals = $this->dba->p("SELECT COUNT(DISTINCT(`resource-id`)) AS `total` FROM `photo` WHERE `contact-id` != ? AND `photo-type` = ?;", 0, Photo::CONTACT_AVATAR);
+               $total  = $this->dba->fetch($totals)['total'] ?? 0;
+               $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);
+               while ($photo = $this->dba->fetch($photos)) {
+                       $contact = Contact::getById($photo['contact-id'], $fields);
+                       if (empty($contact) || in_array($contact['network'], [Protocol::MAIL, Protocol::FEED])) {
                                continue;
                        }
-                       echo '1';
+                       $this->out(++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t", false);
+                       $this->storeAvatar($photo['resource-id'], $contact, true);
+               }
+               return 0;
+       }
+
+       private function storeAvatar(string $resourceid, array $contact, bool $quit_on_invalid)
+       {
+               $valid = !empty($resourceid);
+               if ($valid) {
+                       $this->out('1', false);
                        $photo = Photo::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
                        if (empty($photo)) {
-                               echo $this->l10n->t('no photo') . "\n";
-                               continue;
+                               $this->out(' ' . $this->l10n->t('no photo with id %s', $resourceid) . ' ', false);
+                               $valid = false;
                        }
+               }
 
-                       echo '2';
+               if ($valid) {
+                       $this->out('2', false);
                        $imgdata = Photo::getImageDataForPhoto($photo);
                        if (empty($imgdata)) {
-                               echo $this->l10n->t('no image data') . "\n";
-                               continue;
+                               $this->out(' ' . $this->l10n->t('no image data for photo with id %s', $resourceid) . ' ', false);
+                               $valid = false;
                        }
-                       echo '3';
+               }
+
+               if ($valid) {
+                       $this->out('3', false);
                        $image = new Image($imgdata, Images::getMimeTypeByData($imgdata));
                        if (!$image->isValid()) {
-                               echo $this->l10n->t('invalid image') . "\n";
-                               continue;
+                               $this->out(' ' . $this->l10n->t('invalid image for id %s', $resourceid) . ' ', false);
+                               $valid = false;
                        }
+               }
 
-                       echo '4';
+               if ($valid) {
+                       $this->out('4', false);
                        $fields = Avatar::storeAvatarByImage($contact, $image);
-                       echo '5';
-                       Contact::update($fields, ['uri-id' => $contact['uri-id']]);
-                       echo '6';
+               } else {
+                       $fields = ['photo' => '', 'thumb' => '', 'micro' => ''];
+               }
+
+               if ($quit_on_invalid && $fields['photo'] == '') {
+                       $this->out(' ' . $this->l10n->t('Quit on invalid photo %s', $contact['avatar']));
                        Photo::delete(['resource-id' => $resourceid]);
-                       echo ' ' . $fields['photo'] . "\n";
+                       return;
                }
 
-               return 0;
+               $this->out('5', false);
+               Contact::update($fields, ['uri-id' => $contact['uri-id']]);
+               $this->out('6', false);
+               Photo::delete(['resource-id' => $resourceid]);
+               $this->out(' ' . $fields['photo']);
        }
 }