Reindentinf + fixing error messages

This commit is contained in:
Usuario Moodle 2013-05-07 17:37:50 -03:00
parent d3ec5f3d7c
commit 04669d927b
2 changed files with 222 additions and 229 deletions

View File

@ -30,11 +30,6 @@
* For generic usage, you can try the ANDS_Response_XML defined in xml_creater.php. * For generic usage, you can try the ANDS_Response_XML defined in xml_creater.php.
*/ */
// Report all errors except E_NOTICE
// This is the default value set in php.ini
// If anything else, try them.
// error_reporting (E_ALL ^ E_NOTICE);
/** /**
* An array for collecting erros which can be reported later. It will be checked before a new action is taken. * An array for collecting erros which can be reported later. It will be checked before a new action is taken.
*/ */

View File

@ -6,7 +6,7 @@
* Example usage: * Example usage:
* *
* \code * \code
* $par_array = array("verb"=>"ListRecords","resumptionToken"=>"9CD1DA87F59C3E960871F4F3C9D093887C17D174"); * $par_array = array("verb"=>"ListRecords","resumptionToken"=>"9CD1DA87F59C3E960871F4F3C9D093887C17D174");
* // Example 1: Error response * // Example 1: Error response
* $error_array[] = oai_error("badVerb","Rubish"); * $error_array[] = oai_error("badVerb","Rubish");
* $error_array[] = oai_error("sameVerb"); * $error_array[] = oai_error("sameVerb");
@ -25,7 +25,6 @@
* *
* \see http://www.openarchives.org/OAI/openarchivesprotocol.html#ErrorConditions * \see http://www.openarchives.org/OAI/openarchivesprotocol.html#ErrorConditions
*/ */
/* /*
http://www.openarchives.org/OAI/openarchivesprotocol.html#ErrorConditions http://www.openarchives.org/OAI/openarchivesprotocol.html#ErrorConditions
@ -47,99 +46,99 @@ idDoesNotExist:
noRecordsMatch: noRecordsMatch:
The combination of the values of the from, until, set and metadataPrefix arguments results in an empty list. Applied to ListIdentifiers, ListRecords The combination of the values of the from, until, set and metadataPrefix arguments results in an empty list. Applied to ListIdentifiers, ListRecords
noMetadataFormats: noMetadataFormats:
There are no metadata formats available for the specified item. Applied to ListMetadataFormats. There are no metadata formats available for the specified item. Applied to ListMetadataFormats.
noSetHierarchy: noSetHierarchy:
The repository does not support sets. Applied to ListSets, ListIdentifiers, ListRecords The repository does not support sets. Applied to ListSets, ListIdentifiers, ListRecords
*/ */
/** utility funciton to mapping error codes to readable messages */ /** utility funciton to mapping error codes to readable messages */
function oai_error($code, $argument = '', $value = '') function oai_error($code, $argument = '', $value = '') {
{
switch ($code) {
case 'badArgument' :
$text = "Attribute '{$argument}' is not allowed to appear in element 'request'";
break;
case 'badGranularity' : switch ($code) {
$text = "The value '{$value}' of the argument '{$argument}' is not valid."; case 'badArgument' :
$code = 'badArgument'; $text = "Attribute '{$argument}' is not allowed to appear in element 'request'.";
break; break;
case 'badResumptionToken' : case 'badGranularity' :
$text = "The resumptionToken '{$value}' does not exist or has already expired."; $text = "The value '{$value}' of attribute '{$argument}' on element 'request' is not valid with respect to its type, 'UTCdatetimeType'.";
break; $code = 'badArgument';
break;
case 'badRequestMethod' : case 'badResumptionToken' :
$text = "The request method '{$argument}' is unknown."; $text = "The resumptionToken '{$value}' does not exist or has already expired.";
$code = 'badVerb'; break;
break;
case 'badVerb' : case 'badRequestMethod' :
$text = "The verb '{$argument}' provided in the request is illegal."; $text = "The request method '{$argument}' is unknown.";
break; $code = 'badVerb';
break;
case 'cannotDisseminateFormat' : case 'badVerb' :
$text = "The metadata format '{$value}' given by {$argument} is not supported by this repository."; $text = "The value '{$argument}' of attribute 'verb' on element 'request' is not valid with respect to its type, 'verbType'";
break; break;
case 'exclusiveArgument' : case 'cannotDisseminateFormat' :
$text = 'The usage of resumptionToken as an argument allows no other arguments.'; $text = "The metadata format '{$value}' given by {$argument} is not supported by this repository.";
$code = 'badArgument'; break;
break;
case 'idDoesNotExist' : case 'exclusiveArgument' :
$text = "The value '{$value}' of the identifier does not exist in this repository."; $text = 'The usage of resumptionToken as an argument allows no other arguments.';
if (!is_valid_uri($value)) { $code = 'badArgument';
$code = 'badArgument'; break;
$text .= ' Invalidated URI has been detected.';
}
break;
case 'missingArgument' : case 'idDoesNotExist' :
$text = "The required argument '{$argument}' is missing in the request."; $text = "The value '{$value}' of the identifier does not exist in this repository.";
$code = 'badArgument'; if (!is_valid_uri($value)) {
break; $code = 'badArgument';
$text .= ' Invalidated URI has been detected.';
}
break;
case 'noRecordsMatch' : case 'missingArgument' :
$text = 'The combination of the given values results in an empty list.'; $text = "The required argument '{$argument}' is missing in the request.";
break; $code = 'badArgument';
break;
case 'noMetadataFormats' : case 'noRecordsMatch' :
$text = 'There are no metadata formats available for the specified item.'; $text = 'The combination of the given values results in an empty list.';
break; break;
case 'noVerb' : case 'noMetadataFormats' :
$text = 'The request does not provide any verb.'; $text = 'There are no metadata formats available for the specified item.';
$code = 'badVerb'; break;
break;
case 'noSetHierarchy' : case 'noVerb' :
$text = 'This repository does not support sets.'; $text = 'The request does not provide any verb.';
break; $code = 'badVerb';
break;
case 'sameArgument' : case 'noSetHierarchy' :
$text = 'Do not use the same argument more than once.'; $text = 'This repository does not support sets.';
$code = 'badArgument'; break;
break;
case 'sameVerb' : case 'sameArgument' :
$text = 'Do not use verb more than once.'; $text = 'Do not use the same argument more than once.';
$code = 'badVerb'; $code = 'badArgument';
break; break;
case 'notImp' : case 'sameVerb' :
$text = 'Not yet implemented.'; $text = 'Do not use verb more than once.';
$code = 'debug'; $code = 'badVerb';
break; break;
default: case 'notImp' :
$text = "Unknown error: code: '{$code}', argument: '{$argument}', value: '{$value}'"; $text = 'Not yet implemented.';
$code = 'badArgument'; $code = 'debug';
} break;
return $code."|".$text;
default:
$text = "Unknown error: code: '{$code}', argument: '{$argument}', value: '{$value}'";
$code = 'badArgument';
}
return $code."|".$text;
} }
/** /**
@ -147,76 +146,75 @@ function oai_error($code, $argument = '', $value = '')
*/ */
class ANDS_XML { class ANDS_XML {
public $doc; /**< Type: DOMDocument. Handle of current XML Document object */ public $doc; /**< Type: DOMDocument. Handle of current XML Document object */
/** /**
* Constructs an ANDS_XML object. * Constructs an ANDS_XML object.
* *
* @param $par_array Type: array. * @param $par_array Type: array.
* Array of request parameters for creating an ANDS_XML object. * Array of request parameters for creating an ANDS_XML object.
* \see create_request. * \see create_request.
*/ */
function __construct($par_array) { function __construct($par_array) {
$this->doc = new DOMDocument("1.0","UTF-8"); $this->doc = new DOMDocument("1.0","UTF-8");
// oai_node equals to $this->doc->documentElement; // oai_node equals to $this->doc->documentElement;
$oai_node = $this->doc->createElement("OAI-PMH"); $oai_node = $this->doc->createElement("OAI-PMH");
$oai_node->setAttribute("xmlns","http://www.openarchives.org/OAI/2.0/"); $oai_node->setAttribute("xmlns","http://www.openarchives.org/OAI/2.0/");
$oai_node->setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance"); $oai_node->setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
$oai_node->setAttribute("xsi:schemaLocation","http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"); $oai_node->setAttribute("xsi:schemaLocation","http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd");
$this->addChild($oai_node,"responseDate",gmdate("Y-m-d\TH:i:s\Z")); $this->addChild($oai_node,"responseDate",gmdate("Y-m-d\TH:i:s\Z"));
$this->doc->appendChild($oai_node); $this->doc->appendChild($oai_node);
$this->create_request($par_array); $this->create_request($par_array);
} }
/** /**
* Add a child node to a parent node on a XML Doc: a worker function. * Add a child node to a parent node on a XML Doc: a worker function.
* *
* @param $mom_node * @param $mom_node
* Type: DOMNode. The target node. * Type: DOMNode. The target node.
* *
* @param $name * @param $name
* Type: string. The name of child nade is being added * Type: string. The name of child nade is being added
* *
* @param $value * @param $value
* Type: string. Text for the adding node if it is a text node. * Type: string. Text for the adding node if it is a text node.
* *
* @return DOMElement $added_node * @return DOMElement $added_node
* The newly created node, can be used for further expansion. * The newly created node, can be used for further expansion.
* If no further expansion is expected, return value can be igored. * If no further expansion is expected, return value can be igored.
*/ */
function addChild($mom_node,$name, $value='') { function addChild($mom_node,$name, $value='') {
$added_node = $this->doc->createElement($name,$value); $added_node = $this->doc->createElement($name,$value);
$added_node = $mom_node->appendChild($added_node); $added_node = $mom_node->appendChild($added_node);
return $added_node; return $added_node;
} }
/**
* Create an OAI request node.
*
* @param $par_array Type: array
* The attributes of a request node. They describe the verb of the request and other associated parameters used in the request.
* Keys of the array define attributes, and values are their content.
*/
/** function create_request($par_array) {
* Create an OAI request node. $request = $this->addChild($this->doc->documentElement,"request",MY_URI);
* foreach($par_array as $key => $value) {
* @param $par_array Type: array $request->setAttribute($key,$value);
* The attributes of a request node. They describe the verb of the request and other associated parameters used in the request. }
* Keys of the array define attributes, and values are their content. }
*/
function create_request($par_array) { /**
$request = $this->addChild($this->doc->documentElement,"request",MY_URI); * Display a doc in a readable, well-formatted way for display or saving
foreach($par_array as $key => $value) { */
$request->setAttribute($key,$value); function display() {
} $pr = new DOMDocument();
} $pr->preserveWhiteSpace = false;
$pr->formatOutput = true;
/** $pr->loadXML($this->doc->saveXML());
* Display a doc in a readable, well-formatted way for display or saving echo $pr->saveXML();
*/ }
function display() {
$pr = new DOMDocument();
$pr->preserveWhiteSpace = false;
$pr->formatOutput = true;
$pr->loadXML($this->doc->saveXML());
echo $pr->saveXML();
}
} }
/** /**
@ -225,16 +223,16 @@ class ANDS_XML {
* It has only one derived member function * It has only one derived member function
*/ */
class ANDS_Error_XML extends ANDS_XML { class ANDS_Error_XML extends ANDS_XML {
function __construct($par_array, $error_array) { function __construct($par_array, $error_array) {
parent::__construct($par_array); parent::__construct($par_array);
$oai_node = $this->doc->documentElement; $oai_node = $this->doc->documentElement;
foreach($error_array as $e) { foreach($error_array as $e) {
list($code, $value) = explode("|", $e); list($code, $value) = explode("|", $e);
$node = $this->addChild($oai_node,"error",$value); $node = $this->addChild($oai_node,"error",$value);
$node->setAttribute("code",$code); $node->setAttribute("code",$code);
} }
} }
} }
/** /**
@ -243,86 +241,86 @@ class ANDS_Error_XML extends ANDS_XML {
* This is the class to further develop to suits a publication need * This is the class to further develop to suits a publication need
*/ */
class ANDS_Response_XML extends ANDS_XML { class ANDS_Response_XML extends ANDS_XML {
public $verbNode; /**< Type: DOMElement. Verb node itself. */ public $verbNode; /**< Type: DOMElement. Verb node itself. */
protected $verb; /**< Type: string. The verb in the request */ protected $verb; /**< Type: string. The verb in the request */
function __construct($par_array) {
parent::__construct($par_array);
$this->verb = $par_array["verb"];
$this->verbNode = $this->addChild($this->doc->documentElement,$this->verb);
}
/** Add direct child nodes to verb node (OAI-PMH), e.g. response to ListMetadataFormats. function __construct($par_array) {
* Different verbs can have different required child nodes. parent::__construct($par_array);
* \see create_record, create_header $this->verb = $par_array["verb"];
* \see http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm. $this->verbNode = $this->addChild($this->doc->documentElement,$this->verb);
* }
* \param $nodeName Type: string. The name of appending node.
* \param $value Type: string. The content of appending node.
*/
function add2_verbNode($nodeName, $value=null) {
return $this->addChild($this->verbNode,$nodeName,$value);
}
/** /** Add direct child nodes to verb node (OAI-PMH), e.g. response to ListMetadataFormats.
* Create an empty \<record\> node. Other nodes will be appended to it later. * Different verbs can have different required child nodes.
*/ * \see create_record, create_header
function create_record() { * \see http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm.
return $this->add2_verbNode("record"); *
} * \param $nodeName Type: string. The name of appending node.
* \param $value Type: string. The content of appending node.
/** Headers are enclosed inside of \<record\> to the query of ListRecords, ListIdentifiers and etc. */
* function add2_verbNode($nodeName, $value=null) {
* \param $identifier Type: string. The identifier string for node \<identifier\>. return $this->addChild($this->verbNode,$nodeName,$value);
* \param $timestamp Type: timestamp. Timestapme in UTC format for node \<datastamp\>. }
* \param $ands_class Type: mix. Can be an array or just a string. Content of \<setSpec\>.
* \param $add_to_node Type: DOMElement. Default value is null. /**
* In normal cases, $add_to_node is the \<record\> node created previously. When it is null, the newly created header node is attatched to $this->verbNode. * Create an empty \<record\> node. Other nodes will be appended to it later.
* Otherwise it will be attatched to the desired node defined in $add_to_node. */
*/ function create_record() {
function create_header($identifier,$timestamp,$ands_class, $add_to_node=null) { return $this->add2_verbNode("record");
if(is_null($add_to_node)) { }
$header_node = $this->add2_verbNode("header");
} else { /** Headers are enclosed inside of \<record\> to the query of ListRecords, ListIdentifiers and etc.
$header_node = $this->addChild($add_to_node,"header"); *
} * \param $identifier Type: string. The identifier string for node \<identifier\>.
$this->addChild($header_node,"identifier",$identifier); * \param $timestamp Type: timestamp. Timestapme in UTC format for node \<datastamp\>.
$this->addChild($header_node,"datestamp",$timestamp); * \param $ands_class Type: mix. Can be an array or just a string. Content of \<setSpec\>.
if (is_array($ands_class)) { * \param $add_to_node Type: DOMElement. Default value is null.
foreach ($ands_class as $setspec) { * In normal cases, $add_to_node is the \<record\> node created previously. When it is null, the newly created header node is attatched to $this->verbNode.
$this->addChild($header_node,"setSpec",$setspec); * Otherwise it will be attatched to the desired node defined in $add_to_node.
} */
} else { $this->addChild($header_node,"setSpec",$ands_class); } function create_header($identifier,$timestamp,$ands_class, $add_to_node=null) {
return $header_node; if(is_null($add_to_node)) {
} $header_node = $this->add2_verbNode("header");
} else {
/** Create metadata node for holding metadata. This is always added to \<record\> node. $header_node = $this->addChild($add_to_node,"header");
* }
* \param $mom_record_node DOMElement. A node acts as the parent node. $this->addChild($header_node,"identifier",$identifier);
* $this->addChild($header_node,"datestamp",$timestamp);
* @return $meta_node Type: DOMElement. if (is_array($ands_class)) {
* The newly created registryObject node which will be used for further expansion. foreach ($ands_class as $setspec) {
* metadata node itself is maintained by internally by the Class. $this->addChild($header_node,"setSpec",$setspec);
*/ }
function create_metadata($mom_record_node) { } else { $this->addChild($header_node,"setSpec",$ands_class); }
$meta_node = $this->addChild($mom_record_node,"metadata"); return $header_node;
return $meta_node; }
}
/** Create metadata node for holding metadata. This is always added to \<record\> node.
/** If there are too many records request could not finished a resumpToken is generated to let harvester know *
* * \param $mom_record_node DOMElement. A node acts as the parent node.
* \param $token Type: string. A random number created somewhere? *
* \param $expirationdatetime Type: string. A string representing time. * @return $meta_node Type: DOMElement.
* \param $num_rows Type: integer. Number of records retrieved. * The newly created registryObject node which will be used for further expansion.
* \param $cursor Type: string. Cursor can be used for database to retrieve next time. * metadata node itself is maintained by internally by the Class.
*/ */
function create_resumpToken($token, $expirationdatetime, $num_rows, $cursor=null) { function create_metadata($mom_record_node) {
$resump_node = $this->addChild($this->verbNode,"resumptionToken",$token); $meta_node = $this->addChild($mom_record_node,"metadata");
if(isset($expirationdatetime)) { return $meta_node;
$resump_node->setAttribute("expirationDate",$expirationdatetime); }
}
$resump_node->setAttribute("completeListSize",$num_rows); /** If there are too many records request could not finished a resumpToken is generated to let harvester know
$resump_node->setAttribute("cursor",$cursor); *
} * \param $token Type: string. A random number created somewhere?
* \param $expirationdatetime Type: string. A string representing time.
* \param $num_rows Type: integer. Number of records retrieved.
* \param $cursor Type: string. Cursor can be used for database to retrieve next time.
*/
function create_resumpToken($token, $expirationdatetime, $num_rows, $cursor=null) {
$resump_node = $this->addChild($this->verbNode,"resumptionToken",$token);
if(isset($expirationdatetime)) {
$resump_node->setAttribute("expirationDate",$expirationdatetime);
}
$resump_node->setAttribute("completeListSize",$num_rows);
$resump_node->setAttribute("cursor",$cursor);
}
} }