From a8bf375ff91a34272521bc0baad36fb25bee18fc Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Sat, 9 Mar 2019 10:57:43 +1300 Subject: [PATCH] Move linkUrlFilter from WebController to parent Controller class, so it can be called from RestController too --- controller/Controller.php | 52 ++++++++++++++++++++++++++++++++++- controller/RestController.php | 3 +- controller/WebController.php | 48 -------------------------------- 3 files changed, 53 insertions(+), 50 deletions(-) diff --git a/controller/Controller.php b/controller/Controller.php index e971ee42e..55f04ee66 100644 --- a/controller/Controller.php +++ b/controller/Controller.php @@ -7,7 +7,7 @@ class Controller { /** * The controller has to know the model to access the data stored there. - * @param $model contains the Model object. + * @var Model $model contains the Model object. */ public $model; @@ -106,6 +106,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 = 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); + } + /** * Echos an error message when the request can't be fulfilled. * @param string $code diff --git a/controller/RestController.php b/controller/RestController.php index f713357bc..1f2450c39 100644 --- a/controller/RestController.php +++ b/controller/RestController.php @@ -3,7 +3,7 @@ /** * RestController is responsible for handling all the requests directed to the /rest address. */ -class RestController extends WebController +class RestController extends Controller { /* supported MIME types that can be used to return RDF data */ const SUPPORTED_FORMATS = 'application/rdf+xml text/turtle application/ld+json application/json'; @@ -625,6 +625,7 @@ 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) { diff --git a/controller/WebController.php b/controller/WebController.php index aa96e148a..562c06c88 100644 --- a/controller/WebController.php +++ b/controller/WebController.php @@ -73,54 +73,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".