diff --git a/config/vufind/searches.ini b/config/vufind/searches.ini index eb2f672a90d4bf194ca44a3a624c9514e0048ae5..fe51f11395df68043fcd152ececeefac681324b8 100644 --- a/config/vufind/searches.ini +++ b/config/vufind/searches.ini @@ -382,11 +382,14 @@ CallNumber = callnumber-sort ; ; AlphaBrowseLink:index ; Use the query to generate a link to the specified alphabrowse index -; DOI:[prefix] +; DOI:[prefix]:[redirect_exact_match] ; Detect DOIs in the user search query. If a DOI makes up part of the search, ; display a link to resolve it. If it makes up the entire query, redirect to ; the resolver automatically. [prefix] is the URL of the resolver to which -; the DOI itself will be appended. +; the DOI itself will be appended. [redirect_exact_match] is a boolean; set it +; to true if you want a search query that consists only of a DOI to redirect +; automatically to the DOI resolver, or set it to false to only display a +; message with a link. (Default is "true"). ; ExternalSearch:[link label]:[url template] ; Display a link to an external search system. The contents of the <a> tag ; will be [link label] (which will be run through the translator -- use a diff --git a/module/VuFind/src/VuFind/Recommend/DOI.php b/module/VuFind/src/VuFind/Recommend/DOI.php index 19a1725d65e97165b8d41b52efb5e3dd374ff1c9..0a6cd6251dbaf4703d69168bb70bb5f6ac8b0847 100644 --- a/module/VuFind/src/VuFind/Recommend/DOI.php +++ b/module/VuFind/src/VuFind/Recommend/DOI.php @@ -56,6 +56,13 @@ class DOI implements RecommendInterface */ protected $prefix; + /** + * Are we configured to redirect to the resolver when a full match is found? + * + * @var bool + */ + protected $redirectFullMatch = true; + /** * Does the DOI in $match exactly match the user's query? * @@ -72,7 +79,17 @@ class DOI implements RecommendInterface */ public function setConfig($settings) { - $this->prefix = $settings; + // Find the last colon in the configuration that is not part of a URL: + $breakPoint = strrpos($settings, ':'); + if ($breakPoint && substr($settings, $breakPoint + 1, 2) !== '//') { + $prefix = substr($settings, 0, $breakPoint); + $redirect = substr($settings, $breakPoint + 1); + } else { + $prefix = $settings; + $redirect = true; // no redirect setting; use default + } + $this->prefix = $prefix; + $this->redirectFullMatch = ($redirect && strtolower($redirect) !== 'false'); } /** @@ -138,4 +155,14 @@ class DOI implements RecommendInterface { return $this->exact; } + + /** + * Are we configured to redirect to the resolver when a full match is found? + * + * @return bool + */ + public function redirectFullMatch() + { + return $this->redirectFullMatch; + } } diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Recommend/DOITest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Recommend/DOITest.php index 64bb080f80700410eaab976282020be49e6c4298..07ee35202d3d91bc85c7f60a9e044cc84027a796 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/Recommend/DOITest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Recommend/DOITest.php @@ -51,6 +51,7 @@ class DOITest extends \VuFindTest\Unit\TestCase $this->assertNull($doi->getDOI()); $this->assertNull($doi->getURL()); $this->assertFalse($doi->isFullMatch()); + $this->assertTrue($doi->redirectFullMatch()); } /** @@ -64,6 +65,7 @@ class DOITest extends \VuFindTest\Unit\TestCase $this->assertNull($doi->getDOI()); $this->assertNull($doi->getURL()); $this->assertFalse($doi->isFullMatch()); + $this->assertTrue($doi->redirectFullMatch()); } /** @@ -77,6 +79,7 @@ class DOITest extends \VuFindTest\Unit\TestCase $this->assertEquals('10.1109/CC.2018.8485472', $doi->getDOI()); $this->assertEquals('http://doi/10.1109%2FCC.2018.8485472', $doi->getURL()); $this->assertTrue($doi->isFullMatch()); + $this->assertTrue($doi->redirectFullMatch()); } /** @@ -90,6 +93,34 @@ class DOITest extends \VuFindTest\Unit\TestCase $this->assertEquals('10.1109/CC.2018.8485472', $doi->getDOI()); $this->assertEquals('http://doi/10.1109%2FCC.2018.8485472', $doi->getURL()); $this->assertFalse($doi->isFullMatch()); + $this->assertTrue($doi->redirectFullMatch()); + } + + /** + * Test configuration of the redirect setting: + * + * @return void + */ + public function testDoiRedirectConfigs() + { + $testData = [ + 'true' => true, + 'false' => false, + '' => false, + '1' => true, + '0' => false, + ]; + $url = 'https://doi/'; + foreach ($testData as $config => $expected) { + $doi = $this->getDOI( + $this->getMockResults('Yes 10.1109/CC.2018.8485472'), + $url . ':' . $config + ); + $this->assertEquals('10.1109/CC.2018.8485472', $doi->getDOI()); + $this->assertEquals($url . '10.1109%2FCC.2018.8485472', $doi->getURL()); + $this->assertFalse($doi->isFullMatch()); + $this->assertEquals($expected, $doi->redirectFullMatch()); + } } /** diff --git a/themes/bootstrap3/templates/Recommend/DOI.phtml b/themes/bootstrap3/templates/Recommend/DOI.phtml index 81bf1fae901eb2f6d30a15fbdc8f116d02052511..22397c900b08c194ea8b4888b1127f5c74a3836c 100644 --- a/themes/bootstrap3/templates/Recommend/DOI.phtml +++ b/themes/bootstrap3/templates/Recommend/DOI.phtml @@ -3,7 +3,7 @@ <div class="alert alert-info"> <?=$this->translate('doi_detected_html', ['%%url%%' => $url, '%%doi%%' => $doi])?> </div> - <?php if ($this->recommend->isFullMatch()): ?> + <?php if ($this->recommend->isFullMatch() && $this->recommend->redirectFullMatch()): ?> <?php $redirect = 'document.location.href = "' . $this->escapeJs($url) . '";'; ?> <?=$this->inlineScript(\Laminas\View\Helper\HeadScript::SCRIPT, $redirect, 'SET')?> <?php endif; ?>