Created
March 22, 2017 10:22
-
-
Save Opencontent/a530d38240dd1dfd6c4f7091c662ee88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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