simple-oai-pmh/oai2server.php

364 lines
14 KiB
PHP
Raw Normal View History

2013-05-12 01:06:17 +02:00
<?php
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 = '';
2013-05-14 23:24:59 +02:00
private $token_prefix = '/tmp/oai_pmh-';
private $token_valid = 86400;
2013-05-12 02:18:36 +02:00
2013-05-14 21:46:15 +02:00
function __construct($uri, $args, $identifyResponse, $callbacks) {
$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)) {
2013-05-14 21:46:15 +02:00
2013-05-14 23:24:59 +02:00
$this->verb = $args['verb'];
2013-05-12 02:18:36 +02:00
2013-05-14 23:24:59 +02:00
unset($args['verb']);
2013-05-12 02:18:36 +02:00
2013-05-14 23:24:59 +02:00
$this->args = $args;
2013-05-12 02:18:36 +02:00
2013-05-14 23:24:59 +02:00
$this->identifyResponse = $identifyResponse;
2013-05-12 02:18:36 +02:00
2013-05-14 23:24:59 +02:00
$this->listMetadataFormatsCallback = $callbacks['ListMetadataFormats'];
$this->listSetsCallback = $callbacks['ListSets'];
$this->listRecordsCallback = $callbacks['ListRecords'];
$this->getRecordCallback = $callbacks['GetRecord'];
2013-05-12 02:18:36 +02:00
2013-05-14 23:24:59 +02:00
$this->response = new OAI2XMLResponse($this->uri, $this->verb, $this->args);
2013-05-12 02:18:36 +02:00
2013-05-14 23:24:59 +02:00
call_user_func(array($this, $this->verb));
2013-05-14 21:46:15 +02:00
2013-05-14 23:24:59 +02:00
} 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-12 02:18:36 +02:00
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-12 01:06:17 +02:00
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) {
$cmf = $this->response->addToVerbNode("metadataFormat");
$this->response->addChild($cmf,'metadataPrefix',$key);
$this->response->addChild($cmf,'schema',$val['schema']);
$this->response->addChild($cmf,'metadataNamespace',$val['metadataNamespace']);
}
} 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-12 02:18:36 +02:00
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 {
2013-05-14 23:24:59 +02:00
if ((int)$val+$this->token_valid < time()) {
2013-05-14 21:46:15 +02:00
$this->errors[] = new OAI2Exception('badResumptionToken');
}
}
2013-05-14 23:24:59 +02:00
$resumptionToken = $this->args['resumptionToken'];
} else {
$resumptionToken = null;
2013-05-14 21:46:15 +02:00
}
2013-05-15 02:56:52 +02:00
if (empty($this->errors)) {
2013-05-14 23:24:59 +02:00
if ($sets = call_user_func($this->listSetsCallback, $resumptionToken)) {
2013-05-14 23:24:59 +02:00
foreach($sets as $set) {
2013-05-14 23:24:59 +02:00
$setNode = $this->response->addToVerbNode("set");
2013-05-14 23:24:59 +02:00
foreach($set as $key => $val) {
if($key=='setDescription') {
$desNode = $this->response->addChild($setNode,$key);
$des = $this->response->doc->createDocumentFragment();
$des->appendXML($val);
$desNode->appendChild($des);
} else {
$this->response->addChild($setNode,$key,$val);
}
2013-05-12 01:06:17 +02:00
}
}
2013-05-14 23:24:59 +02:00
} else {
$this->errors[] = new OAI2Exception('noSetHierarchy');
2013-05-12 01:06:17 +02:00
}
}
}
2013-05-14 23:24:59 +02:00
public function GetRecord() {
2013-05-12 01:06:17 +02:00
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['identifier'])) {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-14 21:46:15 +02:00
}
2013-05-12 01:06:17 +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'])) {
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +02:00
$identifier = $record['identifier'];
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +02:00
$datestamp = $this->formatDatestamp($record['datestamp']);
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +02:00
$set = $record['set'];
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +02:00
$status_deleted = (isset($record['deleted']) && ($record['deleted'] == 'true') &&
(($this->identifyResponse['deletedRecord'] == 'transient') ||
($this->identifyResponse['deletedRecord'] == 'persistent')));
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +02:00
$cur_record = $this->response->addToVerbNode('record');
$cur_header = $this->response->createHeader($identifier, $datestamp, $set, $cur_record);
if ($status_deleted) {
$cur_header->setAttribute("status","deleted");
} else {
$this->add_metadata($cur_record, $record);
}
} 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() {
$maxItems = 1000;
$deliveredRecords = 0;
$metadataPrefix = $this->args['metadataPrefix'];
$from = isset($this->args['from']) ? $this->args['from'] : '';
$until = isset($this->args['until']) ? $this->args['until'] : '';
$set = isset($this->args['set']) ? $this->args['set'] : '';
2013-05-12 02:18:36 +02:00
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 {
2013-05-14 23:24:59 +02:00
if ((int)$val+$this->token_valid < time()) {
2013-05-14 21:46:15 +02:00
$this->errors[] = new OAI2Exception('badResumptionToken');
2013-05-14 23:24:59 +02:00
} else {
if (!file_exists($this->token_prefix.$this->args['resumptionToken'])) {
$this->errors[] = new OAI2Exception('badResumptionToken');
2013-05-14 23:24:59 +02:00
} else {
if ($readings = $this->readResumptionToken($this->token_prefix.$this->args['resumptionToken'])) {
list($deliveredRecords, $metadataPrefix, $from, $until, $set) = $readings;
} 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'])) {
2013-05-14 23:24:59 +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'])) {
2013-05-14 23:24:59 +02:00
if(!$this->checkDateFormat($this->args['until'])) {
$this->errors[] = new OAI2Exception('badArgument');
2013-05-14 21:46:15 +02:00
}
}
2013-05-12 02:18:36 +02:00
}
2013-05-12 01:06:17 +02:00
2013-05-15 02:56:52 +02:00
if (empty($this->errors)) {
2013-05-14 23:24:59 +02:00
try {
2013-05-14 23:24:59 +02:00
$records_count = call_user_func($this->listRecordsCallback, $metadataPrefix, $from, $until, $set, true);
2013-05-14 23:24:59 +02:00
$records = call_user_func($this->listRecordsCallback, $metadataPrefix, $from, $until, $set, false, $deliveredRecords, $maxItems);
2013-05-14 21:46:15 +02:00
2013-05-14 23:24:59 +02:00
foreach ($records as $record) {
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +02:00
$identifier = $record['identifier'];
$datestamp = $this->formatDatestamp($record['datestamp']);
$setspec = $record['set'];
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +02:00
$status_deleted = (isset($record['deleted']) && ($record['deleted'] === true) &&
(($this->identifyResponse['deletedRecord'] == 'transient') ||
($this->identifyResponse['deletedRecord'] == 'persistent')));
2013-05-12 01:06:17 +02:00
2013-05-15 02:56:52 +02:00
if($this->verb == 'ListRecords') {
$cur_record = $this->response->addToVerbNode('record');
2013-05-14 23:24:59 +02:00
$cur_header = $this->response->createHeader($identifier, $datestamp,$setspec,$cur_record);
if (!$status_deleted) {
$this->add_metadata($cur_record, $record);
}
} else { // for ListIdentifiers, only identifiers will be returned.
$cur_header = $this->response->createHeader($identifier, $datestamp,$setspec);
}
if ($status_deleted) {
$cur_header->setAttribute("status","deleted");
}
}
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +02:00
// Will we need a new ResumptionToken?
if ($records_count - $deliveredRecords > $maxItems) {
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +02:00
$deliveredRecords += $maxItems;
$restoken = $this->createResumptionToken($deliveredRecords);
2013-05-12 01:06:17 +02:00
2013-05-14 23:24:59 +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)) {
$this->response->createResumptionToken($restoken,$expirationDatetime,$records_count,$deliveredRecords);
}
2013-05-14 23:24:59 +02:00
} catch (OAI2Exception $e) {
$this->errors[] = $e;
2013-05-12 01:06:17 +02:00
}
}
}
private function add_metadata($cur_record, $record) {
2013-05-12 01:06:17 +02:00
2013-05-14 21:46:15 +02:00
$meta_node = $this->response->addChild($cur_record ,"metadata");
2013-05-12 01:06:17 +02:00
2013-05-14 21:46:15 +02:00
$schema_node = $this->response->addChild($meta_node, $record['metadata']['container_name']);
foreach ($record['metadata']['container_attributes'] as $name => $value) {
$schema_node->setAttribute($name, $value);
2013-05-12 01:06:17 +02:00
}
foreach ($record['metadata']['fields'] as $name => $value) {
2013-05-14 21:46:15 +02:00
$this->response->addChild($schema_node, $name, $value);
}
}
private function createResumptionToken($delivered_records) {
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) {
exit("Cannot write. Writer permission needs to be changed.");
}
2013-05-14 23:24:59 +02:00
fputs($fp, "$delivered_records#");
fputs($fp, "$metadataPrefix#");
fputs($fp, "{$this->args['from']}#");
fputs($fp, "{$this->args['until']}#");
fputs($fp, "{$this->args['set']}#");
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;
}
/**
* All datestamps used in this system are GMT even
* return value from database has no TZ information
*/
private function formatDatestamp($datestamp) {
return date("Y-m-d\TH:i:s\Z",strtotime($datestamp));
}
/**
* The database uses datastamp without time-zone information.
* It needs to clean all time-zone informaion from time string and reformat it
*/
private function checkDateFormat($date) {
$date = str_replace(array("T","Z")," ",$date);
$time_val = strtotime($date);
if(!$time_val) return false;
if(strstr($date,":")) {
return date("Y-m-d H:i:s",$time_val);
} else {
return date("Y-m-d",$time_val);
}
2013-05-12 02:18:36 +02:00
}
2013-05-12 01:06:17 +02:00
}