Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.67% |
39 / 45 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
GetResolverLinks | |
86.67% |
39 / 45 |
|
50.00% |
1 / 2 |
12.34 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
handleRequest | |
85.37% |
35 / 41 |
|
0.00% |
0 / 1 |
11.38 |
1 | <?php |
2 | |
3 | /** |
4 | * "Get Resolver Links" AJAX handler |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2018. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package AJAX |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @author Graham Seaman <Graham.Seaman@rhul.ac.uk> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link https://vufind.org/wiki/development Wiki |
29 | */ |
30 | |
31 | namespace VuFind\AjaxHandler; |
32 | |
33 | use Laminas\Config\Config; |
34 | use Laminas\Mvc\Controller\Plugin\Params; |
35 | use Laminas\View\Renderer\RendererInterface; |
36 | use VuFind\I18n\Translator\TranslatorAwareInterface; |
37 | use VuFind\Resolver\Connection; |
38 | use VuFind\Resolver\Driver\PluginManager as ResolverManager; |
39 | use VuFind\Session\Settings as SessionSettings; |
40 | |
41 | /** |
42 | * "Get Resolver Links" AJAX handler |
43 | * |
44 | * Fetch Links from resolver given an OpenURL and format as HTML |
45 | * and output the HTML content in JSON object. |
46 | * |
47 | * @category VuFind |
48 | * @package AJAX |
49 | * @author Demian Katz <demian.katz@villanova.edu> |
50 | * @author Graham Seaman <Graham.Seaman@rhul.ac.uk> |
51 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
52 | * @link https://vufind.org/wiki/development Wiki |
53 | */ |
54 | class GetResolverLinks extends AbstractBase implements TranslatorAwareInterface |
55 | { |
56 | use \VuFind\I18n\Translator\TranslatorAwareTrait; |
57 | |
58 | /** |
59 | * Resolver driver plugin manager |
60 | * |
61 | * @var ResolverManager |
62 | */ |
63 | protected $pluginManager; |
64 | |
65 | /** |
66 | * View renderer |
67 | * |
68 | * @var RendererInterface |
69 | */ |
70 | protected $renderer; |
71 | |
72 | /** |
73 | * Top-level VuFind configuration (config.ini) |
74 | * |
75 | * @var Config |
76 | */ |
77 | protected $config; |
78 | |
79 | /** |
80 | * Constructor |
81 | * |
82 | * @param SessionSettings $ss Session settings |
83 | * @param ResolverManager $pm Resolver driver plugin manager |
84 | * @param RendererInterface $renderer View renderer |
85 | * @param Config $config Top-level VuFind configuration (config.ini) |
86 | */ |
87 | public function __construct( |
88 | SessionSettings $ss, |
89 | ResolverManager $pm, |
90 | RendererInterface $renderer, |
91 | Config $config |
92 | ) { |
93 | $this->sessionSettings = $ss; |
94 | $this->pluginManager = $pm; |
95 | $this->renderer = $renderer; |
96 | $this->config = $config; |
97 | } |
98 | |
99 | /** |
100 | * Handle a request. |
101 | * |
102 | * @param Params $params Parameter helper from controller |
103 | * |
104 | * @return array [response data, HTTP status code] |
105 | */ |
106 | public function handleRequest(Params $params) |
107 | { |
108 | $this->disableSessionWrites(); // avoid session write timing bug |
109 | $openUrl = $params->fromQuery('openurl', ''); |
110 | $searchClassId = $params->fromQuery('searchClassId', ''); |
111 | |
112 | $resolverType = $this->config->OpenURL->resolver ?? 'generic'; |
113 | if (!$this->pluginManager->has($resolverType)) { |
114 | return $this->formatResponse( |
115 | $this->translate("Could not load driver for $resolverType"), |
116 | self::STATUS_HTTP_ERROR |
117 | ); |
118 | } |
119 | $resolver = new Connection($this->pluginManager->get($resolverType)); |
120 | if (isset($this->config->OpenURL->resolver_cache)) { |
121 | $resolver->enableCache($this->config->OpenURL->resolver_cache); |
122 | } |
123 | $result = $resolver->fetchLinks($openUrl); |
124 | |
125 | // Sort the returned links into categories based on service type: |
126 | $electronic = $print = $services = []; |
127 | foreach ($result as $link) { |
128 | $serviceType = $link['service_type'] ?? ''; |
129 | // Special case -- modify DOI text for special display, then apply |
130 | // default $electronic behavior below: |
131 | if ($serviceType === 'getDOI') { |
132 | $link['title'] = $this->translate('Get full text'); |
133 | $link['coverage'] = ''; |
134 | } |
135 | switch ($serviceType) { |
136 | case 'getHolding': |
137 | $print[] = $link; |
138 | break; |
139 | case 'getWebService': |
140 | $services[] = $link; |
141 | break; |
142 | case 'getFullTxt': |
143 | default: |
144 | $electronic[] = $link; |
145 | break; |
146 | } |
147 | } |
148 | |
149 | // Get the OpenURL base: |
150 | if (isset($this->config->OpenURL->url)) { |
151 | // Trim off any parameters (for legacy compatibility -- default config |
152 | // used to include extraneous parameters): |
153 | [$base] = explode('?', $this->config->OpenURL->url); |
154 | } else { |
155 | $base = false; |
156 | } |
157 | |
158 | $moreOptionsLink = $resolver->supportsMoreOptionsLink() |
159 | ? $resolver->getResolverUrl($openUrl) : ''; |
160 | |
161 | // Render the links using the view: |
162 | $view = [ |
163 | 'openUrlBase' => $base, 'openUrl' => $openUrl, 'print' => $print, |
164 | 'electronic' => $electronic, 'services' => $services, |
165 | 'searchClassId' => $searchClassId, |
166 | 'moreOptionsLink' => $moreOptionsLink, |
167 | ]; |
168 | $html = $this->renderer->render('ajax/resolverLinks.phtml', $view); |
169 | |
170 | // output HTML encoded in JSON object |
171 | return $this->formatResponse(compact('html')); |
172 | } |
173 | } |