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

Fix performance issue when displaying concept mapping properties #846

Merged
merged 18 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
50 changes: 50 additions & 0 deletions controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,56 @@ public function getBaseHref()
return ($this->model->getConfig()->getBaseHref() !== null) ? $this->model->getConfig()->getBaseHref() : $this->guessBaseHref();
}

/**
* Creates Skosmos links from uris.
* @param string $uri
* @param Vocabulary $vocab
* @param string $lang
* @param string $type
* @param string $clang content
* @param string $term
* @throws Exception if the vocabulary ID is not found in configuration
* @return string containing the Skosmos link
*/
public function linkUrlFilter($uri, $vocab, $lang, $type = 'page', $clang = null, $term = null) {
// $vocab can either be null, a vocabulary id (string) or a Vocabulary object
if ($vocab === null) {
// target vocabulary is unknown, best bet is to link to the plain URI
return $uri;
} elseif (is_string($vocab)) {
$vocid = $vocab;
$vocab = $this->model->getVocabulary($vocid);
} else {
$vocid = $vocab->getId();
}

$params = array();
if (isset($clang) && $clang !== $lang) {
$params['clang'] = $clang;
}

if (isset($term)) {
$params['q'] = $term;
}

// case 1: URI within vocabulary namespace: use only local name
$localname = $vocab->getLocalName($uri);
if ($localname !== $uri && $localname === urlencode($localname)) {
// check that the prefix stripping worked, and there are no problematic chars in localname
$paramstr = count($params) > 0 ? '?' . http_build_query($params) : '';
if ($type && $type !== '' && $type !== 'vocab' && !($localname === '' && $type === 'page')) {
return "$vocid/$lang/$type/$localname" . $paramstr;
}

return "$vocid/$lang/$localname" . $paramstr;
}

// case 2: URI outside vocabulary namespace, or has problematic chars
// pass the full URI as parameter instead
$params['uri'] = $uri;
return "$vocid/$lang/$type/?" . http_build_query($params);
}

/**
* Echos an error message when the request can't be fulfilled.
* @param string $code
Expand Down
17 changes: 12 additions & 5 deletions controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,15 @@ public function data($request)
* Get the mappings associated with a concept, enriched with labels and notations.
* Returns a JSKOS-compatible JSON object.
* @param Request $request
* @throws Exception if the vocabulary ID is not found in configuration
*/
public function mappings(Request $request)
{
$this->setLanguageProperties($request->getLang());
$vocab = $request->getVocab();

if ($request->getUri()) {
$uri = $request->getUri();
} else {
$uri = $request->getUri();
if (!$uri) {
return $this->returnError(400, 'Bad Request', "uri parameter missing");
}

Expand All @@ -655,13 +656,19 @@ public function mappings(Request $request)

$concept = $results[0];

$ret = [];
$mappings = [];
foreach ($concept->getMappingProperties() as $mappingProperty) {
foreach ($mappingProperty->getValues() as $mappingPropertyValue) {
$ret[] = $mappingPropertyValue->asJskos($queryExVocabs);
$hrefLink = $this->linkUrlFilter($mappingPropertyValue->getUri(), $mappingPropertyValue->getExVocab(), $request->getLang(), 'page', $request->getContentLang());
$mappings[] = $mappingPropertyValue->asJskos($queryExVocabs, $request->getLang(), $hrefLink);
}
}

$ret = array(
'mappings' => $mappings,
'graph' => $concept->dumpJsonLd()
);

return $this->returnJson($ret);
}

Expand Down
48 changes: 0 additions & 48 deletions controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,54 +78,6 @@ public function __construct($model)
$this->twig->addGlobal('honeypot', $this->honeypot);
}

/**
* Creates Skosmos links from uris.
* @param string $uri
* @param Vocabulary $vocab
* @param string $lang
* @param string $type
* @param string $clang content
* @param string $term
*/
public function linkUrlFilter($uri, $vocab, $lang, $type = 'page', $clang = null, $term = null) {
// $vocab can either be null, a vocabulary id (string) or a Vocabulary object
if ($vocab === null) {
// target vocabulary is unknown, best bet is to link to the plain URI
return $uri;
} elseif (is_string($vocab)) {
$vocid = $vocab;
$vocab = $this->model->getVocabulary($vocid);
} else {
$vocid = $vocab->getId();
}

$params = array();
if (isset($clang) && $clang !== $lang) {
$params['clang'] = $clang;
}

if (isset($term)) {
$params['q'] = $term;
}

// case 1: URI within vocabulary namespace: use only local name
$localname = $vocab->getLocalName($uri);
if ($localname !== $uri && $localname === urlencode($localname)) {
// check that the prefix stripping worked, and there are no problematic chars in localname
$paramstr = sizeof($params) > 0 ? '?' . http_build_query($params) : '';
if ($type && $type !== '' && $type !== 'vocab' && !($localname === '' && $type === 'page')) {
return "$vocid/$lang/$type/$localname" . $paramstr;
}

return "$vocid/$lang/$localname" . $paramstr;
}

// case 2: URI outside vocabulary namespace, or has problematic chars
// pass the full URI as parameter instead
$params['uri'] = $uri;
return "$vocid/$lang/$type/?" . http_build_query($params);
}

/**
* Guess the language of the user. Return a language string that is one
* of the supported languages defined in the $LANGUAGES setting, e.g. "fi".
Expand Down
3 changes: 3 additions & 0 deletions model/Concept.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ private function addResourceReifications($sub, $pred, $obj, &$seen)
}
}

/**
* @return ConceptProperty[]
*/
public function getMappingProperties(array $whitelist = null)
{
$ret = array();
Expand Down
20 changes: 18 additions & 2 deletions model/ConceptMappingPropertyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,20 @@ public function getNotation()
* Return the mapping as a JSKOS-compatible array.
* @return array
*/
public function asJskos($queryExVocabs = true)
public function asJskos($queryExVocabs = true, $lang = null, $hrefLink = null)
{
$propertyLabel = $this->getLabel($lang, $queryExVocabs);
$propertyLang = $lang;
if (!is_string($propertyLabel)) {
$propertyLang = $propertyLabel->getLang();
$propertyLabel = $propertyLabel->getValue();
}
$ret = [
// JSKOS
'uri' => $this->source->getUri(),
'notation' => $this->getNotation(),
'type' => [$this->type],
'prefLabel' => $propertyLabel,
'from' => [
'memberSet' => [
[
Expand All @@ -187,7 +197,13 @@ public function asJskos($queryExVocabs = true)
'uri' => (string) $this->getUri()
]
]
]
],
// EXTRA
'description' => gettext($this->type . "_help"), // pop-up text
'hrefLink' => $hrefLink, // link to resource as displayed in the UI
'lang' => $propertyLang, // TBD: could it be part of the prefLabel?
'vocabName' => (string) $this->getVocabName(), // vocabulary as displayed in the UI
'typeLabel' => gettext($this->type), // a text used in the UI instead of, for example, skos:closeMatch
];

$fromScheme = $this->vocab->getDefaultConceptScheme();
Expand Down
36 changes: 32 additions & 4 deletions resource/js/docready.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ $(function() { // DOCUMENT READY
loading = setTimeout(function() { $('.concept-spinner').show() }, 500);
}

function ajaxConceptMapping(data) {
// ajaxing the concept mapping properties on the concept page
var $conceptAppendix = $('.concept-appendix');
if ($conceptAppendix.length) {
var concept = {
uri: $conceptAppendix.data('concept-uri'),
type: $conceptAppendix.data('concept-type')
};

// Defined in scripts.js. Will load the mapping properties via Ajax request to JSKOS REST service, and render them.
loadMappingProperties(concept, lang, clang, $conceptAppendix, data);
} else {
makeCallbacks(data);
}
}

// event handler for clicking the hierarchy concepts
$(document).on('click', '.concept-hierarchy a',
function(event) {
Expand All @@ -275,7 +291,7 @@ $(function() { // DOCUMENT READY
updateJsonLD(data);
updateTitle(data);
updateTopbarLang(data);
makeCallbacks(data);
ajaxConceptMapping(data);
// take the content language buttons from the response
$('.header-float .dropdown-menu').empty().append($('.header-float .dropdown-menu', data).html());
}
Expand Down Expand Up @@ -304,7 +320,7 @@ $(function() { // DOCUMENT READY
updateJsonLD(data);
updateTitle(data);
updateTopbarLang(data);
makeCallbacks(data);
ajaxConceptMapping(data);
// take the content language buttons from the response
$('.header-float .dropdown-menu').empty().append($('.header-float .dropdown-menu', data).html());
}
Expand Down Expand Up @@ -432,7 +448,7 @@ $(function() { // DOCUMENT READY
$('.nav').scrollTop(0);
if (window.history.pushState) { window.history.pushState({}, null, event.target.href); }
updateTitle(data);
makeCallbacks(data);
ajaxConceptMapping(data);
// take the content language buttons from the response
$('.header-float .dropdown-menu').empty().append($('.header-float .dropdown-menu', data).html());
}
Expand Down Expand Up @@ -1098,6 +1114,18 @@ $(function() { // DOCUMENT READY
})});
}

makeCallbacks();
// ajaxing the concept mapping properties on the concept page
var $conceptAppendix = $('.concept-appendix');
if ($conceptAppendix.length) {
var concept = {
uri: $conceptAppendix.data('concept-uri'),
type: $conceptAppendix.data('concept-type')
};

// Defined in scripts.js. Will load the mapping properties via Ajax request to JSKOS REST service, and render them.
loadMappingProperties(concept, lang, clang, $conceptAppendix, null);
} else {
makeCallbacks();
}

});
Loading