Skip to content

Commit

Permalink
Add order option to Store.getGraphsUsingResource()
Browse files Browse the repository at this point in the history
Add a possibility to order the resulting graphs of
getGraphsUsingResource based on the accordance of their namespaces.
  • Loading branch information
white-gecko committed Jan 12, 2014
1 parent d0582f1 commit f77d878
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions library/Erfurt/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -1822,9 +1822,10 @@ public function getBackendAdapter()
* one statement where the given resource URI is used as a subject.
*
* @param string $resourceUri
* @param boolean $order true if the resulting models should be ordered
* @return array
*/
public function getGraphsUsingResource($resourceUri, $useAc = true)
public function getGraphsUsingResource($resourceUri, $useAc = true, $order = false)
{
if (method_exists($this->_backendAdapter, 'getGraphsUsingResource')) {
$backendResult = $this->_backendAdapter->getGraphsUsingResource($resourceUri);
Expand All @@ -1838,38 +1839,45 @@ public function getGraphsUsingResource($resourceUri, $useAc = true)
}
}

return $realResult;
$graphs = $realResult;
} else {
return $backendResult;
$graphs = $backendResult;
}
}

$query = new Erfurt_Sparql_SimpleQuery();
$query->setProloguePart('SELECT DISTINCT ?graph')
->setWherePart('WHERE {GRAPH ?graph {<' . $resourceUri . '> ?p ?o.}}');
} else {
$query = new Erfurt_Sparql_SimpleQuery();
$query->setProloguePart('SELECT DISTINCT ?graph')
->setWherePart('WHERE {GRAPH ?graph {<' . $resourceUri . '> ?p ?o.}}');

$graphResult = array();
$result = $this->sparqlQuery($query, array(Erfurt_Store::USE_AC => $useAc));
$graphResult = array();
$result = $this->sparqlQuery($query, array(Erfurt_Store::USE_AC => $useAc));

if ($result) {
foreach ($result as $row) {
$graphResult[] = $row['graph'];
if ($result) {
foreach ($result as $row) {
$graphResult[] = $row['graph'];
}
}

$graphs = $graphResult;
}

return $graphResult;
if ($order) {
$graphs = $this->_compareResourceUriToModelUri($graphs, $resourceUri);
}

return $graphs;
}

/**
* Returns a list of graph URIs, simmilar to getGraphsUsingResource but checks if it is
* readable.
*
* @param string $resourceUri
* @param boolean $order true if the resulting models should be ordered
* @return array
*/
public function getReadableGraphsUsingResource($resourceUri)
public function getReadableGraphsUsingResource($resourceUri, $order = false)
{
$result = $this->getGraphsUsingResource($resourceUri, false);
$result = $this->getGraphsUsingResource($resourceUri, false, $order);

if ($result) {
// get source graph
Expand Down Expand Up @@ -2171,4 +2179,31 @@ protected function _getErfurtLogger()
{
return $this->_erfurtLogger = Erfurt_App::getInstance()->getLog('erfurt');
}

/**
* This method compares a list of graphs to a resourceUri and orders the graphs according to
* their accordance of the namespace.
*
* @param array $graphs list of graph URIs
* @param string $resourceUri the URI of the resource
* @return array containing the ordered graph URIs
*/
protected function _compareResourceUriToModelUri($graphs, $resourceUri)
{
$namespaceMatch = array();
$rest = array();
foreach ($graphs as $graphUri) {
if (strpos($resourceUri, $graphUri) === 0) {
$namespaceMatch[] = $graphUri;
} else {
$rest[] = $graphUri;
}
}

usort($namespaceMatch, function($a, $b) {
return strlen($b) - strlen($a);
});

return array_merge($namespaceMatch, $rest);
}
}

0 comments on commit f77d878

Please sign in to comment.