Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to Sparql. #75

Merged
merged 3 commits into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
145 changes: 93 additions & 52 deletions includes/handler.inc
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ function islandora_oai_query_solr($start = 0, $field = 'PID', $query = NULL, $se
$sets = explode(':', $set);
$descendants = array();

if ($user->uid === 0) {
$user_name = 'anonymous';
}
else {
$user_name = $user->name;
}

// Get all descendant children (collection and objs) and add to the sets
// array if not already existing.
// Cannot constrain on collections only as name conventions may change
Expand All @@ -447,71 +454,105 @@ function islandora_oai_query_solr($start = 0, $field = 'PID', $query = NULL, $se
$value = trim($value);
$sets[$key] = $value;

$itql_query = '
select $child from <#ri>
where
(
$connection = islandora_get_tuque_connection();
if (!variable_get('islandora_risearch_use_itql_when_necessary', TRUE)) {
$role_filters = array();
foreach ($user->roles as $role) {
$role_filters[] = "?role = \"$role\"";
}
$xacml_filters = array(
'!bound(?role) && !bound(?user)',
'bound(?role) && (' . implode(' || ', $role_filters) . ')',
"bound(?user) && ?user = '$user_name'",
);
$parenthesis_wrap = function($string) {
return "($string)";
};
$xacml_filter = implode(' || ', array_map($parenthesis_wrap, $xacml_filters));
$query = <<<EOQ
SELECT ?child
FROM <#ri>
WHERE {
?child <fedora-model:state> <fedora-model:Active> .
{
?child <fedora-model:hasModel> <info:fedora/islandora:collectionCModel>
} UNION {
?child <fedora-model:hasModel> [<fedora-model:hasModel> <info:fedora/islandora:collectionCModel>]
} .
{
?child <fedora-rels-ext:isMemberOfCollection>+ <info:fedora/$value>
} UNION {
?child <fedora-rels-ext:isMemberOf>+ <info:fedora/$value>
}
OPTIONAL {
?child <http://islandora.ca/ontology/relsext#isViewableByRole> ?role .
}
OPTIONAL {
?child <http://islandora.ca/ontology/relsext#isViewableByUser> ?user .
}
FILTER($xacml_filter)
}
EOQ;
$query_results = $connection->repository->ri->sparqlQuery($query);
}
else {
$itql_query = '
select $child from <#ri>
where
(
(
$child <fedora-model:hasModel> $model
and $model <mulgara:is> <info:fedora/islandora:collectionCModel>
and $parent_model <mulgara:is> <info:fedora/fedora-system:ContentModel-3.0>
(
$child <fedora-model:hasModel> $model
and $model <mulgara:is> <info:fedora/islandora:collectionCModel>
and $model <fedora-model:hasModel> <info:fedora/fedora-system:ContentModel-3.0>
)
or
(
$child <fedora-model:hasModel> $model
and $model <fedora-model:hasModel> <info:fedora/islandora:collectionCModel>
and $child <fedora-model:state> <info:fedora/fedora-system:def/model#Active>
)
minus $child <http://islandora.ca/ontology/relsext#isViewableByRole> $role
minus $child <http://islandora.ca/ontology/relsext#isViewableByUser> $user
)
or
(
$child <fedora-model:hasModel> $model
and $model <fedora-model:hasModel> $parent_model
and $parent_model <mulgara:is> <info:fedora/islandora:collectionCModel>
and $child <fedora-model:state> <info:fedora/fedora-system:def/model#Active>
)
minus $child <http://islandora.ca/ontology/relsext#isViewableByRole> $role
minus $child <http://islandora.ca/ontology/relsext#isViewableByUser> $user
(
$child <fedora-model:hasModel> $model
and $model <mulgara:is> <info:fedora/islandora:collectionCModel>
and $model <fedora-model:hasModel> <info:fedora/fedora-system:ContentModel-3.0>
)
or
(
$child <fedora-model:hasModel> $model
and $model <fedora-model:hasModel> <info:fedora/islandora:collectionCModel>
and $child <fedora-model:state> <info:fedora/fedora-system:def/model#Active>
)
and
(';
foreach ($user->roles as $role) {
$itql_query .= '$child <http://islandora.ca/ontology/relsext#isViewableByRole> ' . "'$role' or ";
}
$itql_query .= '$child <http://islandora.ca/ontology/relsext#isViewableByUser> ' . "'$user_name'" . ')';
$itql_query .= ')
)
or
and
(
walk
(
$child <fedora-model:hasModel> $model
and $model <mulgara:is> <info:fedora/islandora:collectionCModel>
and $parent_model <mulgara:is> <info:fedora/fedora-system:ContentModel-3.0>
$parent <fedora-rels-ext:isMemberOfCollection><info:fedora/' . $value . '>
and $child <fedora-rels-ext:isMemberOfCollection> $parent
)
or
walk
(
$child <fedora-model:hasModel> $model
and $model <fedora-model:hasModel> $parent_model
and $parent_model <mulgara:is> <info:fedora/islandora:collectionCModel>
and $child <fedora-model:state> <info:fedora/fedora-system:def/model#Active>
$parent <fedora-rels-ext:isMemberOf><info:fedora/' . $value . '>
and $child <fedora-rels-ext:isMemberOf> $parent
)
and
(';
foreach ($user->roles as $role) {
$itql_query .= '$child <http://islandora.ca/ontology/relsext#isViewableByRole> ' . "'$role' or ";
}
if ($user->uid === 0) {
$user_name = 'anonymous';
}
else {
$user_name = $user->name;
}
$itql_query .= '$child <http://islandora.ca/ontology/relsext#isViewableByUser> ' . "'$user_name'" . ')';
$itql_query .= ')
)
and
(
walk
(
$parent <fedora-rels-ext:isMemberOfCollection><info:fedora/' . $value . '>
and $child <fedora-rels-ext:isMemberOfCollection> $parent
)
or
walk
(
$parent <fedora-rels-ext:isMemberOf><info:fedora/' . $value . '>
and $child <fedora-rels-ext:isMemberOf> $parent
)
)';
)';

$connection = islandora_get_tuque_connection();
$query_results = $connection->repository->ri->itqlQuery($itql_query);
$query_results = $connection->repository->ri->itqlQuery($itql_query);
}
foreach ($query_results as $result) {
$walk_set = $result['child']['uri'];
$walk_set = str_replace('info:fedora/', '', $walk_set);
Expand Down
35 changes: 20 additions & 15 deletions includes/request.inc
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,19 @@ function islandora_oai_identify(&$writer, $args) {
function islandora_oai_get_earliest_datetime() {
static $earliest_datestamp;
if (!$earliest_datestamp) {
$itql_query = 'select $object $date from <#ri>
where $object <info:fedora/fedora-system:def/model#label> $title
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>
and $object <info:fedora/fedora-system:def/view#lastModifiedDate> $date
order by $date asc';
$query = <<<EOQ
SELECT ?object ?date
FROM <#ri>
WHERE {
?object <fedora-model:label> ?title ;
<fedora-model:state> <fedora-model:Active> ;
<fedora-view:lastModifiedDate> ?date .
}
ORDER BY ?date
LIMIT 1
EOQ;
$connection = islandora_get_tuque_connection();
$query_results = $connection->repository->ri->itqlQuery($itql_query, 1);
$query_results = $connection->repository->ri->sparqlQuery($query);
$result = reset($query_results);
$timestamp = strtotime($result['date']['value']);
$earliest_datestamp = gmdate('Y-m-d\TH:i:s\Z', $timestamp);
Expand Down Expand Up @@ -873,16 +879,15 @@ function islandora_oai_list_metadata_formats(&$writer, $args) {
$identifier = str_replace('oai:' . $repo_id . ':', '', $val);

$pid_query = str_replace('_', ':', $identifier);
$pid_query = 'info:fedora/' . $pid_query;
$itql_query = 'select $object $title from <#ri>
where $object <fedora-model:label> $title and $object <mulgara:is> <' . $pid_query . '>';

$query = <<<EOQ
ASK {
<info:fedora/$pid_query> <fedora-model:hasModel> <info:fedora/fedora-system:FedoraObject-3.0> .
}
EOQ;
$connection = islandora_get_tuque_connection();
$query_results = $connection->repository->ri->itqlQuery($itql_query);
foreach ($query_results as $result) {
$result_pid = $result['object']['uri'];
}
if (empty($result_pid)) {
$query_results = $connection->repository->ri->sparqlQuery($query);
$query_result = reset($query_results);
if (!$query_results || $query_result === FALSE || strtolower($query_result['k0']['value']) != 'true') {
islandora_oai_add_error('idDoesNotExist', 'identifier', $identifier);
return FALSE;
}
Expand Down