|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet; |
|
4
|
|
|
|
|
5
|
|
|
/*************************************************************** |
|
6
|
|
|
* Copyright notice |
|
7
|
|
|
* |
|
8
|
|
|
* (c) 2015-2016 Timo Schmidt <[email protected]> |
|
9
|
|
|
* All rights reserved |
|
10
|
|
|
* |
|
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
12
|
|
|
* free software; you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU General Public License as published by |
|
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
15
|
|
|
* (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* The GNU General Public License can be found at |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
19
|
|
|
* |
|
20
|
|
|
* This script is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
26
|
|
|
***************************************************************/ |
|
27
|
|
|
|
|
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\QueryFields; |
|
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query; |
|
30
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder; |
|
31
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser\ResultParserRegistry; |
|
32
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResultCollection; |
|
33
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest; |
|
34
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestAware; |
|
35
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Variants\VariantsProcessor; |
|
36
|
|
|
use ApacheSolrForTypo3\Solr\Query\Modifier\Modifier; |
|
37
|
|
|
use ApacheSolrForTypo3\Solr\Search; |
|
38
|
|
|
use ApacheSolrForTypo3\Solr\Search\QueryAware; |
|
39
|
|
|
use ApacheSolrForTypo3\Solr\Search\SearchAware; |
|
40
|
|
|
use ApacheSolrForTypo3\Solr\Search\SearchComponentManager; |
|
41
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
|
42
|
|
|
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
|
43
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrIncompleteResponseException; |
|
44
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
45
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResultBuilder; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The SearchResultSetService is responsible to build a SearchResultSet from a SearchRequest. |
|
49
|
|
|
* It encapsulates the logic to trigger a search in order to be able to reuse it in multiple places. |
|
50
|
|
|
* |
|
51
|
|
|
* @author Timo Schmidt <[email protected]> |
|
52
|
|
|
*/ |
|
53
|
|
|
class SearchResultSetService |
|
54
|
|
|
{ |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Track, if the number of results per page has been changed by the current request |
|
58
|
|
|
* |
|
59
|
|
|
* @var bool |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $resultsPerPageChanged = false; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var Search |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $search; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var SearchResultSet |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $lastResultSet = null; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var boolean |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $isSolrAvailable = false; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var TypoScriptConfiguration |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $typoScriptConfiguration; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var SolrLogManager; |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $logger = null; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var SearchResultBuilder |
|
90
|
|
|
*/ |
|
91
|
|
|
protected $searchResultBuilder; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @var QueryBuilder |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $queryBuilder; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param TypoScriptConfiguration $configuration |
|
100
|
|
|
* @param Search $search |
|
101
|
|
|
* @param SolrLogManager $solrLogManager |
|
102
|
|
|
* @param SearchResultBuilder $resultBuilder |
|
103
|
|
|
* @param QueryBuilder $queryBuilder |
|
104
|
|
|
*/ |
|
105
|
54 |
|
public function __construct(TypoScriptConfiguration $configuration, Search $search, SolrLogManager $solrLogManager = null, SearchResultBuilder $resultBuilder = null, QueryBuilder $queryBuilder = null) |
|
106
|
|
|
{ |
|
107
|
54 |
|
$this->search = $search; |
|
108
|
54 |
|
$this->typoScriptConfiguration = $configuration; |
|
109
|
54 |
|
$this->logger = $solrLogManager ?? GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__); |
|
|
|
|
|
|
110
|
54 |
|
$this->searchResultBuilder = $resultBuilder ?? GeneralUtility::makeInstance(SearchResultBuilder::class); |
|
111
|
54 |
|
$this->queryBuilder = $queryBuilder ?? GeneralUtility::makeInstance(QueryBuilder::class, $configuration, $solrLogManager); |
|
|
|
|
|
|
112
|
54 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param bool $useCache |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getIsSolrAvailable($useCache = true) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->isSolrAvailable = $this->search->ping($useCache); |
|
121
|
|
|
return $this->isSolrAvailable; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
35 |
|
public function getHasSearched() |
|
128
|
|
|
{ |
|
129
|
35 |
|
return $this->search->hasSearched(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Retrieves the used search instance. |
|
134
|
|
|
* |
|
135
|
|
|
* @return Search |
|
136
|
|
|
*/ |
|
137
|
2 |
|
public function getSearch() |
|
138
|
|
|
{ |
|
139
|
2 |
|
return $this->search; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param Query $query |
|
144
|
|
|
* @param SearchRequest $searchRequest |
|
145
|
|
|
*/ |
|
146
|
43 |
|
protected function initializeRegisteredSearchComponents(Query $query, SearchRequest $searchRequest) |
|
147
|
|
|
{ |
|
148
|
43 |
|
$searchComponents = $this->getRegisteredSearchComponents(); |
|
149
|
|
|
|
|
150
|
43 |
|
foreach ($searchComponents as $searchComponent) { |
|
151
|
|
|
/** @var Search\SearchComponent $searchComponent */ |
|
152
|
38 |
|
$searchComponent->setSearchConfiguration($this->typoScriptConfiguration->getSearchConfiguration()); |
|
153
|
|
|
|
|
154
|
38 |
|
if ($searchComponent instanceof QueryAware) { |
|
155
|
38 |
|
$searchComponent->setQuery($query); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
38 |
|
if ($searchComponent instanceof SearchRequestAware) { |
|
159
|
37 |
|
$searchComponent->setSearchRequest($searchRequest); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
38 |
|
$searchComponent->initializeSearchComponent(); |
|
163
|
|
|
} |
|
164
|
43 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
47 |
|
protected function getResultSetClassName() |
|
170
|
|
|
{ |
|
171
|
47 |
|
return isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultSetClassName ']) ? |
|
172
|
47 |
|
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultSetClassName '] : SearchResultSet::class; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Performs a search and returns a SearchResultSet. |
|
177
|
|
|
* |
|
178
|
|
|
* @param SearchRequest $searchRequest |
|
179
|
|
|
* @return SearchResultSet |
|
180
|
|
|
*/ |
|
181
|
47 |
|
public function search(SearchRequest $searchRequest) |
|
182
|
|
|
{ |
|
183
|
47 |
|
$resultSet = $this->getInitializedSearchResultSet($searchRequest); |
|
184
|
47 |
|
$this->lastResultSet = $resultSet; |
|
185
|
|
|
|
|
186
|
47 |
|
$resultSet = $this->handleSearchHook('beforeSearch', $resultSet); |
|
187
|
47 |
|
if ($this->shouldReturnEmptyResultSetWithoutExecutedSearch($searchRequest)) { |
|
188
|
4 |
|
$resultSet->setHasSearched(false); |
|
189
|
4 |
|
return $resultSet; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
43 |
|
$query = $this->queryBuilder->buildSearchQuery($searchRequest->getRawUserQuery(), (int)$searchRequest->getResultsPerPage()); |
|
193
|
43 |
|
$this->initializeRegisteredSearchComponents($query, $searchRequest); |
|
194
|
43 |
|
$resultSet->setUsedQuery($query); |
|
195
|
|
|
|
|
196
|
|
|
// performing the actual search, sending the query to the Solr server |
|
197
|
43 |
|
$query = $this->modifyQuery($query, $searchRequest, $this->search); |
|
198
|
43 |
|
$response = $this->doASearch($query, $searchRequest); |
|
199
|
|
|
|
|
200
|
42 |
|
if ((int)$searchRequest->getResultsPerPage() === 0) { |
|
201
|
|
|
// when resultPerPage was forced to 0 we also set the numFound to 0 to hide results, e.g. |
|
202
|
|
|
// when results for the initial search should not be shown. |
|
203
|
2 |
|
$response->response->numFound = 0; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
42 |
|
$resultSet->setHasSearched(true); |
|
207
|
42 |
|
$resultSet->setResponse($response); |
|
208
|
42 |
|
$resultSet->setSearchResults($this->getParsedSearchResults($resultSet)); |
|
209
|
42 |
|
$resultSet->setUsedAdditionalFilters($this->queryBuilder->getAdditionalFilters()); |
|
210
|
|
|
|
|
211
|
|
|
/** @var $variantsProcessor VariantsProcessor */ |
|
212
|
42 |
|
$variantsProcessor = GeneralUtility::makeInstance(VariantsProcessor::class, $this->typoScriptConfiguration, $this->searchResultBuilder); |
|
|
|
|
|
|
213
|
42 |
|
$variantsProcessor->process($resultSet); |
|
214
|
|
|
|
|
215
|
|
|
/** @var $searchResultReconstitutionProcessor ResultSetReconstitutionProcessor */ |
|
216
|
42 |
|
$searchResultReconstitutionProcessor = GeneralUtility::makeInstance(ResultSetReconstitutionProcessor::class); |
|
217
|
42 |
|
$searchResultReconstitutionProcessor->process($resultSet); |
|
218
|
|
|
|
|
219
|
42 |
|
$resultSet = $this->getAutoCorrection($resultSet); |
|
220
|
|
|
|
|
221
|
42 |
|
return $this->handleSearchHook('afterSearch', $resultSet); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Uses the configured parser and retrieves the parsed search resutls. |
|
226
|
|
|
* |
|
227
|
|
|
* @param SearchResultSet $resultSet |
|
228
|
|
|
* @return Result\SearchResultCollection |
|
229
|
|
|
*/ |
|
230
|
42 |
|
protected function getParsedSearchResults($resultSet): SearchResultCollection |
|
231
|
|
|
{ |
|
232
|
|
|
/** @var ResultParserRegistry $parserRegistry */ |
|
233
|
42 |
|
$parserRegistry = GeneralUtility::makeInstance(ResultParserRegistry::class, $this->typoScriptConfiguration); |
|
|
|
|
|
|
234
|
42 |
|
$useRawDocuments = (bool)$this->typoScriptConfiguration->getValueByPathOrDefaultValue('plugin.tx_solr.features.useRawDocuments', false); |
|
235
|
42 |
|
$searchResults = $parserRegistry->getParser($resultSet)->parse($resultSet, $useRawDocuments); |
|
236
|
42 |
|
return $searchResults; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Evaluates conditions on the request and configuration and returns true if no search should be triggered and an empty |
|
241
|
|
|
* SearchResultSet should be returned. |
|
242
|
|
|
* |
|
243
|
|
|
* @param SearchRequest $searchRequest |
|
244
|
|
|
* @return bool |
|
245
|
|
|
*/ |
|
246
|
47 |
|
protected function shouldReturnEmptyResultSetWithoutExecutedSearch(SearchRequest $searchRequest) |
|
247
|
|
|
{ |
|
248
|
47 |
|
if ($searchRequest->getRawUserQueryIsNull() && !$this->getInitialSearchIsConfigured()) { |
|
249
|
|
|
// when no rawQuery was passed or no initialSearch is configured, we pass an empty result set |
|
250
|
3 |
|
return true; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
44 |
|
if ($searchRequest->getRawUserQueryIsEmptyString() && !$this->typoScriptConfiguration->getSearchQueryAllowEmptyQuery()) { |
|
254
|
|
|
// the user entered an empty query string "" or " " and empty querystring is not allowed |
|
255
|
1 |
|
return true; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
43 |
|
return false; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Initializes the SearchResultSet from the SearchRequest |
|
263
|
|
|
* |
|
264
|
|
|
* @param SearchRequest $searchRequest |
|
265
|
|
|
* @return SearchResultSet |
|
266
|
|
|
*/ |
|
267
|
47 |
|
protected function getInitializedSearchResultSet(SearchRequest $searchRequest):SearchResultSet |
|
268
|
|
|
{ |
|
269
|
|
|
/** @var $resultSet SearchResultSet */ |
|
270
|
47 |
|
$resultSetClass = $this->getResultSetClassName(); |
|
271
|
47 |
|
$resultSet = GeneralUtility::makeInstance($resultSetClass); |
|
272
|
|
|
|
|
273
|
47 |
|
$resultSet->setUsedSearchRequest($searchRequest); |
|
274
|
47 |
|
$resultSet->setUsedPage((int)$searchRequest->getPage()); |
|
275
|
47 |
|
$resultSet->setUsedResultsPerPage((int)$searchRequest->getResultsPerPage()); |
|
276
|
47 |
|
$resultSet->setUsedSearch($this->search); |
|
277
|
47 |
|
return $resultSet; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Retrieves the configuration filters from the TypoScript configuration, except the __pageSections filter. |
|
282
|
|
|
* |
|
283
|
|
|
* @return array |
|
284
|
|
|
*/ |
|
285
|
37 |
|
public function getAdditionalFilters() |
|
286
|
|
|
{ |
|
287
|
37 |
|
return $this->queryBuilder->getAdditionalFilters(); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Executes the search and builds a fake response for a current bug in Apache Solr 6.3 |
|
292
|
|
|
* |
|
293
|
|
|
* @param Query $query |
|
294
|
|
|
* @param SearchRequest $searchRequest |
|
295
|
|
|
* @return \Apache_Solr_Response |
|
296
|
|
|
*/ |
|
297
|
43 |
|
protected function doASearch($query, $searchRequest) |
|
298
|
|
|
{ |
|
299
|
|
|
// the offset mulitplier is page - 1 but not less then zero |
|
300
|
43 |
|
$offsetMultiplier = max(0, $searchRequest->getPage() - 1); |
|
301
|
43 |
|
$offSet = $offsetMultiplier * (int)$searchRequest->getResultsPerPage(); |
|
302
|
|
|
|
|
303
|
43 |
|
$response = $this->search->search($query, $offSet, null); |
|
304
|
42 |
|
if($response === null) { |
|
305
|
|
|
throw new SolrIncompleteResponseException('The response retrieved from solr was incomplete', 1505989678); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
42 |
|
return $response; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @param SearchResultSet $searchResultSet |
|
313
|
|
|
* @return SearchResultSet |
|
314
|
|
|
*/ |
|
315
|
42 |
|
protected function getAutoCorrection(SearchResultSet $searchResultSet) |
|
316
|
|
|
{ |
|
317
|
|
|
// no secondary search configured |
|
318
|
42 |
|
if (!$this->typoScriptConfiguration->getSearchSpellcheckingSearchUsingSpellCheckerSuggestion()) { |
|
319
|
41 |
|
return $searchResultSet; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
// more then zero results |
|
323
|
1 |
|
if ($searchResultSet->getAllResultCount() > 0) { |
|
324
|
1 |
|
return $searchResultSet; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
// no corrections present |
|
328
|
1 |
|
if (!$searchResultSet->getHasSpellCheckingSuggestions()) { |
|
329
|
|
|
return $searchResultSet; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
1 |
|
$searchResultSet = $this->peformAutoCorrection($searchResultSet); |
|
333
|
|
|
|
|
334
|
1 |
|
return $searchResultSet; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @param SearchResultSet $searchResultSet |
|
339
|
|
|
* @return SearchResultSet |
|
340
|
|
|
*/ |
|
341
|
1 |
|
protected function peformAutoCorrection(SearchResultSet $searchResultSet) |
|
342
|
|
|
{ |
|
343
|
1 |
|
$searchRequest = $searchResultSet->getUsedSearchRequest(); |
|
344
|
1 |
|
$suggestions = $searchResultSet->getSpellCheckingSuggestions(); |
|
345
|
|
|
|
|
346
|
1 |
|
$maximumRuns = $this->typoScriptConfiguration->getSearchSpellcheckingNumberOfSuggestionsToTry(1); |
|
347
|
1 |
|
$runs = 0; |
|
348
|
|
|
|
|
349
|
1 |
|
foreach ($suggestions as $suggestion) { |
|
350
|
1 |
|
$runs++; |
|
351
|
|
|
|
|
352
|
1 |
|
$correction = $suggestion->getSuggestion(); |
|
353
|
1 |
|
$initialQuery = $searchRequest->getRawUserQuery(); |
|
354
|
|
|
|
|
355
|
1 |
|
$searchRequest->setRawQueryString($correction); |
|
356
|
1 |
|
$searchResultSet = $this->search($searchRequest); |
|
357
|
1 |
|
if ($searchResultSet->getAllResultCount() > 0) { |
|
358
|
1 |
|
$searchResultSet->setIsAutoCorrected(true); |
|
359
|
1 |
|
$searchResultSet->setCorrectedQueryString($correction); |
|
360
|
1 |
|
$searchResultSet->setInitialQueryString($initialQuery); |
|
361
|
1 |
|
break; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
if ($runs > $maximumRuns) { |
|
365
|
|
|
break; |
|
366
|
|
|
} |
|
367
|
|
|
} |
|
368
|
1 |
|
return $searchResultSet; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* Allows to modify a query before eventually handing it over to Solr. |
|
373
|
|
|
* |
|
374
|
|
|
* @param Query $query The current query before it's being handed over to Solr. |
|
375
|
|
|
* @param SearchRequest $searchRequest The searchRequest, relevant in the current context |
|
376
|
|
|
* @param Search $search The search, relevant in the current context |
|
377
|
|
|
* @throws \UnexpectedValueException |
|
378
|
|
|
* @return Query The modified query that is actually going to be given to Solr. |
|
379
|
|
|
*/ |
|
380
|
43 |
|
protected function modifyQuery(Query $query, SearchRequest $searchRequest, Search $search) |
|
381
|
|
|
{ |
|
382
|
|
|
// hook to modify the search query |
|
383
|
43 |
|
if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifySearchQuery'])) { |
|
384
|
37 |
|
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifySearchQuery'] as $classReference) { |
|
385
|
37 |
|
$queryModifier = GeneralUtility::makeInstance($classReference); |
|
386
|
|
|
|
|
387
|
37 |
|
if ($queryModifier instanceof Modifier) { |
|
388
|
37 |
|
if ($queryModifier instanceof SearchAware) { |
|
389
|
|
|
$queryModifier->setSearch($search); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
37 |
|
if ($queryModifier instanceof SearchRequestAware) { |
|
393
|
33 |
|
$queryModifier->setSearchRequest($searchRequest); |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
37 |
|
$query = $queryModifier->modifyQuery($query); |
|
397
|
|
|
} else { |
|
398
|
|
|
throw new \UnexpectedValueException( |
|
399
|
|
|
get_class($queryModifier) . ' must implement interface ' . Modifier::class, |
|
400
|
37 |
|
1310387414 |
|
401
|
|
|
); |
|
402
|
|
|
} |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
43 |
|
return $query; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* Retrieves a single document from solr by document id. |
|
411
|
|
|
* |
|
412
|
|
|
* @param string $documentId |
|
413
|
|
|
* @return SearchResult |
|
414
|
|
|
*/ |
|
415
|
3 |
|
public function getDocumentById($documentId) |
|
416
|
|
|
{ |
|
417
|
|
|
/* @var $query Query */ |
|
418
|
3 |
|
$query = GeneralUtility::makeInstance(Query::class, $documentId); |
|
|
|
|
|
|
419
|
3 |
|
$query->setQueryFields(QueryFields::fromString('id')); |
|
420
|
3 |
|
$response = $this->search->search($query, 0, 1); |
|
421
|
2 |
|
$parsedData = $response->getParsedData(); |
|
422
|
2 |
|
$resultDocument = isset($parsedData->response->docs[0]) ? $parsedData->response->docs[0] : null; |
|
423
|
|
|
|
|
424
|
2 |
|
return $this->searchResultBuilder->fromApacheSolrDocument($resultDocument); |
|
|
|
|
|
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
/** |
|
428
|
|
|
* This method is used to call the registered hooks during the search execution. |
|
429
|
|
|
* |
|
430
|
|
|
* @param string $eventName |
|
431
|
|
|
* @param SearchResultSet $resultSet |
|
432
|
|
|
* @return SearchResultSet |
|
433
|
|
|
*/ |
|
434
|
47 |
|
private function handleSearchHook($eventName, SearchResultSet $resultSet) |
|
435
|
|
|
{ |
|
436
|
47 |
|
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr'][$eventName])) { |
|
437
|
47 |
|
return $resultSet; |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
32 |
|
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr'][$eventName] as $classReference) { |
|
441
|
32 |
|
$afterSearchProcessor = GeneralUtility::makeInstance($classReference); |
|
442
|
32 |
|
if ($afterSearchProcessor instanceof SearchResultSetProcessor) { |
|
443
|
32 |
|
$afterSearchProcessor->process($resultSet); |
|
444
|
|
|
} |
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
32 |
|
return $resultSet; |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
/** |
|
451
|
|
|
* @return SearchResultSet |
|
452
|
|
|
*/ |
|
453
|
33 |
|
public function getLastResultSet() |
|
454
|
|
|
{ |
|
455
|
33 |
|
return $this->lastResultSet; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* This method returns true when the last search was executed with an empty query |
|
460
|
|
|
* string or whitespaces only. When no search was triggered it will return false. |
|
461
|
|
|
* |
|
462
|
|
|
* @return bool |
|
463
|
|
|
*/ |
|
464
|
|
|
public function getLastSearchWasExecutedWithEmptyQueryString() |
|
465
|
|
|
{ |
|
466
|
|
|
$wasEmptyQueryString = false; |
|
467
|
|
|
if ($this->lastResultSet != null) { |
|
468
|
|
|
$wasEmptyQueryString = $this->lastResultSet->getUsedSearchRequest()->getRawUserQueryIsEmptyString(); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
return $wasEmptyQueryString; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
/** |
|
475
|
|
|
* @return bool |
|
476
|
|
|
*/ |
|
477
|
7 |
|
protected function getInitialSearchIsConfigured() |
|
478
|
|
|
{ |
|
479
|
7 |
|
return $this->typoScriptConfiguration->getSearchInitializeWithEmptyQuery() || $this->typoScriptConfiguration->getSearchShowResultsOfInitialEmptyQuery() || $this->typoScriptConfiguration->getSearchInitializeWithQuery() || $this->typoScriptConfiguration->getSearchShowResultsOfInitialQuery(); |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
/** |
|
483
|
|
|
* @return mixed |
|
484
|
|
|
*/ |
|
485
|
37 |
|
protected function getRegisteredSearchComponents() |
|
486
|
|
|
{ |
|
487
|
37 |
|
return GeneralUtility::makeInstance(SearchComponentManager::class)->getSearchComponents(); |
|
488
|
|
|
} |
|
489
|
|
|
} |
|
490
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: