Skip to content

Instantly share code, notes, and snippets.

@Opencontent
Created March 22, 2017 10:22
Show Gist options
  • Save Opencontent/a530d38240dd1dfd6c4f7091c662ee88 to your computer and use it in GitHub Desktop.
Save Opencontent/a530d38240dd1dfd6c4f7091c662ee88 to your computer and use it in GitHub Desktop.
<?php
require 'autoload.php';
$script = eZScript::instance( array( 'description' => ( 'Download images from endpoint' ),
'use-session' => false,
'use-modules' => true,
'use-extensions' => true ) );
$script->startup();
$options = $script->getOptions( '[url:][vardir:]',
'',
array(
'url' => 'Endpoint url',
'vardir' => 'Remote var directory (eg ezflow_site)'
)
);
$script->initialize();
$script->setUseDebugAccumulators( true );
$cli = eZCLI::instance();
$output = new ezcConsoleOutput();
try
{
$cli->warning( "Count images ... ", false );
$db = eZDB::instance();
$length = 50;
$limit = array(
'offset' => 0,
'length' => $length
);
$count = (int)eZPersistentObject::count(eZImageFile::definition());
$cli->warning( $count );
$bar = new ezcConsoleProgressbar( $output, $count );
$errors = array();
do {
$imageObjectList = eZPersistentObject::fetchObjectList(eZImageFile::definition(), null, null, null, $limit, false);
foreach( $imageObjectList as $image )
{
if ( strpos( $image['filepath'], 'trash' ) === false )
{
$filepath = $image['filepath'];
if ( $options['url'] )
{
$remoteFilepath = $filepath;
if ( $options['vardir'] ){
$varDir = eZINI::instance()->variable('FileSettings','VarDir');
$remoteVarDir = $options['vardir'];
$remoteFilepath = str_replace( $varDir, "var/$remoteVarDir", $filepath);
}
if (!file_exists($filepath))
{
$name = basename( $filepath );
$dir = str_replace( $name, '', $filepath );
$url = rtrim( $options['url'] ) . '/';
$data = eZHTTPTool::getDataByURL( $url . $remoteFilepath );
if (!empty($data) && strpos( $data, '404 Not Found' ) === false){
if ( eZFile::create( $name, $dir, $data ) )
$result = 'ok';
else
$result = 'Error while creating local file';
$errors[$result][] = $filepath;
}else{
$result = 'Remote file not found';
$errors[$result][] = $filepath;
}
}
else
{
$result = 'Local file already exists';
}
}
else
{
$cli->notice( $image['filepath'] );
}
}
else
{
$result = 'Image in trash?';
$errors[$result][] = $filepath;
}
$bar->setOptions(array('formatString'=> "%act% / %max% [%bar%] %fraction%%"));
$bar->advance();
}
$limit['offset'] += $length;
} while ( count( $imageObjectList ) == $length );
$bar->finish();
$output->outputLine();
foreach( $errors as $key => $value ){
$items = array_unique($value);
$trans = \eZCharTransform::instance();
$filename = 'download_images-' . $trans->transformByGroup( $key, 'identifier' ) . '.log';
foreach($items as $item){
eZLog::write($item, $filename);
}
$cli->error($key . ' => ' . count($items));
}
$script->shutdown();
}
catch( Exception $e )
{
$errCode = $e->getCode();
$errCode = $errCode != 0 ? $errCode : 1; // If an error has occured, script must terminate with a status other than 0
$script->shutdown( $errCode, $e->getMessage() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment