simple-oai-pmh/update.php

144 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2017-12-10 22:45:44 +01:00
<?php
/**
* Simple OAI-PMH 2.0 Data Provider
* Copyright (C) 2017 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
// Make this script only executable via commandline interface!
2020-01-16 15:36:33 +01:00
if (php_sapi_name() !== 'cli') {
exit;
}
2017-12-10 22:45:44 +01:00
2020-01-24 00:47:21 +01:00
require __DIR__.'/Configuration/Main.php';
2017-12-10 22:45:44 +01:00
2017-12-11 11:03:54 +01:00
/**
* 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;
}
2017-12-10 22:45:44 +01:00
// 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";
2017-12-21 19:04:24 +01:00
echo "\n";
2017-12-10 22:45:44 +01:00
exit;
}
list(, $sourceDir, $metadataPrefix) = $argv;
// Check metadataPrefix
if (empty($config['metadataPrefix'][$metadataPrefix])) {
2017-12-10 22:45:44 +01:00
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;
}
2017-12-11 11:03:54 +01:00
$sourceDir = rtrim($sourceDir, '/').'/';
// Prepend script's path if dataDir is not an absolute path
2017-12-10 22:45:44 +01:00
$dataDir = rtrim($config['dataDirectory'], '/').'/'.$metadataPrefix.'/';
if (strpos($dataDir, '/') !== 0) {
2020-01-24 00:47:21 +01:00
$dataDir = __DIR__.'/'.$dataDir;
}
// Check dataDir permissions
2017-12-10 22:45:44 +01:00
if (!is_dir($dataDir) || !is_writable($dataDir)) {
echo "Error: $dataDir not writable\n";
exit;
}
// Alright, let's start!
echo "Updating $dataDir from $sourceDir\n";
2021-08-14 09:28:45 +02:00
$todo = [];
2017-12-11 11:03:54 +01:00
$error = false;
2017-12-10 22:45:44 +01:00
$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';
}
2021-08-14 09:28:45 +02:00
$files = [
'added' => 0,
'updated' => 0,
'deleted' => 0,
'total' => count($newFiles)
];
2017-12-10 22:45:44 +01:00
foreach ($todo as $identifier => $task) {
2017-12-11 11:03:54 +01:00
echo " Checking record $identifier ... ";
2017-12-10 22:45:44 +01:00
if ($task === 'update') {
if (!file_exists($dataDir.$identifier.'.xml')) {
// Add file
if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) {
echo format('added', 'green')."\n";
2021-08-14 09:28:45 +02:00
$files['added']++;
} else {
echo format('addition failed', 'red')."\n";
$error = true;
}
} elseif (md5_file($sourceDir.$identifier.'.xml') !== md5_file($dataDir.$identifier.'.xml')) {
2017-12-10 22:45:44 +01:00
// Replace file
if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) {
2017-12-11 11:03:54 +01:00
echo format('updated', 'green')."\n";
2021-08-14 09:28:45 +02:00
$files['updated']++;
2017-12-10 22:45:44 +01:00
} else {
2017-12-11 11:03:54 +01:00
echo format('update failed', 'red')."\n";
$error = true;
2017-12-10 22:45:44 +01:00
}
2017-12-11 11:03:54 +01:00
} else {
echo "unchanged\n";
2017-12-10 22:45:44 +01:00
}
} elseif ($task === 'delete') {
if (filesize($dataDir.$identifier.'.xml') !== 0) {
// Truncate file
if (fclose(fopen($dataDir.$identifier.'.xml', 'w'))) {
2017-12-11 11:03:54 +01:00
echo format('deleted', 'green')."\n";
2021-08-14 09:28:45 +02:00
$files['deleted']++;
2017-12-10 22:45:44 +01:00
} else {
2017-12-11 11:03:54 +01:00
echo format('deletion failed', 'red')."\n";
$error = true;
2017-12-10 22:45:44 +01:00
}
2017-12-11 11:03:54 +01:00
} else {
echo "unchanged\n";
2017-12-10 22:45:44 +01:00
}
}
}
2021-08-14 09:28:45 +02:00
echo 'Summary: '.$files['added'].' added, '.$files['updated'].' updated, '.$files['deleted'].' deleted. Total records served: '.$files['total']."\n";
2017-12-11 11:03:54 +01:00
if ($error) {
echo "Update completed, but errors occurred. Please check the logs!\n";
} else {
echo "Update successfully completed!\n";
}