1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Plugin\Results; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2009-2015 Ingo Renner <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\CommandResolver; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest; |
30
|
|
|
use ApacheSolrForTypo3\Solr\Plugin\CommandPluginBase; |
31
|
|
|
use ApacheSolrForTypo3\Solr\Plugin\PluginCommand; |
32
|
|
|
use ApacheSolrForTypo3\Solr\Query; |
33
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
34
|
|
|
use ApacheSolrForTypo3\Solr\Template; |
35
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Plugin 'Solr Search' for the 'solr' extension. |
39
|
|
|
* |
40
|
|
|
* @author Ingo Renner <[email protected]> |
41
|
|
|
* @author Timo Schmidt <[email protected]> |
42
|
|
|
*/ |
43
|
|
|
class Results extends CommandPluginBase |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* Path to this script relative to the extension dir. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
public $scriptRelPath = 'Classes/Plugin/Results/Results.php'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var SearchResultSet |
54
|
|
|
*/ |
55
|
|
|
protected $searchResultSet; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Perform the action for the plugin. In this case it calls the search() |
59
|
|
|
* method which internally performs the search. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
23 |
|
protected function performAction() |
64
|
|
|
{ |
65
|
23 |
|
if ($this->getSearchResultSetService()->getIsSolrAvailable()) { |
66
|
23 |
|
$searchRequest = $this->buildSearchRequest(); |
67
|
23 |
|
$this->searchResultSet = $this->getSearchResultSetService()->search($searchRequest); |
68
|
23 |
|
} |
69
|
23 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return SearchRequest |
73
|
|
|
*/ |
74
|
23 |
|
private function buildSearchRequest() |
75
|
|
|
{ |
76
|
23 |
|
$solrParameters = []; |
77
|
23 |
|
$solrPostParameters = GeneralUtility::_POST('tx_solr'); |
78
|
23 |
|
$solrGetParameters = GeneralUtility::_GET('tx_solr'); |
79
|
|
|
|
80
|
|
|
// check for GET parameters, POST takes precedence |
81
|
23 |
|
if (isset($solrGetParameters) && is_array($solrGetParameters)) { |
82
|
1 |
|
$solrParameters = $solrGetParameters; |
83
|
1 |
|
} |
84
|
23 |
|
if (isset($solrPostParameters) && is_array($solrPostParameters)) { |
85
|
|
|
$solrParameters = $solrPostParameters; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @var $searchRequest \ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest */ |
89
|
23 |
|
$searchRequest = GeneralUtility::makeInstance( |
90
|
23 |
|
\ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest::class, |
91
|
23 |
|
array('tx_solr' => $solrParameters), |
92
|
23 |
|
$GLOBALS['TSFE']->id, |
93
|
23 |
|
$GLOBALS['TSFE']->sys_language_uid, |
94
|
23 |
|
$this->typoScriptConfiguration |
95
|
23 |
|
); |
96
|
23 |
|
$searchRequest->mergeArguments(array('tx_solr' => $this->piVars)); |
97
|
23 |
|
$searchRequest->mergeArguments(array('q' => $this->getRawUserQuery())); |
98
|
|
|
|
99
|
23 |
|
return $searchRequest; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the number of results per page. |
104
|
|
|
* |
105
|
|
|
* @deprecated use $this->searchResultSet->getResultsPerPage() instead , will be removed in version 5.0 |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function getNumberOfResultsPerPage() |
109
|
|
|
{ |
110
|
|
|
GeneralUtility::logDeprecatedFunction(); |
111
|
|
|
return $this->searchResultSet->getResultsPerPage(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Implementation of preRender() method. Used to include CSS files. |
116
|
|
|
* |
117
|
|
|
*/ |
118
|
23 |
|
protected function preRender() |
119
|
|
|
{ |
120
|
23 |
|
$resultsCss = $this->typoScriptConfiguration->getCssFileByFileKey('results'); |
121
|
23 |
|
if ($resultsCss !== '') { |
122
|
23 |
|
$cssFile = GeneralUtility::createVersionNumberedFilename($GLOBALS['TSFE']->tmpl->getFileName($resultsCss)); |
123
|
23 |
|
$GLOBALS['TSFE']->additionalHeaderData['tx_solr-resultsCss'] = '<link href="' . $cssFile . '" rel="stylesheet" type="text/css" />'; |
124
|
23 |
|
} |
125
|
23 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns an initialized CommandResolver. |
129
|
|
|
* |
130
|
|
|
*/ |
131
|
23 |
|
protected function getCommandResolver() |
132
|
|
|
{ |
133
|
23 |
|
return GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\CommandResolver'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Retrieves the list of commands to process for the results view. |
138
|
|
|
* |
139
|
|
|
* @return array An array of command names to process for the result view |
140
|
|
|
*/ |
141
|
23 |
|
protected function getCommandList() |
142
|
|
|
{ |
143
|
23 |
|
$requirements = PluginCommand::REQUIREMENT_NONE; |
144
|
|
|
|
145
|
23 |
|
if ($this->getSearchResultSetService()->getHasSearched()) { |
146
|
20 |
|
$requirements = PluginCommand::REQUIREMENT_HAS_SEARCHED; |
147
|
|
|
|
148
|
20 |
|
if ($this->searchResultSet->getUsedSearch()->getNumberOfResults() > 0) { |
149
|
17 |
|
$requirements += PluginCommand::REQUIREMENT_HAS_RESULTS; |
150
|
17 |
|
} else { |
151
|
3 |
|
$requirements += PluginCommand::REQUIREMENT_NO_RESULTS; |
152
|
|
|
} |
153
|
20 |
|
} |
154
|
|
|
|
155
|
23 |
|
return CommandResolver::getPluginCommands('results', $requirements); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Performs post initialization. |
160
|
|
|
* |
161
|
|
|
*/ |
162
|
23 |
|
protected function postInitialize() |
163
|
|
|
{ |
164
|
|
|
// disable caching |
165
|
23 |
|
$this->pi_USER_INT_obj = 1; |
|
|
|
|
166
|
23 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Overrides certain TypoScript configuration options with their values |
170
|
|
|
* from FlexForms. |
171
|
|
|
* |
172
|
|
|
*/ |
173
|
23 |
|
protected function overrideTyposcriptWithFlexformSettings() |
174
|
|
|
{ |
175
|
23 |
|
$flexFormConfiguration = []; |
176
|
|
|
|
177
|
|
|
// initialize with empty query, useful when no search has been |
178
|
|
|
// conducted yet but needs to show facets already. |
179
|
23 |
|
$initializeWithEmptyQuery = $this->getFlexFormValue('initializeWithEmptyQuery', 'sQuery'); |
180
|
|
|
|
181
|
23 |
|
if (boolval($initializeWithEmptyQuery)) { |
182
|
|
|
$flexFormConfiguration['search.']['initializeWithEmptyQuery'] = 1; |
183
|
|
|
} |
184
|
|
|
|
185
|
23 |
|
$showResultsOfInitialEmptyQuery = $this->getFlexFormValue('showResultsOfInitialEmptyQuery', 'sQuery'); |
186
|
23 |
|
if (boolval($showResultsOfInitialEmptyQuery)) { |
187
|
|
|
$flexFormConfiguration['search.']['showResultsOfInitialEmptyQuery'] = 1; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// initialize with non-empty query |
191
|
23 |
|
$initialQuery = trim((string)$this->getFlexFormValue('initializeWithQuery', 'sQuery')); |
192
|
23 |
|
if ($initialQuery !== '') { |
193
|
|
|
$flexFormConfiguration['search.']['initializeWithQuery'] = $initialQuery; |
194
|
|
|
} |
195
|
|
|
|
196
|
23 |
|
$showResultsOfInitialQuery = $this->getFlexFormValue('showResultsOfInitialQuery', 'sQuery'); |
197
|
23 |
|
if (boolval($showResultsOfInitialQuery)) { |
198
|
|
|
$flexFormConfiguration['search.']['showResultsOfInitialQuery'] = 1; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// target page |
202
|
23 |
|
$flexformTargetPage = $this->getFlexFormValue('targetPage'); |
203
|
23 |
|
if (!empty($flexformTargetPage)) { |
204
|
|
|
$flexFormConfiguration['search.']['targetPage'] = (int)$flexformTargetPage; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// boost function |
208
|
23 |
|
$boostFunction = trim((string)$this->getFlexFormValue('boostFunction', 'sQuery')); |
209
|
23 |
|
if ($boostFunction !== '') { |
210
|
|
|
$flexFormConfiguration['search.']['query.']['boostFunction'] = $boostFunction; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// boost query |
214
|
23 |
|
$boostQuery = trim((string)$this->getFlexFormValue('boostQuery', 'sQuery')); |
215
|
23 |
|
if ($boostQuery !== '') { |
216
|
|
|
$flexFormConfiguration['search.']['query.']['boostQuery'] = $boostQuery; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// sorting |
220
|
23 |
|
$flexformSorting = trim((string)$this->getFlexFormValue('sortBy', 'sQuery')); |
221
|
23 |
|
if ($flexformSorting !== '') { |
222
|
|
|
$flexFormConfiguration['search.']['query.']['sortBy'] = $flexformSorting; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// results per page |
226
|
23 |
|
$resultsPerPage = trim((string)$this->getFlexFormValue('resultsPerPage', 'sQuery')); |
227
|
23 |
|
if ($resultsPerPage !== '') { |
228
|
|
|
$flexFormConfiguration['search.']['results.']['resultsPerPage'] = intval($resultsPerPage); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// flexform overwrites _all_ filters set through TypoScript |
232
|
23 |
|
$flexformFilters = $this->getFlexFormValue('filter', 'sQuery'); |
233
|
23 |
|
if (!empty($flexformFilters)) { |
234
|
|
|
$additionalFilters = GeneralUtility::trimExplode('|', $flexformFilters); |
235
|
|
|
|
236
|
|
|
// we keep the pageSections filter but replace all other filters |
237
|
|
|
$filterConfiguration = $this->typoScriptConfiguration->getSearchQueryFilterConfiguration(); |
238
|
|
|
if (isset($filterConfiguration['__pageSections'])) { |
239
|
|
|
$additionalFilters['__pageSections'] = $filterConfiguration['__pageSections']; |
240
|
|
|
} |
241
|
|
|
$this->typoScriptConfiguration->setSearchQueryFilterConfiguration($additionalFilters); |
242
|
|
|
} |
243
|
|
|
|
244
|
23 |
|
$this->typoScriptConfiguration->mergeSolrConfiguration($flexFormConfiguration); |
245
|
23 |
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Post initialization of the template engine, adding some Solr variables. |
249
|
|
|
* |
250
|
|
|
* @param Template $template The template object as initialized thus far. |
251
|
|
|
* @return Template The modified template instance with additional variables available for rendering. |
252
|
|
|
*/ |
253
|
23 |
|
protected function postInitializeTemplateEngine(Template $template) |
254
|
|
|
{ |
255
|
23 |
|
$template->addVariable('tx_solr', $this->getSolrVariables()); |
256
|
|
|
|
257
|
23 |
|
return $template; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Gets a list of EXT:solr variables like the prefix ID. |
262
|
|
|
* |
263
|
|
|
* @return array array of EXT:solr variables |
264
|
|
|
*/ |
265
|
23 |
|
protected function getSolrVariables() |
266
|
|
|
{ |
267
|
23 |
|
$currentUrl = $this->getCurrentUrlWithQueryLinkBuilder(); |
268
|
|
|
|
269
|
23 |
|
return array('prefix' => $this->prefixId, 'query_parameter' => 'q', 'current_url' => $currentUrl, 'q' => $this->getCleanUserQuery()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Gets the plugin's configuration. |
274
|
|
|
* |
275
|
|
|
* @return TypoScriptConfiguration Configuration |
276
|
|
|
*/ |
277
|
15 |
|
public function getConfiguration() |
278
|
|
|
{ |
279
|
15 |
|
return $this->typoScriptConfiguration; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Returns the key which is used to determine the template file from the typoscript setup. |
284
|
|
|
* |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
23 |
|
protected function getTemplateFileKey() |
288
|
|
|
{ |
289
|
23 |
|
return 'results'; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Returns the plugin key, used in various base methods. |
294
|
|
|
* |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
23 |
|
protected function getPluginKey() |
298
|
|
|
{ |
299
|
23 |
|
return 'PiResults'; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns the main subpart to work on. |
304
|
|
|
* |
305
|
|
|
*/ |
306
|
23 |
|
protected function getSubpart() |
307
|
|
|
{ |
308
|
23 |
|
return 'solr_search'; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.