simple-oai-pmh/oai2.php

184 lines
5.2 KiB
PHP
Raw Normal View History

<?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-config.php : Configuration of provider
* - oaidp-util.php : Utility functions
* - xml_creater.php : XML generating functions
* - Actions:
* - identify.php : About the provider
* - listmetadataformats.php : List supported metadata formats
* - listrecords.php : List identifiers and records
* - listsets.php : List sets
* - getrecord.php : Get a record
* - Your own implementation for providing metadata records.
*
* It also initiates:
* - PDO datbase connection object $db.
* - ANDS_XML XML document handler $outputObj.
*
* \todo <b>Remember:</b> to define your own classess for generating metadata records.
* In common cases, you have to implement your own code to act fully and correctly.
* For generic usage, you can try the ANDS_Response_XML defined in xml_creater.php.
*/
2013-05-07 19:52:33 +02:00
/**
* An array for collecting erros which can be reported later. It will be checked before a new action is taken.
*/
$errors = array();
/**
* Supported attributes associate to verbs.
*/
$attribs = array ('from', 'identifier', 'metadataPrefix', 'set', 'resumptionToken', 'until');
if (in_array($_SERVER['REQUEST_METHOD'],array('GET','POST'))) {
2013-05-07 19:52:33 +02:00
$args = $_REQUEST;
} else {
2013-05-07 19:52:33 +02:00
$errors[] = oai_error('badRequestMethod', $_SERVER['REQUEST_METHOD']);
}
require_once('oaidp-util.php');
// Always using htmlentities() function to encodes the HTML entities submitted by others.
// No one can be trusted.
foreach ($args as $key => $val) {
2013-05-07 19:52:33 +02:00
$checking = htmlspecialchars(stripslashes($val));
if (!is_valid_attrb($checking)) {
$errors[] = oai_error('badArgument', $checking);
} else {$args[$key] = $checking; }
}
if (!empty($errors)) {
oai_exit();
}
foreach($attribs as $val) {
2013-05-07 19:52:33 +02:00
unset($$val);
}
require_once('oaidp-config.php');
2013-05-07 22:18:02 +02:00
require_once('config/metadataformats.php');
require_once('config/sets.php');
require_once('config/database.php');
// For generic usage or just trying:
// require_once('xml_creater.php');
// In common cases, you have to implement your own code to act fully and correctly.
require_once('ands_tpa.php');
// Default, there is no compression supported
$compress = FALSE;
if (isset($compression) && is_array($compression)) {
2013-05-07 19:52:33 +02:00
if (in_array('gzip', $compression) && ini_get('output_buffering')) {
$compress = TRUE;
}
}
if (SHOW_QUERY_ERROR) {
2013-05-07 19:52:33 +02:00
echo "Args:\n"; print_r($args);
}
2013-05-12 01:06:17 +02:00
if (isset($args['verb'])) {
2013-05-12 01:06:17 +02:00
require_once('oai2server.php');
$oai2 = new OAI2Server($args);
2013-05-07 19:52:33 +02:00
switch ($args['verb']) {
case 'Identify':
2013-05-12 01:06:17 +02:00
2013-05-07 19:52:33 +02:00
// we never use compression in Identify
$compress = FALSE;
2013-05-12 01:06:17 +02:00
if (count($args)>1) {
2013-05-07 19:52:33 +02:00
foreach($args as $key => $val) {
if(strcmp($key,"verb")!=0) {
$errors[] = oai_error('badArgument', $key, $val);
}
}
}
2013-05-12 01:06:17 +02:00
if (empty($errors)) {
$outputObj = $oai2->identify($show_identifier, $repositoryIdentifier, $delimiter, $sampleIdentifier);
}
2013-05-07 19:52:33 +02:00
break;
case 'ListMetadataFormats':
2013-05-12 01:06:17 +02:00
2013-05-07 19:52:33 +02:00
$checkList = array("ops"=>array("identifier"));
checkArgs($args, $checkList);
2013-05-12 01:06:17 +02:00
if (empty($errors)) {
$outputObj = $oai2->listMetadataFormats();
}
2013-05-07 19:52:33 +02:00
break;
case 'ListSets':
if (isset($args['resumptionToken']) && count($args) > 2) {
2013-05-07 19:52:33 +02:00
$errors[] = oai_error('exclusiveArgument');
}
$checkList = array("ops"=>array("resumptionToken"));
checkArgs($args, $checkList);
2013-05-12 01:06:17 +02:00
if (empty($errors)) {
$outputObj = $oai2->listSets($SETS);
}
2013-05-07 19:52:33 +02:00
break;
case 'GetRecord':
$checkList = array("required"=>array("metadataPrefix","identifier"));
checkArgs($args, $checkList);
2013-05-12 01:06:17 +02:00
if (empty($errors)) {
$outputObj = $oai2->getRecord();
}
2013-05-07 19:52:33 +02:00
break;
case 'ListIdentifiers':
case 'ListRecords':
if(isset($args['resumptionToken'])) {
if (count($args) > 2) {
$errors[] = oai_error('exclusiveArgument');
}
$checkList = array("ops"=>array("resumptionToken"));
} else {
$checkList = array("required"=>array("metadataPrefix"),"ops"=>array("from","until","set"));
}
checkArgs($args, $checkList);
2013-05-12 01:06:17 +02:00
if (empty($errors)) {
$outputObj = $oai2->listRecords($SETS);
}
2013-05-07 19:52:33 +02:00
break;
default:
// we never use compression with errors
$compress = FALSE;
$errors[] = oai_error('badVerb', $args['verb']);
} /*switch */
} else {
2013-05-07 19:52:33 +02:00
$errors[] = oai_error('noVerb');
}
2013-05-07 22:01:22 +02:00
if (!empty($errors)) {
oai_exit();
}
if ($compress) {
2013-05-07 19:52:33 +02:00
ob_start('ob_gzhandler');
}
header(CONTENT_TYPE);
2013-05-07 22:01:22 +02:00
if (isset($outputObj)) {
2013-05-07 19:52:33 +02:00
$outputObj->display();
} else {
2013-05-07 19:52:33 +02:00
exit("There is a bug in codes");
}
2013-05-07 19:52:33 +02:00
if ($compress) {
ob_end_flush();
}