Streamline formatting of update.php

This commit is contained in:
Sebastian Meyer 2017-12-22 09:56:44 +01:00
parent 4bbcdd190a
commit 50075d05b8
1 changed files with 0 additions and 77 deletions

View File

@ -31,178 +31,101 @@ require_once('oai2config.php');
* @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
if (empty($argc) || $argc != 3) {
echo "Usage:\n";
echo " php update.php [sourceDir] [metadataPrefix]\n";
echo "\n";
echo "Example:\n";
echo " php update.php /tmp/import oai_dc\n";
echo "\n";
exit;
}
list(, $sourceDir, $metadataPrefix) = $argv;
// Check metadataPrefix
if (empty($config['metadataFormats'][$metadataPrefix])) {
echo "Error: metadataPrefix $metadataPrefix not defined in oai2config.php\n";
exit;
}
// Check sourceDir permissions
if (!is_dir($sourceDir) || !is_readable($sourceDir)) {
echo "Error: $sourceDir not readable\n";
exit;
}
$sourceDir = rtrim($sourceDir, '/').'/';
// Prepend script's path if dataDir is not an absolute path
$dataDir = rtrim($config['dataDirectory'], '/').'/'.$metadataPrefix.'/';
if (strpos($dataDir, '/') !== 0) {
$dataDir = dirname(__FILE__).'/'.$dataDir;
}
// Check dataDir permissions
if (!is_dir($dataDir) || !is_writable($dataDir)) {
echo "Error: $dataDir not writable\n";
exit;
}
// Alright, let's start!
echo "Updating $dataDir from $sourceDir\n";
$todo = array ();
$error = false;
$oldFiles = glob($dataDir.'*.xml');
foreach ($oldFiles as $oldFile) {
$todo[pathinfo($oldFile, PATHINFO_FILENAME)] = 'delete';
}
$newFiles = glob($sourceDir.'*.xml');
foreach ($newFiles as $newFile) {
$todo[pathinfo($newFile, PATHINFO_FILENAME)] = 'update';
}
foreach ($todo as $identifier => $task) {
echo " Checking record $identifier ... ";
if ($task === 'update') {
if (!file_exists($dataDir.$identifier.'.xml')) {
// Add file
if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) {
echo format('added', 'green')."\n";
} else {
echo format('addition failed', 'red')."\n";
$error = true;
}
} elseif (md5_file($sourceDir.$identifier.'.xml') !== md5_file($dataDir.$identifier.'.xml')) {
// Replace file
if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) {
echo format('updated', 'green')."\n";
} else {
echo format('update failed', 'red')."\n";
$error = true;
}
} else {
echo "unchanged\n";
}
} elseif ($task === 'delete') {
if (filesize($dataDir.$identifier.'.xml') !== 0) {
// Truncate file
if (fclose(fopen($dataDir.$identifier.'.xml', 'w'))) {
echo format('deleted', 'green')."\n";
} else {
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";
}