From 330695eaa82fe13aa35681cc225d2a2e5da6ce27 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Sun, 10 Dec 2017 22:45:44 +0100 Subject: [PATCH 1/3] Add update script --- update.php | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 update.php diff --git a/update.php b/update.php new file mode 100644 index 0000000..3b1cf29 --- /dev/null +++ b/update.php @@ -0,0 +1,135 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +// Make this script only executable via commandline interface! +if (php_sapi_name() !== 'cli') exit; + +require_once('oai2config.php'); + +// 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 +$sourceDir = rtrim($sourceDir, '/').'/'; + +if (!is_dir($sourceDir) || !is_readable($sourceDir)) { + + echo "Error: $sourceDir not readable\n"; + + exit; + +} + +// Check dataDir permissions +$dataDir = rtrim($config['dataDirectory'], '/').'/'.$metadataPrefix.'/'; + +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 (); + +$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) { + + if ($task === 'update') { + + if (md5_file($sourceDir.$identifier.'.xml') !== md5_file($dataDir.$identifier.'.xml')) { + + echo " Updating record $identifier ..."; + + // Replace file + if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) { + + echo " done!\n"; + + } else { + + echo " failed!\n"; + + } + + } + + } elseif ($task === 'delete') { + + if (filesize($dataDir.$identifier.'.xml') !== 0) { + + echo " Deleting record $identifier ..."; + + // Truncate file + if (fclose(fopen($dataDir.$identifier.'.xml', 'w'))) { + + echo " done!\n"; + + } else { + + echo " failed!\n"; + + } + + } + + } + +} From 8aba6243af7d369bd5c8e51eef8e47a20c1a5404 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Mon, 11 Dec 2017 11:03:54 +0100 Subject: [PATCH 2/3] Better error handling --- update.php | 71 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/update.php b/update.php index 3b1cf29..2c9e4ea 100644 --- a/update.php +++ b/update.php @@ -22,6 +22,35 @@ if (php_sapi_name() !== 'cli') exit; 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 if (empty($argc) || $argc != 3) { @@ -48,8 +77,6 @@ if (empty($config['metadataFormats'][$metadataPrefix])) { } // Check sourceDir permissions -$sourceDir = rtrim($sourceDir, '/').'/'; - if (!is_dir($sourceDir) || !is_readable($sourceDir)) { echo "Error: $sourceDir not readable\n"; @@ -58,6 +85,8 @@ if (!is_dir($sourceDir) || !is_readable($sourceDir)) { } +$sourceDir = rtrim($sourceDir, '/').'/'; + // Check dataDir permissions $dataDir = rtrim($config['dataDirectory'], '/').'/'.$metadataPrefix.'/'; @@ -74,6 +103,8 @@ echo "Updating $dataDir from $sourceDir\n"; $todo = array (); +$error = false; + $oldFiles = glob($dataDir.'*.xml'); foreach ($oldFiles as $oldFile) { @@ -92,44 +123,64 @@ foreach ($newFiles as $newFile) { foreach ($todo as $identifier => $task) { + echo " Checking record $identifier ... "; + if ($task === 'update') { if (md5_file($sourceDir.$identifier.'.xml') !== md5_file($dataDir.$identifier.'.xml')) { - echo " Updating record $identifier ..."; - // Replace file if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) { - echo " done!\n"; + echo format('updated', 'green')."\n"; } else { - echo " failed!\n"; + echo format('update failed', 'red')."\n"; + + $error = true; } + } else { + + echo "unchanged\n"; + } } elseif ($task === 'delete') { if (filesize($dataDir.$identifier.'.xml') !== 0) { - echo " Deleting record $identifier ..."; - // Truncate file if (fclose(fopen($dataDir.$identifier.'.xml', 'w'))) { - echo " done!\n"; + echo format('deleted', 'green')."\n"; } 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"; + +} From ce2d4ea1ebd90881da8659583f31719d11d92ba2 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Tue, 12 Dec 2017 16:21:13 +0100 Subject: [PATCH 3/3] Distinguish newly added files from updates --- update.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/update.php b/update.php index 2c9e4ea..e3dde2a 100644 --- a/update.php +++ b/update.php @@ -127,7 +127,22 @@ foreach ($todo as $identifier => $task) { if ($task === 'update') { - if (md5_file($sourceDir.$identifier.'.xml') !== md5_file($dataDir.$identifier.'.xml')) { + 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')) {