Skip to content

Commit

Permalink
Obtain namespace prefixes from vocabularies.ttl and use them in RDF e…
Browse files Browse the repository at this point in the history
…xport. Fixes #387
  • Loading branch information
osma committed Jan 19, 2016
1 parent d10434f commit 802a7f1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
40 changes: 35 additions & 5 deletions model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Model
{
/** EasyRdf_Graph graph instance */
private $graph;
/** Namespaces from vocabularies configuration file */
private $namespaces;
/** cache for Vocabulary objects */
private $all_vocabularies = null;
/** cache for Vocabulary objects */
Expand All @@ -40,6 +42,7 @@ public function __construct($config)
{
$this->global_config = $config;
$this->initializeVocabularies();
$this->initializeNamespaces();
}

/**
Expand All @@ -63,21 +66,48 @@ private function initializeVocabularies()
// use APC user cache to store parsed vocabularies.ttl configuration
if (function_exists('apc_store') && function_exists('apc_fetch')) {
$key = realpath($this->getConfig()->getVocabularyConfigFile()) . ", " . filemtime($this->getConfig()->getVocabularyConfigFile());
$nskey = "namespaces of " . $key;
$this->graph = apc_fetch($key);
if ($this->graph === false) { // was not found in cache
$this->graph = new EasyRdf_Graph();
$this->graph->parse(file_get_contents($this->getConfig()->getVocabularyConfigFile()));
$this->namespaces = apc_fetch($nskey);
if ($this->graph === false || $this->namespaces === false) { // was not found in cache
$this->parseVocabularies($this->getConfig()->getVocabularyConfigFile());
apc_store($key, $this->graph);
apc_store($nskey, $this->namespaces);
}
} else { // APC not available, parse on every request
$this->graph = new EasyRdf_Graph();
$this->graph->parse(file_get_contents($this->getConfig()->getVocabularyConfigFile()));
$this->parseVocabularies($this->getConfig()->getVocabularyConfigFile());
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
}

/**
* Parses vocabulary configuration and RDF namespaces from the vocabularies.ttl file
* @param string $filename path to vocabularies.ttl file
*/

private function parseVocabularies($filename)
{
$this->graph = new EasyRdf_Graph();
$parser = new NamespaceExposingTurtleParser();
$parser->parse($this->graph, file_get_contents($filename), 'turtle', $filename);
$this->namespaces = $parser->getNamespaces();
}

/**
* Registers RDF namespaces from the vocabularies.ttl file for use by EasyRdf (e.g. serializing)
*/

private function initializeNamespaces() {
foreach ($this->namespaces as $prefix => $full_uri) {
if ($prefix != '' && EasyRdf_Namespace::get($prefix) === null) // if not already defined
{
EasyRdf_Namespace::set($prefix, $full_uri);
}
}
}

/**
* Return the version of this Skosmos installation, or "unknown" if
* it cannot be determined. The version information is based on Git tags.
Expand Down
18 changes: 18 additions & 0 deletions model/NamespaceExposingTurtleParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Copyright (c) 2016 University of Helsinki
* MIT License
* see LICENSE.txt for more information
*/

class NamespaceExposingTurtleParser extends EasyRdf_Parser_Turtle
{
/**
* Returns the namespace prefixes as an array of prefix => URI
* @return array $namespaces
*/
public function getNamespaces()
{
return $this->namespaces;
}
}
10 changes: 10 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ public function testSearchConceptsAndInfoWithOneVocabCaseInsensitivity() {
$this->assertEquals('http://www.skosmos.skos/test/ta116', $result['results'][0]->getUri());
$this->assertEquals(1, $result['count']);
}

/**
* Test for issue #387: make sure namespaces defined in vocabularies.ttl are used for RDF export
* @covers Model::getRDF
*/

public function testGetRdfCustomPrefix() {
$result = $this->model->getRDF('prefix', 'http://www.skosmos.skos/prefix/p1', 'text/turtle');
$this->assertContains("@prefix my: <http://example.com/myns#> .", $result);
}

/**
* @covers Model::getRDF
Expand Down
9 changes: 9 additions & 0 deletions tests/testvocabularies.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@prefix skosmos: <http://purl.org/net/skosmos#> .
@prefix isothes: <http://purl.org/iso25964/skos-thes#>.
@prefix meta: <http://www.skosmos.skos/test-meta/> .
@prefix my: <http://example.com/myns#> .
@prefix : <#> .


Expand Down Expand Up @@ -102,6 +103,14 @@
skosmos:language "en";
skosmos:sparqlGraph <http://www.skosmos.skos/changes/> .

:prefix a skosmos:Vocabulary, void:Dataset ;
dc11:title "A vocabulary for testing custom prefixes"@en ;
dc:subject :cat_general ;
void:uriSpace "http://www.skosmos.skos/prefix/";
void:sparqlEndpoint <http://localhost:3030/ds/sparql> ;
skosmos:language "en";
skosmos:sparqlGraph <http://www.skosmos.skos/prefix/> .

<http://skosmos.skos/dump/test/groups> dc:format "application/rdf+xml" .

:cat_science a skos:Concept ;
Expand Down

0 comments on commit 802a7f1

Please sign in to comment.