Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
FacetCache | |
0.00% |
0 / 27 |
|
0.00% |
0 / 6 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getCacheNamespace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
0 | |||
getCacheKey | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getFacetResults | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
getList | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getResults | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Abstract Base FacetCache. |
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 Search_Base |
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/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\Search\Base; |
31 | |
32 | use VuFind\Cache\Manager as CacheManager; |
33 | |
34 | use function in_array; |
35 | |
36 | /** |
37 | * Solr FacetCache Factory. |
38 | * |
39 | * @category VuFind |
40 | * @package Search_Base |
41 | * @author Demian Katz <demian.katz@villanova.edu> |
42 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
43 | * @link https://vufind.org/wiki/development Wiki |
44 | */ |
45 | abstract class FacetCache |
46 | { |
47 | use \VuFind\Log\VarDumperTrait; |
48 | |
49 | /** |
50 | * Cache manager |
51 | * |
52 | * @var CacheManager |
53 | */ |
54 | protected $cacheManager; |
55 | |
56 | /** |
57 | * Currently selected language |
58 | * |
59 | * @var string |
60 | */ |
61 | protected $language; |
62 | |
63 | /** |
64 | * Search results object. |
65 | * |
66 | * @var Results |
67 | */ |
68 | protected $results; |
69 | |
70 | /** |
71 | * Constructor |
72 | * |
73 | * @param Results $r Search results object |
74 | * @param CacheManager $cm Cache manager |
75 | * @param string $language Active UI language |
76 | */ |
77 | public function __construct(Results $r, CacheManager $cm, $language = 'en') |
78 | { |
79 | $this->results = $r; |
80 | $this->cacheManager = $cm; |
81 | $this->language = $language; |
82 | } |
83 | |
84 | /** |
85 | * Get the namespace to use for caching facets. |
86 | * |
87 | * @return string |
88 | */ |
89 | abstract protected function getCacheNamespace(); |
90 | |
91 | /** |
92 | * Get the cache key for the provided method. |
93 | * |
94 | * @return string |
95 | */ |
96 | protected function getCacheKey() |
97 | { |
98 | $params = $this->results->getParams(); |
99 | $facetConfig = $params->getFacetConfig(); |
100 | $settings = [ |
101 | $facetConfig, |
102 | $params->getHiddenFilters(), |
103 | // Factor operator settings into cache key: |
104 | array_map([$params, 'getFacetOperator'], array_keys($facetConfig)), |
105 | ]; |
106 | return $this->language . md5($this->varDump($settings)); |
107 | } |
108 | |
109 | /** |
110 | * Perform the actual facet lookup. |
111 | * |
112 | * @param string $initMethod Name of params method to use to request facets |
113 | * |
114 | * @return array |
115 | */ |
116 | protected function getFacetResults($initMethod) |
117 | { |
118 | // Check if we have facet results cached, and build them if we don't. |
119 | $cache = $this->cacheManager->getCache('object', $this->getCacheNamespace()); |
120 | $params = $this->results->getParams(); |
121 | |
122 | // Note that we need to initialize the parameters BEFORE generating the |
123 | // cache key to ensure that the key is based on the proper settings. |
124 | $params->$initMethod(); |
125 | $cacheKey = $this->getCacheKey(); |
126 | if (!($list = $cache->getItem($cacheKey))) { |
127 | // Avoid a backend request if there are no facets configured by the given |
128 | // init method. |
129 | if (!empty($params->getFacetConfig())) { |
130 | // We only care about facet lists, so don't get any results (this |
131 | // improves performance): |
132 | $params->setLimit(0); |
133 | $list = $this->results->getFacetList(); |
134 | } else { |
135 | $list = []; |
136 | } |
137 | $cache->setItem($cacheKey, $list); |
138 | } |
139 | |
140 | return $list; |
141 | } |
142 | |
143 | /** |
144 | * Return facet information. This data may come from the cache. |
145 | * |
146 | * @param string $context Context of list to retrieve ('Advanced' or 'HomePage') |
147 | * |
148 | * @return array |
149 | */ |
150 | public function getList($context = 'Advanced') |
151 | { |
152 | if (!in_array($context, ['Advanced', 'HomePage', 'NewItems'])) { |
153 | throw new \Exception('Invalid context: ' . $context); |
154 | } |
155 | // For now, all contexts are handled the same way. |
156 | return $this->getFacetResults('init' . $context . 'Facets'); |
157 | } |
158 | |
159 | /** |
160 | * Get results object used to retrieve facets. |
161 | * |
162 | * @return Results |
163 | */ |
164 | public function getResults() |
165 | { |
166 | return $this->results; |
167 | } |
168 | } |