From cf94201820161dcd59f5a9af28137e64072f6ce3 Mon Sep 17 00:00:00 2001 From: Osma Suominen Date: Tue, 20 Aug 2024 16:03:34 +0300 Subject: [PATCH 1/2] convert linkUrlFilter to a Twig extension class --- src/controller/Controller.php | 51 ---------------------- src/controller/LinkUrlExtension.php | 67 +++++++++++++++++++++++++++++ src/controller/WebController.php | 2 +- 3 files changed, 68 insertions(+), 52 deletions(-) create mode 100644 src/controller/LinkUrlExtension.php diff --git a/src/controller/Controller.php b/src/controller/Controller.php index 029ce140d..962531118 100644 --- a/src/controller/Controller.php +++ b/src/controller/Controller.php @@ -97,57 +97,6 @@ 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 diff --git a/src/controller/LinkUrlExtension.php b/src/controller/LinkUrlExtension.php new file mode 100644 index 000000000..e3c899586 --- /dev/null +++ b/src/controller/LinkUrlExtension.php @@ -0,0 +1,67 @@ +model = $model; + } + + public function getFilters() + { + return [ + new TwigFilter('link_url', [$this, 'linkUrlFilter']), + ]; + } + + /** + * 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) { + return $uri; + } elseif (is_string($vocab)) { + $vocid = $vocab; + $vocab = $this->model->getVocabulary($vocid); + } else { + $vocid = $vocab->getId(); + } + + $params = []; + if (isset($clang) && $clang !== $lang) { + $params['clang'] = $clang; + } + + if (isset($term)) { + $params['q'] = $term; + } + + $localname = $vocab->getLocalName($uri); + if ($localname !== $uri && $localname === urlencode($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; + } + + $params['uri'] = $uri; + return "$vocid/$lang/$type/?" . http_build_query($params); + } +} diff --git a/src/controller/WebController.php b/src/controller/WebController.php index 2a85085b7..2c3aca1f1 100644 --- a/src/controller/WebController.php +++ b/src/controller/WebController.php @@ -50,7 +50,7 @@ public function __construct($model) $this->twig->addGlobal("PreferredProperties", array('skos:prefLabel', 'skos:narrower', 'skos:broader', 'skosmos:memberOf', 'skos:altLabel', 'skos:related')); // register a Twig filter for generating URLs for vocabulary resources (concepts and groups) - $this->twig->addFilter(new \Twig\TwigFilter('link_url', array($this, 'linkUrlFilter'))); + $this->twig->addExtension(new LinkUrlExtension($model)); // register a Twig filter for generating strings from language codes with CLDR $langFilter = new \Twig\TwigFilter('lang_name', function ($langcode, $lang) { From 50e684ec93a3600863d329b55b08e1b6778399e4 Mon Sep 17 00:00:00 2001 From: Osma Suominen Date: Tue, 20 Aug 2024 16:17:03 +0300 Subject: [PATCH 2/2] fix use of linkUrlFilter in RestController --- src/controller/RestController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controller/RestController.php b/src/controller/RestController.php index cec6fdf23..42fad8ab8 100644 --- a/src/controller/RestController.php +++ b/src/controller/RestController.php @@ -706,9 +706,10 @@ public function mappings(Request $request) } $mappings = []; + $linkUrlExtension = new LinkUrlExtension($this->model); foreach ($concept->getMappingProperties() as $mappingProperty) { foreach ($mappingProperty->getValues() as $mappingPropertyValue) { - $hrefLink = $this->linkUrlFilter($mappingPropertyValue->getUri(), $mappingPropertyValue->getExVocab(), $request->getLang(), 'page', $request->getContentLang()); + $hrefLink = $linkUrlExtension->linkUrlFilter($mappingPropertyValue->getUri(), $mappingPropertyValue->getExVocab(), $request->getLang(), 'page', $request->getContentLang()); $mappings[] = $mappingPropertyValue->asJskos($queryExVocabs, $request->getLang(), $hrefLink); } }