diff --git a/local/config/vufind/config.ini b/local/config/vufind/config.ini index 336c633977e56d3869521d78678d4a0b6cff5afd..7a05100d552814eac475fabed2bb29b81175a52d 100644 --- a/local/config/vufind/config.ini +++ b/local/config/vufind/config.ini @@ -1552,9 +1552,13 @@ remove[] = "gndmusic" ; identifier[replace] is the replacing phrase ; identifier[method] calls an alternative method with the link as parameter. ; The method is expected in View\Helper\Root\Record.php +; identifier[function] calls a function if available with the link as parameter. +; The function performed on the url *before* replacing with the replacement. It is +; intended to perform url-encoding links so they can be used as parameter value. ; Examples: ; url[pattern] = url.pattern.to.resolve.link ; url[method] = resolveLink +; url[method] = urlencode (https://www.php.net/manual/de/function.urlencode.php) nbn[pattern] = "^urn:nbn:" nbn[search] = "urn:nbn:" nbn[replace] = "http://nbn-resolving.de/urn:nbn:" diff --git a/module/finc/src/finc/View/Helper/Root/Record.php b/module/finc/src/finc/View/Helper/Root/Record.php index 7c1968e2b53841e34abaa6262774a7ca4cea5c9a..f326277cf94206114db2980f4b43304b7017742e 100644 --- a/module/finc/src/finc/View/Helper/Root/Record.php +++ b/module/finc/src/finc/View/Helper/Root/Record.php @@ -326,55 +326,46 @@ class Record extends \VuFind\View\Helper\Root\Record { $rewrite = $this->config->LinksRewrite->toArray(); foreach ($rewrite as $r) { - // is pattern set so try rewrite url - if (isset($r['pattern']) || $r['remove']) { - // is remove pattern than suppress link refs #10834 - if (isset($r['remove'])) { - if (0 != preg_match( - '/' . addcslashes($r['remove'], '/') . '/i', - trim($link['url']) - )) { - unset($link); - return; - } + // is remove pattern than suppress link refs #10834 + if (isset($r['remove'])) { + if (0 != preg_match( + '/' . addcslashes($r['remove'], '/') . '/i', + trim($link['url']) + )) { + unset($link); + return; + } + } + // is pattern set and matches so try rewrite url + if (isset($r['pattern']) && 0 != preg_match('/' . $r['pattern'] . '/i', trim($link['url']))) { + // if function is set and available then perform on url + if (isset($r['function']) && is_callable($r['function'])) { + $link['url'] = $r['function']($link['url']); } // is search and replace set so try to rewrite url if (isset($r['search']) && isset($r['replace'])) { - // check if pattern exists. if at least one match than continue - if (0 != preg_match( - '/' . $r['pattern'] . '/i', - trim($link['url']) - )) { - // prepare search pattern - // should be free of conflicting meta characters - $pattern - = str_replace(['.'], ['\.'], $r['search']); - $pattern = '/(' . $pattern . ')/i'; - // replace it only one time - $link['url'] = preg_replace( - $pattern, - trim($r['replace']), - trim($link['url']), - 1, - $count - ); - // add http if needed - // @to-do make it https compatible - if (!preg_match('/^(https?:\/\/)/', $link['url'])) { - $link['url'] = 'http://' . $link['url']; - } + // prepare search pattern + // should be free of conflicting meta characters + $pattern = str_replace(['.'], ['\.'], $r['search']); + $pattern = '/(' . $pattern . ')/i'; + // replace it only one time + $link['url'] = preg_replace( + $pattern, + trim($r['replace']), + trim($link['url']), + 1, + $count + ); + // add http if needed + // @to-do make it https compatible + if (!preg_match('/^(https?:\/\/)/', $link['url'])) { + $link['url'] = 'http://' . $link['url']; } } // is method set so call alternatively method proceed link if (isset($r['method']) && method_exists($this, $r['method'])) { - /* && $count > 0) { @todo fix */ - if (0 != preg_match( - '/' . $r['pattern'] . '/i', - trim($link['url']) - )) { - $link['url'] = $this->$r['method']($link['url']); - } + $link['url'] = $this->$r['method']($link['url']); } // end if isset method } // end if isset pattern } // end foreach