From 802a7f1b4107a2c5703e3721803bfbc5c3f459be Mon Sep 17 00:00:00 2001 From: Osma Suominen Date: Tue, 19 Jan 2016 18:13:52 +0200 Subject: [PATCH] Obtain namespace prefixes from vocabularies.ttl and use them in RDF export. Fixes #387 --- model/Model.php | 40 +++++++++++++++++++++---- model/NamespaceExposingTurtleParser.php | 18 +++++++++++ tests/ModelTest.php | 10 +++++++ tests/testvocabularies.ttl | 9 ++++++ 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 model/NamespaceExposingTurtleParser.php diff --git a/model/Model.php b/model/Model.php index ea0dbb904..39acd52b6 100644 --- a/model/Model.php +++ b/model/Model.php @@ -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 */ @@ -40,6 +42,7 @@ public function __construct($config) { $this->global_config = $config; $this->initializeVocabularies(); + $this->initializeNamespaces(); } /** @@ -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. diff --git a/model/NamespaceExposingTurtleParser.php b/model/NamespaceExposingTurtleParser.php new file mode 100644 index 000000000..d24e7e111 --- /dev/null +++ b/model/NamespaceExposingTurtleParser.php @@ -0,0 +1,18 @@ + URI + * @return array $namespaces + */ + public function getNamespaces() + { + return $this->namespaces; + } +} diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 2bd82f572..1932a545c 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -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: .", $result); + } /** * @covers Model::getRDF diff --git a/tests/testvocabularies.ttl b/tests/testvocabularies.ttl index f8ad299b1..5b04fdbd2 100644 --- a/tests/testvocabularies.ttl +++ b/tests/testvocabularies.ttl @@ -12,6 +12,7 @@ @prefix skosmos: . @prefix isothes: . @prefix meta: . +@prefix my: . @prefix : <#> . @@ -102,6 +103,14 @@ skosmos:language "en"; skosmos:sparqlGraph . +: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 ; + skosmos:language "en"; + skosmos:sparqlGraph . + dc:format "application/rdf+xml" . :cat_science a skos:Concept ;