only error codes defined by the OAI-PMH protocol

This commit is contained in:
Daniel Neis Araujo 2013-05-14 23:27:55 -03:00
parent 7d9c86c70a
commit e18e5fff48
3 changed files with 31 additions and 55 deletions

View File

@ -60,7 +60,7 @@ $oai2 = new OAI2Server($uri, $args, $identifyResponse,
'ListMetadataFormats' =>
function($identifier = '') {
if (!empty($identifier) && $identifier != 'a.b.c') {
throw new OAI2Exception('idDoesNotExist', '', $identifier);
throw new OAI2Exception('idDoesNotExist');
}
return
array('rif' => array('metadataPrefix'=>'rif',
@ -95,18 +95,22 @@ $oai2 = new OAI2Server($uri, $args, $identifyResponse,
'ListRecords' =>
function($metadataPrefix, $from = '', $until = '', $set = '', $count = false, $deliveredRecords = 0, $maxItems = 0) use ($example_record) {
// throws new OAI2Exception('noRecordsMatch')
// throws new OAI2Exception('noSetHierarchy')
if ($count) {
return 1;
}
if ($set != '') {
throw new OAI2Exception('noSetHierarchy');
}
if ($metadataPrefix != 'oai_dc') {
throw new OAI2Exception('noRecordsMatch');
}
return array($example_record);
},
'GetRecord' =>
function($identifier, $metadataPrefix) use ($example_record) {
if ($identifier != 'a.b.c') {
throw new OAI2Exception('idDoesNotExist', '', $identifier);
throw new OAI2Exception('idDoesNotExist');
}
return $example_record;
},

View File

@ -2,61 +2,33 @@
class OAI2Exception extends Exception {
function __construct($code = 0, $argument = '', $value = '') {
function __construct($code) {
$this->errorTable = array(
'badArgument' => array(
'text' => "Attribute '{$argument}' is not allowed to appear in element 'request'.",
'text' => "The request includes illegal arguments, is missing required arguments, includes a repeated argument, or values for arguments have an illegal syntax.",
),
'badResumptionToken' => array(
'text' => "The resumptionToken '{$value}' does not exist or has already expired.",
),
'badGranularity' => array(
'text' => "The value '{$value}' of attribute '{$argument}' on element 'request' is not valid with respect to its type, 'UTCdatetimeType'.",
'code' => 'badArgument',
'text' => "The value of the resumptionToken argument is invalid or expired",
),
'badVerb' => array(
'text' => "Illegal OAI verb",
'text' => "Value of the verb argument is not a legal OAI-PMH verb, the verb argument is missing, or the verb argument is repeated.",
),
'cannotDisseminateFormat' => array(
'text' => "The metadata format '{$value}' given by {$argument} is not supported by this repository.",
),
'exclusiveArgument' => array(
'text' => 'The usage of resumptionToken as an argument allows no other arguments.',
'code' => 'badArgument',
'text' => "The metadata format identified by the value given for the metadataPrefix argument is not supported by the item or by the repository.",
),
'idDoesNotExist' => array(
'text' => "The value '{$value}' of the identifier does not exist in this repository.",
),
'missingArgument' => array(
'text' => "The required argument '{$argument}' is missing in the request.",
'code' => 'badArgument',
'text' => "The value of the identifier argument is unknown or illegal in this repository.",
),
'noRecordsMatch' => array(
'text' => 'The combination of the given values results in an empty list.',
'text' => 'The combination of the values of the from, until, set and metadataPrefix arguments results in an empty list.',
),
'noMetadataFormats' => array(
'text' => 'There are no metadata formats available for the specified item.',
),
'noSetHierarchy' => array(
'text' => 'This repository does not support sets.',
'text' => 'The repository does not support sets.',
),
'sameArgument' => array(
'text' => 'Do not use the same argument more than once.',
'code' => 'badArgument',
),
'sameVerb' => array(
'text' => 'Do not use verb more than once.',
'code' => 'badVerb',
),
'notImp' => array(
'text' => 'Not yet implemented.',
'code' => 'debug',
),
''=> array(
'text' => "Unknown error: code: '{'code'}', argument: '{$argument}', value: '{$value}'",
'code' => 'badArgument',
)
);
parent::__construct($this->errorTable[$code]['text']);
$this->code = $code;

View File

@ -42,7 +42,7 @@ class OAI2Server {
call_user_func(array($this, $this->verb));
} else {
$this->errors[] = new OAI2Exception('badVerb', $args['verb']);
$this->errors[] = new OAI2Exception('badVerb');
}
}
@ -66,7 +66,7 @@ class OAI2Server {
if (count($this->args) > 0) {
foreach($this->args as $key => $val) {
$this->errors[] = new OAI2Exception('badArgument', $key, $val);
$this->errors[] = new OAI2Exception('badArgument');
}
} else {
foreach($this->identifyResponse as $key => $val) {
@ -79,7 +79,7 @@ class OAI2Server {
foreach ($this->args as $argument => $value) {
if ($argument != 'identifier') {
$this->errors[] = new OAI2Exception('badArgument', $argument, $value);
$this->errors[] = new OAI2Exception('badArgument');
}
}
if (isset($this->args['identifier'])) {
@ -109,7 +109,7 @@ class OAI2Server {
if (isset($this->args['resumptionToken'])) {
if (count($this->args) > 1) {
$this->errors[] = new OAI2Exception('exclusiveArgument');
$this->errors[] = new OAI2Exception('badArgument');
} else {
if ((int)$val+$this->token_valid < time()) {
$this->errors[] = new OAI2Exception('badResumptionToken');
@ -146,15 +146,15 @@ class OAI2Server {
public function GetRecord() {
if (!isset($this->args['metadataPrefix'])) {
$this->errors[] = new OAI2Exception('missingArgument', 'metadataPrefix');
$this->errors[] = new OAI2Exception('badArgument');
} else {
$metadataFormats = call_user_func($this->listMetadataFormatsCallback);
if (!isset($metadataFormats[$this->args['metadataPrefix']])) {
$this->errors[] = new OAI2Exception('cannotDisseminateFormat', 'metadataPrefix', $this->args['metadataPrefix']);
$this->errors[] = new OAI2Exception('cannotDisseminateFormat');
}
}
if (!isset($this->args['identifier'])) {
$this->errors[] = new OAI2Exception('missingArgument', 'identifier');
$this->errors[] = new OAI2Exception('badArgument');
}
if (empty($this->errors)) {
@ -179,7 +179,7 @@ class OAI2Server {
$this->add_metadata($cur_record, $record);
}
} else {
$this->errors[] = new OAI2Exception('idDoesNotExist', 'identifier', $identifier);
$this->errors[] = new OAI2Exception('idDoesNotExist');
}
} catch (OAI2Exception $e) {
$this->errors[] = $e;
@ -202,39 +202,39 @@ class OAI2Server {
if (isset($this->args['resumptionToken'])) {
if (count($this->args) > 1) {
$this->errors[] = new OAI2Exception('exclusiveArgument');
$this->errors[] = new OAI2Exception('badArgument');
} else {
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']);
$this->errors[] = new OAI2Exception('badResumptionToken');
} 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']);
$this->errors[] = new OAI2Exception('badResumptionToken');
}
}
}
}
} else {
if (!isset($this->args['metadataPrefix'])) {
$this->errors[] = new OAI2Exception('missingArgument', 'metadataPrefix');
$this->errors[] = new OAI2Exception('badArgument');
} else {
$metadataFormats = call_user_func($this->listMetadataFormatsCallback);
if (!isset($metadataFormats[$this->args['metadataPrefix']])) {
$this->errors[] = new OAI2Exception('cannotDisseminateFormat', 'metadataPrefix', $this->args['metadataPrefix']);
$this->errors[] = new OAI2Exception('cannotDisseminateFormat');
}
}
if (isset($this->args['from'])) {
if(!$this->checkDateFormat($this->args['from'])) {
$this->errors[] = new OAI2Exception('badGranularity', 'from', $this->args['from']);
$this->errors[] = new OAI2Exception('badArgument');
}
}
if (isset($this->args['until'])) {
if(!$this->checkDateFormat($this->args['until'])) {
$this->errors[] = new OAI2Exception('badGranularity', 'until', $this->args['until']);
$this->errors[] = new OAI2Exception('badArgument');
}
}
}