simple-oai-pmh/oai2server.php

302 lines
13 KiB
PHP
Raw 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 = '';
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;
2017-05-12 18:13:53 +02:00
private $max_records = 100;
2013-05-12 02:18:36 +02:00
2017-05-12 18:13:53 +02:00
public function __construct($uri, $args, $identifyResponse, $callbacks, $config) {
$this->uri = $uri;
2013-05-14 21:46:15 +02:00
if (!isset($args['verb']) || empty($args['verb'])) {
$this->errors[] = new OAI2Exception('badVerb');
2013-05-14 23:24:59 +02:00
} else {
$verbs = array('Identify', 'ListMetadataFormats', 'ListSets', 'ListIdentifiers', 'ListRecords', 'GetRecord');
if (in_array($args['verb'], $verbs)) {
$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'];
2017-05-12 18:13:53 +02:00
$this->token_prefix = $config['tokenPrefix'];
$this->token_valid = $config['tokenValid'];
$this->max_records = $config['maxRecords'];
2013-05-14 23:24:59 +02:00
$this->response = new OAI2XMLResponse($this->uri, $this->verb, $this->args);
call_user_func(array($this, $this->verb));
} else {
$this->errors[] = new OAI2Exception('badVerb');
2013-05-14 23:24:59 +02:00
}
2013-05-12 02:18:36 +02:00
}
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-12 02:18:36 +02:00
} else {
2013-05-14 23:24:59 +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());
}
2013-05-15 02:56:52 +02:00
return $errorResponse->doc;
2013-05-14 21:46:15 +02:00
}
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() {
2013-05-14 21:46:15 +02:00
foreach ($this->args as $argument => $value) {
if ($argument != 'identifier') {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-14 21:46:15 +02:00
}
}
2013-05-15 02:56:52 +02:00
if (isset($this->args['identifier'])) {
$identifier = $this->args['identifier'];
} else {
$identifier = '';
}
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-05-12 18:13:53 +02:00
$cur_header = $this->response->createHeader($record['identifier'], $this->formatDatestamp($record['timestamp']), $cur_record);
$this->add_metadata($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-05-12 18:13:53 +02:00
if ($this->verb == 'ListRecords') {
2013-05-15 02:56:52 +02:00
$cur_record = $this->response->addToVerbNode('record');
2017-05-12 18:13:53 +02:00
$cur_header = $this->response->createHeader($record['identifier'], $this->formatDatestamp($record['timestamp']), $cur_record);
$this->add_metadata($cur_record, $record['metadata']);
2013-05-14 23:24:59 +02:00
} else { // for ListIdentifiers, only identifiers will be returned.
2017-05-12 18:13:53 +02:00
$cur_header = $this->response->createHeader($record['identifier'], $this->formatDatestamp($record['timestamp']));
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);
2013-05-14 23:24:59 +02:00
} elseif (isset($args['resumptionToken'])) {
// 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
}
}
}
2017-05-12 18:13:53 +02:00
private function add_metadata($cur_record, $file) {
$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));
2013-05-14 23:24:59 +02:00
$fp = fopen ($this->token_prefix.$token, 'w');
if($fp==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'] : ''
);
fputs($fp, $values['deliveredRecords'].'#');
fputs($fp, $values['metadataPrefix'].'#');
fputs($fp, $values['from'].'#');
fputs($fp, $values['until'].'#');
fclose($fp);
2013-05-14 23:24:59 +02:00
return $token;
}
private function readResumptionToken($resumptionToken) {
$rtVal = false;
$fp = fopen($resumptionToken, 'r');
if ($fp != false) {
$filetext = fgets($fp, 255);
$textparts = explode('#', $filetext);
2013-05-14 23:24:59 +02:00
fclose($fp);
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) {
$dt = DateTime::createFromFormat('Y-m-d\TH:i:s\Z', $date);
if ($dt === false) {
$dt = DateTime::createFromFormat('Y-m-d', $date);
2013-05-14 23:24:59 +02:00
}
2017-05-12 18:13:53 +02:00
return ($dt !== false) && !array_sum($dt->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
}