Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
27.91% |
12 / 43 |
|
38.46% |
5 / 13 |
CRAP | |
0.00% |
0 / 1 |
Memory | |
27.91% |
12 / 43 |
|
38.46% |
5 / 13 |
259.19 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
disable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
forgetSearch | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
rememberLastSettings | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
rememberParams | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
rememberSearch | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
retrieveLastSetting | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
retrieveSearch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCurrentSearchId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getCurrentSearch | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getLastSearchId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getLastSearch | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getSearchById | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * VuFind Search Memory |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
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 Search |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Page |
28 | */ |
29 | |
30 | namespace VuFind\Search; |
31 | |
32 | use Laminas\Http\Request; |
33 | use Laminas\Session\Container; |
34 | use VuFind\Db\Service\SearchServiceInterface; |
35 | use VuFind\Search\Results\PluginManager as ResultsManager; |
36 | |
37 | use function array_key_exists; |
38 | use function intval; |
39 | use function strlen; |
40 | |
41 | /** |
42 | * Wrapper class to handle search memory |
43 | * |
44 | * @category VuFind |
45 | * @package Search |
46 | * @author Demian Katz <demian.katz@villanova.edu> |
47 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
48 | * @link https://vufind.org Main Page |
49 | */ |
50 | class Memory |
51 | { |
52 | /** |
53 | * Is memory currently active? (i.e. will we save new URLs?) |
54 | * |
55 | * @var bool |
56 | */ |
57 | protected $active = true; |
58 | |
59 | /** |
60 | * Cached searches |
61 | * |
62 | * @var array |
63 | */ |
64 | protected $searchCache = []; |
65 | |
66 | /** |
67 | * Constructor |
68 | * |
69 | * @param Container $session Session container for storing URLs |
70 | * @param string $sessionId Current session ID |
71 | * @param Request $request Request |
72 | * @param SearchServiceInterface $searchService Search service |
73 | * @param ResultsManager $resultsManager Results plugin manager |
74 | */ |
75 | public function __construct( |
76 | protected Container $session, |
77 | protected string $sessionId, |
78 | protected Request $request, |
79 | protected SearchServiceInterface $searchService, |
80 | protected ResultsManager $resultsManager |
81 | ) { |
82 | } |
83 | |
84 | /** |
85 | * Stop updating the URL in memory -- used in combined search to prevent |
86 | * multiple search URLs from overwriting one another. |
87 | * |
88 | * @return void |
89 | */ |
90 | public function disable() |
91 | { |
92 | $this->active = false; |
93 | } |
94 | |
95 | /** |
96 | * Clear the last accessed search URL in the session. |
97 | * |
98 | * @return void |
99 | */ |
100 | public function forgetSearch() |
101 | { |
102 | unset($this->session->last); |
103 | unset($this->session->lastId); |
104 | } |
105 | |
106 | /** |
107 | * Remember a user's last search parameters. |
108 | * |
109 | * @param string $context Context of search (usually search class ID). |
110 | * @param array $params Associative array of keys/values to store. |
111 | * |
112 | * @return void |
113 | */ |
114 | public function rememberLastSettings($context, $params) |
115 | { |
116 | if (!$this->active) { |
117 | return; |
118 | } |
119 | foreach ($params as $setting => $value) { |
120 | $this->session->{"params|$context|$setting"} = $value; |
121 | } |
122 | } |
123 | |
124 | /** |
125 | * Wrapper around rememberLastSettings() to extract key values from a |
126 | * search Params object. |
127 | * |
128 | * @param \VuFind\Search\Base\Params $params Parameter object |
129 | * |
130 | * @return void |
131 | */ |
132 | public function rememberParams(\VuFind\Search\Base\Params $params) |
133 | { |
134 | // Since default sort may vary based on search handler, we don't want |
135 | // to force the sort value to stick unless the user chose a non-default |
136 | // option. Otherwise, if you switch between search types, unpredictable |
137 | // sort options may result. |
138 | $sort = $params->getSort(); |
139 | $defaultSort = $params->getDefaultSort(); |
140 | $settings = [ |
141 | 'hiddenFilters' => $params->getHiddenFilters(), |
142 | 'limit' => $params->getLimit(), |
143 | 'sort' => $sort === $defaultSort ? null : $sort, |
144 | 'view' => $params->getView(), |
145 | ]; |
146 | // Special case: RSS view should not be persisted: |
147 | if (strtolower($settings['view']) == 'rss') { |
148 | unset($settings['view']); |
149 | } |
150 | $this->rememberLastSettings($params->getSearchClassId(), $settings); |
151 | } |
152 | |
153 | /** |
154 | * Store the last accessed search URL in the session for future reference. |
155 | * |
156 | * @param string $url URL to remember |
157 | * @param int $id Search ID to remember |
158 | * |
159 | * @return void |
160 | */ |
161 | public function rememberSearch($url, $id = null) |
162 | { |
163 | // Do nothing if disabled. |
164 | if (!$this->active) { |
165 | return; |
166 | } |
167 | |
168 | // Only remember URL if string is non-empty... otherwise clear the memory. |
169 | if (strlen(trim($url)) > 0) { |
170 | $this->session->last = $url; |
171 | if ($id) { |
172 | $this->session->lastId = $id; |
173 | } |
174 | } else { |
175 | $this->forgetSearch(); |
176 | } |
177 | } |
178 | |
179 | /** |
180 | * Retrieve a previous user parameter, if available. Return $default if |
181 | * not found. |
182 | * |
183 | * @param string $context Context of search (usually search class ID). |
184 | * @param string $setting Name of setting to retrieve. |
185 | * @param mixed $default Default value if setting is absent. |
186 | * |
187 | * @return mixed |
188 | */ |
189 | public function retrieveLastSetting($context, $setting, $default = null) |
190 | { |
191 | return $this->session->{"params|$context|$setting"} ?? $default; |
192 | } |
193 | |
194 | /** |
195 | * Retrieve last accessed search URL, if available. Returns null if no URL |
196 | * is available. |
197 | * |
198 | * @return string|null |
199 | */ |
200 | public function retrieveSearch() |
201 | { |
202 | return $this->session->last ?? null; |
203 | } |
204 | |
205 | /** |
206 | * Get current search id |
207 | * |
208 | * @return ?int |
209 | */ |
210 | public function getCurrentSearchId(): ?int |
211 | { |
212 | $sid = $this->request->getQuery('sid') |
213 | ?? $this->request->getPost('sid'); |
214 | return intval($sid) ?: null; |
215 | } |
216 | |
217 | /** |
218 | * Get current search |
219 | * |
220 | * @return ?\VuFind\Search\Base\Results |
221 | */ |
222 | public function getCurrentSearch(): ?\VuFind\Search\Base\Results |
223 | { |
224 | if (!($id = $this->getCurrentSearchId())) { |
225 | return null; |
226 | } |
227 | return $this->getSearchById($id); |
228 | } |
229 | |
230 | /** |
231 | * Get latest search id from current request or session |
232 | * |
233 | * @return ?int |
234 | */ |
235 | public function getLastSearchId(): ?int |
236 | { |
237 | $id = $this->getCurrentSearchId() ?? $this->session->lastId; |
238 | return $id ? (int)$id : null; |
239 | } |
240 | |
241 | /** |
242 | * Get latest search from current request or session |
243 | * |
244 | * @return ?\VuFind\Search\Base\Results |
245 | */ |
246 | public function getLastSearch(): ?\VuFind\Search\Base\Results |
247 | { |
248 | if (!($id = $this->getLastSearchId())) { |
249 | return null; |
250 | } |
251 | return $this->getSearchById($id); |
252 | } |
253 | |
254 | /** |
255 | * Get a search by id |
256 | * |
257 | * @param int $id Search ID |
258 | * |
259 | * @return ?\VuFind\Search\Base\Results |
260 | */ |
261 | protected function getSearchById(int $id): ?\VuFind\Search\Base\Results |
262 | { |
263 | if (!array_key_exists($id, $this->searchCache)) { |
264 | $search = $this->searchService->getSearchByIdAndOwner($id, $this->sessionId, null); |
265 | $this->searchCache[$id] = $search?->getSearchObject()?->deminify($this->resultsManager); |
266 | } |
267 | return $this->searchCache[$id]; |
268 | } |
269 | } |