2022-05-23 08:15:30 -04:00
< ? php
/**
2023-01-01 09:36:24 -05:00
* @ copyright Copyright ( C ) 2010 - 2023 , the Friendica project
2022-05-23 08:15:30 -04:00
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*
*/
namespace Friendica\Console ;
use Friendica\App\BaseURL ;
use Friendica\Contact\Avatar ;
use Friendica\Core\L10n ;
use Friendica\Model\Contact ;
use Friendica\Model\Photo ;
use Friendica\Util\Images ;
use Friendica\Object\Image ;
2022-05-26 15:06:16 -04:00
use Friendica\Core\Config\Capability\IManageConfigValues ;
2022-05-27 02:32:19 -04:00
use Friendica\Core\Protocol ;
2022-05-23 08:15:30 -04:00
/**
2022-05-24 01:58:14 -04:00
* tool to move cached avatars to the avatar file cache .
2022-05-23 08:15:30 -04:00
*/
class MoveToAvatarCache extends \Asika\SimpleConsole\Console
{
protected $helpOptions = [ 'h' , 'help' , '?' ];
/**
* @ var $dba Friendica\Database\Database
*/
private $dba ;
/**
* @ var $baseurl Friendica\App\BaseURL
*/
2022-08-13 16:42:30 -04:00
private $baseUrl ;
2022-05-23 08:15:30 -04:00
/**
* @ var L10n
*/
private $l10n ;
2022-05-26 15:06:16 -04:00
/**
* @ var IManageConfigValues
*/
private $config ;
2022-05-23 08:15:30 -04:00
protected function getHelp ()
{
$help = <<< HELP
console movetoavatarcache - Move all cached avatars to the file based avatar cache
Synopsis
bin / console movetoavatarcache
Description
bin / console movetoavatarcache
Move all cached avatars to the file based avatar cache
Options
2022-05-23 08:35:44 -04:00
- h |-- help |- ? Show help information
2022-05-23 08:15:30 -04:00
HELP ;
return $help ;
}
2022-08-13 16:42:30 -04:00
public function __construct ( \Friendica\Database\Database $dba , BaseURL $baseUrl , L10n $l10n , IManageConfigValues $config , array $argv = null )
2022-05-23 08:15:30 -04:00
{
parent :: __construct ( $argv );
$this -> dba = $dba ;
2022-08-13 16:42:30 -04:00
$this -> baseUrl = $baseUrl ;
2022-05-23 08:15:30 -04:00
$this -> l10n = $l10n ;
2022-05-26 15:06:16 -04:00
$this -> config = $config ;
2022-05-23 08:15:30 -04:00
}
2022-07-13 15:09:49 -04:00
protected function doExecute () : int
2022-05-23 08:15:30 -04:00
{
2022-05-27 02:32:19 -04:00
if ( ! $this -> config -> get ( 'system' , 'avatar_cache' )) {
2022-05-26 15:06:16 -04:00
$this -> err ( $this -> l10n -> t ( 'The avatar cache needs to be enabled to use this command.' ));
return 2 ;
}
2022-05-27 02:32:19 -04:00
$fields = [ 'id' , 'avatar' , 'photo' , 'thumb' , 'micro' , 'uri-id' , 'url' , 'avatar' , 'network' ];
2022-05-29 02:50:34 -04:00
$condition = [ " NOT `self` AND `avatar` != ? AND `photo` LIKE ? AND `uid` = ? AND `uri-id` != ? AND NOT `uri-id` IS NULL AND NOT `network` IN (?, ?) " ,
2023-02-18 14:57:30 -05:00
'' , $this -> baseUrl . '/photo/%' , 0 , 0 , Protocol :: MAIL , Protocol :: FEED ];
2022-05-23 08:33:25 -04:00
$count = 0 ;
$total = $this -> dba -> count ( 'contact' , $condition );
2022-05-26 15:06:16 -04:00
$contacts = $this -> dba -> select ( 'contact' , $fields , $condition , [ 'order' => [ 'id' ]]);
2022-05-23 08:15:30 -04:00
while ( $contact = $this -> dba -> fetch ( $contacts )) {
2022-05-29 02:50:34 -04:00
if ( Contact :: isLocal ( $contact [ 'url' ])) {
continue ;
}
2022-05-25 01:24:31 -04:00
$this -> out ( ++ $count . '/' . $total . " \t " . $contact [ 'id' ] . " \t " . $contact [ 'url' ] . " \t " , false );
2022-05-23 08:15:30 -04:00
$resourceid = Photo :: ridFromURI ( $contact [ 'photo' ]);
if ( empty ( $resourceid )) {
2022-05-25 01:24:31 -04:00
$this -> out ( $this -> l10n -> t ( 'no resource in photo %s' , $contact [ 'photo' ]) . ' ' , false );
2022-05-23 08:15:30 -04:00
}
2022-05-24 13:49:29 -04:00
2022-05-24 15:47:49 -04:00
$this -> storeAvatar ( $resourceid , $contact , false );
2022-05-24 15:41:38 -04:00
}
$count = 0 ;
2022-05-24 16:32:30 -04:00
$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 ;
2022-05-24 15:41:38 -04:00
$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 )) {
2022-05-26 15:06:16 -04:00
$contact = Contact :: getById ( $photo [ 'contact-id' ], $fields );
2022-05-29 02:50:34 -04:00
if ( empty ( $contact ) || in_array ( $contact [ 'network' ], [ Protocol :: MAIL , Protocol :: FEED ]) || Contact :: isLocal ( $contact [ 'url' ])) {
2022-05-24 15:41:38 -04:00
continue ;
2022-05-24 13:49:29 -04:00
}
2022-05-25 01:24:31 -04:00
$this -> out ( ++ $count . '/' . $total . " \t " . $contact [ 'id' ] . " \t " . $contact [ 'url' ] . " \t " , false );
2022-05-24 15:47:49 -04:00
$this -> storeAvatar ( $photo [ 'resource-id' ], $contact , true );
2022-05-24 15:41:38 -04:00
}
return 0 ;
}
2022-05-24 13:49:29 -04:00
2022-05-24 15:47:49 -04:00
private function storeAvatar ( string $resourceid , array $contact , bool $quit_on_invalid )
2022-05-24 15:41:38 -04:00
{
$valid = ! empty ( $resourceid );
if ( $valid ) {
2022-05-25 01:24:31 -04:00
$this -> out ( '1' , false );
2022-05-24 15:41:38 -04:00
$photo = Photo :: selectFirst ([], [ 'resource-id' => $resourceid ], [ 'order' => [ 'scale' ]]);
if ( empty ( $photo )) {
2022-05-25 01:24:31 -04:00
$this -> out ( ' ' . $this -> l10n -> t ( 'no photo with id %s' , $resourceid ) . ' ' , false );
2022-05-24 15:41:38 -04:00
$valid = false ;
2022-05-23 08:15:30 -04:00
}
2022-05-24 15:41:38 -04:00
}
2022-05-23 08:33:25 -04:00
2022-05-24 15:41:38 -04:00
if ( $valid ) {
2022-05-25 01:24:31 -04:00
$this -> out ( '2' , false );
2022-05-24 15:41:38 -04:00
$imgdata = Photo :: getImageDataForPhoto ( $photo );
if ( empty ( $imgdata )) {
2022-05-25 01:24:31 -04:00
$this -> out ( ' ' . $this -> l10n -> t ( 'no image data for photo with id %s' , $resourceid ) . ' ' , false );
2022-05-24 15:41:38 -04:00
$valid = false ;
2022-05-23 08:15:30 -04:00
}
2022-05-24 15:41:38 -04:00
}
2022-05-24 13:49:29 -04:00
2022-05-24 15:41:38 -04:00
if ( $valid ) {
2022-05-25 01:24:31 -04:00
$this -> out ( '3' , false );
2022-05-24 15:41:38 -04:00
$image = new Image ( $imgdata , Images :: getMimeTypeByData ( $imgdata ));
if ( ! $image -> isValid ()) {
2022-05-25 01:24:31 -04:00
$this -> out ( ' ' . $this -> l10n -> t ( 'invalid image for id %s' , $resourceid ) . ' ' , false );
2022-05-24 15:41:38 -04:00
$valid = false ;
2022-05-23 08:15:30 -04:00
}
2022-05-24 15:41:38 -04:00
}
2022-05-23 08:33:25 -04:00
2022-05-24 15:41:38 -04:00
if ( $valid ) {
2022-05-25 01:24:31 -04:00
$this -> out ( '4' , false );
2022-05-24 15:41:38 -04:00
$fields = Avatar :: storeAvatarByImage ( $contact , $image );
} else {
$fields = [ 'photo' => '' , 'thumb' => '' , 'micro' => '' ];
2022-05-23 08:15:30 -04:00
}
2022-05-23 08:33:25 -04:00
2022-05-24 15:47:49 -04:00
if ( $quit_on_invalid && $fields [ 'photo' ] == '' ) {
2022-05-25 01:24:31 -04:00
$this -> out ( ' ' . $this -> l10n -> t ( 'Quit on invalid photo %s' , $contact [ 'avatar' ]));
2022-05-24 16:02:54 -04:00
Photo :: delete ([ 'resource-id' => $resourceid ]);
2022-05-24 15:47:49 -04:00
return ;
}
2022-05-25 01:24:31 -04:00
$this -> out ( '5' , false );
2022-05-24 15:41:38 -04:00
Contact :: update ( $fields , [ 'uri-id' => $contact [ 'uri-id' ]]);
2022-05-25 01:24:31 -04:00
$this -> out ( '6' , false );
2022-05-24 15:41:38 -04:00
Photo :: delete ([ 'resource-id' => $resourceid ]);
2022-05-25 01:24:31 -04:00
$this -> out ( ' ' . $fields [ 'photo' ]);
2022-05-23 08:15:30 -04:00
}
}