More refactoring

This commit is contained in:
Daniel Neis Araujo 2013-05-14 18:24:59 -03:00
parent 1a2e5be242
commit cef72091cf
6 changed files with 202 additions and 314 deletions

View File

@ -1,18 +1,5 @@
<?php
/**
* OAI Data Provider command processor
*
* OAI Data Provider is not designed for human to retrieve data.
*
* This is an implementation of OAI Data Provider version 2.0.
* @see http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm
*
* It needs other files:
* - oaidp-util.php : Utility functions
*
*/
require_once('oaidp-util.php');
require_once('oai2server.php');
/**

View File

@ -12,7 +12,6 @@ class OAI2Exception extends Exception {
'text' => "The value '{$value}' of attribute '{$argument}' on element 'request' is not valid with respect to its type, 'UTCdatetimeType'.",
'code' => 'badArgument',
),
'badResumptionToken' => array(
'text' => "The resumptionToken '{$value}' does not exist or has already expired.",
),
@ -32,12 +31,6 @@ class OAI2Exception extends Exception {
),
'idDoesNotExist' => array(
'text' => "The value '{$value}' of the identifier does not exist in this repository.",
/*
if (!is_valid_uri($value)) {
'code' = 'badArgument',
'text' .= ' Invalidated URI has been detected.',
}
*/
),
'missingArgument' => array(
'text' => "The required argument '{$argument}' is missing in the request.",

View File

@ -1,35 +1,31 @@
<?php
require_once('oai2exception.php');
require_once('oai2xml.php');
/**
* The content-type the WWW-server delivers back. For debug-puposes, "text/plain"
* is easier to view. On a production site you should use "text/xml".
* This is an implementation of OAI Data Provider version 2.0.
* @see http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm
*/
define('CONTENT_TYPE', 'Content-Type: text/xml');
/** After 24 hours resumptionTokens become invalid. Unit is second. */
define('TOKEN_VALID',24*3600);
/** Where token is saved and path is included */
define('TOKEN_PREFIX','/tmp/oai_pmh-');
class OAI2Server {
public $errors = array();
private $args = array();
private $verb = '';
private $token_prefix = '/tmp/oai_pmh-';
private $token_valid = 86400;
function __construct($uri, $args, $identifyResponse, $callbacks) {
if (!isset($args['verb']) || empty($args['verb'])) {
$this->errors[] = new OAI2Exception('noVerb');
$this->errorResponse();
}
} 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->uri = $uri;
@ -43,84 +39,47 @@ class OAI2Server {
$this->response = new OAI2XMLResponse($this->uri, $this->verb, $this->args);
$this->respond();
call_user_func(array($this, $this->verb));
} else {
$this->errors[] = new OAI2Exception('badVerb', $args['verb']);
}
private function respond() {
switch ($this->verb) {
case 'Identify': $this->identify(); break;
case 'ListMetadataFormats': $this->listMetadataFormats(); break;
case 'ListSets': $this->listSets(); break;
case 'ListIdentifiers':
case 'ListRecords': $this->listRecords(); break;
case 'GetRecord': $this->getRecord(); break;
default: $this->errors[] = new OAI2Exception('badVerb', $this->args['verb']);
}
if (empty($this->errors)) {
header(CONTENT_TYPE);
$this->response->display();
} else {
$this->errorResponse();
}
}
private function errorResponse() {
$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());
}
header(CONTENT_TYPE);
$errorResponse->display();
exit();
}
}
/**
* Response to Verb Identify
*
* Tell the world what the data provider is. Usually it is static once the provider has been set up.
*
* http://www.openarchives.org/OAI/2.0/guidelines-oai-identifier.htm for details
*/
public function identify() {
public function Identify() {
if (count($this->args) > 0) {
foreach($args as $key => $val) {
foreach($this->args as $key => $val) {
$this->errors[] = new OAI2Exception('badArgument', $key, $val);
}
$this->errorResponse();
}
} else {
foreach($this->identifyResponse as $key => $val) {
$this->response->addToVerbNode($key, $val);
}
}
}
/**
* Response to Verb ListMetadataFormats
*
* The information of supported metadata formats
*/
public function listMetadataFormats() {
public function ListMetadataFormats() {
foreach ($this->args as $argument => $value) {
if ($argument != 'identifier') {
$this->errors[] = new OAI2Exception('badArgument', $argument, $value);
}
}
if (!empty($this->errors)) {
$this->errorResponse();
}
if (empty($this->errors)) {
try {
if ($formats = call_user_func($this->listMetadataFormatsCallback, $this->args['identifier'])) {
foreach($formats as $key => $val) {
@ -136,29 +95,24 @@ class OAI2Server {
$this->errors[] = $e;
}
}
}
/**
* Response to Verb ListSets
*
* Lists what sets are available to records in the system.
* This variable is filled in config-sets.php
*/
public function listSets() {
public function ListSets() {
if (isset($this->args['resumptionToken'])) {
if (count($this->args) > 1) {
$this->errors[] = new OAI2Exception('exclusiveArgument');
} else {
if ((int)$val+TOKEN_VALID < time()) {
if ((int)$val+$this->token_valid < time()) {
$this->errors[] = new OAI2Exception('badResumptionToken');
}
}
$resumptionToken = $this->args['resumptionToken'];
} else {
$resumptionToken = null;
}
if (!empty($this->errors)) {
$this->errorResponse();
}
if ($sets = call_user_func($this->listSetsCallback)) {
if ($sets = call_user_func($this->listSetsCallback, $resumptionToken)) {
foreach($sets as $set) {
@ -179,17 +133,9 @@ class OAI2Server {
$this->errors[] = new OAI2Exception('noSetHierarchy');
}
}
}
/**
* Response to Verb GetRecord
*
* Retrieve a record based its identifier.
*
* Local variables <B>$metadataPrefix</B> and <B>$identifier</B> need to be provided through global array variable <B>$args</B>
* by their indexes 'metadataPrefix' and 'identifier'.
* The reset of information will be extracted from database based those two parameters.
*/
public function getRecord() {
public function GetRecord() {
if (!isset($this->args['metadataPrefix'])) {
$this->errors[] = new OAI2Exception('missingArgument', 'metadataPrefix');
@ -202,16 +148,14 @@ class OAI2Server {
if (!isset($this->args['identifier'])) {
$this->errors[] = new OAI2Exception('missingArgument', 'identifier');
}
if (!empty($this->errors)) {
$this->errorResponse();
}
if (empty($this->errors)) {
try {
if ($record = call_user_func($this->getRecordCallback, $this->args['identifier'], $this->args['metadataPrefix'])) {
$identifier = $record['identifier'];
$datestamp = formatDatestamp($record['datestamp']);
$datestamp = $this->formatDatestamp($record['datestamp']);
$set = $record['set'];
@ -226,28 +170,44 @@ class OAI2Server {
} else {
$this->add_metadata($cur_record, $record);
}
} else {
$this->errors[] = new OAI2Exception('idDoesNotExist', 'identifier', $identifier);
}
} catch (OAI2Exception $e) {
$this->errors[] = $e;
}
}
}
/**
* Response to Verb ListRecords
*
* Lists records according to conditions. If there are too many, a resumptionToken is generated.
* - If a request comes with a resumptionToken and is still valid, read it and send back records.
* - Otherwise, set up a query with conditions such as: 'metadataPrefix', 'from', 'until', 'set'.
* Only 'metadataPrefix' is compulsory. All conditions are accessible through global array variable <B>$args</B> by keywords.
*/
public function listRecords() {
public function ListIdentifiers() {
return $this->ListRecords();
}
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'] : '';
if (isset($this->args['resumptionToken'])) {
if (count($this->args) > 1) {
$this->errors[] = new OAI2Exception('exclusiveArgument');
} else {
if ((int)$val+TOKEN_VALID < time()) {
if ((int)$val+$this->token_valid < time()) {
$this->errors[] = new OAI2Exception('badResumptionToken');
} else {
if (!file_exists($this->token_prefix.$this->args['resumptionToken'])) {
$this->errors[] = new OAI2Exception('badResumptionToken', '', $this->args['resumptionToken']);
} else {
if ($readings = $this->readResumptionToken($this->token_prefix.$this->args['resumptionToken'])) {
list($deliveredRecords, $metadataPrefix, $from, $until, $set) = $readings;
} else {
$this->errors[] = new OAI2Exception('badResumptionToken', '', $this->args['resumptionToken']);
}
}
}
}
} else {
@ -260,49 +220,18 @@ class OAI2Server {
}
}
if (isset($this->args['from'])) {
if(!checkDateFormat($this->args['from'])) {
if(!$this->checkDateFormat($this->args['from'])) {
$this->errors[] = new OAI2Exception('badGranularity', 'from', $this->args['from']);
}
}
if (isset($this->args['until'])) {
if(!checkDateFormat($this->args['until'])) {
if(!$this->checkDateFormat($this->args['until'])) {
$this->errors[] = new OAI2Exception('badGranularity', 'until', $this->args['until']);
}
}
}
if (!empty($this->errors)) {
$this->errorResponse();
}
// Resume previous session?
if (isset($this->args['resumptionToken'])) {
if (!file_exists(TOKEN_PREFIX.$this->args['resumptionToken'])) {
$this->errors[] = new OAI2Exception('badResumptionToken', '', $this->args['resumptionToken']);
} else {
if ($readings = $this->readResumptionToken(TOKEN_PREFIX.$this->args['resumptionToken'])) {
list($deliveredRecords, $metadataPrefix, $from, $until, $set) = $readings;
} else {
$this->errors[] = new OAI2Exception('badResumptionToken', '', $this->args['resumptionToken']);
}
}
if (!empty($this->errors)) {
$this->errorResponse();
}
} else {
$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'] : '';
}
$maxItems = 1000;
try {
$records_count = call_user_func($this->listRecordsCallback, $metadataPrefix, $from, $until, $set, true);
@ -312,7 +241,7 @@ class OAI2Server {
foreach ($records as $record) {
$identifier = $record['identifier'];
$datestamp = formatDatestamp($record['datestamp']);
$datestamp = $this->formatDatestamp($record['datestamp']);
$setspec = $record['set'];
$status_deleted = (isset($record['deleted']) && ($record['deleted'] === true) &&
@ -339,7 +268,7 @@ class OAI2Server {
$deliveredRecords += $maxItems;
$restoken = $this->createResumptionToken($deliveredRecords);
$expirationDatetime = gmstrftime('%Y-%m-%dT%TZ', time()+TOKEN_VALID);
$expirationDatetime = gmstrftime('%Y-%m-%dT%TZ', time()+$this->token_valid);
} elseif (isset($args['resumptionToken'])) {
// Last delivery, return empty ResumptionToken
@ -355,6 +284,7 @@ class OAI2Server {
$this->errors[] = $e;
}
}
}
private function add_metadata($cur_record, $record) {
@ -374,7 +304,7 @@ class OAI2Server {
list($usec, $sec) = explode(" ", microtime());
$token = ((int)($usec*1000) + (int)($sec*1000));
$fp = fopen (TOKEN_PREFIX.$token, 'w');
$fp = fopen ($this->token_prefix.$token, 'w');
if($fp==false) {
exit("Cannot write. Writer permission needs to be changed.");
}
@ -399,4 +329,27 @@ class OAI2Server {
}
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);
}
}
}

View File

@ -27,6 +27,7 @@ class OAI2XMLResponse {
function display() {
$this->doc->formatOutput = true;
$this->doc->preserveWhiteSpace = false;
header('Content-Type: text/xml');
echo $this->doc->saveXML();
}

View File

@ -1,46 +0,0 @@
<?php
/**
* \file
* \brief Utilities for the OAI Data Provider
*
* A collection of functions used.
*/
/** Validates an identifier. The pattern is: '/^[-a-z\.0-9]+$/i' which means
* it accepts -, letters and numbers.
* Used only by function <B>oai_error</B> code idDoesNotExist.
* \param $url Type: string
*/
function is_valid_uri($url) {
return((bool)preg_match('/^[-a-z\.0-9]+$/i', $url));
}
/** Validates attributes come with the query.
* It accepts letters, numbers, ':', '_', '.' and -.
* Here there are few more match patterns than is_valid_uri(): ':_'.
* \param $attrb Type: string
*/
function is_valid_attrb($attrb) {
return preg_match("/^[_a-zA-Z0-9\-\:\.]+$/",$attrb);
}
/** All datestamps used in this system are GMT even
* return value from database has no TZ information
*/
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
*/
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);
}
}