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

Refactor: Convert linkUrlFilter to a Twig extension class #1655

Merged
merged 2 commits into from
Aug 21, 2024
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
51 changes: 0 additions & 51 deletions src/controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions src/controller/LinkUrlExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class LinkUrlExtension extends AbstractExtension
{
private $model;

public function __construct($model)
{
$this->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);
}
}
3 changes: 2 additions & 1 deletion src/controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading