@ -938,58 +938,62 @@ class ObjectHelper {
/**
/**
* Builds an array of drupal links for use in breadcrumbs.
* Builds an array of drupal links for use in breadcrumbs.
*
*
* @todo Make fully recursive...
*
* @global type $base_url
* @global type $base_url
* @param type $pid
* @param type $pid
* @param type $breadcrumbs
* @param type $breadcrumbs
* @param type $level
* @param type $level
*/
*/
function getBreadcrumbs($pid, & $breadcrumbs, $level=10 ) {
function getBreadcrumbs($pid, & $breadcrumbs) {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
// Before executing the query, we hve a base case of accessing the top-level collection
// Before executing the query, we hve a base case of accessing the top-level collection
global $base_url;
global $base_url;
if ($pid == variable_get('fedora_repository_pid', 'islandora:root')) {
static $max_level = 10;
//$breadcrumbs[] = l(t('Digital repository'), 'fedora/repository');
static $level = -1;
$breadcrumbs[] = l(variable_get('fedora_repository_title', 'Digital repository'), 'fedora/repository');
$breadcrumbs[] = l(t('Home'), $base_url);
if (count($breadcrumbs) === 0) {
$level = $max_level;
}
$root = variable_get('fedora_repository_pid', 'islandora:root');
if ($pid == $root) {
$breadcrumbs[] = l(menu_get_active_title(), 'fedora/repository');
$breadcrumbs[] = l(t('Home'), '< front > ');
}
}
else {
else {
$query_string = 'select $parentObject $title $content from < #ri>
$query_string = 'select $parentObject $title $content from < #ri>
where (< info:fedora / ' . $ pid . ' > < fedora-model:label > $title
where (
and $parentObject < fedora-model:hasModel > $content
< info:fedora / ' . $ pid . ' > < fedora-model:label > $title
and (< info:fedora / ' . $ pid . ' > < fedora-rels-ext:isMemberOfCollection > $parentObject
and $parentObject < fedora-model:hasModel > $content
or < info:fedora / ' . $ pid . ' > < fedora-rels-ext:isMemberOf > $parentObject
and (
or < info:fedora / ' . $ pid . ' > < fedora-rels-ext:isPartOf > $parentObject)
< info:fedora / ' . $ pid . ' > < fedora-rels-ext:isMemberOfCollection > $parentObject
and $parentObject < fedora-model:state > < info:fedora / fedora-system:def / model # Active > )
or < info:fedora / ' . $ pid . ' > < fedora-rels-ext:isMemberOf > $parentObject
minus $content < mulgara:is > < info:fedora / fedora-system:FedoraObject-3 . 0 >
or < info:fedora / ' . $ pid . ' > < fedora-rels-ext:isPartOf > $parentObject
order by $title desc';
)
$query_string = htmlentities(urlencode($query_string));
and $parentObject < fedora-model:state > < info:fedora / fedora-system:def / model # Active >
)
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
minus $content < mulgara:is > < info:fedora / fedora-system:FedoraObject-3 . 0 >
$url .= "?type=tuples& flush=TRUE& format=CSV& limit=1& offset=0& lang=itql& stream=on& query=" . $query_string;
order by $title desc';
$result = preg_split('/[\r\n]+/', do_curl($url));
if (count($results = self::perform_itql_query($query_string)) > 0 & & $level > 0) {
array_shift($result); // throw away first line
$parent = $results[0]['parentObject'];
$matches = str_getcsv(join("\n", $result));
$this_title = $results[0]['title'];
if (count($matches) >= 2) {
$parent = preg_replace('/^info:fedora\//', '', $matches[0]);
if (empty($this_title)) {
$this_title = t('Unlabeled Object');
if (0 == strlen($matches[1])) {
$matches[1] = "Unlabeled Object";
}
}
$breadcrumbs[] = l($matches[1], 'fedora/repository/' . $pid);
$breadcrumbs[] = l($this_title, "fedora/repository/$pid");
if ($parent == variable_get('fedora_repository_pid', 'islandora:root')) {
//$breadcrumbs[] = l(t('Digital repository'), 'fedora/repository');
$level--;
$breadcrumbs[] = l(variable_get('fedora_repository_name', 'Digital repository'), 'fedora/repository');
$this->getBreadcrumbs($parent, $breadcrumbs);
$breadcrumbs[] = l(t('Home'), $base_url);
}
elseif ($level > 0) {
$this->getBreadcrumbs($parent, $breadcrumbs, $level - 1);
}
}
}
else {
else {
$breadcrumbs[] = l(t("Path Calculation Error"), 'fedora/repository/' . $pid);
watchdog('fedora_repository', 'Error generating breadcrumbs for %pid. Verify there exists relationships back up to %root. (May also be due to a hierarchy deeper than %max_depth).', array('%pid' => $pid, '%root' => $root, '%max_depth' => $max_depth), WATCHDOG_WARNING);
$breadcrumbs[] = '...'; //Add an non-link, as we don't know how to get back to the root.
$this->getBreadcrumbs($root, $breadcrumbs); //And render the last two links and break (on the next pass).
}
}
}
}
}
}
@ -1012,5 +1016,121 @@ class ObjectHelper {
drupal_set_message(t($configMess . "< br / > " . $messMap[$app] . "< hr width = '40%' align = 'left' / > ", array('%app' => $app)), 'warning', FALSE);
drupal_set_message(t($configMess . "< br / > " . $messMap[$app] . "< hr width = '40%' align = 'left' / > ", array('%app' => $app)), 'warning', FALSE);
}
}
/**
* Performs the given Resource Index query and return the results.
*
* @param $query string
* A string containing the RI query to perform.
* @param $type string
* The type of query to perform, as used by the risearch interface.
* @param $limit int
* An integer, used to limit the number of results to return.
* @param $offset int
* An integer, used to offset the results (results should be ordered, to
* maintain consistency.
*
* @return array
* Indexed (numerical) array, containing a number of associative arrays,
* with keys being the same as the variable names in the query.
* URIs beginning with 'info:fedora/' will have this beginning stripped
* off, to facilitate their use as PIDs.
*/
protected static function perform_ri_query($query, $type = 'itql', $limit = -1, $offset = 0) {
//Setup the query options...
$options = array(
'type' => 'tuples',
'flush' => TRUE,
'format' => 'Sparql', //Sparql XML is processed into the array below.
'lang' => $type,
'query' => $query
);
//Add limit if provided.
if ($limit > 0) {
$options['limit'] = $limit;
}
//Add offset if provided.
if ($offset > 0) {
$options['offset'] = $offset;
}
//Construct the query URL.
$queryUrl = url(variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch'), array('query' => $options));
//Perform the query.
$curl_result = do_curl_ext($queryUrl);
//If the query failed, write message to the logs and return.
if (!$curl_result[0]) {
watchdog('fedora_repository', 'Failed to perform %type resource index query: %query', array('%type' => $type, '%query' => $query), WATCHDOG_ERROR);
return FALSE;
}
//Load the results into a SimpleXMLElement
$doc = new SimpleXMLElement($curl_result[0], 0, FALSE, 'http://www.w3.org/2001/sw/DataAccess/rf1/result');
$results = array(); //Storage.
//Build the results.
foreach ($doc->results->children() as $result) {
//Built a single result.
$r = array();
foreach ($result->children() as $element) {
$val = NULL;
$attrs = $element->attributes();
if (!empty($attrs['uri'])) {
$val = self::pid_uri_to_bare_pid((string)$attrs['uri']);
}
else {
$val = (string)$element;
}
//Map the name to the value in the array.
$r[$element->getName()] = $val;
}
//Add the single result to the set to return.
$results[] = $r;
}
return $results;
}
/**
* Thin wrapper for self::_perform_ri_query().
*
* @see self::_perform_ri_query()
*/
public static function perform_itql_query($query, $limit = -1, $offset = 0) {
return self::perform_ri_query($query, 'itql', $limit, $offset);
}
/**
* Thin wrapper for self::_perform_ri_query().
*
* @see self::_perform_ri_query()
*/
public static function perform_sparql_query($query, $limit = -1, $offset = 0) {
return self::perform_ri_query($query, 'sparql', $limit, $offset);
}
/**
* Utility function used in self::_perform_ri_query().
*
* Strips off the 'info:fedora/' prefix from the passed in string.
*
* @param $uri string
* A string containing a URI.
*
* @return string
* The input string less the 'info:fedora/' prefix (if it has it).
* The original string otherwise.
*/
protected static function pid_uri_to_bare_pid($uri) {
$chunk = 'info:fedora/';
$pos = strpos($uri, $chunk);
if ($pos === 0) { //Remove info:fedora/ chunk
return substr($uri, strlen($chunk));
}
else { //Doesn't start with info:fedora/ chunk...
return $uri;
}
}
}
}