Browse Source

Merge pull request #168 from willtp87/ISLANDORA-624

Islandora 624
pull/164/merge
Jonathan Green 12 years ago
parent
commit
e70586e27f
  1. 189
      api/fedora_utils.inc
  2. 327
      fedora_repository.module

189
api/fedora_utils.inc

@ -1,23 +1,23 @@
<?php
/**
* @file
* Base utilities used by the Islandora fedora module.
*/
if (!function_exists('str_getcsv')) {
/**
* Functions that emulate php5.3 functionality for backwards compatiablity
*
* @param type $input
* @param type $delimiter
* @param type $enclosure
* @param type $escape
* @param type $eol
* @return type
*
* @return type
*/
function str_getcsv($input, $delimiter=',', $enclosure='"', $escape=NULL, $eol=NULL) {
function str_getcsv($input, $delimiter = ',', $enclosure = '"', $escape = NULL, $eol = NULL) {
$temp = fopen("php://memory", "rw");
fwrite($temp, $input);
fseek($temp, 0);
@ -29,16 +29,16 @@ if (!function_exists('str_getcsv')) {
}
/**
*
*
* Utility method to get data from a url location using curl_exec and return
*
*
* This method takes identical parameters to do_curl_ext and passes them
* directly to do_curl_ext. The output from do_curl_ext is processed into
* either a TRUE, FALSE, or NULL value.
*
*
* This method exists for historical reasons as existing code makes use of
* do_curl directly and expects a simple return value and not an array.
*
*
* @param $url
* URL to be accessed by the function
* @param $return_to_variable
@ -49,8 +49,8 @@ if (!function_exists('str_getcsv')) {
* Number of variable sot be posted
* @param $post
* Whether the curl_exec is done as a "get" or a "post"
*
* @return
*
* @return mixed
* TRUE, FALSE, NULL, or the data returned from accessing the URL
*/
function do_curl($url, $return_to_variable = 1, $number_of_post_vars = 0, $post = NULL) {
@ -59,18 +59,17 @@ function do_curl($url, $return_to_variable = 1, $number_of_post_vars = 0, $post
}
/**
*
* Utility method to get data from a url location using curl_exec
*
*
* This method takes a URL and three associated parameters and initializes the
* structure required to make a call to curl_exec. As a part of the
* initialization the permissions associated with the (global) user are added
* to the call. This ensures access to any potentially user restricted URLs.
*
*
* Various defaults are used for the parameters required by curl_exec including
* the user agent and timeout. These are hard-coded.
*
* @param $url
*
* @param string $url
* URL to be accessed by the function
* @param $return_to_variable
* Indicates whether the resource accessed by the curl call (HTML page,
@ -80,8 +79,8 @@ function do_curl($url, $return_to_variable = 1, $number_of_post_vars = 0, $post
* Number of variable sot be posted
* @param $post
* Whether the curl_exec is done as a "get" or a "post"
*
* @return
*
* @return mixed
* an array that consists of three value or NULL if curl is not suported:
* - element 0:
* The value returned from the curl_exec function call.
@ -112,11 +111,15 @@ function do_curl_ext($url, $return_to_variable = TRUE, $number_of_post_vars = 0,
$user_agent = "Mozilla/4.0 pp(compatible; MSIE 5.01; Windows NT 5.0)";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // allow redirects
curl_setopt($ch, CURLOPT_TIMEOUT, 90); // times out after 90s
// Fail on errors.
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
// Allow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// Times out after 90s.
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $return_to_variable); // return into a variable
// Return into a variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $return_to_variable);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$fedora_user:$fedora_pass");
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
@ -141,7 +144,8 @@ function do_curl_ext($url, $return_to_variable = TRUE, $number_of_post_vars = 0,
/**
* Fedora available
* @return type
*
* @return mixed
*/
function fedora_available() {
@ -151,7 +155,8 @@ function fedora_available() {
/**
* Resource index search available
* @return type
*
* @return mixed
*/
function risearch_available() {
@ -161,7 +166,9 @@ function risearch_available() {
/**
* Returns a UTF-8-encoded transcripiton of the string given in $in_str.
*
* @param string $in_str
*
* @return string A UTF-8 encoded string.
*/
function fix_encoding($in_str) {
@ -176,8 +183,10 @@ function fix_encoding($in_str) {
/**
* valid pid ??
*
* @param type $pid
* @return boolean
*
* @return boolean
*/
function valid_pid($pid) {
$valid = FALSE;
@ -190,8 +199,10 @@ function valid_pid($pid) {
/**
* Valid Dsid ??
*
* @param type $dsid
* @return boolean
*
* @return boolean
*/
function valid_dsid($dsid) {
$valid = FALSE;
@ -204,8 +215,10 @@ function valid_dsid($dsid) {
/**
* fixDsid ??
*
* @param type $dsid
* @return string
*
* @return string
*/
function fix_dsid($dsid) {
$new_dsid = trim($dsid);
@ -214,14 +227,17 @@ function fix_dsid($dsid) {
$replace = '';
$new_dsid = preg_replace($find, $replace, $new_dsid);
if (strlen($new_dsid) > 63)
if (strlen($new_dsid) > 63) {
$new_dsid = substr($new_dsid, -63);
}
if (preg_match('/^[^a-zA-Z]/', $dsid))
if (preg_match('/^[^a-zA-Z]/', $dsid)) {
$new_dsid = 'x' . $new_dsid;
}
if (strlen($new_dsid) == 0)
if (strlen($new_dsid) == 0) {
$new_dsid = 'item' . rand(1, 100);
}
return $new_dsid;
}
@ -229,9 +245,8 @@ function fix_dsid($dsid) {
/**
* Function: get_collections_as_option_array
*
* Description: Returns an associative array of all collection objects in Fedora instance
*
* @return array
* Returns an associative array of all collection objects in Fedora instance
*/
function get_collections_as_option_array() {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
@ -244,7 +259,7 @@ function get_collections_as_option_array() {
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
order by $title';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$options = array(
'type' => 'tuples',
'flush' => 'TRUE',
@ -254,9 +269,9 @@ function get_collections_as_option_array() {
'stream' => 'on',
'query' => $query,
);
//The url function will take care of URL encoding...
// The url function will take care of URL encoding.
$content = do_curl(url($url, array('query' => $options)));
$list = explode("\n", $content);
array_shift($list);
$list = preg_replace('/info:fedora\//', '', $list);
@ -264,7 +279,8 @@ function get_collections_as_option_array() {
$trimmed_names[] = trim($namespace);
}
$options = array();
foreach ($list as $item) { //removes blanks
// Removes blanks.
foreach ($list as $item) {
if ($item) {
$parts = explode(',', $item);
$namespace = explode(':', $parts[0]);
@ -283,46 +299,75 @@ function get_collections_as_option_array() {
/**
* Function: get_content_models_as_option_array
*
* Description: Returns an associative array of all available content models in Fedora instance
*
* @return array
* associative array of all available content models in Fedora instance
*/
function get_content_models_as_option_array() {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$restricted = variable_get('fedora_namespace_restriction_enforced', TRUE);
$allowed_string = variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora:');
$namespaces = explode(':', $allowed_string);
foreach ($namespaces as $namespace) {
if ($namespace) {
$allowed[] = trim($namespace);
}
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$restricted = variable_get('fedora_namespace_restriction_enforced', TRUE);
$allowed_string = variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora:');
$namespaces = explode(':', $allowed_string);
foreach ($namespaces as $namespace) {
if ($namespace) {
$allowed[] = trim($namespace);
}
$query = 'select $object $title from <#ri>
where ($object <fedora-model:label> $title
and ($object <fedora-model:hasModel> <info:fedora/fedora-system:ContentModel-3.0>
or $object <fedora-rels-ext:isMemberOfCollection> <info:fedora/islandora:ContentModelsCollection>)
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
order by $title';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=TRUE&format=csv&limit=1000&lang=itql&stream=on&query=";
$content = do_curl($url . htmlentities(urlencode($query)));
$list = explode("\n", $content);
array_shift($list);
$list = preg_replace('/info:fedora\//', '', $list);
foreach ($list as $item) { //removes blanks
if ($item) {
$parts = explode(',', $item);
$nameparts = explode(':', $parts[0]);
if (!$restricted || in_array($nameparts[0], $allowed)) {
if (!preg_match('/fedora-system/', $nameparts[0])) {
$options[$parts[0]] = $parts[1] . ' ~ ' . $parts[0];
}
}
}
$query = 'select $object $title from <#ri>
where ($object <fedora-model:label> $title
and ($object <fedora-model:hasModel> <info:fedora/fedora-system:ContentModel-3.0>
or $object <fedora-rels-ext:isMemberOfCollection> <info:fedora/islandora:ContentModelsCollection>)
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
order by $title';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=TRUE&format=csv&limit=1000&lang=itql&stream=on&query=";
$content = do_curl($url . htmlentities(urlencode($query)));
$list = explode("\n", $content);
array_shift($list);
$list = preg_replace('/info:fedora\//', '', $list);
// Removes blanks.
foreach ($list as $item) {
if ($item) {
$parts = explode(',', $item);
$nameparts = explode(':', $parts[0]);
if (!$restricted || in_array($nameparts[0], $allowed)) {
if (!preg_match('/fedora-system/', $nameparts[0])) {
$options[$parts[0]] = $parts[1] . ' ~ ' . $parts[0];
}
}
}
}
return $options;
}
return $options;
}
/**
* This function will retrieve the collections that the given PID belongs to.
*
* @param string $PID
* The PID to find the parents of.
*
* @return array
* $object_PIDs the list of PIDs of the collections that the
* indicated object is a member of.
*/
function get_parent_collections_from_pid($PID) {
$query_string = 'select $parent from <#ri>
where ($object <fedora-rels-ext:isMemberOf> $parent
or $object <fedora-rels-ext:isMemberOfCollection> $parent)
and $object <dc:identifier> \'' . $PID . '\'
order by $object';
$query_string = htmlentities(urlencode($query_string));
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= '?type=tuples&flush=true&format=csv&limit=13000&lang=itql&stream=on&query=' . $query_string;
$content = do_curl($url, TRUE);
$results = explode("\n", $content);
$object_PIDs = preg_replace('/^info:fedora\/|"parent"| /', '', $results);
$object_PIDs = array_values(array_filter($object_PIDs));
return $object_PIDs;
}

327
fedora_repository.module

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save