Skip to content
Snippets Groups Projects
Commit 2e3568e2 authored by Demian Katz's avatar Demian Katz Committed by Robert Lange
Browse files

Add option to filter BrowZine DOI handler results (#1625)

parent 1d205c9a
No related merge requests found
......@@ -15,6 +15,18 @@ timeout = 30
; should be one of the options present in the [Views] section below.
default_view = list
; This section controls the behavior of the BrowZine DOI handler; see also
; the [DOI] section of config.ini to activate the handler.
[DOI]
; Can be set to "include" to include only the options listed in the "filter"
; setting, or to "exclude" to filter out the options listed in the "filter"
; setting. The default is "none," which performs no filtering.
;filterType = exclude
; This repeatable section can be used to filter based on link type; legal
; options are "browzineWebLink" and "fullTextFile"
;filter[] = "browzineWebLink"
;filter[] = "fullTextFile"
; This section defines the view options available on standard search results.
; If only one view is required, set default_view under [General] above, and
; leave this section commented out.
......
......@@ -50,14 +50,47 @@ class BrowZine implements DoiLinkerInterface, TranslatorAwareInterface
*/
protected $connector;
/**
* Configuration options
*
* @var array
*/
protected $config;
/**
* Constructor
*
* @param Connector $connector Connector
* @param array $config Configuration settings
*/
public function __construct(Connector $connector)
public function __construct(Connector $connector, array $config = [])
{
$this->connector = $connector;
$this->config = $config;
}
/**
* Check if an array key is available in the data and allowed by filter settings.
*
* @param string $key Key to check
* @param array $data Available data
*
* @return bool
*/
protected function arrayKeyAvailable(string $key, array $data): bool
{
if (empty($data[$key])) {
return false;
}
switch (strtolower(trim($this->config['filterType'] ?? 'none'))) {
case 'include':
return in_array($key, (array)($this->config['filter'] ?? []));
case 'exclude':
return !in_array($key, (array)($this->config['filter'] ?? []));
default:
}
// If we got this far, no filter setting is applied, so the option is legal:
return true;
}
/**
......@@ -76,7 +109,7 @@ class BrowZine implements DoiLinkerInterface, TranslatorAwareInterface
$response = [];
foreach ($doiArray as $doi) {
$data = $this->connector->lookupDoi($doi)['data'] ?? null;
if (!empty($data['browzineWebLink'])) {
if ($this->arrayKeyAvailable('browzineWebLink', $data)) {
$response[$doi][] = [
'link' => $data['browzineWebLink'],
'label' => $this->translate('View Complete Issue'),
......@@ -84,7 +117,7 @@ class BrowZine implements DoiLinkerInterface, TranslatorAwareInterface
'data' => $data,
];
}
if (!empty($data['fullTextFile'])) {
if ($this->arrayKeyAvailable('fullTextFile', $data)) {
$response[$doi][] = [
'link' => $data['fullTextFile'],
'label' => $this->translate('PDF Full Text'),
......
......@@ -64,6 +64,9 @@ class BrowZineFactory implements \Laminas\ServiceManager\Factory\FactoryInterfac
}
$backend = $container->get(\VuFind\Search\BackendManager::class)
->get('BrowZine');
return new $requestedName($backend->getConnector());
$fullConfig = $container->get(\VuFind\Config\PluginManager::class)
->get('BrowZine');
$config = isset($fullConfig->DOI) ? $fullConfig->DOI->toArray() : [];
return new $requestedName($backend->getConnector(), $config);
}
}
{"data":{"id":377796791,"type":"articles","title":"Comparison of Functional Movement Screen, Star Excursion Balance Test, and Physical Fitness in Junior Athletes with Different Sports Injury Risk","date":"2020-03-26","authors":"Chang, Wen-Dien; Chou, Li-Wei; Chang, Nai-Jen; Chen, Shuya","inPress":false,"doi":"10.1155/2020/8690540","ILLURL":"https://ill","pmid":"32309441","fullTextFile":"https://fulltext","contentLocation":"https://content","availableThroughBrowzine":true,"startPage":"1","endPage":"8","browzineWebLink":"https://weblink"}}
\ No newline at end of file
<?php
/**
* BrowZine Test Class
*
* PHP version 7
*
* Copyright (C) Villanova University 2020.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Tests
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki
*/
namespace VuFindTest\DoiLinker;
use VuFind\DoiLinker\BrowZine;
use VuFindSearch\Backend\BrowZine\Connector;
/**
* BrowZine Test Class
*
* @category VuFind
* @package Tests
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki
*/
class BrowZineTest extends \VuFindTest\Unit\TestCase
{
/**
* Get a mock connector
*
* @param string $doi DOI expected by connector
* @param array $response Response for connector to return
*
* @return void
*/
protected function getMockConnector($doi, $response)
{
$connector = $this->getMockBuilder(Connector::class)
->disableOriginalConstructor()
->getMock();
$connector->expects($this->once())
->method('lookupDoi')
->with($this->equalTo($doi))
->will($this->returnValue($response));
return $connector;
}
/**
* Test an API response.
*
* @return void
*/
public function testApiSuccess()
{
$fixture = realpath(
__DIR__
. '/../../../../../tests/fixtures/browzine/doi.json'
);
$rawData = json_decode(file_get_contents($fixture), true);
$testData = [
[
'config' => [],
'response' => [
'10.1155/2020/8690540' => [
[
'link' => 'https://weblink',
'label' => 'View Complete Issue',
'icon' => 'https://assets.thirdiron.com/images/integrations/browzine-open-book-icon.svg',
'data' => $rawData['data'],
],
[
'link' => 'https://fulltext',
'label' => 'PDF Full Text',
'icon' => 'https://assets.thirdiron.com/images/integrations/browzine-pdf-download-icon.svg',
'data' => $rawData['data'],
]
]
]
],
[
'config' => ['filterType' => 'exclude', 'filter' => ['browzineWebLink']],
'response' => [
'10.1155/2020/8690540' => [
[
'link' => 'https://fulltext',
'label' => 'PDF Full Text',
'icon' => 'https://assets.thirdiron.com/images/integrations/browzine-pdf-download-icon.svg',
'data' => $rawData['data'],
]
]
]
],
[
'config' => ['filterType' => 'include', 'filter' => ['browzineWebLink']],
'response' => [
'10.1155/2020/8690540' => [
[
'link' => 'https://weblink',
'label' => 'View Complete Issue',
'icon' => 'https://assets.thirdiron.com/images/integrations/browzine-open-book-icon.svg',
'data' => $rawData['data'],
]
]
]
],
];
foreach ($testData as $data) {
$dois = array_keys($data['response']);
$connector = $this->getMockConnector($dois[0], $rawData);
$browzine = new BrowZine($connector, $data['config']);
$this->assertEquals(
$data['response'],
$browzine->getLinks($dois)
);
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment