Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to show deprecated concepts / skosmos:showDeprecated #653

Merged
merged 4 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,18 @@ public function searchConcepts($params)
}

$vocabs = $params->getVocabs();

$showDeprecated=false;
if (sizeof($vocabs) === 1) { // search within vocabulary
$voc = $vocabs[0];
$sparql = $voc->getSparql();
$showDeprecated=$voc->getConfig()->getShowDeprecated();
} else { // multi-vocabulary or global search
$voc = null;
$sparql = $this->getDefaultSparql();
// @TODO : in a global search showDeprecated will always be false and cannot be set globally
}

$results = $sparql->queryConcepts($vocabs, $params->getAdditionalFields(), $params->getUnique(), $params);
$results = $sparql->queryConcepts($vocabs, $params->getAdditionalFields(), $params->getUnique(), $params,$showDeprecated);
if ($params->getRest() && $results && $params->getSearchLimit() !== 0) {
$results = array_slice($results, $params->getOffset(), $params->getSearchLimit());
}
Expand Down
5 changes: 3 additions & 2 deletions model/Vocabulary.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public function listConceptGroupContents($glname, $clang)
}
// no group class defined, so empty result
$group = $this->getConceptURI($glname);
$contents = $this->getSparql()->listConceptGroupContents($gclass, $group, $clang);
$contents = $this->getSparql()->listConceptGroupContents($gclass, $group, $clang, $this->config->getShowDeprecated());
foreach ($contents as $uri => $label) {
$ret[$uri] = $label;
}
Expand Down Expand Up @@ -482,7 +482,7 @@ public function getAlphabet($clang)
*/
public function searchConceptsAlphabetical($letter, $limit = null, $offset = null, $clang = null)
{
return $this->getSparql()->queryConceptsAlphabetical($letter, $clang, $limit, $offset, $this->config->getIndexClasses());
return $this->getSparql()->queryConceptsAlphabetical($letter, $clang, $limit, $offset, $this->config->getIndexClasses(),$this->config->getShowDeprecated());
}

/**
Expand Down Expand Up @@ -609,4 +609,5 @@ public function getShortName() {
public function getId() {
return $this->config->getId();
}

}
9 changes: 9 additions & 0 deletions model/VocabularyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ public function showAlphabeticalIndex()
{
return $this->getBoolean('skosmos:showAlphabeticalIndex', true);
}

/**
* Returns a boolean value set in the vocabularies.ttl config.
* @return boolean
*/
public function getShowDeprecated()
{
return $this->getBoolean('skosmos:showDeprecated', false);
}

/**
* Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration.
Expand Down
45 changes: 31 additions & 14 deletions model/sparql/GenericSparql.php
Original file line number Diff line number Diff line change
Expand Up @@ -922,10 +922,11 @@ protected function generateConceptSearchQueryInner($term, $lang, $searchLang, $p
* Query for concepts using a search term.
* @param array|null $fields extra fields to include in the result (array of strings). (default: null = none)
* @param boolean $unique restrict results to unique concepts (default: false)
* @param boolean $showDeprecated whether to include deprecated concepts in search results (default: false)
* @param ConceptSearchParameters $params
* @return string sparql query
*/
protected function generateConceptSearchQuery($fields, $unique, $params) {
protected function generateConceptSearchQuery($fields, $unique, $params, $showDeprecated = false) {
$vocabs = $params->getVocabs();
$gcl = $this->graphClause;
$fcl = empty($vocabs) ? '' : $this->generateFromClause($vocabs);
Expand All @@ -941,7 +942,11 @@ protected function generateConceptSearchQuery($fields, $unique, $params) {
$schemecond .= "?s skos:inScheme <$scheme> . ";
}
}

$filterDeprecated="";
//show or hide deprecated concepts
if(!$showDeprecated){
$filterDeprecated="FILTER NOT EXISTS { ?s owl:deprecated true }";
}
// extra conditions for parent and group, if specified
$parentcond = ($params->getParentLimit()) ? "?s skos:broader+ <" . $params->getParentLimit() . "> ." : "";
$groupcond = ($params->getGroupLimit()) ? "<" . $params->getGroupLimit() . "> skos:member ?s ." : "";
Expand Down Expand Up @@ -989,7 +994,7 @@ protected function generateConceptSearchQuery($fields, $unique, $params) {
?s a ?type .
$extrafields $schemecond
}
FILTER NOT EXISTS { ?s owl:deprecated true }
$filterDeprecated
}
$filterGraph
}
Expand Down Expand Up @@ -1106,11 +1111,12 @@ private function transformConceptSearchResults($results, $vocabs, $fields) {
* @param array $vocabs array of Vocabulary objects to search; empty for global search
* @param array $fields extra fields to include in the result (array of strings). (default: null = none)
* @param boolean $unique restrict results to unique concepts (default: false)
* @param boolean $showDeprecated whether to include deprecated concepts in the result (default: false)
* @param ConceptSearchParameters $params
* @return array query result object
*/
public function queryConcepts($vocabs, $fields = null, $unique = false, $params) {
$query = $this->generateConceptSearchQuery($fields, $unique, $params);
public function queryConcepts($vocabs, $fields = null, $unique = false, $params, $showDeprecated = false) {
$query = $this->generateConceptSearchQuery($fields, $unique, $params,$showDeprecated);
$results = $this->query($query);
return $this->transformConceptSearchResults($results, $vocabs, $fields);
}
Expand Down Expand Up @@ -1153,17 +1159,21 @@ private function formatFilterConditions($letter, $lang) {
* @param integer $limit limits the amount of results
* @param integer $offset offsets the result set
* @param array|null $classes
* @param boolean $showDeprecated whether to include deprecated concepts in the result (default: false)
* @return string sparql query
*/
protected function generateAlphabeticalListQuery($letter, $lang, $limit, $offset, $classes) {
protected function generateAlphabeticalListQuery($letter, $lang, $limit, $offset, $classes, $showDeprecated = false) {
$fcl = $this->generateFromClause();
$classes = ($classes) ? $classes : array('http://www.w3.org/2004/02/skos/core#Concept');
$values = $this->formatValues('?type', $classes, 'uri');
$limitandoffset = $this->formatLimitAndOffset($limit, $offset);
$conditions = $this->formatFilterConditions($letter, $lang);
$filtercondLabel = $conditions['filterpref'];
$filtercondALabel = $conditions['filteralt'];

$filterDeprecated="";
if(!$showDeprecated){
$filterDeprecated="FILTER NOT EXISTS { ?s owl:deprecated true }";
}
$query = <<<EOQ
SELECT DISTINCT ?s ?label ?alabel $fcl
WHERE {
Expand All @@ -1187,7 +1197,7 @@ protected function generateAlphabeticalListQuery($letter, $lang, $limit, $offset
}
}
?s a ?type .
FILTER NOT EXISTS { ?s owl:deprecated true }
$filterDeprecated
$values
}
ORDER BY LCASE(IF(BOUND(?alabel), STR(?alabel), STR(?label))) $limitandoffset
Expand Down Expand Up @@ -1236,9 +1246,10 @@ private function transformAlphabeticalListResults($results) {
* @param integer $limit limits the amount of results
* @param integer $offset offsets the result set
* @param array $classes
* @param boolean $showDeprecated whether to include deprecated concepts in the result (default: false)
*/
public function queryConceptsAlphabetical($letter, $lang, $limit = null, $offset = null, $classes = null) {
$query = $this->generateAlphabeticalListQuery($letter, $lang, $limit, $offset, $classes);
public function queryConceptsAlphabetical($letter, $lang, $limit = null, $offset = null, $classes = null,$showDeprecated = false) {
$query = $this->generateAlphabeticalListQuery($letter, $lang, $limit, $offset, $classes,$showDeprecated);
$results = $this->query($query);
return $this->transformAlphabeticalListResults($results);
}
Expand Down Expand Up @@ -1945,16 +1956,21 @@ public function listConceptGroups($groupClass, $lang) {
* @param string $groupClass URI of concept group class
* @param string $group URI of the concept group instance
* @param string $lang language of labels to return
* @param boolean $showDeprecated whether to include deprecated in the result
* @return string sparql query
*/
private function generateConceptGroupContentsQuery($groupClass, $group, $lang) {
private function generateConceptGroupContentsQuery($groupClass, $group, $lang, $showDeprecated = false) {
$fcl = $this->generateFromClause();
$filterDeprecated="";
if(!$showDeprecated){
$filterDeprecated=" FILTER NOT EXISTS { ?conc owl:deprecated true }";
}
$query = <<<EOQ
SELECT ?conc ?super ?label ?members ?type ?notation $fcl
WHERE {
<$group> a <$groupClass> .
{ <$group> skos:member ?conc . } UNION { ?conc isothes:superGroup <$group> }
FILTER NOT EXISTS { ?conc owl:deprecated true }
$filterDeprecated
?conc a ?type .
OPTIONAL { ?conc skos:prefLabel ?label .
FILTER (langMatches(lang(?label), '$lang'))
Expand Down Expand Up @@ -2014,10 +2030,11 @@ private function transformConceptGroupContentsResults($result, $lang) {
* @param string $groupClass URI of concept group class
* @param string $group URI of the concept group instance
* @param string $lang language of labels to return
* @param boolean $showDeprecated whether to include deprecated concepts in search results
* @return array Result array with concept URI as key and concept label as value
*/
public function listConceptGroupContents($groupClass, $group, $lang) {
$query = $this->generateConceptGroupContentsQuery($groupClass, $group, $lang);
public function listConceptGroupContents($groupClass, $group, $lang,$showDeprecated = false) {
$query = $this->generateConceptGroupContentsQuery($groupClass, $group, $lang,$showDeprecated);
$result = $this->query($query);
return $this->transformConceptGroupContentsResults($result, $lang);
}
Expand Down
10 changes: 8 additions & 2 deletions model/sparql/JenaTextSparql.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ protected function generateConceptSearchQueryCondition($term, $searchLang)
* @param integer $limit limits the amount of results
* @param integer $offset offsets the result set
* @param array|null $classes
* @param boolean $showDeprecated whether to include deprecated concepts in the result (default: false)
* @return string sparql query
*/

public function generateAlphabeticalListQuery($letter, $lang, $limit = null, $offset = null, $classes = null)
public function generateAlphabeticalListQuery($letter, $lang, $limit = null, $offset = null, $classes = null, $showDeprecated = false)
{
if ($letter == '*' || $letter == '0-9' || $letter == '!*') {
// text index cannot support special character queries, use the generic implementation for these
Expand All @@ -100,6 +101,11 @@ public function generateAlphabeticalListQuery($letter, $lang, $limit = null, $of
$textcondPref = $this->createTextQueryCondition($letter . '*', 'skos:prefLabel', $lang);
$textcondAlt = $this->createTextQueryCondition($letter . '*', 'skos:altLabel', $lang);

$filterDeprecated="";
if(!$showDeprecated){
$filterDeprecated="FILTER NOT EXISTS { ?s owl:deprecated true }";
}

$query = <<<EOQ
SELECT DISTINCT ?s ?label ?alabel
WHERE {
Expand All @@ -122,7 +128,7 @@ public function generateAlphabeticalListQuery($letter, $lang, $limit = null, $of
}
}
?s a ?type .
FILTER NOT EXISTS { ?s owl:deprecated true }
$filterDeprecated
} $values
}
ORDER BY LCASE(?match) $limitandoffset
Expand Down
33 changes: 32 additions & 1 deletion tests/GenericSparqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ public function testListConceptGroups()
* @covers GenericSparql::generateConceptGroupContentsQuery
* @covers GenericSparql::transformConceptGroupContentsResults
*/
public function testListConceptGroupContentsIncludingDeprecatedConcept()
public function testListConceptGroupContentsExcludingDeprecatedConcept()
{
$voc = $this->model->getVocabulary('groups');
$graph = $voc->getGraph();
Expand All @@ -829,6 +829,37 @@ public function testListConceptGroupContentsIncludingDeprecatedConcept()
$this->assertEquals('http://www.skosmos.skos/groups/ta113', $actual[0]['uri']);
$this->assertEquals(1, sizeof($actual));
}

/**
* @covers GenericSparql::listConceptGroupContents
* @covers GenericSparql::generateConceptGroupContentsQuery
* @covers GenericSparql::transformConceptGroupContentsResults
*/
public function testListConceptGroupContentsIncludingDeprecatedConcept()
{
$voc = $this->model->getVocabulary('showDeprecated');
$graph = $voc->getGraph();
$sparql = new GenericSparql('http://localhost:3030/ds/sparql', $graph, $this->model);
$actual = $sparql->ListConceptGroupContents('http://www.w3.org/2004/02/skos/core#Collection', 'http://www.skosmos.skos/groups/salt', 'en', $voc->getConfig()->getShowDeprecated());
$expected = array (
0 => array (
'uri' => 'http://www.skosmos.skos/groups/ta113',
'isSuper' => false,
'hasMembers' => false,
'type' => array('skos:Concept'),
'prefLabel' => 'Flatfish'
),
1 => array (
'uri' => 'http://www.skosmos.skos/groups/ta111',
'isSuper' => false,
'hasMembers' => false,
'type' => array('skos:Concept'),
'prefLabel' => 'Tuna'
)
);
$this->assertEquals($expected, $actual);
$this->assertEquals(2, sizeof($actual));
}

/**
* @covers GenericSparql::queryChangeList
Expand Down
13 changes: 13 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ public function testSearchConceptsUnique() {
$this->assertCount(2, $result);
}

/**
* @covers Model::searchConcepts
*/
public function testSearchConceptsIncludingDeprecated() {
$params = $this->getMockBuilder('ConceptSearchParameters')->disableOriginalConstructor()->getMock();
$params->method('getSearchTerm')->will($this->returnValue('Tuna'));
$params->method('getVocabIds')->will($this->returnValue('showDeprecated'));
$params->method('getVocabs')->will($this->returnValue(array($this->model->getVocabulary('showDeprecated'))));
$result = $this->model->searchConcepts($params);
$this->assertCount(1, $result);
$this->assertEquals('http://www.skosmos.skos/groups/ta111', $result[0]['uri']);
}

/**
* @covers Model::searchConcepts
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/VocabularyConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ public function testGetTypes() {
$vocab = $this->model->getVocabulary('test');
$this->assertEquals(array(0 => array('uri' => 'http://publications.europa.eu/resource/authority/dataset-type/ONTOLOGY', 'prefLabel' => 'Ontology')), $vocab->getConfig()->getTypes('en'));
}

/**
* @covers VocabularyConfig::getShowDeprecated
*/
public function testShowDeprecated() {
$vocab = $this->model->getVocabulary('showDeprecated');
$this->assertEquals(true, $vocab->getConfig()->getShowDeprecated());
}

/**
* @covers VocabularyConfig::getTypes
Expand Down
19 changes: 19 additions & 0 deletions tests/VocabularyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,25 @@ public function testSearchConceptsAlphabetical() {
$this->assertEquals('Grouped fish', $concepts[0]['prefLabel']);
$this->assertEquals('Guppy', $concepts[1]['prefLabel']);
}

/**
* @covers Vocabulary::searchConceptsAlphabetical
*/
public function testSearchConceptsAlphabeticalExcludingDeprecated() {
$vocab = $this->model->getVocabulary('groups');
$concepts = $vocab->searchConceptsAlphabetical('T', null, null, 'en');
$this->assertCount(0, $concepts);
}

/**
* @covers Vocabulary::searchConceptsAlphabetical
*/
public function testSearchConceptsAlphabeticalIncludingDeprecated() {
$vocab = $this->model->getVocabulary('showDeprecated');
$concepts = $vocab->searchConceptsAlphabetical('T', null, null, 'en');
$this->assertCount(1, $concepts);
$this->assertEquals('Tuna', $concepts[0]['prefLabel']);
}

/**
* @covers Vocabulary::searchConceptsAlphabetical
Expand Down
15 changes: 15 additions & 0 deletions tests/testvocabularies.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@
skosmos:indexShowClass meta:TestClass, meta:TestClass2;
skosmos:sparqlGraph <http://www.skosmos.skos/groups/> .


:showDeprecated a skosmos:Vocabulary, void:Dataset ;
dc11:title "Show deprecated test vocabulary"@en ;
dc:subject :cat_general ;
void:dataDump <http://skosmos.skos/dump/test/groups.ttl>,
<http://skosmos.skos/dump/test/groups> ;
void:uriSpace "http://www.skosmos.skos/onto/groups/";
skosmos:arrayClass isothes:ThesaurusArray ;
skosmos:groupClass skos:Collection ;
void:sparqlEndpoint <http://localhost:3030/ds/sparql> ;
skosmos:language "fi", "en";
skosmos:defaultLanguage "fi";
skosmos:showDeprecated "true";
skosmos:sparqlGraph <http://www.skosmos.skos/groups/> .

:cycle a skosmos:Vocabulary, void:Dataset ;
dc11:title "Cycle test vocabulary"@en ;
dc:subject :cat_general ;
Expand Down