simple-oai-pmh/oaidp-util.php

190 lines
5.6 KiB
PHP
Raw Normal View History

<?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
*/
2013-05-07 22:01:22 +02:00
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
*/
2013-05-07 22:01:22 +02:00
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
*/
2013-05-07 22:01:22 +02:00
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) {
2013-05-07 22:01:22 +02:00
$date = str_replace(array("T","Z")," ",$date);
$time_val = strtotime($date);
if (SHOW_QUERY_ERROR) { echo "timeval: $time_val\n"; }
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);
}
}
/** Retrieve all defined 'setSpec' from configuraiton of $SETS.
* It is used by ANDS_TPA::create_obj_node();
2013-05-07 22:01:22 +02:00
*/
function prepare_set_names() {
2013-05-07 22:01:22 +02:00
global $SETS;
$n = count($SETS);
$a = array_fill(0,$n,'');
for ($i = 0; $i <$n; $i++) {
$a[$i] = $SETS[$i]['setSpec'];
}
return $a;
}
// ResumToken section
/** Generate a string based on the current Unix timestamp in microseconds for creating resumToken file name. */
2013-05-07 22:01:22 +02:00
function get_token() {
list($usec, $sec) = explode(" ", microtime());
return ((int)($usec*1000) + (int)($sec*1000));
}
/** Create a token file.
* It has three parts which is separated by '#': cursor, extension of query, metadataPrefix.
* Called by listrecords.php.
*/
function createResumToken($cursor, $extquery, $metadataPrefix) {
2013-05-07 22:01:22 +02:00
$token = get_token();
$fp = fopen (TOKEN_PREFIX.$token, 'w');
if($fp==false) {
exit("Cannot write. Writer permission needs to be changed.");
}
fputs($fp, "$cursor#");
fputs($fp, "$extquery#");
fputs($fp, "$metadataPrefix#");
fclose($fp);
return $token;
}
/** Read a saved ResumToken */
function readResumToken($resumptionToken) {
2013-05-07 22:01:22 +02:00
$rtVal = false;
$fp = fopen($resumptionToken, 'r');
if ($fp!=false) {
$filetext = fgets($fp, 255);
$textparts = explode('#', $filetext);
fclose($fp);
unlink ($resumptionToken);
$rtVal = array((int)$textparts[0], $textparts[1], $textparts[2]);
}
return $rtVal;
}
// Here are a couple of queries which might need to be adjusted to
// your needs. Normally, if you have correctly named the columns above,
// this does not need to be done.
/** this function should generate a query which will return
* all records
* the useless condition id_column = id_column is just there to ease
* further extensions to the query, please leave it as it is.
*/
2013-05-07 22:01:22 +02:00
function selectallQuery($metadPrefix = "rif", $id = ''){
global $SQL;
$query = "SELECT * FROM ".$SQL['table'] . " WHERE ".$SQL['metadataPrefix']." LIKE '%$metadPrefix%'";
if ($id != '') {
$query .= " AND ".$SQL['identifier']." ='$id'";
}
return $query;
}
/** this function will return identifier and datestamp for all records
* not very useful
*/
2013-05-07 22:01:22 +02:00
function idQuery($metadPrefix = "rif", $id = '') {
global $SQL;
if ($SQL['set'] != '') {
$query = 'select '.$SQL['identifier'].','.$SQL['datestamp'].','.$SQL['set'].' FROM '.$SQL['table']. " WHERE ".$SQL['metadataPrefix']." LIKE '%$metadPrefix%'";
} else {
$query = 'select '.$SQL['identifier'].','.$SQL['datestamp'].' FROM '.$SQL['table']. " WHERE ".$SQL['metadataPrefix']." LIKE '%$metadPrefix%'";
}
if ($id != '') {
$query .= " AND ".$SQL['identifier']." = '$id'";
}
return $query;
}
/** filter for until, appends to the end of SQL query */
2013-05-07 22:01:22 +02:00
function untilQuery($until) {
global $SQL;
2013-05-07 22:01:22 +02:00
return ' AND '.$SQL['datestamp']." <= '$until'";
}
/** filter for from , appends to the end of SQL query */
2013-05-07 22:01:22 +02:00
function fromQuery($from) {
global $SQL;
2013-05-07 22:01:22 +02:00
return ' AND '.$SQL['datestamp']." >= '$from'";
}
/** filter for sets, appends to the end of SQL query */
2013-05-07 22:01:22 +02:00
function setQuery($set) {
global $SQL;
// strip off "class:" which is not saved in database
if(strstr($set,"class:")) $set = substr($set,6);
return ' AND '.$SQL['set']." LIKE '%$set%'";
}
/** for accurately to assess how many records satisfy conditions for all DBs */
function rowCount($metadataPrefix, $extQuery, $db) {
2013-05-07 22:01:22 +02:00
global $SQL;
$n = 0;
$sql = "SELECT COUNT(*) FROM ".$SQL['table'] . " WHERE ".$SQL['metadataPrefix']." LIKE '%$metadataPrefix%'" . $extQuery;
if ($res = $db->query($sql)) {
$n = $res->fetchColumn();
}
return $n;
}
/** A worker function for processing an error when a query was executed
* \param $query string, original query
* \param $e PDOException, the PDOException object
2013-05-07 22:01:22 +02:00
*/
function process_pdo_error($query, $e) {
2013-05-07 22:01:22 +02:00
echo $query.' was failed\n';
echo $e->getMessage();
}
/** When query return no result, throw an Exception of Not found.
* \param $db PDO
* \param $query string
* \return $res PDOStatement
*/
2013-05-07 22:01:22 +02:00
function exec_pdo_query($db, $query) {
$res = $db->query($query);
if ($res===false) {
throw new Exception($query.":\nIt found nothing.\n");
} else return $res;
}