Better error handling

This commit is contained in:
Sebastian Meyer 2017-12-11 11:03:54 +01:00
parent 330695eaa8
commit 8aba6243af
1 changed files with 61 additions and 10 deletions

View File

@ -22,6 +22,35 @@ if (php_sapi_name() !== 'cli') exit;
require_once('oai2config.php'); require_once('oai2config.php');
/**
* Format output string
*
* @param string $text
* @param string $format 'green' or 'red'
*
* @return string
*/
function format($text, $format = '') {
switch ($format) {
case 'green':
$text = "\033[0;92m$text\033[0m";
break;
case 'red':
$text = "\033[1;91m$text\033[0m";
break;
default:
break;
}
return $text;
}
// Check mandatory cli arguments // Check mandatory cli arguments
if (empty($argc) || $argc != 3) { if (empty($argc) || $argc != 3) {
@ -48,8 +77,6 @@ if (empty($config['metadataFormats'][$metadataPrefix])) {
} }
// Check sourceDir permissions // Check sourceDir permissions
$sourceDir = rtrim($sourceDir, '/').'/';
if (!is_dir($sourceDir) || !is_readable($sourceDir)) { if (!is_dir($sourceDir) || !is_readable($sourceDir)) {
echo "Error: $sourceDir not readable\n"; echo "Error: $sourceDir not readable\n";
@ -58,6 +85,8 @@ if (!is_dir($sourceDir) || !is_readable($sourceDir)) {
} }
$sourceDir = rtrim($sourceDir, '/').'/';
// Check dataDir permissions // Check dataDir permissions
$dataDir = rtrim($config['dataDirectory'], '/').'/'.$metadataPrefix.'/'; $dataDir = rtrim($config['dataDirectory'], '/').'/'.$metadataPrefix.'/';
@ -74,6 +103,8 @@ echo "Updating $dataDir from $sourceDir\n";
$todo = array (); $todo = array ();
$error = false;
$oldFiles = glob($dataDir.'*.xml'); $oldFiles = glob($dataDir.'*.xml');
foreach ($oldFiles as $oldFile) { foreach ($oldFiles as $oldFile) {
@ -92,44 +123,64 @@ foreach ($newFiles as $newFile) {
foreach ($todo as $identifier => $task) { foreach ($todo as $identifier => $task) {
echo " Checking record $identifier ... ";
if ($task === 'update') { if ($task === 'update') {
if (md5_file($sourceDir.$identifier.'.xml') !== md5_file($dataDir.$identifier.'.xml')) { if (md5_file($sourceDir.$identifier.'.xml') !== md5_file($dataDir.$identifier.'.xml')) {
echo " Updating record $identifier ...";
// Replace file // Replace file
if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) { if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) {
echo " done!\n"; echo format('updated', 'green')."\n";
} else { } else {
echo " failed!\n"; echo format('update failed', 'red')."\n";
$error = true;
} }
} else {
echo "unchanged\n";
} }
} elseif ($task === 'delete') { } elseif ($task === 'delete') {
if (filesize($dataDir.$identifier.'.xml') !== 0) { if (filesize($dataDir.$identifier.'.xml') !== 0) {
echo " Deleting record $identifier ...";
// Truncate file // Truncate file
if (fclose(fopen($dataDir.$identifier.'.xml', 'w'))) { if (fclose(fopen($dataDir.$identifier.'.xml', 'w'))) {
echo " done!\n"; echo format('deleted', 'green')."\n";
} else { } else {
echo " failed!\n"; echo format('deletion failed', 'red')."\n";
$error = true;
} }
} else {
echo "unchanged\n";
} }
} }
} }
if ($error) {
echo "Update completed, but errors occurred. Please check the logs!\n";
} else {
echo "Update successfully completed!\n";
}