2013-05-07 19:48:37 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
debug_message("\nI am debuging". __FILE__) ;
|
|
|
|
|
|
|
|
// Resume previous session?
|
|
|
|
if (isset($args['resumptionToken'])) {
|
2013-05-07 22:01:22 +02:00
|
|
|
if (!file_exists(TOKEN_PREFIX.$args['resumptionToken'])) {
|
|
|
|
$errors[] = oai_error('badResumptionToken', '', $args['resumptionToken']);
|
|
|
|
} else {
|
|
|
|
$readings = readResumToken(TOKEN_PREFIX.$args['resumptionToken']);
|
|
|
|
if ($readings == false) {
|
|
|
|
$errors[] = oai_error('badResumptionToken', '', $args['resumptionToken']);
|
|
|
|
} else {
|
|
|
|
debug_var_dump('readings',$readings);
|
|
|
|
list($deliveredrecords, $extquery, $metadataPrefix) = $readings;
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 19:48:37 +02:00
|
|
|
} else { // no, we start a new session
|
2013-05-07 22:01:22 +02:00
|
|
|
$deliveredrecords = 0;
|
|
|
|
$extquery = '';
|
2013-05-07 19:48:37 +02:00
|
|
|
|
2013-05-07 22:01:22 +02:00
|
|
|
$metadataPrefix = $args['metadataPrefix'];
|
2013-05-07 19:48:37 +02:00
|
|
|
|
2013-05-07 22:01:22 +02:00
|
|
|
if (isset($args['from'])) {
|
|
|
|
$from = checkDateFormat($args['from']);
|
|
|
|
$extquery .= fromQuery($from);
|
|
|
|
}
|
2013-05-07 19:48:37 +02:00
|
|
|
|
2013-05-07 22:01:22 +02:00
|
|
|
if (isset($args['until'])) {
|
|
|
|
$until = checkDateFormat($args['until']);
|
|
|
|
$extquery .= untilQuery($until);
|
|
|
|
}
|
2013-05-07 19:48:37 +02:00
|
|
|
|
|
|
|
if (isset($args['set'])) {
|
2013-05-07 22:01:22 +02:00
|
|
|
if (is_array($SETS)) {
|
|
|
|
$extquery .= setQuery($args['set']);
|
|
|
|
} else {
|
|
|
|
$errors[] = oai_error('noSetHierarchy');
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
2013-05-07 22:01:22 +02:00
|
|
|
oai_exit();
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the handler
|
2013-05-07 22:01:22 +02:00
|
|
|
if (is_array($METADATAFORMATS[$metadataPrefix]) && isset($METADATAFORMATS[$metadataPrefix]['myhandler'])) {
|
|
|
|
$inc_record = $METADATAFORMATS[$metadataPrefix]['myhandler'];
|
|
|
|
include($inc_record);
|
2013-05-07 19:48:37 +02:00
|
|
|
} else {
|
2013-05-07 22:01:22 +02:00
|
|
|
$errors[] = oai_error('cannotDisseminateFormat', 'metadataPrefix', $metadataPrefix);
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
2013-05-07 22:01:22 +02:00
|
|
|
oai_exit();
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($errors)) {
|
2013-05-07 22:01:22 +02:00
|
|
|
$query = selectallQuery($metadataPrefix) . $extquery;
|
|
|
|
debug_message("Query: $query") ;
|
|
|
|
|
|
|
|
$res = $db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
|
|
|
|
$r = $res->execute();
|
|
|
|
if (($r===false) ) {
|
|
|
|
if ( (SHOW_QUERY_ERROR)) {
|
|
|
|
echo __FILE__.','.__LINE__."<br />";
|
|
|
|
echo "Query: $query<br />\n";
|
|
|
|
print_r($db->errorInfo());
|
|
|
|
exit();
|
|
|
|
} else {
|
|
|
|
$errors[] = oai_error('noRecordsMatch');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$r = $res->setFetchMode(PDO::FETCH_ASSOC);
|
|
|
|
if ($r===false) {
|
|
|
|
exit("FetchMode is not supported");
|
|
|
|
}
|
|
|
|
$num_rows = rowCount($metadataPrefix, $extquery, $db);
|
|
|
|
if ($num_rows==0) {
|
|
|
|
if (SHOW_QUERY_ERROR) {
|
|
|
|
echo "Cannot find records: $query\n";
|
|
|
|
}
|
|
|
|
$errors[] = oai_error('noRecordsMatch');
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
2013-05-07 22:01:22 +02:00
|
|
|
oai_exit();
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Will we need a new ResumptionToken?
|
|
|
|
if($args['verb']=='ListRecords') {
|
2013-05-07 22:01:22 +02:00
|
|
|
$maxItems = MAXRECORDS;
|
2013-05-07 19:48:37 +02:00
|
|
|
} elseif($args['verb']=='ListIdentifiers') {
|
2013-05-07 22:01:22 +02:00
|
|
|
$maxItems = MAXIDS;
|
2013-05-07 19:48:37 +02:00
|
|
|
} else {
|
2013-05-07 22:01:22 +02:00
|
|
|
exit("Check ".__FILE__." ".__LINE__.", there is something wrong.");
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
$maxrec = min($num_rows - $deliveredrecords, $maxItems);
|
|
|
|
|
|
|
|
if ($num_rows - $deliveredrecords > $maxItems) {
|
2013-05-07 22:01:22 +02:00
|
|
|
$cursor = (int)$deliveredrecords + $maxItems;
|
|
|
|
$restoken = createResumToken($cursor, $extquery, $metadataPrefix);
|
|
|
|
$expirationdatetime = gmstrftime('%Y-%m-%dT%TZ', time()+TOKEN_VALID);
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
// Last delivery, return empty ResumptionToken
|
|
|
|
elseif (isset($args['resumptionToken'])) {
|
2013-05-07 22:01:22 +02:00
|
|
|
$restoken = $args['resumptionToken']; // just used as an indicator
|
|
|
|
unset($expirationdatetime);
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($args['resumptionToken'])) {
|
2013-05-07 22:01:22 +02:00
|
|
|
debug_message("Try to resume because a resumptionToken supplied.") ;
|
|
|
|
$record = $res->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $deliveredrecords);
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
// Record counter
|
|
|
|
$countrec = 0;
|
|
|
|
|
|
|
|
// Publish a batch to $maxrec number of records
|
|
|
|
$outputObj = new ANDS_Response_XML($args);
|
|
|
|
while ($countrec++ < $maxrec) {
|
2013-05-07 22:01:22 +02:00
|
|
|
$record = $res->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($record===false) {
|
|
|
|
if (SHOW_QUERY_ERROR) {
|
|
|
|
echo __FILE__.",". __LINE__."<br />";
|
|
|
|
print_r($db->errorInfo());
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$identifier = $oaiprefix.$record[$SQL['identifier']];
|
|
|
|
$datestamp = formatDatestamp($record[$SQL['datestamp']]);
|
|
|
|
$setspec = $record[$SQL['set']];
|
|
|
|
|
|
|
|
// debug_var_dump('record', $record);
|
|
|
|
$status_deleted = (isset($record[$SQL['deleted']]) && ($record[$SQL['deleted']] === true) &&
|
|
|
|
($deletedRecord == 'transient' || $deletedRecord == 'persistent'));
|
|
|
|
|
|
|
|
//debug_var_dump('status_deleted', $status_deleted);
|
|
|
|
if($args['verb']=='ListRecords') {
|
|
|
|
$cur_record = $outputObj->create_record();
|
|
|
|
$cur_header = $outputObj->create_header($identifier, $datestamp,$setspec,$cur_record);
|
|
|
|
// return the metadata record itself
|
|
|
|
if (!$status_deleted) {
|
|
|
|
debug_var_dump('inc_record',$inc_record);
|
|
|
|
create_metadata($outputObj, $cur_record, $identifier, $setspec, $db);
|
|
|
|
}
|
|
|
|
} else { // for ListIdentifiers, only identifiers will be returned.
|
|
|
|
$cur_header = $outputObj->create_header($identifier, $datestamp,$setspec);
|
|
|
|
}
|
|
|
|
if ($status_deleted) {
|
|
|
|
$cur_header->setAttribute("status","deleted");
|
|
|
|
}
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResumptionToken
|
|
|
|
if (isset($restoken)) {
|
2013-05-07 22:01:22 +02:00
|
|
|
if(isset($expirationdatetime)) {
|
|
|
|
$outputObj->create_resumpToken($restoken,$expirationdatetime,$num_rows,$cursor);
|
|
|
|
} else {
|
|
|
|
$outputObj->create_resumpToken('',null,$num_rows,$deliveredrecords);
|
|
|
|
}
|
2013-05-07 19:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// end ListRecords
|
|
|
|
if (SHOW_QUERY_ERROR) {echo "Debug listrecord.php reached to the end.\n\n";}
|