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

Issue835 altlabel sort #993

Merged
merged 3 commits into from
Jun 12, 2020
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
60 changes: 46 additions & 14 deletions model/Concept.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,36 +818,68 @@ public function getArrayProperties() {
}

/**
* Reads the literal language code and gets a name for it from Punic or alternatively
* Given a language code, gets its name in UI language via Punic or alternatively
* tries to search for a gettext translation.
* @param EasyRdf\Literal $lit
* @param string $langCode
* @return string e.g. 'English'
*/
private function literalLanguageToString($lit) {
// using empty string as the language literal when there is no langcode set
private function langToString($langCode) {
// using empty string as the language name when there is no langcode set
$langName = '';
if ($lit->getLang() !== null) {
$langName = Punic\Language::getName($lit->getLang(), $this->getEnvLang()) !== $lit->getLang() ? Punic\Language::getName($lit->getLang(), $this->getEnvLang()) : gettext($lit->getLang());
if (!empty($langCode)) {
$langName = Punic\Language::getName($langCode, $this->getEnvLang()) !== $langCode ? Punic\Language::getName($langCode, $this->getEnvLang()) : gettext($langCode);
}
return $langName;
}

/**
* Gets the values for the property in question in all other languages than the ui language.
* Gets the values of a property in all other languages than in the current language
* and places them in a [langCode][userDefinedKey] array.
* @param string $prop The property for which the values are looked upon to
* @param string $key User-defined key for accessing the values
* @return array LangCode-based multidimensional array ([string][string][ConceptPropertyValueLiteral]) or empty array if no values
*/
public function getForeignLabels()
{
$prefLabels = $this->resource->allLiterals('skos:prefLabel');
$labels = array_merge($prefLabels, $this->resource->allLiterals('skos:altLabel'));
private function getForeignLabelList($prop, $key) {
$ret = array();
$labels = $this->resource->allLiterals($prop);

foreach ($labels as $lit) {
// filtering away subsets of the current language eg. en vs en-GB
if ($lit->getLang() != $this->clang && strpos($lit->getLang(), $this->getEnvLang() . '-') !== 0) {
$prop = in_array($lit, $prefLabels) ? 'skos:prefLabel' : 'skos:altLabel';
$ret[$this->literalLanguageToString($lit)][] = new ConceptPropertyValueLiteral($this->model, $this->vocab, $this->resource, $lit, $prop);
$langCode = $lit->getLang() ? $lit->getLang() : '';
$ret[$langCode][$key][] = new ConceptPropertyValueLiteral($this->model, $this->vocab, $this->resource, $lit, $prop);
}
}
return $ret;
}

/**
* Gets the values of skos:prefLabel and skos:altLabel in all other languages than in the current language.
* @return array Language-based multidimensional sorted array ([string][string][ConceptPropertyValueLiteral]) or empty array if no values
*/
public function getForeignLabels()
{
$prefLabels = $this->getForeignLabelList('skos:prefLabel', 'prefLabel');
$altLabels = $this->getForeignLabelList('skos:altLabel', 'altLabel');
$ret = array_merge_recursive($prefLabels, $altLabels);

$langArray = array_keys($ret);
foreach ($langArray as $lang) {
$coll = collator_create($lang);
if (isset($ret[$lang]['prefLabel']))
{
$coll->sort($ret[$lang]['prefLabel'], Collator::SORT_STRING);
}
if (isset($ret[$lang]['altLabel']))
{
$coll->sort($ret[$lang]['altLabel'], Collator::SORT_STRING);
}
if ($lang !== '') {
$ret[$this->langToString($lang)] = $ret[$lang];
unset($ret[$lang]);
}
}
ksort($ret);
uksort($ret, 'strcoll');
return $ret;
}

Expand Down
24 changes: 22 additions & 2 deletions tests/ConceptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,33 @@ public function testSetFoundBy()

/**
* @covers Concept::getForeignLabels
* @covers Concept::literalLanguageToString
* @covers Concept::getForeignLabelList
* @covers Concept::langToString
*/
public function testGetForeignLabels()
{
$labels = $this->concept->getForeignLabels();

$this->assertEquals('Karppi', $labels['Finnish'][0]->getLabel());
$this->assertEquals('Karppi', $labels['Finnish']['prefLabel'][0]->getLabel());
$this->assertArrayNotHasKey('altLabel', $labels['Finnish']);
$this->assertArrayNotHasKey('English', $labels);

$results = $this->vocab->getConceptInfo('http://www.skosmos.skos/test/ta115', 'en');
$concept = reset($results);
$this->assertEmpty($concept->getForeignLabels());

$results = $this->vocab->getConceptInfo('http://www.skosmos.skos/test/ta127', 'en');
$concept = reset($results);
$labels = $concept->getForeignLabels();
$this->assertEquals(['', 'Finnish', 'Swedish'], array_keys($labels));

$this->assertEquals("Ä before 'A first' in English (default) collation", $labels['']['prefLabel'][0]->getLabel());
$this->assertEquals("Test sorting labels 2", $labels['']['prefLabel'][3]->getLabel());

$this->assertArrayHasKey('prefLabel', $labels['Finnish']);
$fiAltLabels = array_map(function ($elem) {return $elem->getLabel();}, $labels['Finnish']['altLabel']);
$fiCorrectSort = ["A sort first", "B sorts second", "Ä way after B in Finnish collation"];
$this->assertEquals($fiCorrectSort, $fiAltLabels);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/GenericSparqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testGetGraph() {
*/
public function testCountConcepts() {
$actual = $this->sparql->countConcepts();
$this->assertEquals(16, $actual['http://www.w3.org/2004/02/skos/core#Concept']['count']);
$this->assertEquals(17, $actual['http://www.w3.org/2004/02/skos/core#Concept']['count']);
}

/**
Expand All @@ -65,7 +65,7 @@ public function testCountLangConceptsMultipleLangs() {
$actual = $this->sparql->countLangConcepts(array('en','fi'));
$this->assertEquals(11, $actual['en']['skos:prefLabel']);
$this->assertEquals(1, $actual['en']['skos:altLabel']);
$this->assertEquals(2, $actual['fi']['skos:prefLabel']);
$this->assertEquals(3, $actual['fi']['skos:prefLabel']);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/VocabularyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function testGetLabelStatistics() {
public function testGetStatistics() {
$vocab = $this->model->getVocabulary('test');
$stats = $vocab->getStatistics();
$this->assertEquals(16, $stats['http://www.w3.org/2004/02/skos/core#Concept']['count']);
$this->assertEquals(17, $stats['http://www.w3.org/2004/02/skos/core#Concept']['count']);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/test-vocab-data/test.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ test:ta126
skos:notation "12.34" ;
skos:prefLabel "Europa"@nb .

test:ta127
a skos:Concept ;
skos:definition "For testing sorting and language names of getForeignLabels()"@en ;
skos:prefLabel "Test sorting labels 1",
"Test sorting labels 2",
"A first",
"Ä before 'A first' in English (default) collation",
"Test prefLabel"@fi;

skos:altLabel "A sort first"@fi,
"B sorts second"@fi,
"Ä way after B in Finnish collation"@fi,
"Finnish before Swedish in result array"@sv,
"No language tag becomes very first in result language array" .

test:ta1 a skos:Concept, meta:TestClass ;
skos:inScheme test:conceptscheme ;
skos:narrower test:ta111,
Expand Down
13 changes: 6 additions & 7 deletions view/concept-shared.twig
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,17 @@
</div></div>
</div>
{% endif %}
{% if concept.foreignLabels %}
{% set foreignLabels = concept.foreignLabels %}
{% if foreignLabels %}
<div class="row">
<div class="property-label"><span class="versal property-click" title="{% trans "foreign prefLabel help" %}" >{{ 'foreign prefLabels'|trans|upper }}</span></div>
<div class="property-value-column"><div class="property-value-wrapper">
{% set prevlang = '' %}{# Only displaying the language when it appears for the very first time #}
{% for language,labels in concept.foreignLabels %}
{% for value in labels %}
<div class="row other-languages{% if prevlang != language %} first-of-language{% endif %}">
{% for language,labels in foreignLabels %}
{% for value in labels.prefLabel|default([])|merge(labels.altLabel|default([])) %}
<div class="row other-languages{% if loop.first %} first-of-language{% endif %}">
<div class="col-xs-6 versal{% if value.type == "skos:altLabel" %} replaced{%else %} versal-pref{% endif %}">{% if value.hasXlProperties %}<span class="reified-property-value xl-label"><img src="resource/pics/about.png"></span><div class="reified-tooltip">{% for key, val in value.getXlProperties %}{% if key != 'rdf:type' and key != 'skosxl:literalForm' and val != '' %}<p>{{ key|trans }}: <span class="versal">{{ val }}</span></p>{% endif %}{% endfor %}</div>{% endif %}{%if value.type == "skos:prefLabel" and value.lang in request.vocab.config.languages %}<a href='{{ concept.uri|link_url(request.vocabid,request.lang, 'page', value.lang) }}' hreflang='{{ value.lang }}'>{{ value.label }}</a>{% else %}{{ value.label }}{%endif%}</div>
<div class="col-xs-6 versal">{% if prevlang != language %}<p>{{ language }}</p>{% endif %}</div>
<div class="col-xs-6 versal">{% if loop.first %}<p>{{ language }}</p>{% endif %}</div>
</div>
{% set prevlang = language %}
{% endfor %}
{% endfor %}
</div>
Expand Down