Created
January 17, 2011 19:16
-
-
Save kwijibo/783299 to your computer and use it in GitHub Desktop.
command line script that takes a voiD Dataset URI and generates a Linked Data API config file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('DCT_DESCRIPTION', 'http://purl.org/dc/terms/description'); | |
define('MORIARTY_ARC_DIR', 'arc/'); | |
define('MORIARTY_ALWAYS_CACHE_EVERYTHING', 'cache'); | |
define('MORIARTY_HTTP_CACHE_DIR', 'cache'); | |
define('VOID_NS', 'http://rdfs.org/ns/void#'); | |
define('VOID_SPARQL', VOID_NS.'sparqlEndpoint'); | |
define('VOID_EXAMPLE', VOID_NS.'exampleResource'); | |
define('API','http://purl.org/linked-data/api/vocab#' ); | |
require 'moriarty/moriarty.inc.php'; | |
require 'moriarty/sparqlservice.class.php'; | |
require 'moriarty/simplegraph.class.php'; | |
class voiDtoLDA extends SimpleGraph { | |
var $voidUri ; | |
var $sparqlEndpointUri ; | |
var $baseUri ; | |
var $sparql ; | |
var $errors=array(); | |
var $slugMappings = array( | |
RDF_TYPE => 'type', | |
); | |
function __construct($voidUri, $sparqlEndpointUri=false, $baseUri='http://example.com/api#'){ | |
$this->voidUri = $voidUri; | |
$this->voiD = new SimpleGraph(); | |
$this->sparqlEndpointUri = $this->getSparqlEndpoint($sparqlEndpointUri ); | |
$this->baseUri = $baseUri; | |
if(!$baseUri) $this->baseUri = 'http://example.org/api#'; | |
$this->apiUri = $this->baseUri.'api'; | |
$this->sparql = new SparqlService($this->sparqlEndpointUri); | |
$this->getVoiDGraph(); | |
$this->makeAPI(); | |
$this->makeItemEndpoints(); | |
$this->makeListEndpoints(); | |
$this->addApiLabels(); | |
parent::__construct(); | |
} | |
function addApiLabels(){ | |
$this->getExampleProperties(); | |
foreach ($this->slugMappings as $uri => $name) { | |
$this->add_literal_triple($uri, API.'label', $name); | |
} | |
} | |
function getSparqlEndpoint($uri){ | |
if($uri) return $uri; | |
$this->voiD->read_data($this->voidUri); | |
if($endpoint = $this->voiD->get_first_resource($this->voidUri, VOID_SPARQL)){ | |
return $endpoint; | |
} else { | |
$this->errors[]="Couldn't find SPARQL endpoint. You should link to one in the voiD description, or provide one as the second parameter"; | |
} | |
} | |
# | |
# getVoiDGraph | |
# | |
# | |
public function getVoiDGraph(){ | |
$query = "DESCRIBE <{$this->voidUri}> ?example { <{$this->voidUri}> <http://rdfs.org/ns/void#exampleResource> ?example . }"; | |
$response = $this->sparql->graph($query); | |
if($response->is_success()){ | |
$this->voiD->add_rdf($response->body); | |
} else { | |
$this->errors[]='Failed to retrieve voiD from sparql endpoint: '.$response->body."\n\n---------\n\n {$query}"; | |
} | |
} | |
# makeAPI | |
# | |
# | |
public function makeAPI(){ | |
$apiSparqlUri = $this->voiD->get_first_resource($this->voidUri, VOID_SPARQL); | |
$this->add_resource_triple($this->apiUri, RDF_TYPE, API.'API'); | |
$this->add_resource_triple($this->apiUri, VOID_SPARQL, $apiSparqlUri); | |
$this->add_literal_triple($this->apiUri, RDFS_LABEL, $this->voiD->get_label($this->voidUri).': A Linked Data API'); | |
$this->add_literal_triple($this->apiUri, DCT_DESCRIPTION, $this->voiD->get_description($this->voidUri)); | |
$this->add_resource_triple($this->apiUri, API.'sparqlEndpoint', $this->voiD->get_first_resource($this->voidUri, VOID_SPARQL)); | |
} | |
# makeItemEndpoints | |
# | |
# | |
public function makeItemEndpoints(){ | |
$no = 1; | |
foreach($this->voiD->get_resource_triple_values($this->voidUri, VOID_EXAMPLE) as $exampleUri){ | |
$type = $this->voiD->get_first_resource($exampleUri, RDF_TYPE); | |
$typeSlug = $this->getSlug($type); | |
$endpointUri = $this->baseUri.$typeSlug.'_ItemEndpoint'; | |
$this->add_resource_triple($this->apiUri, API.'endpoint', $endpointUri); | |
$this->add_resource_triple($endpointUri, RDF_TYPE, API.'ItemEndpoint'); | |
$this->add_literal_triple($endpointUri, API.'uriTemplate', $this->getUriTemplateFromUri($exampleUri)); | |
$this->add_literal_triple($endpointUri, API.'itemTemplate', $this->getItemTemplateFromUri($exampleUri)); | |
} | |
} | |
function getExampleProperties(){ | |
$properties = array(); | |
foreach($this->voiD->get_resource_triple_values($this->voidUri, VOID_EXAMPLE) as $exampleUri){ | |
foreach ($this->voiD->get_subject_properties($exampleUri) as $property) { | |
$properties[]= $property; | |
$this->getSlug($property); | |
} | |
} | |
return array_unique($properties); | |
} | |
function getItemTemplateFromUri($uri){ | |
preg_match('@^(.+[/#])[^/#]+$@', $uri, $m); | |
return $m[1].'{localname}'; | |
} | |
function getUriTemplateFromUri($uri){ | |
$path = parse_url($uri, PHP_URL_PATH); | |
preg_match('@^(.+[/#])[^/#]+$@', $path, $m); | |
return $m[1].'{localname}'; | |
} | |
# makeListEndpoints | |
# | |
# | |
public function makeListEndpoints(){ | |
foreach($this->getExampleTypes() as $exampleType){ | |
$slug = $this->getSlug($exampleType); | |
$endpointUri = $this->baseUri.ucwords($slug).'_ListEndpoint'; | |
$this->add_resource_triple($this->apiUri, API.'endpoint', $endpointUri); | |
$this->add_resource_triple($endpointUri, RDF_TYPE, API.'ListEndpoint'); | |
$uriTemplate = '/'.$this->pluralise($slug); | |
$this->add_literal_triple($endpointUri, API.'uriTemplate', $uriTemplate); | |
$selectorUri = $endpointUri.'/selector'; | |
$this->add_literal_triple($selectorUri, API.'filter', "type={$slug}"); | |
$this->add_resource_triple($endpointUri, API.'selector', $selectorUri); | |
} | |
} | |
function getExampleTypes(){ | |
$types = array(); | |
foreach($this->voiD->get_resource_triple_values($this->voidUri, VOID_EXAMPLE) as $exampleUri){ | |
$types = array_merge($this->voiD->get_resource_triple_values($exampleUri, RDF_TYPE), $types); | |
} | |
return array_filter(array_unique($types)); | |
} | |
function getSlug($uri){ | |
if(isset($this->slugMappings[$uri])){ | |
return $this->slugMappings[$uri]; | |
} else { | |
$slugs = array_values($this->slugMappings); | |
preg_match('@[^/#]+$@', $uri, $m); | |
$localname = $m[0]; | |
$slug = $localname; | |
$no=1; | |
while(in_array($slug, $slugs)){ | |
$slug = $localname.$n++; | |
} | |
$this->slugMappings[$uri] = $slug; | |
return $slug; | |
} | |
} | |
function pluralise($in){ | |
$in = strtolower($in); | |
$exceptions = array( | |
'person' => 'people', | |
); | |
if(isset($exceptions[$in])){ | |
return $exceptions[$in]; | |
} else { | |
return $in.'s'; | |
} | |
} | |
} | |
$dsURI = $_SERVER['argv'][1]; | |
$dsSPARQL = isset($_SERVER['argv'][2] )? $_SERVER['argv'][2] : false; | |
$apiBaseUri = isset($_SERVER['argv'][3] )? $_SERVER['argv'][3] : false; | |
$g = new voiDtoLDA($dsURI, $dsSPARQL, $apiBaseUri); | |
if(!empty($g->errors)){ | |
echo "\nThere were errors generating your API Configuration\n"; | |
foreach ($g->errors as $error) { | |
echo "\n----------\n $error"; | |
} | |
} else echo $g->to_turtle(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expects the Dataset to have at least one exampleResource and a void:sparqlEndpoint
Will generate an ItemEndpoint and ListEndpoint from each void:exampleResource.
Usage:
php voiDtoLDA.php http://trafficscotland.dataincubator.org/