simple-oai-pmh/oai2server.php

299 lines
13 KiB
PHP
Raw Permalink Normal View History

2013-05-12 01:06:17 +02:00
<?php
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>
*
* 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/>.
*/
require_once('oai2exception.php');
require_once('oai2xml.php');
/**
2013-05-14 23:24:59 +02:00
* This is an implementation of OAI Data Provider version 2.0.
* @see http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm
2013-05-12 02:18:36 +02:00
*/
2013-05-12 01:06:17 +02:00
class OAI2Server {
2013-05-12 02:18:36 +02:00
public $errors = array();
2013-05-14 21:46:15 +02:00
private $args = array();
private $verb = '';
private $deleted_record = 'transient';
private $max_records = 100;
2017-05-12 18:13:53 +02:00
private $token_prefix = '/tmp/oai2-';
2013-05-14 23:24:59 +02:00
private $token_valid = 86400;
2013-05-12 02:18:36 +02:00
2017-05-12 18:13:53 +02:00
public function __construct($uri, $args, $identifyResponse, $callbacks, $config) {
2017-10-19 09:51:33 +02:00
$this->uri = $uri;
2017-10-15 18:05:50 +02:00
$verbs = array('Identify', 'ListMetadataFormats', 'ListSets', 'ListIdentifiers', 'ListRecords', 'GetRecord');
if (empty($args['verb']) || !in_array($args['verb'], $verbs)) {
$this->errors[] = new OAI2Exception('badVerb');
2017-10-15 18:05:50 +02:00
return;
2013-05-12 02:18:36 +02:00
}
2017-10-15 18:05:50 +02:00
$this->verb = $args['verb'];
unset($args['verb']);
$this->args = $args;
$this->identifyResponse = $identifyResponse;
$this->listMetadataFormatsCallback = $callbacks['ListMetadataFormats'];
$this->listRecordsCallback = $callbacks['ListRecords'];
$this->getRecordCallback = $callbacks['GetRecord'];
$this->deleted_record = $config['deletedRecord'];
$this->max_records = $config['maxRecords'];
$this->token_prefix = $config['tokenPrefix'];
$this->token_valid = $config['tokenValid'];
$this->response = new OAI2XMLResponse($this->uri, $this->verb, $this->args);
call_user_func(array($this, $this->verb));
2013-05-15 02:56:52 +02:00
}
public function response() {
2013-05-14 21:46:15 +02:00
if (empty($this->errors)) {
2013-05-15 02:56:52 +02:00
return $this->response->doc;
2013-05-14 21:46:15 +02:00
}
2017-10-15 18:11:50 +02:00
$errorResponse = new OAI2XMLResponse($this->uri, $this->verb, $this->args);
$oai_node = $errorResponse->doc->documentElement;
foreach($this->errors as $e) {
$node = $errorResponse->addChild($oai_node, 'error', $e->getMessage());
$node->setAttribute('code', $e->getOAI2Code());
}
return $errorResponse->doc;
2013-05-12 02:18:36 +02:00
}
2013-05-14 23:24:59 +02:00
public function Identify() {
2013-05-14 21:46:15 +02:00
if (count($this->args) > 0) {
2013-05-14 23:24:59 +02:00
foreach($this->args as $key => $val) {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-12 02:18:36 +02:00
}
2013-05-14 23:24:59 +02:00
} else {
foreach($this->identifyResponse as $key => $val) {
$this->response->addToVerbNode($key, $val);
}
2013-05-12 01:06:17 +02:00
}
}
2013-05-14 23:24:59 +02:00
public function ListMetadataFormats() {
2017-10-15 17:51:27 +02:00
$identifier = '';
2013-05-14 21:46:15 +02:00
foreach ($this->args as $argument => $value) {
if ($argument != 'identifier') {
$this->errors[] = new OAI2Exception('badArgument');
2017-10-15 17:51:27 +02:00
} else {
$identifier = $value;
2013-05-14 21:46:15 +02:00
}
}
2013-05-14 23:24:59 +02:00
if (empty($this->errors)) {
try {
2013-05-15 02:56:52 +02:00
if ($formats = call_user_func($this->listMetadataFormatsCallback, $identifier)) {
2013-05-14 23:24:59 +02:00
foreach($formats as $key => $val) {
2017-05-12 18:13:53 +02:00
$cmf = $this->response->addToVerbNode('metadataFormat');
$this->response->addChild($cmf, 'metadataPrefix', $key);
$this->response->addChild($cmf, 'schema', $val['schema']);
2017-05-13 12:21:26 +02:00
$this->response->addChild($cmf, 'metadataNamespace', $val['namespace']);
2013-05-14 23:24:59 +02:00
}
} else {
$this->errors[] = new OAI2Exception('noMetadataFormats');
2013-05-12 01:06:17 +02:00
}
2013-05-14 23:24:59 +02:00
} catch (OAI2Exception $e) {
$this->errors[] = $e;
2013-05-12 01:06:17 +02:00
}
}
}
2013-05-14 23:24:59 +02:00
public function ListSets() {
2013-05-14 21:46:15 +02:00
if (isset($this->args['resumptionToken'])) {
if (count($this->args) > 1) {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-14 21:46:15 +02:00
} else {
2017-05-12 18:13:53 +02:00
$this->errors[] = new OAI2Exception('badResumptionToken');
2013-05-14 21:46:15 +02:00
}
2013-05-14 23:24:59 +02:00
} else {
2017-05-12 18:13:53 +02:00
$this->errors[] = new OAI2Exception('noSetHierarchy');
2013-05-12 01:06:17 +02:00
}
}
2013-05-14 23:24:59 +02:00
public function GetRecord() {
2017-05-13 13:23:24 +02:00
if (!isset($this->args['identifier']) || !isset($this->args['metadataPrefix'])) {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-14 21:46:15 +02:00
} else {
$metadataFormats = call_user_func($this->listMetadataFormatsCallback);
if (!isset($metadataFormats[$this->args['metadataPrefix']])) {
$this->errors[] = new OAI2Exception('cannotDisseminateFormat');
2013-05-14 21:46:15 +02:00
}
}
2013-05-14 23:24:59 +02:00
if (empty($this->errors)) {
try {
if ($record = call_user_func($this->getRecordCallback, $this->args['identifier'], $this->args['metadataPrefix'])) {
$cur_record = $this->response->addToVerbNode('record');
2017-10-15 17:51:27 +02:00
$this->response->createHeader($record['identifier'], $this->formatDatestamp($record['timestamp']), $record['deleted'], $cur_record);
if (!$record['deleted']) {
2017-10-12 10:17:43 +02:00
$this->addMetadata($cur_record, $record['metadata']);
}
} else {
$this->errors[] = new OAI2Exception('idDoesNotExist');
}
2013-05-14 23:24:59 +02:00
} catch (OAI2Exception $e) {
$this->errors[] = $e;
}
2013-05-12 01:06:17 +02:00
}
}
2013-05-14 23:24:59 +02:00
public function ListIdentifiers() {
$this->ListRecords();
2013-05-14 23:24:59 +02:00
}
public function ListRecords() {
2017-05-12 18:13:53 +02:00
$maxItems = $this->max_records;
2013-05-14 23:24:59 +02:00
$deliveredRecords = 0;
$metadataPrefix = $this->args['metadataPrefix'];
$from = isset($this->args['from']) ? $this->args['from'] : '';
$until = isset($this->args['until']) ? $this->args['until'] : '';
if (isset($this->args['resumptionToken'])) {
2013-05-14 21:46:15 +02:00
if (count($this->args) > 1) {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-14 21:46:15 +02:00
} else {
if (!file_exists($this->token_prefix.$this->args['resumptionToken'])) {
2013-05-14 21:46:15 +02:00
$this->errors[] = new OAI2Exception('badResumptionToken');
2013-05-14 23:24:59 +02:00
} else {
if (filemtime($this->token_prefix.$this->args['resumptionToken'])+$this->token_valid < time()) {
$this->errors[] = new OAI2Exception('badResumptionToken');
2013-05-14 23:24:59 +02:00
} else {
if ($readings = $this->readResumptionToken($this->token_prefix.$this->args['resumptionToken'])) {
2017-05-12 18:13:53 +02:00
list($deliveredRecords, $metadataPrefix, $from, $until) = $readings;
2013-05-14 23:24:59 +02:00
} else {
$this->errors[] = new OAI2Exception('badResumptionToken');
2013-05-14 23:24:59 +02:00
}
}
2013-05-14 21:46:15 +02:00
}
2013-05-12 02:18:36 +02:00
}
} else {
2013-05-14 21:46:15 +02:00
if (!isset($this->args['metadataPrefix'])) {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-14 21:46:15 +02:00
} else {
$metadataFormats = call_user_func($this->listMetadataFormatsCallback);
if (!isset($metadataFormats[$this->args['metadataPrefix']])) {
$this->errors[] = new OAI2Exception('cannotDisseminateFormat');
2013-05-14 21:46:15 +02:00
}
}
if (isset($this->args['from'])) {
2017-05-12 18:13:53 +02:00
if (!$this->checkDateFormat($this->args['from'])) {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-14 21:46:15 +02:00
}
}
if (isset($this->args['until'])) {
2017-05-12 18:13:53 +02:00
if (!$this->checkDateFormat($this->args['until'])) {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-14 21:46:15 +02:00
}
}
2017-05-12 18:13:53 +02:00
if (isset($this->args['set'])) {
$this->errors[] = new OAI2Exception('noSetHierarchy');
}
2013-05-12 02:18:36 +02:00
}
2013-05-15 02:56:52 +02:00
if (empty($this->errors)) {
2013-05-14 23:24:59 +02:00
try {
2017-05-13 13:23:24 +02:00
if (!($records_count = call_user_func($this->listRecordsCallback, $metadataPrefix, $this->formatTimestamp($from), $this->formatTimestamp($until), true))) {
throw new OAI2Exception('noRecordsMatch');
}
2017-05-12 18:13:53 +02:00
$records = call_user_func($this->listRecordsCallback, $metadataPrefix, $this->formatTimestamp($from), $this->formatTimestamp($until), false, $deliveredRecords, $maxItems);
2013-05-14 23:24:59 +02:00
foreach ($records as $record) {
2017-10-12 10:14:05 +02:00
if ($this->verb == 'ListRecords') { // for ListIdentifiers, only headers will be returned.
2017-10-12 10:17:43 +02:00
$cur_record = $this->response->addToVerbNode('record');
2017-10-12 10:14:05 +02:00
}
2017-10-15 17:51:27 +02:00
$this->response->createHeader($record['identifier'], $this->formatDatestamp($record['timestamp']), $record['deleted'], $cur_record);
2017-10-12 10:14:05 +02:00
if (!$record['deleted'] && $this->verb == 'ListRecords') { // for ListIdentifiers, only headers will be returned.
2017-10-12 10:17:43 +02:00
$this->addMetadata($cur_record, $record['metadata']);
2013-05-14 23:24:59 +02:00
}
}
// Will we need a new ResumptionToken?
if ($records_count - $deliveredRecords > $maxItems) {
$deliveredRecords += $maxItems;
$restoken = $this->createResumptionToken($deliveredRecords);
2017-05-12 18:13:53 +02:00
$expirationDatetime = gmstrftime('%Y-%m-%dT%TZ', time()+$this->token_valid);
2017-10-15 17:51:27 +02:00
} elseif (isset($this->args['resumptionToken'])) {
2013-05-14 23:24:59 +02:00
// Last delivery, return empty ResumptionToken
$restoken = null;
$expirationDatetime = null;
2013-05-12 01:06:17 +02:00
}
2013-05-14 23:24:59 +02:00
if (isset($restoken)) {
2017-05-13 13:23:24 +02:00
$this->response->createResumptionToken($restoken, $expirationDatetime, $records_count, $deliveredRecords-$maxItems);
2013-05-14 23:24:59 +02:00
}
} catch (OAI2Exception $e) {
$this->errors[] = $e;
2013-05-12 01:06:17 +02:00
}
}
}
private function addMetadata($cur_record, $file) {
2017-10-12 10:17:43 +02:00
$meta_node = $this->response->addChild($cur_record, 'metadata');
$fragment = new DOMDocument();
$fragment->load($file);
$this->response->importFragment($meta_node, $fragment);
}
private function createResumptionToken($delivered_records) {
2017-05-13 12:49:13 +02:00
list($usec, $sec) = explode(' ', microtime());
$token = ((int)($usec*1000) + (int)($sec*1000));
2017-10-15 18:05:50 +02:00
$file = fopen ($this->token_prefix.$token, 'w');
if($file == false) {
2017-05-12 18:13:53 +02:00
exit('Cannot write resumption token. Writing permission needs to be changed.');
}
2017-05-13 12:49:13 +02:00
$values = array(
'deliveredRecords' => $delivered_records,
'metadataPrefix' => isset($this->args['metadataPrefix']) ? $this->args['metadataPrefix'] : '',
'from' => isset($this->args['from']) ? $this->args['from'] : '',
'until' => isset($this->args['until']) ? $this->args['until'] : ''
);
2017-10-15 18:05:50 +02:00
fputs($file, $values['deliveredRecords'].'#');
fputs($file, $values['metadataPrefix'].'#');
fputs($file, $values['from'].'#');
fputs($file, $values['until'].'#');
fclose($file);
2013-05-14 23:24:59 +02:00
return $token;
}
private function readResumptionToken($resumptionToken) {
$rtVal = false;
2017-10-15 18:05:50 +02:00
$file = fopen($resumptionToken, 'r');
if ($file != false) {
$filetext = fgets($file, 255);
$textparts = explode('#', $filetext);
2017-10-15 18:05:50 +02:00
fclose($file);
unlink($resumptionToken);
$rtVal = array_values($textparts);
2013-05-14 23:24:59 +02:00
}
return $rtVal;
}
2017-05-12 18:13:53 +02:00
private function formatDatestamp($timestamp) {
return gmdate('Y-m-d\TH:i:s\Z', $timestamp);
2013-05-14 23:24:59 +02:00
}
2017-05-12 18:13:53 +02:00
private function formatTimestamp($datestamp) {
if (is_array($time = strptime($datestamp, '%Y-%m-%dT%H:%M:%SZ')) || is_array($time = strptime($datestamp, '%Y-%m-%d'))) {
return gmmktime($time['tm_hour'], $time['tm_min'], $time['tm_sec'], $time['tm_mon'] + 1, $time['tm_mday'], $time['tm_year']+1900);
2013-05-14 23:24:59 +02:00
} else {
2017-05-12 18:13:53 +02:00
return null;
}
}
private function checkDateFormat($date) {
2017-10-15 18:05:50 +02:00
$datetime = DateTime::createFromFormat('Y-m-d\TH:i:s\Z', $date);
if ($datetime === false) {
$datetime = DateTime::createFromFormat('Y-m-d', $date);
2013-05-14 23:24:59 +02:00
}
2017-10-15 18:09:01 +02:00
return ($datetime !== false) && !array_sum($datetime->getLastErrors());
2013-05-12 02:18:36 +02:00
}
2017-05-12 18:13:53 +02:00
2013-05-12 01:06:17 +02:00
}