Skip to content

Instantly share code, notes, and snippets.

@cgueret
Created June 24, 2016 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgueret/255e3ab24b9c76baedcd0f53d704e0ae to your computer and use it in GitHub Desktop.
Save cgueret/255e3ab24b9c76baedcd0f53d704e0ae to your computer and use it in GitHub Desktop.
Searching for assets on the RES platform
<?php
/**
* Find assets associated to a particular topic
*
* In this example we search for all the assets related to a particular topic.
* This topic has to be identified by its URI. In order to find the relevant
* URI a first call to a search (see "search.php") can be necessary.
*
* @copyright Copyright (c) 2016 BBC
* @license http://unlicense.org/
*/
# Do some init to include EasyRDF and define useful namespaces
require_once realpath(__DIR__)."/vendor/autoload.php";
EasyRdf_Namespace::set('olo', 'http://purl.org/ontology/olo/core#');
# This is the identifier for the play "Romeo and Juliet"
$topic = "http://acropolis.org.uk/af28bd78a968460c9bba91839359539e#id";
# Load the description of the topic
$graph = EasyRdf_Graph::newAndLoad(str_replace('#id', '.rdf', $topic));
# Convert the ordered list of results into an array
$results = array();
foreach($graph->allOfType('olo:Slot') as $entry) {
$index = $entry->get('olo:index')->getValue();
$item = array();
$item['uri'] = $entry->get('olo:item')->getUri();
$results[$index] = $item;
}
ksort($results);
# Iterate over all the results to fetch additional information
foreach($results as $index => $item) {
$graph->load(str_replace('#id', '.rdf', $item['uri']));
foreach($graph->properties($item['uri']) as $property) {
$item[$property] = array();
foreach($graph->all($item['uri'], $property) as $value) {
if (is_a($value, "EasyRdf_Literal"))
array_push($item[$property], $value->getValue());
else if (is_a($value, "EasyRdf_Resource"))
array_push($item[$property], $value->getUri());
}
}
$results[$index] = $item;
}
# Print all the results
print_r ($results);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment