simple-oai-pmh/index.php

144 lines
5.1 KiB
PHP
Raw Normal View History

<?php
2013-05-12 02:18:36 +02:00
/**
2017-05-12 18:13:53 +02:00
* Simple OAI-PMH 2.0 Data Provider
* Copyright (C) 2005 Heinrich Stamerjohanns <stamer@uni-oldenburg.de>
* Copyright (C) 2011 Jianfeng Li <jianfeng.li@adelaide.edu.au>
2017-05-12 18:13:53 +02:00
* Copyright (C) 2013 Daniel Neis Araujo <danielneis@gmail.com>
* Copyright (C) 2017 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
2013-05-12 02:18:36 +02:00
*
2017-05-12 18:13:53 +02:00
* 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.
2013-05-12 02:18:36 +02:00
*
2017-05-12 18:13:53 +02:00
* 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/>.
2013-05-12 02:18:36 +02:00
*/
2020-01-24 00:47:21 +01:00
use OCC\OAI2\Exception;
use OCC\OAI2\Server;
2020-01-24 00:13:17 +01:00
2020-01-24 00:47:21 +01:00
// Register PSR-4 autoloader
require __DIR__.'/vendor/autoload.php';
2020-01-24 00:13:17 +01:00
// Load configuration
2020-01-24 00:47:21 +01:00
require __DIR__.'/Configuration/Main.php';
2013-05-14 21:46:15 +02:00
// Get all available records and their respective status and timestamps
2020-01-08 17:24:22 +01:00
$records = [];
$deleted = [];
$timestamps = [];
2017-10-05 18:02:15 +02:00
$earliest = time();
2017-05-12 18:13:53 +02:00
2020-01-08 17:24:22 +01:00
foreach ($config['metadataPrefix'] as $prefix => $uris) {
$files = glob(rtrim($config['dataDirectory'], '/').'/'.$prefix.'/*.xml');
foreach ($files as $file) {
$records[$prefix][pathinfo($file, PATHINFO_FILENAME)] = $file;
$deleted[$prefix][pathinfo($file, PATHINFO_FILENAME)] = !filesize($file);
$timestamps[$prefix][filemtime($file)][] = pathinfo($file, PATHINFO_FILENAME);
if (filemtime($file) < $earliest) {
$earliest = filemtime($file);
}
2017-10-05 18:02:15 +02:00
}
2020-01-08 17:24:22 +01:00
ksort($records[$prefix]);
reset($records[$prefix]);
ksort($timestamps[$prefix]);
reset($timestamps[$prefix]);
2017-10-05 18:02:15 +02:00
}
2017-05-21 20:01:51 +02:00
// Get current base URL
2017-06-09 18:50:11 +02:00
$baseURL = $_SERVER['HTTP_HOST'].parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
2017-05-21 20:01:51 +02:00
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
2020-01-08 17:24:22 +01:00
$baseURL = 'https://'.$baseURL;
2017-05-21 20:01:51 +02:00
} else {
2020-01-08 17:24:22 +01:00
$baseURL = 'http://'.$baseURL;
2017-05-21 20:01:51 +02:00
}
2017-05-12 18:13:53 +02:00
// Build the Identify response
2020-01-08 17:24:22 +01:00
$identifyResponse = [
'repositoryName' => $config['repositoryName'],
'baseURL' => $baseURL,
'protocolVersion' => '2.0',
'adminEmail' => $config['adminEmail'],
'earliestDatestamp' => gmdate('Y-m-d\TH:i:s\Z', $earliest),
'deletedRecord' => $config['deletedRecord'],
'granularity' => 'YYYY-MM-DDThh:mm:ssZ'
];
2017-05-12 18:13:53 +02:00
2020-01-24 00:13:17 +01:00
$oai2 = new Server(
2020-01-08 17:24:22 +01:00
$baseURL,
2022-07-01 16:18:23 +02:00
array_merge($_POST, $_GET),
2020-01-08 17:24:22 +01:00
$identifyResponse,
[
'GetRecord' => function ($identifier, $metadataPrefix) {
global $records, $deleted;
if (empty($records[$metadataPrefix][$identifier])) {
return [];
} else {
return [
'identifier' => $identifier,
'timestamp' => filemtime($records[$metadataPrefix][$identifier]),
'deleted' => $deleted[$metadataPrefix][$identifier],
'metadata' => $records[$metadataPrefix][$identifier]
];
}
},
'ListRecords' => function ($metadataPrefix, $from = null, $until = null, $count = false, $deliveredRecords = 0, $maxItems = 100) {
global $records, $deleted, $timestamps;
$resultSet = [];
foreach ($timestamps[$metadataPrefix] as $timestamp => $identifiers) {
if ((is_null($from) || $timestamp >= $from) && (is_null($until) || $timestamp <= $until)) {
foreach ($identifiers as $identifier) {
$resultSet[] = [
'identifier' => $identifier,
'timestamp' => filemtime($records[$metadataPrefix][$identifier]),
'deleted' => $deleted[$metadataPrefix][$identifier],
'metadata' => $records[$metadataPrefix][$identifier]
];
}
}
}
if ($count) {
return count($resultSet);
} else {
return array_slice($resultSet, $deliveredRecords, $maxItems);
}
},
'ListMetadataFormats' => function ($identifier = '') {
global $config, $records;
if (!empty($identifier)) {
$formats = [];
foreach ($records as $format => $record) {
if (!empty($record[$identifier])) {
$formats[$format] = $config['metadataPrefix'][$format];
}
}
if (!empty($formats)) {
return $formats;
} else {
2020-01-24 00:13:17 +01:00
throw new Exception('idDoesNotExist');
2020-01-08 17:24:22 +01:00
}
} else {
return $config['metadataPrefix'];
}
2017-10-05 18:02:15 +02:00
}
2020-01-08 17:24:22 +01:00
],
$config
);
2013-05-15 02:56:52 +02:00
$response = $oai2->response();
2017-05-12 18:13:53 +02:00
2013-05-15 02:56:52 +02:00
if (isset($return)) {
2020-01-08 17:24:22 +01:00
return $response;
2013-05-15 02:56:52 +02:00
} else {
2020-01-08 17:24:22 +01:00
$response->formatOutput = true;
$response->preserveWhiteSpace = false;
header('Content-Type: text/xml');
echo $response->saveXML();
2013-05-15 02:56:52 +02:00
}