1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 <[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 3 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\Domain\Search\Query\ParameterBuilder\BigramPhraseFields; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Elevation; |
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Faceting; |
30
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\FieldCollapsing; |
31
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Filters; |
32
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Grouping; |
33
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Highlighting; |
34
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\PhraseFields; |
35
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\QueryFields; |
36
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\ReturnFields; |
37
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Slops; |
38
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Sorting; |
39
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Sortings; |
40
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Spellchecking; |
41
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\TrigramPhraseFields; |
42
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Site\SiteHashService; |
43
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository; |
44
|
|
|
use ApacheSolrForTypo3\Solr\FieldProcessor\PageUidToHierarchy; |
45
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
46
|
|
|
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
47
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
48
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
49
|
|
|
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The concrete QueryBuilder contains all TYPO3 specific initialization logic of solr queries, for TYPO3. |
53
|
|
|
*/ |
54
|
|
|
class QueryBuilder extends AbstractQueryBuilder { |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Additional filters, which will be added to the query, as well as to |
58
|
|
|
* suggest queries. |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $additionalFilters = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var TypoScriptConfiguration |
66
|
|
|
*/ |
67
|
|
|
protected $typoScriptConfiguration = null; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var SolrLogManager; |
71
|
|
|
*/ |
72
|
|
|
protected $logger = null; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var SiteHashService |
76
|
|
|
*/ |
77
|
|
|
protected $siteHashService = null; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* QueryBuilder constructor. |
81
|
|
|
* @param TypoScriptConfiguration|null $configuration |
82
|
|
|
* @param SolrLogManager|null $solrLogManager |
83
|
|
|
* @param SiteHashService|null $siteHashService |
84
|
|
|
*/ |
85
|
|
|
public function __construct(TypoScriptConfiguration $configuration = null, SolrLogManager $solrLogManager = null, SiteHashService $siteHashService = null) |
86
|
|
|
{ |
87
|
|
|
$this->typoScriptConfiguration = $configuration ?? Util::getSolrConfiguration(); |
88
|
|
|
$this->logger = $solrLogManager ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__); |
89
|
|
|
$this->siteHashService = $siteHashService ?? GeneralUtility::makeInstance(SiteHashService::class); |
90
|
|
|
} |
91
|
171 |
|
|
92
|
|
|
/** |
93
|
171 |
|
* @param string $queryString |
94
|
171 |
|
* @return QueryBuilder |
95
|
171 |
|
*/ |
96
|
171 |
|
public function newSearchQuery($queryString): QueryBuilder |
97
|
|
|
{ |
98
|
|
|
$this->queryToBuild = $this->getSearchQueryInstance($queryString); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
59 |
|
/** |
103
|
|
|
* @param string $queryString |
104
|
59 |
|
* @return QueryBuilder |
105
|
59 |
|
*/ |
106
|
|
|
public function newSuggestQuery($queryString): QueryBuilder |
107
|
|
|
{ |
108
|
|
|
$this->queryToBuild = $this->getSuggestQueryInstance($queryString); |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
142 |
|
/** |
113
|
|
|
* Initializes the Query object and SearchComponents and returns |
114
|
142 |
|
* the initialized query object, when a search should be executed. |
115
|
142 |
|
* |
116
|
|
|
* @param string|null $rawQuery |
117
|
|
|
* @param int $resultsPerPage |
118
|
|
|
* @param array $additionalFiltersFromRequest |
119
|
|
|
* @return SearchQuery |
120
|
|
|
*/ |
121
|
|
|
public function buildSearchQuery($rawQuery, $resultsPerPage = 10, array $additionalFiltersFromRequest = []) : SearchQuery |
122
|
2 |
|
{ |
123
|
|
|
if ($this->typoScriptConfiguration->getLoggingQuerySearchWords()) { |
124
|
2 |
|
$this->logger->log(SolrLogManager::INFO, 'Received search query', [$rawQuery]); |
125
|
2 |
|
} |
126
|
|
|
|
127
|
|
|
/* @var $query SearchQuery */ |
128
|
|
|
return $this->newSearchQuery($rawQuery) |
|
|
|
|
129
|
|
|
->useResultsPerPage($resultsPerPage) |
130
|
|
|
->useReturnFieldsFromTypoScript() |
131
|
155 |
|
->useQueryFieldsFromTypoScript() |
132
|
|
|
->useInitialQueryFromTypoScript() |
133
|
155 |
|
->useFiltersFromTypoScript() |
134
|
|
|
->useFilterArray($additionalFiltersFromRequest) |
135
|
|
|
->useFacetingFromTypoScript() |
136
|
|
|
->useVariantsFromTypoScript() |
137
|
|
|
->useGroupingFromTypoScript() |
138
|
|
|
->useHighlightingFromTypoScript() |
139
|
|
|
->usePhraseFieldsFromTypoScript() |
140
|
|
|
->useBigramPhraseFieldsFromTypoScript() |
141
|
|
|
->useTrigramPhraseFieldsFromTypoScript() |
142
|
|
|
->useOmitHeader(false) |
143
|
|
|
->getQuery(); |
144
|
141 |
|
} |
145
|
|
|
|
146
|
141 |
|
/** |
147
|
|
|
* Builds a SuggestQuery with all applied filters. |
148
|
|
|
* |
149
|
|
|
* @param string $queryString |
150
|
|
|
* @param array $additionalFilters |
151
|
141 |
|
* @param integer $requestedPageId |
152
|
141 |
|
* @param string $groupList |
153
|
141 |
|
* @return SuggestQuery |
154
|
141 |
|
*/ |
155
|
141 |
|
public function buildSuggestQuery(string $queryString, array $additionalFilters, int $requestedPageId, string $groupList) : SuggestQuery |
156
|
141 |
|
{ |
157
|
141 |
|
$this->newSuggestQuery($queryString) |
158
|
141 |
|
->useFiltersFromTypoScript() |
159
|
141 |
|
->useSiteHashFromTypoScript($requestedPageId) |
160
|
141 |
|
->useUserAccessGroups(explode(',', $groupList)) |
161
|
141 |
|
->useOmitHeader(); |
162
|
141 |
|
|
163
|
141 |
|
|
164
|
141 |
|
if (!empty($additionalFilters)) { |
165
|
|
|
$this->useFilterArray($additionalFilters); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->queryToBuild; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns Query for Search which finds document for given page. |
173
|
|
|
* Note: The Connection is per language as recommended in ext-solr docs. |
174
|
|
|
* |
175
|
|
|
* @return Query |
176
|
2 |
|
*/ |
177
|
|
|
public function buildPageQuery($pageId) |
178
|
2 |
|
{ |
179
|
2 |
|
$siteRepository = GeneralUtility::makeInstance(SiteRepository::class); |
180
|
2 |
|
$site = $siteRepository->getSiteByPageId($pageId); |
181
|
2 |
|
|
182
|
|
|
return $this->newSearchQuery('') |
183
|
2 |
|
->useQueryString('*:*') |
184
|
|
|
->useFilter('(type:pages AND uid:' . $pageId . ') OR (*:* AND pid:' . $pageId . ' NOT type:pages)', 'type') |
185
|
2 |
|
->useFilter('siteHash:' . $site->getSiteHash(), 'siteHash') |
186
|
|
|
->useReturnFields(ReturnFields::fromString('*')) |
187
|
|
|
->useSortings(Sortings::fromString('type asc, title asc')) |
188
|
|
|
->useQueryType('standard') |
189
|
|
|
->getQuery(); |
190
|
|
|
} |
191
|
2 |
|
|
192
|
|
|
/** |
193
|
|
|
* Returns a query for single record |
194
|
|
|
* |
195
|
|
|
* @return Query |
196
|
|
|
*/ |
197
|
|
|
public function buildRecordQuery($type, $uid, $pageId): Query |
198
|
|
|
{ |
199
|
|
|
$siteRepository = GeneralUtility::makeInstance(SiteRepository::class); |
200
|
143 |
|
$site = $siteRepository->getSiteByPageId($pageId); |
201
|
|
|
|
202
|
143 |
|
return $this->newSearchQuery('') |
203
|
3 |
|
->useQueryString('*:*') |
204
|
|
|
->useFilter('type:' . $type . ' AND uid:' . $uid, 'type') |
205
|
|
|
->useFilter('siteHash:' . $site->getSiteHash(), 'siteHash') |
206
|
143 |
|
->useReturnFields(ReturnFields::fromString('*')) |
207
|
|
|
->useSortings(Sortings::fromString('type asc, title asc')) |
208
|
|
|
->useQueryType('standard') |
209
|
|
|
->getQuery(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return QueryBuilder |
214
|
|
|
*/ |
215
|
1 |
|
public function useSlopsFromTypoScript(): QueryBuilder |
216
|
|
|
{ |
217
|
1 |
|
return $this->useSlops(Slops::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
218
|
1 |
|
} |
219
|
|
|
|
220
|
1 |
|
/** |
221
|
1 |
|
* Uses the configured boost queries from typoscript |
222
|
1 |
|
* |
223
|
1 |
|
* @return QueryBuilder |
224
|
1 |
|
*/ |
225
|
1 |
|
public function useBoostQueriesFromTypoScript(): QueryBuilder |
226
|
1 |
|
{ |
227
|
1 |
|
$searchConfiguration = $this->typoScriptConfiguration->getSearchConfiguration(); |
228
|
1 |
|
|
229
|
|
|
if (!empty($searchConfiguration['query.']['boostQuery'])) { |
230
|
|
|
return $this->useBoostQueries($searchConfiguration['query.']['boostQuery']); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if (!empty($searchConfiguration['query.']['boostQuery.'])) { |
234
|
|
|
$boostQueries = $searchConfiguration['query.']['boostQuery.']; |
235
|
|
|
return $this->useBoostQueries(array_values($boostQueries)); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Uses the configured boostFunction from the typoscript configuration. |
243
|
|
|
* |
244
|
|
|
* @return QueryBuilder |
245
|
|
|
*/ |
246
|
|
|
public function useBoostFunctionFromTypoScript(): QueryBuilder |
247
|
|
|
{ |
248
|
|
|
$searchConfiguration = $this->typoScriptConfiguration->getSearchConfiguration(); |
249
|
|
|
if (!empty($searchConfiguration['query.']['boostFunction'])) { |
250
|
|
|
return $this->useBoostFunction($searchConfiguration['query.']['boostFunction']); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Uses the configured minimumMatch from the typoscript configuration. |
258
|
1 |
|
* |
259
|
|
|
* @return QueryBuilder |
260
|
1 |
|
*/ |
261
|
1 |
|
public function useMinimumMatchFromTypoScript(): QueryBuilder |
262
|
|
|
{ |
263
|
|
|
$searchConfiguration = $this->typoScriptConfiguration->getSearchConfiguration(); |
264
|
|
|
if (!empty($searchConfiguration['query.']['minimumMatch'])) { |
265
|
|
|
return $this->useMinimumMatch($searchConfiguration['query.']['minimumMatch']); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
1 |
|
|
271
|
|
|
/** |
272
|
1 |
|
* @return QueryBuilder |
273
|
1 |
|
*/ |
274
|
|
|
public function useTieParameterFromTypoScript(): QueryBuilder |
275
|
|
|
{ |
276
|
|
|
$searchConfiguration = $this->typoScriptConfiguration->getSearchConfiguration(); |
277
|
|
|
if (empty($searchConfiguration['query.']['tieParameter'])) { |
278
|
|
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $this->useTieParameter($searchConfiguration['query.']['tieParameter']); |
282
|
1 |
|
} |
283
|
|
|
|
284
|
1 |
|
/** |
285
|
1 |
|
* Applies the configured query fields from the typoscript configuration. |
286
|
|
|
* |
287
|
|
|
* @return QueryBuilder |
288
|
|
|
*/ |
289
|
|
|
public function useQueryFieldsFromTypoScript(): QueryBuilder |
290
|
|
|
{ |
291
|
|
|
return $this->useQueryFields(QueryFields::fromString($this->typoScriptConfiguration->getSearchQueryQueryFields())); |
292
|
|
|
} |
293
|
|
|
|
294
|
1 |
|
/** |
295
|
|
|
* Applies the configured return fields from the typoscript configuration. |
296
|
1 |
|
* |
297
|
1 |
|
* @return QueryBuilder |
298
|
|
|
*/ |
299
|
|
|
public function useReturnFieldsFromTypoScript(): QueryBuilder |
300
|
|
|
{ |
301
|
|
|
$returnFieldsArray = (array)$this->typoScriptConfiguration->getSearchQueryReturnFieldsAsArray(['*', 'score']); |
302
|
|
|
return $this->useReturnFields(ReturnFields::fromArray($returnFieldsArray)); |
303
|
|
|
} |
304
|
141 |
|
|
305
|
|
|
|
306
|
141 |
|
|
307
|
141 |
|
/** |
308
|
|
|
* Can be used to apply the allowed sites from plugin.tx_solr.search.query.allowedSites to the query. |
309
|
|
|
* |
310
|
|
|
* @param int $requestedPageId |
311
|
|
|
* @return QueryBuilder |
312
|
|
|
*/ |
313
|
|
|
public function useSiteHashFromTypoScript(int $requestedPageId): QueryBuilder |
314
|
|
|
{ |
315
|
|
|
$queryConfiguration = $this->typoScriptConfiguration->getObjectByPathOrDefault('plugin.tx_solr.search.query.', []); |
316
|
|
|
$allowedSites = $this->siteHashService->getAllowedSitesForPageIdAndAllowedSitesConfiguration($requestedPageId, $queryConfiguration['allowedSites']); |
317
|
|
|
return $this->useSiteHashFromAllowedSites($allowedSites); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Can be used to apply a list of allowed sites to the query. |
322
|
|
|
* |
323
|
|
|
* @param string $allowedSites |
324
|
|
|
* @return QueryBuilder |
325
|
|
|
*/ |
326
|
|
|
public function useSiteHashFromAllowedSites($allowedSites): QueryBuilder |
327
|
|
|
{ |
328
|
|
|
$isAnySiteAllowed = trim($allowedSites) === '*'; |
329
|
|
|
if ($isAnySiteAllowed) { |
330
|
|
|
// no filter required |
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
50 |
|
|
334
|
|
|
$allowedSites = GeneralUtility::trimExplode(',', $allowedSites); |
335
|
50 |
|
$filters = []; |
336
|
|
|
foreach ($allowedSites as $site) { |
337
|
|
|
$siteHash = $this->siteHashService->getSiteHashForDomain($site); |
338
|
|
|
$filters[] = 'siteHash:"' . $siteHash . '"'; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
$siteHashFilterString = implode(' OR ', $filters); |
342
|
50 |
|
return $this->useFilter($siteHashFilterString, 'siteHash'); |
343
|
|
|
} |
344
|
50 |
|
|
345
|
50 |
|
/** |
346
|
|
|
* Can be used to filter the result on an applied list of user groups. |
347
|
|
|
* |
348
|
|
|
* @param array $groups |
349
|
|
|
* @return QueryBuilder |
350
|
|
|
*/ |
351
|
|
|
public function useUserAccessGroups(array $groups): QueryBuilder |
352
|
|
|
{ |
353
|
50 |
|
$groups = array_map('intval', $groups); |
354
|
|
|
$groups[] = 0; // always grant access to public documents |
355
|
50 |
|
$groups = array_unique($groups); |
356
|
|
|
sort($groups, SORT_NUMERIC); |
357
|
50 |
|
|
358
|
1 |
|
$accessFilter = '{!typo3access}' . implode(',', $groups); |
359
|
|
|
$this->queryToBuild->removeFilterQuery('access'); |
360
|
|
|
return $this->useFilter($accessFilter, 'access'); |
361
|
49 |
|
} |
362
|
1 |
|
|
363
|
1 |
|
/** |
364
|
|
|
* Applies the configured initial query settings to set the alternative query for solr as required. |
365
|
|
|
* |
366
|
48 |
|
* @return QueryBuilder |
367
|
|
|
*/ |
368
|
|
|
public function useInitialQueryFromTypoScript(): QueryBuilder |
369
|
|
|
{ |
370
|
|
|
if ($this->typoScriptConfiguration->getSearchInitializeWithEmptyQuery() || $this->typoScriptConfiguration->getSearchQueryAllowEmptyQuery()) { |
371
|
|
|
// empty main query, but using a "return everything" |
372
|
|
|
// alternative query in q.alt |
373
|
|
|
$this->useAlternativeQuery('*:*'); |
374
|
|
|
} |
375
|
2 |
|
|
376
|
|
|
if ($this->typoScriptConfiguration->getSearchInitializeWithQuery()) { |
377
|
2 |
|
$this->useAlternativeQuery($this->typoScriptConfiguration->getSearchInitializeWithQuery()); |
378
|
2 |
|
} |
379
|
|
|
|
380
|
|
|
return $this; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Applies the configured facets from the typoscript configuration on the query. |
385
|
|
|
* |
386
|
50 |
|
* @return QueryBuilder |
387
|
|
|
*/ |
388
|
50 |
|
public function useFacetingFromTypoScript(): QueryBuilder |
389
|
50 |
|
{ |
390
|
1 |
|
return $this->useFaceting(Faceting::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
391
|
|
|
} |
392
|
|
|
|
393
|
49 |
|
/** |
394
|
|
|
* Applies the configured variants from the typoscript configuration on the query. |
395
|
|
|
* |
396
|
|
|
* @return QueryBuilder |
397
|
|
|
*/ |
398
|
|
|
public function useVariantsFromTypoScript(): QueryBuilder |
399
|
|
|
{ |
400
|
|
|
return $this->useFieldCollapsing(FieldCollapsing::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
401
|
|
|
} |
402
|
1 |
|
|
403
|
|
|
/** |
404
|
1 |
|
* Applies the configured groupings from the typoscript configuration to the query. |
405
|
1 |
|
* |
406
|
|
|
* @return QueryBuilder |
407
|
|
|
*/ |
408
|
|
|
public function useGroupingFromTypoScript(): QueryBuilder |
409
|
|
|
{ |
410
|
|
|
return $this->useGrouping(Grouping::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
411
|
|
|
} |
412
|
|
|
|
413
|
50 |
|
/** |
414
|
|
|
* Applies the configured highlighting from the typoscript configuration to the query. |
415
|
50 |
|
* |
416
|
50 |
|
* @return QueryBuilder |
417
|
1 |
|
*/ |
418
|
|
|
public function useHighlightingFromTypoScript(): QueryBuilder |
419
|
|
|
{ |
420
|
49 |
|
return $this->useHighlighting(Highlighting::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Applies the configured filters (page section and other from typoscript). |
425
|
|
|
* |
426
|
|
|
* @return QueryBuilder |
427
|
|
|
*/ |
428
|
|
|
public function useFiltersFromTypoScript(): QueryBuilder |
429
|
1 |
|
{ |
430
|
|
|
$filters = Filters::fromTypoScriptConfiguration($this->typoScriptConfiguration); |
431
|
1 |
|
$this->queryToBuild->setFilterQueries($filters->getValues()); |
432
|
1 |
|
|
433
|
|
|
$this->useFilterArray($this->getAdditionalFilters()); |
434
|
|
|
|
435
|
|
|
$searchQueryFilters = $this->typoScriptConfiguration->getSearchQueryFilterConfiguration(); |
436
|
|
|
|
437
|
|
|
if (!is_array($searchQueryFilters) || count($searchQueryFilters) <= 0) { |
|
|
|
|
438
|
50 |
|
return $this; |
439
|
|
|
} |
440
|
50 |
|
|
441
|
50 |
|
// special filter to limit search to specific page tree branches |
442
|
49 |
|
if (array_key_exists('__pageSections', $searchQueryFilters)) { |
443
|
|
|
$pageIds = GeneralUtility::trimExplode(',', $searchQueryFilters['__pageSections']); |
444
|
|
|
$this->usePageSectionsFromPageIds($pageIds); |
445
|
1 |
|
$this->typoScriptConfiguration->removeSearchQueryFilterForPageSections(); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
return $this; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Applies the configured elevation from the typoscript configuration. |
453
|
|
|
* |
454
|
1 |
|
* @return QueryBuilder |
455
|
|
|
*/ |
456
|
1 |
|
public function useElevationFromTypoScript(): QueryBuilder |
457
|
1 |
|
{ |
458
|
|
|
return $this->useElevation(Elevation::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Applies the configured spellchecking from the typoscript configuration. |
463
|
|
|
* |
464
|
|
|
* @return QueryBuilder |
465
|
141 |
|
*/ |
466
|
|
|
public function useSpellcheckingFromTypoScript(): QueryBuilder |
467
|
141 |
|
{ |
468
|
|
|
return $this->useSpellchecking(Spellchecking::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Applies the passed pageIds as __pageSection filter. |
473
|
|
|
* |
474
|
|
|
* @param array $pageIds |
475
|
|
|
* @return QueryBuilder |
476
|
141 |
|
*/ |
477
|
|
|
public function usePageSectionsFromPageIds(array $pageIds = []): QueryBuilder |
478
|
141 |
|
{ |
479
|
141 |
|
$filters = []; |
480
|
|
|
|
481
|
|
|
/** @var $processor PageUidToHierarchy */ |
482
|
|
|
$processor = GeneralUtility::makeInstance(PageUidToHierarchy::class); |
483
|
|
|
$hierarchies = $processor->process($pageIds); |
484
|
|
|
|
485
|
|
|
foreach ($hierarchies as $hierarchy) { |
486
|
|
|
$lastLevel = array_pop($hierarchy); |
487
|
141 |
|
$filters[] = 'rootline:"' . $lastLevel . '"'; |
488
|
|
|
} |
489
|
141 |
|
|
490
|
141 |
|
$pageSectionsFilterString = implode(' OR ', $filters); |
491
|
|
|
return $this->useFilter($pageSectionsFilterString, 'pageSections'); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Applies the configured phrase fields from the typoscript configuration to the query. |
496
|
|
|
* |
497
|
|
|
* @return QueryBuilder |
498
|
|
|
*/ |
499
|
142 |
|
public function usePhraseFieldsFromTypoScript(): QueryBuilder |
500
|
|
|
{ |
501
|
142 |
|
return $this->usePhraseFields(PhraseFields::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
502
|
142 |
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* Applies the configured bigram phrase fields from the typoscript configuration to the query. |
506
|
|
|
* |
507
|
|
|
* @return QueryBuilder |
508
|
|
|
*/ |
509
|
|
|
public function useBigramPhraseFieldsFromTypoScript(): QueryBuilder |
510
|
|
|
{ |
511
|
39 |
|
return $this->useBigramPhraseFields(BigramPhraseFields::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
512
|
|
|
} |
513
|
39 |
|
|
514
|
39 |
|
/** |
515
|
39 |
|
* Applies the configured trigram phrase fields from the typoscript configuration to the query. |
516
|
|
|
* |
517
|
|
|
* @return QueryBuilder |
518
|
|
|
*/ |
519
|
|
|
public function useTrigramPhraseFieldsFromTypoScript(): QueryBuilder |
520
|
|
|
{ |
521
|
|
|
return $this->useTrigramPhraseFields(TrigramPhraseFields::fromTypoScriptConfiguration($this->typoScriptConfiguration)); |
522
|
|
|
} |
523
|
|
|
|
524
|
39 |
|
/** |
525
|
|
|
* Retrieves the configuration filters from the TypoScript configuration, except the __pageSections filter. |
526
|
39 |
|
* |
527
|
39 |
|
* @return array |
528
|
|
|
*/ |
529
|
1 |
|
public function getAdditionalFilters() : array |
530
|
|
|
{ |
531
|
|
|
// when we've build the additionalFilter once, we could return them |
532
|
38 |
|
if (count($this->additionalFilters) > 0) { |
533
|
38 |
|
return $this->additionalFilters; |
534
|
38 |
|
} |
535
|
38 |
|
|
536
|
38 |
|
$searchQueryFilters = $this->typoScriptConfiguration->getSearchQueryFilterConfiguration(); |
537
|
|
|
if (!is_array($searchQueryFilters) || count($searchQueryFilters) <= 0) { |
|
|
|
|
538
|
|
|
return []; |
539
|
38 |
|
} |
540
|
38 |
|
|
541
|
|
|
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class); |
542
|
|
|
|
543
|
|
|
// all other regular filters |
544
|
|
|
foreach ($searchQueryFilters as $filterKey => $filter) { |
545
|
|
|
// the __pageSections filter should not be handled as additional filter |
546
|
|
|
if ($filterKey === '__pageSections') { |
547
|
|
|
continue; |
548
|
|
|
} |
549
|
|
|
|
550
|
47 |
|
$filterIsArray = is_array($searchQueryFilters[$filterKey]); |
551
|
|
|
if ($filterIsArray) { |
552
|
47 |
|
continue; |
553
|
47 |
|
} |
554
|
|
|
|
555
|
|
|
$hasSubConfiguration = is_array($searchQueryFilters[$filterKey . '.']); |
556
|
|
|
if ($hasSubConfiguration) { |
557
|
|
|
$filter = $cObj->stdWrap($searchQueryFilters[$filterKey], $searchQueryFilters[$filterKey . '.']); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
$this->additionalFilters[$filterKey] = $filter; |
561
|
|
|
} |
562
|
43 |
|
|
563
|
|
|
return $this->additionalFilters; |
564
|
43 |
|
} |
565
|
43 |
|
|
566
|
43 |
|
/** |
567
|
43 |
|
* @param string $rawQuery |
568
|
|
|
* @return SearchQuery |
569
|
43 |
|
*/ |
570
|
43 |
|
protected function getSearchQueryInstance($rawQuery): SearchQuery |
571
|
|
|
{ |
572
|
|
|
$query = GeneralUtility::makeInstance(SearchQuery::class); |
573
|
|
|
$query->setQuery($rawQuery); |
574
|
|
|
return $query; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
141 |
|
* @param string $rawQuery |
579
|
|
|
* @return SuggestQuery |
580
|
141 |
|
*/ |
581
|
|
|
protected function getSuggestQueryInstance($rawQuery): SuggestQuery |
582
|
|
|
{ |
583
|
38 |
|
$query = GeneralUtility::makeInstance(SuggestQuery::class, /** @scrutinizer ignore-type */ $rawQuery, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration); |
584
|
|
|
|
585
|
|
|
return $query; |
586
|
141 |
|
} |
587
|
|
|
} |
588
|
|
|
|