Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
DOI
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
7 / 7
12
100.00% covered (success)
100.00%
1 / 1
 setConfig
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 init
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getDOI
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getURL
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 isFullMatch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 redirectFullMatch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * DOI Recommendations Module
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2016.
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  Recommendations
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @author   Chris Hallberg <challber@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development:plugins:recommendation_modules Wiki
29 */
30
31namespace VuFind\Recommend;
32
33/**
34 * DOI Recommendations Module
35 *
36 * This class directs the user to a DOI resolver when appropriate.
37 *
38 * @category VuFind
39 * @package  Recommendations
40 * @author   Demian Katz <demian.katz@villanova.edu>
41 * @author   Chris Hallberg <challber@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development:plugins:recommendation_modules Wiki
44 */
45class DOI implements RecommendInterface
46{
47    /**
48     * DOI found in search query (or null for none)
49     *
50     * @var string
51     */
52    protected $match = null;
53
54    /**
55     * URL prefix for resolving DOIs
56     *
57     * @var string
58     */
59    protected $prefix;
60
61    /**
62     * Are we configured to redirect to the resolver when a full match is found?
63     *
64     * @var bool
65     */
66    protected $redirectFullMatch = true;
67
68    /**
69     * Does the DOI in $match exactly match the user's query?
70     *
71     * @var bool
72     */
73    protected $exact = false;
74
75    /**
76     * Store the configuration of the recommendation module.
77     *
78     * @param string $settings Settings from searches.ini.
79     *
80     * @return void
81     */
82    public function setConfig($settings)
83    {
84        // Find the last colon in the configuration that is not part of a URL:
85        $breakPoint = strrpos($settings, ':');
86        if ($breakPoint && substr($settings, $breakPoint + 1, 2) !== '//') {
87            $prefix = substr($settings, 0, $breakPoint);
88            $redirect = substr($settings, $breakPoint + 1);
89        } else {
90            $prefix = $settings;
91            $redirect = true;       // no redirect setting; use default
92        }
93        $this->prefix = $prefix;
94        $this->redirectFullMatch = ($redirect && strtolower($redirect) !== 'false');
95    }
96
97    /**
98     * Called before the Search Results object performs its main search
99     * (specifically, in response to \VuFind\Search\SearchRunner::EVENT_CONFIGURED).
100     * This method is responsible for setting search parameters needed by the
101     * recommendation module and for reading any existing search parameters that may
102     * be needed.
103     *
104     * @param \VuFind\Search\Base\Params $params  Search parameter object
105     * @param \Laminas\Stdlib\Parameters $request Parameter object representing user
106     * request.
107     *
108     * @return void
109     */
110    public function init($params, $request)
111    {
112    }
113
114    /**
115     * Called after the Search Results object has performed its main search. This
116     * may be used to extract necessary information from the Search Results object
117     * or to perform completely unrelated processing.
118     *
119     * @param \VuFind\Search\Base\Results $results Search results object
120     *
121     * @return void
122     */
123    public function process($results)
124    {
125        $query = $results->getParams()->getDisplayQuery();
126        preg_match('|10\.[^\s/]{4,}/[^\s]{1,}|', $query, $matches);
127        $this->match = $matches[0] ?? null;
128        $this->exact = (null !== $this->match) && ($this->match === $query);
129    }
130
131    /**
132     * Get the matched DOI (or null if no match found)
133     *
134     * @return string
135     */
136    public function getDOI()
137    {
138        return $this->match;
139    }
140
141    /**
142     * Get the URL to resolve the matched DOI (or null if no match found)
143     *
144     * @return string
145     */
146    public function getURL()
147    {
148        return null === $this->match
149            ? null : $this->prefix . urlencode($this->match);
150    }
151
152    /**
153     * Is the DOI returned by getDOI a match to the user's full search query?
154     *
155     * @return bool
156     */
157    public function isFullMatch()
158    {
159        return $this->exact;
160    }
161
162    /**
163     * Are we configured to redirect to the resolver when a full match is found?
164     *
165     * @return bool
166     */
167    public function redirectFullMatch()
168    {
169        return $this->redirectFullMatch;
170    }
171}