Complex classes like Query often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class Query |
||
47 | { |
||
48 | |||
49 | // FIXME extract link building from the query, it's not the query's domain |
||
50 | |||
51 | const SORT_ASC = 'ASC'; |
||
52 | const SORT_DESC = 'DESC'; |
||
53 | |||
54 | const OPERATOR_AND = 'AND'; |
||
55 | const OPERATOR_OR = 'OR'; |
||
56 | |||
57 | /** |
||
58 | * Used to identify the queries. |
||
59 | * |
||
60 | * @var int |
||
61 | */ |
||
62 | protected static $idCount = 0; |
||
63 | |||
64 | /** |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $id; |
||
68 | |||
69 | /** |
||
70 | * @var TypoScriptConfiguration |
||
71 | */ |
||
72 | protected $solrConfiguration; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $keywords; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $keywordsRaw; |
||
83 | |||
84 | /** |
||
85 | * ParameterBuilder for filters. |
||
86 | * |
||
87 | * @var Filters |
||
88 | */ |
||
89 | protected $filters = null; |
||
90 | |||
91 | /** |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $sorting; |
||
95 | |||
96 | // TODO check usage of these two variants, especially the check for $rawQueryString in getQueryString() |
||
97 | /** |
||
98 | * @var |
||
99 | */ |
||
100 | protected $queryString; |
||
101 | |||
102 | /** |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $queryParameters = []; |
||
106 | |||
107 | /** |
||
108 | * @var int |
||
109 | */ |
||
110 | protected $resultsPerPage; |
||
111 | |||
112 | /** |
||
113 | * @var int |
||
114 | */ |
||
115 | protected $page; |
||
116 | |||
117 | /** |
||
118 | * @var int |
||
119 | */ |
||
120 | protected $linkTargetPageId; |
||
121 | |||
122 | /** |
||
123 | * Holds the query fields with their associated boosts. The key represents |
||
124 | * the field name, value represents the field's boost. These are the fields |
||
125 | * that will actually be searched. |
||
126 | * |
||
127 | * Used in Solr's qf parameter |
||
128 | * |
||
129 | * @var QueryFields |
||
130 | * @see http://wiki.apache.org/solr/DisMaxQParserPlugin#qf_.28Query_Fields.29 |
||
131 | */ |
||
132 | protected $queryFields = null; |
||
133 | |||
134 | /** |
||
135 | * List of fields that will be returned in the result documents. |
||
136 | * |
||
137 | * used in Solr's fl parameter |
||
138 | * |
||
139 | * @var ReturnFields |
||
140 | * @see http://wiki.apache.org/solr/CommonQueryParameters#fl |
||
141 | */ |
||
142 | protected $returnFields = null; |
||
143 | |||
144 | /** |
||
145 | * ParameterBuilder for the highlighting. |
||
146 | * |
||
147 | * @var Highlighting |
||
148 | */ |
||
149 | protected $highlighting = null; |
||
150 | |||
151 | /** |
||
152 | * ParameterBuilder for the faceting. |
||
153 | * |
||
154 | * @var Faceting |
||
155 | */ |
||
156 | protected $faceting = null; |
||
157 | |||
158 | /** |
||
159 | * ParameterBuilder for the grouping. |
||
160 | * |
||
161 | * @var Grouping |
||
162 | */ |
||
163 | protected $grouping = null; |
||
164 | |||
165 | /** |
||
166 | * @var bool |
||
167 | */ |
||
168 | private $rawQueryString = false; |
||
169 | |||
170 | /** |
||
171 | * The field by which the result will be collapsed |
||
172 | * @var string |
||
173 | */ |
||
174 | protected $variantField = 'variantId'; |
||
175 | |||
176 | /** |
||
177 | * @var SiteHashService |
||
178 | */ |
||
179 | protected $siteHashService = null; |
||
180 | |||
181 | /** |
||
182 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
183 | */ |
||
184 | protected $logger = null; |
||
185 | |||
186 | /** |
||
187 | * @var EscapeService |
||
188 | */ |
||
189 | protected $escapeService = null; |
||
190 | |||
191 | /** |
||
192 | * Query constructor. |
||
193 | * @param string $keywords |
||
194 | * @param TypoScriptConfiguration $solrConfiguration |
||
195 | * @param SiteHashService|null $siteHashService |
||
196 | * @param EscapeService|null $escapeService |
||
197 | * @param SolrLogManager|null $solrLogManager |
||
198 | */ |
||
199 | 128 | public function __construct($keywords, $solrConfiguration = null, SiteHashService $siteHashService = null, EscapeService $escapeService = null, SolrLogManager $solrLogManager = null) |
|
200 | { |
||
201 | 128 | $keywords = (string)$keywords; |
|
202 | |||
203 | 128 | $this->logger = is_null($solrLogManager) ? GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__) : $solrLogManager; |
|
204 | 128 | $this->solrConfiguration = is_null($solrConfiguration) ? Util::getSolrConfiguration() : $solrConfiguration; |
|
205 | 128 | $this->siteHashService = is_null($siteHashService) ? GeneralUtility::makeInstance(SiteHashService::class) : $siteHashService; |
|
206 | 128 | $this->escapeService = is_null($escapeService) ? GeneralUtility::makeInstance(EscapeService::class) : $escapeService; |
|
207 | 128 | $this->setKeywords($keywords); |
|
208 | 128 | $this->sorting = ''; |
|
209 | |||
210 | 128 | $this->linkTargetPageId = $this->solrConfiguration->getSearchTargetPage(); |
|
211 | |||
212 | 128 | $this->initializeQuery(); |
|
213 | |||
214 | 128 | $this->id = ++self::$idCount; |
|
215 | 128 | } |
|
216 | |||
217 | /** |
||
218 | * @return void |
||
219 | */ |
||
220 | 127 | protected function initializeQuery() |
|
221 | { |
||
222 | // Filters |
||
223 | 127 | $this->initializeFilters(); |
|
224 | |||
225 | // What fields to search |
||
226 | 127 | $queryFields = QueryFields::fromString($this->solrConfiguration->getSearchQueryQueryFields()); |
|
227 | 127 | $this->setQueryFields($queryFields); |
|
228 | |||
229 | // What fields to return from Solr |
||
230 | 127 | $returnFieldsArray = $this->solrConfiguration->getSearchQueryReturnFieldsAsArray(['*', 'score']); |
|
231 | 127 | $returnFields = ReturnFields::fromArray($returnFieldsArray); |
|
232 | 127 | $this->setReturnFields($returnFields); |
|
233 | |||
234 | // Configure highlighting |
||
235 | 127 | $highlighting = Highlighting::fromTypoScriptConfiguration($this->solrConfiguration); |
|
236 | 127 | $this->setHighlighting($highlighting); |
|
237 | |||
238 | // Configure faceting |
||
239 | 127 | $this->initializeFaceting(); |
|
240 | |||
241 | // Initialize grouping |
||
242 | 127 | $this->initializeGrouping(); |
|
243 | |||
244 | // Configure collapsing |
||
245 | 127 | $this->initializeCollapsingFromConfiguration(); |
|
246 | 127 | } |
|
247 | |||
248 | /** |
||
249 | * Takes a string of comma separated query fields and _overwrites_ the |
||
250 | * currently set query fields. Boost can also be specified in through the |
||
251 | * given string. |
||
252 | * |
||
253 | * Example: "title^5, subtitle^2, content, author^0.5" |
||
254 | * This sets the query fields to title with a boost of 5.0, subtitle with |
||
255 | * a boost of 2.0, content with a default boost of 1.0 and the author field |
||
256 | * with a boost of 0.5 |
||
257 | * |
||
258 | * @deprecated use setQueryFields with QueryFields instead, will be removed in 8.0 |
||
259 | * @param string $queryFields A string defining which fields to query and their associated boosts |
||
260 | * @return void |
||
261 | */ |
||
262 | 1 | public function setQueryFieldsFromString($queryFields) |
|
263 | { |
||
264 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
265 | 1 | $this->setQueryFields(QueryFields::fromString($queryFields)); |
|
266 | 1 | } |
|
267 | |||
268 | /** |
||
269 | * Sets a query field and its boost. If the field does not exist yet, it |
||
270 | * gets added. Boost is optional, if left out a default boost of 1.0 is |
||
271 | * applied. |
||
272 | * |
||
273 | * @deprecated use getQueryFields()->set($fieldName, $boost) instead, will be removed in 8.0 |
||
274 | * @param string $fieldName The field's name |
||
275 | * @param float $boost Optional field boost, defaults to 1.0 |
||
276 | * @return void |
||
277 | */ |
||
278 | 1 | public function setQueryField($fieldName, $boost = 1.0) |
|
279 | { |
||
280 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
281 | 1 | $this->getQueryFields()->set($fieldName, $boost); |
|
282 | 1 | } |
|
283 | |||
284 | /** |
||
285 | * @param QueryFields $queryFields |
||
286 | */ |
||
287 | 127 | public function setQueryFields(QueryFields $queryFields) |
|
288 | { |
||
289 | 127 | $this->queryFields = $queryFields; |
|
290 | 127 | } |
|
291 | |||
292 | /** |
||
293 | * @return QueryFields |
||
294 | */ |
||
295 | 81 | public function getQueryFields() |
|
296 | { |
||
297 | 81 | return $this->queryFields; |
|
298 | } |
||
299 | |||
300 | /** |
||
301 | * magic implementation for clone(), makes sure that the id counter is |
||
302 | * incremented |
||
303 | * |
||
304 | * @return void |
||
305 | */ |
||
306 | public function __clone() |
||
307 | { |
||
308 | $this->id = ++self::$idCount; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * returns a string representation of the query |
||
313 | * |
||
314 | * @return string the string representation of the query |
||
315 | */ |
||
316 | 1 | public function __toString() |
|
317 | { |
||
318 | 1 | return $this->getQueryString(); |
|
319 | } |
||
320 | |||
321 | /** |
||
322 | * Builds the query string which is then used for Solr's q parameters |
||
323 | * |
||
324 | * @return string Solr query string |
||
325 | */ |
||
326 | 38 | public function getQueryString() |
|
327 | { |
||
328 | 38 | if (!$this->rawQueryString) { |
|
329 | 35 | $this->buildQueryString(); |
|
330 | } |
||
331 | |||
332 | 38 | return $this->queryString; |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Sets the query string without any escaping. |
||
337 | * |
||
338 | * Be cautious with this function! |
||
339 | * TODO remove this method as it basically just sets the q parameter / keywords |
||
340 | * |
||
341 | * @param string $queryString The raw query string. |
||
342 | */ |
||
343 | 4 | public function setQueryString($queryString) |
|
344 | { |
||
345 | 4 | $this->queryString = $queryString; |
|
346 | 4 | } |
|
347 | |||
348 | /** |
||
349 | * Creates the string that is later used as the q parameter in the solr query |
||
350 | * |
||
351 | * @return void |
||
352 | */ |
||
353 | 35 | protected function buildQueryString() |
|
354 | { |
||
355 | // very simple for now |
||
356 | 35 | $this->queryString = $this->keywords; |
|
357 | 35 | } |
|
358 | |||
359 | /** |
||
360 | * Sets whether a raw query sting should be used, that is, whether the query |
||
361 | * string should be escaped or not. |
||
362 | * |
||
363 | * @param bool $useRawQueryString TRUE to use raw queries (like Lucene Query Language) or FALSE for regular, escaped queries |
||
364 | */ |
||
365 | 4 | public function useRawQueryString($useRawQueryString) |
|
366 | { |
||
367 | 4 | $this->rawQueryString = (boolean)$useRawQueryString; |
|
368 | 4 | } |
|
369 | |||
370 | /** |
||
371 | * Returns the query's ID. |
||
372 | * |
||
373 | * @return int The query's ID. |
||
374 | */ |
||
375 | public function getId() |
||
376 | { |
||
377 | return $this->id; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Quote and escape search strings |
||
382 | * |
||
383 | * @param string $string String to escape |
||
384 | * @deprecated Please use EscapeService noew, will be removed in 8.0 |
||
385 | * @return string The escaped/quoted string |
||
386 | */ |
||
387 | public function escape($string) |
||
388 | { |
||
389 | GeneralUtility::logDeprecatedFunction(); |
||
390 | /** @var EscapeService $escapeService */ |
||
391 | $escapeService = GeneralUtility::makeInstance(EscapeService::class); |
||
392 | return $escapeService->escape($string); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Gets the currently showing page's number |
||
397 | * |
||
398 | * @return int page number currently showing |
||
399 | */ |
||
400 | 1 | public function getPage() |
|
401 | { |
||
402 | 1 | return $this->page; |
|
403 | } |
||
404 | |||
405 | /** |
||
406 | * Sets the page that should be shown |
||
407 | * |
||
408 | * @param int $page page number to show |
||
409 | * @return void |
||
410 | */ |
||
411 | 2 | public function setPage($page) |
|
412 | { |
||
413 | 2 | $this->page = max(intval($page), 0); |
|
414 | 2 | } |
|
415 | |||
416 | /** |
||
417 | * Gets the index of the first result document we're showing |
||
418 | * |
||
419 | * @return int index of the currently first document showing |
||
420 | */ |
||
421 | public function getStartIndex() |
||
422 | { |
||
423 | return ($this->page - 1) * $this->resultsPerPage; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Gets the index of the last result document we're showing |
||
428 | * |
||
429 | * @return int index of the currently last document showing |
||
430 | */ |
||
431 | public function getEndIndex() |
||
432 | { |
||
433 | return $this->page * $this->resultsPerPage; |
||
434 | } |
||
435 | |||
436 | // query elevation |
||
437 | |||
438 | /** |
||
439 | * Activates and deactivates query elevation for the current query. |
||
440 | * |
||
441 | * @param bool $elevation True to enable query elevation (default), FALSE to disable query elevation. |
||
442 | * @param bool $forceElevation Optionally force elevation so that the elevated documents are always on top regardless of sorting, default to TRUE. |
||
443 | * @param bool $markElevatedResults Mark elevated results |
||
444 | * @return void |
||
445 | */ |
||
446 | 32 | public function setQueryElevation($elevation = true, $forceElevation = true, $markElevatedResults = true) |
|
447 | { |
||
448 | 32 | if ($elevation) { |
|
449 | 28 | $this->queryParameters['enableElevation'] = 'true'; |
|
450 | 28 | $this->setForceElevation($forceElevation); |
|
451 | 28 | if ($markElevatedResults) { |
|
452 | 28 | $this->getReturnFields()->add('isElevated:[elevated]'); |
|
453 | } |
||
454 | } else { |
||
455 | 5 | $this->queryParameters['enableElevation'] = 'false'; |
|
456 | 5 | unset($this->queryParameters['forceElevation']); |
|
457 | 5 | $this->getReturnFields()->remove('isElevated:[elevated]'); |
|
458 | 5 | $this->getReturnFields()->remove('[elevated]'); // fallback |
|
459 | } |
||
460 | 32 | } |
|
461 | |||
462 | /** |
||
463 | * Enables or disables the forceElevation query parameter. |
||
464 | * |
||
465 | * @param bool $forceElevation |
||
466 | */ |
||
467 | 28 | protected function setForceElevation($forceElevation) |
|
468 | { |
||
469 | 28 | if ($forceElevation) { |
|
470 | 27 | $this->queryParameters['forceElevation'] = 'true'; |
|
471 | } else { |
||
472 | 1 | $this->queryParameters['forceElevation'] = 'false'; |
|
473 | } |
||
474 | 28 | } |
|
475 | |||
476 | // collapsing |
||
477 | |||
478 | /** |
||
479 | * Check whether collapsing is active |
||
480 | * |
||
481 | * @return bool |
||
482 | */ |
||
483 | 3 | public function getIsCollapsing() |
|
484 | { |
||
485 | 3 | return $this->getFilters()->hasWithName('collapsing'); |
|
486 | } |
||
487 | |||
488 | /** |
||
489 | * @param string $fieldName |
||
490 | */ |
||
491 | 4 | public function setVariantField($fieldName) |
|
492 | { |
||
493 | 4 | $this->variantField = $fieldName; |
|
494 | 4 | } |
|
495 | |||
496 | /** |
||
497 | * @return string |
||
498 | */ |
||
499 | 1 | public function getVariantField() |
|
500 | { |
||
501 | 1 | return $this->variantField; |
|
502 | } |
||
503 | |||
504 | /** |
||
505 | * @param bool $collapsing |
||
506 | */ |
||
507 | 6 | public function setCollapsing($collapsing = true) |
|
508 | { |
||
509 | 6 | if ($collapsing) { |
|
510 | 6 | $this->getFilters()->add('{!collapse field=' . $this->variantField . '}', 'collapsing'); |
|
511 | 6 | if ($this->solrConfiguration->getSearchVariantsExpand()) { |
|
512 | 2 | $this->queryParameters['expand'] = 'true'; |
|
513 | 6 | $this->queryParameters['expand.rows'] = $this->solrConfiguration->getSearchVariantsLimit(); |
|
514 | } |
||
515 | } else { |
||
516 | 1 | $this->getFilters()->removeByName('collapsing'); |
|
517 | 1 | unset($this->queryParameters['expand']); |
|
518 | 1 | unset($this->queryParameters['expand.rows']); |
|
519 | } |
||
520 | 6 | } |
|
521 | |||
522 | |||
523 | /** |
||
524 | * Adds a field to the list of fields to return. Also checks whether * is |
||
525 | * set for the fields, if so it's removed from the field list. |
||
526 | * |
||
527 | * @deprecated Use getReturnFields()->add() instead, will be removed in 8.0 |
||
528 | * @param string $fieldName Name of a field to return in the result documents |
||
529 | */ |
||
530 | public function addReturnField($fieldName) |
||
531 | { |
||
532 | GeneralUtility::logDeprecatedFunction(); |
||
533 | $this->returnFields->add($fieldName); |
||
534 | } |
||
535 | |||
536 | /** |
||
537 | * Removes a field from the list of fields to return (fl parameter). |
||
538 | * |
||
539 | * @deprecated Use getReturnFields()->remove() instead, will be removed in 8.0 |
||
540 | * @param string $fieldName Field to remove from the list of fields to return |
||
541 | */ |
||
542 | public function removeReturnField($fieldName) |
||
543 | { |
||
544 | GeneralUtility::logDeprecatedFunction(); |
||
545 | $this->returnFields->remove($fieldName); |
||
546 | } |
||
547 | |||
548 | // grouping |
||
549 | |||
550 | /** |
||
551 | * Activates and deactivates grouping for the current query. |
||
552 | * |
||
553 | * @param bool|Grouping $grouping TRUE to enable grouping, FALSE to disable grouping |
||
554 | * @return void |
||
555 | */ |
||
556 | 128 | public function setGrouping($grouping = true) |
|
557 | { |
||
558 | 128 | if ($grouping instanceof Grouping) { |
|
559 | 128 | $this->grouping = $grouping; |
|
560 | 128 | return; |
|
561 | } |
||
562 | |||
563 | /** |
||
564 | * @deprecated |
||
565 | * @todo When starting with 8.0 we can add a typehint Grouping to the grouping argument, to drop backwards compatibility. |
||
566 | */ |
||
567 | 3 | $grouping = (bool)$grouping; |
|
568 | |||
569 | 3 | if ($grouping) { |
|
570 | 2 | GeneralUtility::deprecationLog('Usage of setGrouping with boolean deprecated please use getGrouping()->setIsEnabled()'); |
|
571 | 2 | $this->getGrouping()->setIsEnabled($grouping); |
|
572 | } else { |
||
573 | 2 | $this->initializeGrouping(); |
|
574 | } |
||
575 | 3 | } |
|
576 | |||
577 | /** |
||
578 | * @return Grouping |
||
579 | */ |
||
580 | 90 | public function getGrouping() |
|
581 | { |
||
582 | 90 | return $this->grouping; |
|
583 | } |
||
584 | |||
585 | /** |
||
586 | * Sets the number of groups to return per group field or group query |
||
587 | * |
||
588 | * Internally uses the rows parameter. |
||
589 | * |
||
590 | * @deprecated Use getGrouping()->setNumberOfGroups() instead, will be removed in 8.0 |
||
591 | * @param int $numberOfGroups Number of groups per group.field or group.query |
||
592 | */ |
||
593 | 2 | public function setNumberOfGroups($numberOfGroups) |
|
594 | { |
||
595 | 2 | GeneralUtility::logDeprecatedFunction(); |
|
596 | 2 | $this->getGrouping()->setNumberOfGroups($numberOfGroups); |
|
597 | 2 | } |
|
598 | |||
599 | /** |
||
600 | * Gets the number of groups to return per group field or group query |
||
601 | * |
||
602 | * Internally uses the rows parameter. |
||
603 | * |
||
604 | * @deprecated Use getGrouping()->getNumberOfGroups() instead, will be removed in 8.0 |
||
605 | * @return int Number of groups per group.field or group.query |
||
606 | */ |
||
607 | 1 | public function getNumberOfGroups() |
|
608 | { |
||
609 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
610 | 1 | return $this->getGrouping()->getNumberOfGroups(); |
|
611 | } |
||
612 | |||
613 | /** |
||
614 | * Returns the number of results that should be shown per page |
||
615 | * |
||
616 | * @return int number of results to show per page |
||
617 | */ |
||
618 | 32 | public function getResultsPerPage() |
|
619 | { |
||
620 | 32 | if ($this->getGrouping() instanceof Grouping && $this->getGrouping()->getIsEnabled()) { |
|
621 | 2 | return $this->getGrouping()->getNumberOfGroups(); |
|
622 | } |
||
623 | |||
624 | 32 | return $this->resultsPerPage; |
|
625 | } |
||
626 | |||
627 | /** |
||
628 | * Sets the number of results that should be shown per page |
||
629 | * |
||
630 | * @param int $resultsPerPage Number of results to show per page |
||
631 | * @return void |
||
632 | */ |
||
633 | 39 | public function setResultsPerPage($resultsPerPage) |
|
634 | { |
||
635 | 39 | $this->resultsPerPage = max(intval($resultsPerPage), 0); |
|
636 | 39 | } |
|
637 | |||
638 | /** |
||
639 | * Adds a field that should be used for grouping. |
||
640 | * |
||
641 | * @deprecated Use getGrouping()->addField() instead, will be removed in 8.0 |
||
642 | * @param string $fieldName Name of a field for grouping |
||
643 | */ |
||
644 | 1 | public function addGroupField($fieldName) |
|
645 | { |
||
646 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
647 | 1 | $this->getGrouping()->addField($fieldName); |
|
648 | 1 | } |
|
649 | |||
650 | /** |
||
651 | * Gets the fields set for grouping. |
||
652 | * |
||
653 | * @deprecated Use getGrouping()->getFields() instead, will be removed in 8.0 |
||
654 | * @return array An array of fields set for grouping. |
||
655 | */ |
||
656 | 1 | public function getGroupFields() |
|
657 | { |
||
658 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
659 | 1 | return $this->getGrouping()->getFields(); |
|
660 | } |
||
661 | |||
662 | /** |
||
663 | * Adds sorting configuration for grouping. |
||
664 | * |
||
665 | * @deprecated Use getGrouping()->addSorting() instead, will be removed in 8.0 |
||
666 | * @param string $sorting value of sorting configuration |
||
667 | * @param string $sorting value of sorting configuration |
||
668 | */ |
||
669 | 1 | public function addGroupSorting($sorting) |
|
670 | { |
||
671 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
672 | 1 | $this->getGrouping()->addSorting($sorting); |
|
673 | 1 | } |
|
674 | |||
675 | /** |
||
676 | * Gets the sorting set for grouping. |
||
677 | * |
||
678 | * @deprecated Use getGrouping()->getSortings() instead, will be removed in 8.0 |
||
679 | * @return array An array of sorting configurations for grouping. |
||
680 | */ |
||
681 | 2 | public function getGroupSortings() |
|
682 | { |
||
683 | 2 | GeneralUtility::logDeprecatedFunction(); |
|
684 | 2 | return $this->getGrouping()->getSortings(); |
|
685 | } |
||
686 | |||
687 | // faceting |
||
688 | |||
689 | /** |
||
690 | * Adds a query that should be used for grouping. |
||
691 | * |
||
692 | * @deprecated Use getGrouping()->addQuery() instead, will be removed in 8.0 |
||
693 | * @param string $query Lucene query for grouping |
||
694 | */ |
||
695 | 1 | public function addGroupQuery($query) |
|
696 | { |
||
697 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
698 | 1 | $this->getGrouping()->addQuery($query); |
|
699 | 1 | } |
|
700 | |||
701 | /** |
||
702 | * Gets the queries set for grouping. |
||
703 | * |
||
704 | * @deprecated Use getGrouping()->getQueries() instead, will be removed in 8.0 |
||
705 | * @return array An array of queries set for grouping. |
||
706 | */ |
||
707 | 1 | public function getGroupQueries() |
|
708 | { |
||
709 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
710 | 1 | return $this->getGrouping()->getQueries(); |
|
711 | } |
||
712 | |||
713 | /** |
||
714 | * Sets the maximum number of results to be returned per group. |
||
715 | * |
||
716 | * @deprecated Use getGrouping()->setResultsPerGroup() instead, will be removed in 8.0 |
||
717 | * @param int $numberOfResults Maximum number of results per group to return |
||
718 | */ |
||
719 | 1 | public function setNumberOfResultsPerGroup($numberOfResults) |
|
720 | { |
||
721 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
722 | 1 | $this->getGrouping()->setResultsPerGroup($numberOfResults); |
|
723 | 1 | } |
|
724 | |||
725 | |||
726 | /** |
||
727 | * Gets the maximum number of results to be returned per group. |
||
728 | * |
||
729 | * @deprecated Use getGrouping()->getResultsPerGroup() instead, will be removed in 8.0 |
||
730 | * @return int Maximum number of results per group to return |
||
731 | */ |
||
732 | 1 | public function getNumberOfResultsPerGroup() |
|
733 | { |
||
734 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
735 | 1 | return $this->getGrouping()->getResultsPerGroup(); |
|
736 | } |
||
737 | |||
738 | /** |
||
739 | * Activates and deactivates faceting for the current query. |
||
740 | * |
||
741 | * @param bool|Faceting $faceting TRUE to enable faceting, FALSE to disable faceting |
||
742 | * @return void |
||
743 | */ |
||
744 | 127 | public function setFaceting($faceting = true) |
|
745 | { |
||
746 | 127 | if ($faceting instanceof Faceting) { |
|
747 | 127 | $this->faceting = $faceting; |
|
748 | 127 | return; |
|
749 | } |
||
750 | |||
751 | /** |
||
752 | * @deprecated |
||
753 | * @todo When starting with 8.0 we can add a typehint Faceting to the faceting argument, to drop backwards compatibility. |
||
754 | */ |
||
755 | 4 | $faceting = (bool)$faceting; |
|
756 | |||
757 | 4 | if ($faceting) { |
|
758 | 3 | GeneralUtility::deprecationLog('Usage of setFaceting with boolean deprecated please use getFaceting()->setIsEnabled()'); |
|
759 | 3 | $this->getFaceting()->setIsEnabled($faceting); |
|
760 | } else { |
||
761 | 1 | $this->initializeFaceting(); |
|
762 | } |
||
763 | 4 | } |
|
764 | |||
765 | /** |
||
766 | * @return Faceting |
||
767 | */ |
||
768 | 78 | public function getFaceting() |
|
769 | { |
||
770 | 78 | return $this->faceting; |
|
771 | } |
||
772 | |||
773 | /** |
||
774 | * Sets facet fields for a query. |
||
775 | * |
||
776 | * @deprecated Use getFaceting()->setFields() instead, will be removed in 8.0 |
||
777 | * @param array $facetFields Array of field names |
||
778 | */ |
||
779 | 1 | public function setFacetFields(array $facetFields) |
|
780 | { |
||
781 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
782 | 1 | $this->getFaceting()->setIsEnabled(true); |
|
783 | 1 | $this->getFaceting()->setFields($facetFields); |
|
784 | 1 | } |
|
785 | |||
786 | /** |
||
787 | * Adds a single facet field. |
||
788 | * |
||
789 | * @deprecated Use getFaceting()->addField() instead, will be removed in 8.0 |
||
790 | * @param string $facetField field name |
||
791 | */ |
||
792 | 1 | public function addFacetField($facetField) |
|
793 | { |
||
794 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
795 | 1 | $this->getFaceting()->setIsEnabled(true); |
|
796 | 1 | $this->getFaceting()->addField($facetField); |
|
797 | 1 | } |
|
798 | |||
799 | /** |
||
800 | * Removes a filter on a field |
||
801 | * |
||
802 | * @deprecated Use getFilters()->removeByFieldName() instead, will be removed in 8.0 |
||
803 | * @param string $filterFieldName The field name the filter should be removed for |
||
804 | * @return void |
||
805 | */ |
||
806 | 1 | public function removeFilter($filterFieldName) |
|
807 | { |
||
808 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
809 | 1 | $this->getFilters()->removeByFieldName($filterFieldName); |
|
810 | 1 | } |
|
811 | |||
812 | /** |
||
813 | * Removes a filter based on key of filter array |
||
814 | * |
||
815 | * @deprecated Use getFilters()->removeByName() instead, will be removed in 8.0 |
||
816 | * @param string $key array key |
||
817 | */ |
||
818 | 1 | public function removeFilterByKey($key) |
|
819 | { |
||
820 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
821 | 1 | $this->getFilters()->removeByName($key); |
|
822 | 1 | } |
|
823 | |||
824 | /** |
||
825 | * Removes a filter by the filter value. The value has the following format: |
||
826 | * |
||
827 | * "fieldname:value" |
||
828 | * |
||
829 | * @deprecated Use getFilters()->removeByValue() instead, will be removed in 8.0 |
||
830 | * @param string $filterString The filter to remove, in the form of field:value |
||
831 | */ |
||
832 | public function removeFilterByValue($filterString) |
||
833 | { |
||
834 | GeneralUtility::logDeprecatedFunction(); |
||
835 | $this->getFilters()->removeByValue($filterString); |
||
836 | } |
||
837 | |||
838 | /** |
||
839 | * Gets all currently applied filters. |
||
840 | * |
||
841 | * @return Filters Array of filters |
||
842 | */ |
||
843 | 94 | public function getFilters() |
|
844 | { |
||
845 | 94 | return $this->filters; |
|
846 | } |
||
847 | |||
848 | /** |
||
849 | * Sets the filters to use. |
||
850 | * |
||
851 | * @param Filters $filters |
||
852 | */ |
||
853 | 128 | public function setFilters(Filters $filters) |
|
854 | { |
||
855 | 128 | $this->filters = $filters; |
|
856 | 128 | } |
|
857 | |||
858 | // sorting |
||
859 | |||
860 | /** |
||
861 | * Sets access restrictions for a frontend user. |
||
862 | * |
||
863 | * @param array $groups Array of groups a user has been assigned to |
||
864 | */ |
||
865 | 36 | public function setUserAccessGroups(array $groups) |
|
866 | { |
||
867 | 36 | $groups = array_map('intval', $groups); |
|
868 | 36 | $groups[] = 0; // always grant access to public documents |
|
869 | 36 | $groups = array_unique($groups); |
|
870 | 36 | sort($groups, SORT_NUMERIC); |
|
871 | |||
872 | 36 | $accessFilter = '{!typo3access}' . implode(',', $groups); |
|
873 | 36 | $this->getFilters()->removeByPrefix('{!typo3access}'); |
|
874 | 36 | $this->getFilters()->add($accessFilter); |
|
875 | 36 | } |
|
876 | |||
877 | /** |
||
878 | * Adds a filter parameter. |
||
879 | * |
||
880 | * @deprecated Use getFilters()->add() instead, will be removed in 8.0 |
||
881 | * @param string $filterString The filter to add, in the form of field:value |
||
882 | * @return void |
||
883 | */ |
||
884 | 1 | public function addFilter($filterString) |
|
885 | { |
||
886 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
887 | |||
888 | 1 | $this->getFilters()->add($filterString); |
|
889 | 1 | } |
|
890 | |||
891 | |||
892 | // query parameters |
||
893 | |||
894 | /** |
||
895 | * Limits the query to certain sites |
||
896 | * |
||
897 | * @param string $allowedSites Comma-separated list of domains |
||
898 | */ |
||
899 | 32 | public function setSiteHashFilter($allowedSites) |
|
900 | { |
||
901 | 32 | if (trim($allowedSites) === '*') { |
|
902 | 1 | return; |
|
903 | } |
||
904 | |||
905 | 31 | $allowedSites = GeneralUtility::trimExplode(',', $allowedSites); |
|
906 | 31 | $filters = []; |
|
907 | |||
908 | 31 | foreach ($allowedSites as $site) { |
|
909 | 31 | $siteHash = $this->siteHashService->getSiteHashForDomain($site); |
|
910 | 31 | $filters[] = 'siteHash:"' . $siteHash . '"'; |
|
911 | } |
||
912 | |||
913 | 31 | $this->getFilters()->add(implode(' OR ', $filters)); |
|
914 | 31 | } |
|
915 | |||
916 | /** |
||
917 | * Limits the query to certain page tree branches |
||
918 | * |
||
919 | * @param string $pageIds Comma-separated list of page IDs |
||
920 | */ |
||
921 | public function setRootlineFilter($pageIds) |
||
922 | { |
||
923 | $pageIds = GeneralUtility::trimExplode(',', $pageIds); |
||
924 | $filters = []; |
||
925 | |||
926 | /** @var $processor PageUidToHierarchy */ |
||
927 | $processor = GeneralUtility::makeInstance(PageUidToHierarchy::class); |
||
928 | $hierarchies = $processor->process($pageIds); |
||
929 | |||
930 | foreach ($hierarchies as $hierarchy) { |
||
931 | $lastLevel = array_pop($hierarchy); |
||
932 | $filters[] = 'rootline:"' . $lastLevel . '"'; |
||
933 | } |
||
934 | |||
935 | $this->getFilters()->add(implode(' OR ', $filters)); |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * Gets the list of fields a query will return. |
||
940 | * |
||
941 | * @deprecated Use method getReturnFields() instead, will be removed in 8.0 |
||
942 | * @return array List of field names the query will return |
||
943 | */ |
||
944 | 2 | public function getFieldList() |
|
945 | { |
||
946 | 2 | GeneralUtility::logDeprecatedFunction(); |
|
947 | 2 | return $this->getReturnFields()->getValues(); |
|
948 | } |
||
949 | |||
950 | /** |
||
951 | * Sets the fields to return by a query. |
||
952 | * |
||
953 | * @deprecated Use method setReturnFields() instead, will be removed in 8.0 |
||
954 | * @param array|string $fieldList an array or comma-separated list of field names |
||
955 | * @throws \UnexpectedValueException on parameters other than comma-separated lists and arrays |
||
956 | */ |
||
957 | 1 | public function setFieldList($fieldList = ['*', 'score']) |
|
958 | { |
||
959 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
960 | 1 | if ($fieldList === null) { |
|
961 | $this->setReturnFields(ReturnFields::fromArray(['*', 'score'])); |
||
962 | return; |
||
963 | } |
||
964 | |||
965 | |||
966 | 1 | if (is_string($fieldList)) { |
|
967 | 1 | $this->setReturnFields(ReturnFields::fromString($fieldList)); |
|
968 | 1 | return; |
|
969 | } |
||
970 | |||
971 | 1 | if (is_array($fieldList)) { |
|
972 | 1 | $this->setReturnFields(ReturnFields::fromArray($fieldList)); |
|
973 | 1 | return; |
|
974 | } |
||
975 | |||
976 | 1 | throw new \UnexpectedValueException('Field list must be a FieldList object.', 1310740308); |
|
977 | } |
||
978 | |||
979 | /** |
||
980 | * @param ReturnFields $returnFields |
||
981 | */ |
||
982 | 127 | public function setReturnFields(ReturnFields $returnFields) |
|
983 | { |
||
984 | 127 | $this->returnFields = $returnFields; |
|
985 | 127 | } |
|
986 | |||
987 | /** |
||
988 | * @return ReturnFields |
||
989 | */ |
||
990 | 84 | public function getReturnFields() |
|
991 | { |
||
992 | 84 | return $this->returnFields; |
|
993 | } |
||
994 | |||
995 | /** |
||
996 | * Gets the query type, Solr's qt parameter. |
||
997 | * |
||
998 | * @return string Query type, qt parameter. |
||
999 | */ |
||
1000 | 1 | public function getQueryType() |
|
1001 | { |
||
1002 | 1 | return $this->queryParameters['qt']; |
|
1003 | } |
||
1004 | |||
1005 | /** |
||
1006 | * Sets the query type, Solr's qt parameter. |
||
1007 | * |
||
1008 | * @param string|bool $queryType String query type or boolean FALSE to disable / reset the qt parameter. |
||
1009 | * @see http://wiki.apache.org/solr/CoreQueryParameters#qt |
||
1010 | */ |
||
1011 | 2 | public function setQueryType($queryType) |
|
1012 | { |
||
1013 | 2 | $this->setQueryParameterWhenStringOrUnsetWhenEmpty('qt', $queryType); |
|
1014 | 2 | } |
|
1015 | |||
1016 | /** |
||
1017 | * Sets the query operator to AND or OR. Unsets the query operator (actually |
||
1018 | * sets it back to default) for FALSE. |
||
1019 | * |
||
1020 | * @param string|bool $operator AND or OR, FALSE to unset |
||
1021 | */ |
||
1022 | 1 | public function setOperator($operator) |
|
1023 | { |
||
1024 | 1 | if (in_array($operator, [self::OPERATOR_AND, self::OPERATOR_OR])) { |
|
1025 | 1 | $this->queryParameters['q.op'] = $operator; |
|
1026 | } |
||
1027 | |||
1028 | 1 | if ($operator === false) { |
|
1029 | 1 | unset($this->queryParameters['q.op']); |
|
1030 | } |
||
1031 | 1 | } |
|
1032 | |||
1033 | /** |
||
1034 | * Gets the alternative query, Solr's q.alt parameter. |
||
1035 | * |
||
1036 | * @return string Alternative query, q.alt parameter. |
||
1037 | */ |
||
1038 | 1 | public function getAlternativeQuery() |
|
1039 | { |
||
1040 | 1 | return $this->queryParameters['q.alt']; |
|
1041 | } |
||
1042 | |||
1043 | /** |
||
1044 | * Sets an alternative query, Solr's q.alt parameter. |
||
1045 | * |
||
1046 | * This query supports the complete Lucene Query Language. |
||
1047 | * |
||
1048 | * @param mixed $alternativeQuery String alternative query or boolean FALSE to disable / reset the q.alt parameter. |
||
1049 | * @see http://wiki.apache.org/solr/DisMaxQParserPlugin#q.alt |
||
1050 | */ |
||
1051 | 32 | public function setAlternativeQuery($alternativeQuery) |
|
1052 | { |
||
1053 | 32 | $this->setQueryParameterWhenStringOrUnsetWhenEmpty('q.alt', $alternativeQuery); |
|
1054 | 32 | } |
|
1055 | |||
1056 | // keywords |
||
1057 | |||
1058 | /** |
||
1059 | * Set the query to omit the response header |
||
1060 | * |
||
1061 | * @param bool $omitHeader TRUE (default) to omit response headers, FALSE to re-enable |
||
1062 | */ |
||
1063 | 1 | public function setOmitHeader($omitHeader = true) |
|
1064 | { |
||
1065 | 1 | $omitHeader = ($omitHeader === true) ? 'true' : $omitHeader; |
|
1066 | 1 | $this->setQueryParameterWhenStringOrUnsetWhenEmpty('omitHeader', $omitHeader); |
|
1067 | 1 | } |
|
1068 | |||
1069 | /** |
||
1070 | * Get the query keywords, keywords are escaped. |
||
1071 | * |
||
1072 | * @return string query keywords |
||
1073 | */ |
||
1074 | 33 | public function getKeywords() |
|
1075 | { |
||
1076 | 33 | return $this->keywords; |
|
1077 | } |
||
1078 | |||
1079 | /** |
||
1080 | * Sets the query keywords, escapes them as needed for Solr/Lucene. |
||
1081 | * |
||
1082 | * @param string $keywords user search terms/keywords |
||
1083 | */ |
||
1084 | 128 | public function setKeywords($keywords) |
|
1085 | { |
||
1086 | 128 | $this->keywords = $this->escapeService->escape($keywords); |
|
|
|||
1087 | 128 | $this->keywordsRaw = $keywords; |
|
1088 | 128 | } |
|
1089 | |||
1090 | /** |
||
1091 | * Gets the cleaned keywords so that it can be used in templates f.e. |
||
1092 | * |
||
1093 | * @return string The cleaned keywords. |
||
1094 | */ |
||
1095 | 26 | public function getKeywordsCleaned() |
|
1096 | { |
||
1097 | 26 | return $this->cleanKeywords($this->keywordsRaw); |
|
1098 | } |
||
1099 | |||
1100 | /** |
||
1101 | * Helper method to escape/encode keywords for use in HTML |
||
1102 | * |
||
1103 | * @param string $keywords Keywords to prepare for use in HTML |
||
1104 | * @return string Encoded keywords |
||
1105 | */ |
||
1106 | 26 | public static function cleanKeywords($keywords) |
|
1107 | { |
||
1108 | 26 | $keywords = trim($keywords); |
|
1109 | 26 | $keywords = htmlspecialchars($keywords); |
|
1110 | 26 | return $keywords; |
|
1111 | } |
||
1112 | |||
1113 | /** |
||
1114 | * Escapes marker hashes and the pipe symbol so that they will not be |
||
1115 | * executed in templates. |
||
1116 | * |
||
1117 | * @param string $content Content potentially containing markers |
||
1118 | * @deprecated Only needed for old templating. Will be removed in 8.0 |
||
1119 | * @return string Content with markers escaped |
||
1120 | */ |
||
1121 | protected static function escapeMarkers($content) |
||
1122 | { |
||
1123 | GeneralUtility::logDeprecatedFunction(); |
||
1124 | |||
1125 | // escape marker hashes |
||
1126 | $content = str_replace('###', '###', $content); |
||
1127 | // escape pipe character used for parameter separation |
||
1128 | $content = str_replace('|', '|', $content); |
||
1129 | |||
1130 | return $content; |
||
1131 | } |
||
1132 | |||
1133 | // relevance, matching |
||
1134 | |||
1135 | /** |
||
1136 | * Gets the raw, unescaped, unencoded keywords. |
||
1137 | * |
||
1138 | * USE WITH CAUTION! |
||
1139 | * |
||
1140 | * @return string raw keywords |
||
1141 | */ |
||
1142 | public function getKeywordsRaw() |
||
1143 | { |
||
1144 | return $this->keywordsRaw; |
||
1145 | } |
||
1146 | |||
1147 | /** |
||
1148 | * Sets the minimum match (mm) parameter |
||
1149 | * |
||
1150 | * @param mixed $minimumMatch Minimum match parameter as string or boolean FALSE to disable / reset the mm parameter |
||
1151 | * @see http://wiki.apache.org/solr/DisMaxRequestHandler#mm_.28Minimum_.27Should.27_Match.29 |
||
1152 | */ |
||
1153 | 1 | public function setMinimumMatch($minimumMatch) |
|
1154 | { |
||
1155 | 1 | $this->setQueryParameterWhenStringOrUnsetWhenEmpty('mm', $minimumMatch); |
|
1156 | 1 | } |
|
1157 | |||
1158 | /** |
||
1159 | * Sets the boost function (bf) parameter |
||
1160 | * |
||
1161 | * @param mixed $boostFunction boost function parameter as string or boolean FALSE to disable / reset the bf parameter |
||
1162 | * @see http://wiki.apache.org/solr/DisMaxRequestHandler#bf_.28Boost_Functions.29 |
||
1163 | */ |
||
1164 | 1 | public function setBoostFunction($boostFunction) |
|
1165 | { |
||
1166 | 1 | $this->setQueryParameterWhenStringOrUnsetWhenEmpty('bf', $boostFunction); |
|
1167 | 1 | } |
|
1168 | |||
1169 | // query fields |
||
1170 | // TODO move up to field list methods |
||
1171 | |||
1172 | /** |
||
1173 | * Sets the boost query (bq) parameter |
||
1174 | * |
||
1175 | * @param mixed $boostQuery boost query parameter as string or array to set a boost query or boolean FALSE to disable / reset the bq parameter |
||
1176 | * @see http://wiki.apache.org/solr/DisMaxQParserPlugin#bq_.28Boost_Query.29 |
||
1177 | */ |
||
1178 | 1 | public function setBoostQuery($boostQuery) |
|
1179 | { |
||
1180 | 1 | if (is_array($boostQuery)) { |
|
1181 | $this->queryParameters['bq'] = $boostQuery; |
||
1182 | return; |
||
1183 | } |
||
1184 | 1 | $this->setQueryParameterWhenStringOrUnsetWhenEmpty('bq', $boostQuery); |
|
1185 | 1 | } |
|
1186 | |||
1187 | /** |
||
1188 | * Gets a specific query parameter by its name. |
||
1189 | * |
||
1190 | * @param string $parameterName The parameter to return |
||
1191 | * @param mixed $defaultIfEmpty |
||
1192 | * @return mixed The parameter's value or $defaultIfEmpty if not set |
||
1193 | */ |
||
1194 | 9 | public function getQueryParameter($parameterName, $defaultIfEmpty = null) |
|
1195 | { |
||
1196 | 9 | $parameters = $this->getQueryParameters(); |
|
1197 | 9 | return isset($parameters[$parameterName]) ? $parameters[$parameterName] : $defaultIfEmpty; |
|
1198 | } |
||
1199 | |||
1200 | /** |
||
1201 | * Builds an array of query parameters to use for the search query. |
||
1202 | * |
||
1203 | * @return array An array ready to use with query parameters |
||
1204 | */ |
||
1205 | 78 | public function getQueryParameters() |
|
1206 | { |
||
1207 | 78 | $queryParameters = $this->getReturnFields()->build(); |
|
1208 | 78 | $queryParameters = array_merge($queryParameters, $this->getFilters()->build()); |
|
1209 | 78 | $queryParameters = array_merge($queryParameters, $this->queryParameters); |
|
1210 | 78 | $queryParameters = array_merge($queryParameters, $this->getQueryFields()->build()); |
|
1211 | 78 | $queryParameters = array_merge($queryParameters, $this->getHighlighting()->build()); |
|
1212 | 78 | $queryParameters = array_merge($queryParameters, $this->getFaceting()->build()); |
|
1213 | 78 | $queryParameters = array_merge($queryParameters, $this->getGrouping()->build()); |
|
1214 | |||
1215 | 78 | return $queryParameters; |
|
1216 | } |
||
1217 | |||
1218 | // general query parameters |
||
1219 | |||
1220 | /** |
||
1221 | * Compiles the query fields into a string to be used in Solr's qf parameter. |
||
1222 | * |
||
1223 | * @deprecated Use getQueryFields()->toString() please. Will be removed in 8.0 |
||
1224 | * @return string A string of query fields with their associated boosts |
||
1225 | */ |
||
1226 | 1 | public function getQueryFieldsAsString() |
|
1227 | { |
||
1228 | 1 | GeneralUtility::logDeprecatedFunction(); |
|
1229 | 1 | return $this->getQueryFields()->toString(); |
|
1230 | } |
||
1231 | |||
1232 | /** |
||
1233 | * Enables or disables highlighting of search terms in result teasers. |
||
1234 | * |
||
1235 | * @param Highlighting|bool $highlighting Enables highlighting when set to TRUE, deactivates highlighting when set to FALSE, defaults to TRUE. |
||
1236 | * @param int $fragmentSize Size, in characters, of fragments to consider for highlighting. |
||
1237 | * @see http://wiki.apache.org/solr/HighlightingParameters |
||
1238 | * @return void |
||
1239 | */ |
||
1240 | 127 | public function setHighlighting($highlighting = true, $fragmentSize = 200) |
|
1241 | { |
||
1242 | 127 | if ($highlighting instanceof Highlighting) { |
|
1243 | 127 | $this->highlighting = $highlighting; |
|
1244 | 127 | return; |
|
1245 | } |
||
1246 | |||
1247 | /** |
||
1248 | * @deprecated |
||
1249 | * @todo When starting with 8.0 we can add a typehint Highlighting to the highlighting argument and remove fragmentsize, to drop backwards compatibility. |
||
1250 | */ |
||
1251 | 7 | GeneralUtility::deprecationLog('Usage of setHighlighting with boolean or fragmentSize is deprecated please use getHighlighting()->setIsEnabled() or getHighlighting()->setFragmentSize() please'); |
|
1252 | 7 | $highlighting = (bool)$highlighting; |
|
1253 | 7 | $this->getHighlighting()->setIsEnabled($highlighting); |
|
1254 | 7 | $this->getHighlighting()->setFragmentSize($fragmentSize); |
|
1255 | 7 | } |
|
1256 | |||
1257 | /** |
||
1258 | * @return Highlighting |
||
1259 | */ |
||
1260 | 78 | public function getHighlighting() |
|
1261 | { |
||
1262 | 78 | return $this->highlighting; |
|
1263 | } |
||
1264 | |||
1265 | // misc |
||
1266 | |||
1267 | /** |
||
1268 | * Enables or disables spellchecking for the query. |
||
1269 | * |
||
1270 | * @param bool $spellchecking Enables spellchecking when set to TRUE, deactivates spellchecking when set to FALSE, defaults to TRUE. |
||
1271 | */ |
||
1272 | 28 | public function setSpellchecking($spellchecking = true) |
|
1273 | { |
||
1274 | 28 | if ($spellchecking) { |
|
1275 | 28 | $this->queryParameters['spellcheck'] = 'true'; |
|
1276 | 28 | $this->queryParameters['spellcheck.collate'] = 'true'; |
|
1277 | 28 | $maxCollationTries = $this->solrConfiguration->getSearchSpellcheckingNumberOfSuggestionsToTry(); |
|
1278 | 28 | $this->addQueryParameter('spellcheck.maxCollationTries', $maxCollationTries); |
|
1279 | } else { |
||
1280 | 1 | unset($this->queryParameters['spellcheck']); |
|
1281 | 1 | unset($this->queryParameters['spellcheck.collate']); |
|
1282 | 1 | unset($this->queryParameters['spellcheck.maxCollationTries']); |
|
1283 | } |
||
1284 | 28 | } |
|
1285 | |||
1286 | /** |
||
1287 | * This method can be used to set a query parameter when the value is a string and not empty or unset it |
||
1288 | * in any other case. Extracted to avoid duplicate code. |
||
1289 | * |
||
1290 | * @param string $parameterName |
||
1291 | * @param mixed $value |
||
1292 | */ |
||
1293 | 38 | private function setQueryParameterWhenStringOrUnsetWhenEmpty($parameterName, $value) |
|
1294 | { |
||
1295 | 38 | if (is_string($value) && !empty($value)) { |
|
1296 | 38 | $this->addQueryParameter($parameterName, $value); |
|
1297 | } else { |
||
1298 | 6 | unset($this->queryParameters[$parameterName]); |
|
1299 | } |
||
1300 | 38 | } |
|
1301 | |||
1302 | /** |
||
1303 | * Adds any generic query parameter. |
||
1304 | * |
||
1305 | * @param string $parameterName Query parameter name |
||
1306 | * @param string $parameterValue Parameter value |
||
1307 | */ |
||
1308 | 40 | public function addQueryParameter($parameterName, $parameterValue) |
|
1312 | |||
1313 | /** |
||
1314 | * Sets the sort parameter. |
||
1315 | * |
||
1316 | * $sorting must include a field name (or the pseudo-field score), |
||
1317 | * followed by a space, |
||
1318 | * followed by a sort direction (asc or desc). |
||
1319 | * |
||
1320 | * Multiple fallback sortings can be separated by comma, |
||
1321 | * ie: <field name> <direction>[,<field name> <direction>]... |
||
1322 | * |
||
1323 | * @param string|bool $sorting Either a comma-separated list of sort fields and directions or FALSE to reset sorting to the default behavior (sort by score / relevance) |
||
1324 | * @see http://wiki.apache.org/solr/CommonQueryParameters#sort |
||
1325 | */ |
||
1326 | 2 | public function setSorting($sorting) |
|
1327 | { |
||
1328 | 2 | if ($sorting) { |
|
1329 | 2 | if (!is_string($sorting)) { |
|
1330 | throw new \InvalidArgumentException('Sorting needs to be a string!'); |
||
1331 | } |
||
1332 | 2 | $sortParameter = $this->removeRelevanceSortField($sorting); |
|
1333 | 2 | $this->queryParameters['sort'] = $sortParameter; |
|
1334 | } else { |
||
1335 | 1 | unset($this->queryParameters['sort']); |
|
1336 | } |
||
1337 | 2 | } |
|
1338 | |||
1339 | /** |
||
1340 | * Removes the relevance sort field if present in the sorting field definition. |
||
1341 | * |
||
1342 | * @param string $sorting |
||
1343 | * @return string |
||
1344 | */ |
||
1345 | 2 | protected function removeRelevanceSortField($sorting) |
|
1346 | { |
||
1347 | 2 | $sortParameter = $sorting; |
|
1348 | 2 | list($sortField) = explode(' ', $sorting); |
|
1349 | 2 | if ($sortField === 'relevance') { |
|
1350 | 1 | $sortParameter = ''; |
|
1351 | 1 | return $sortParameter; |
|
1352 | } |
||
1353 | |||
1354 | 2 | return $sortParameter; |
|
1355 | } |
||
1356 | |||
1357 | /** |
||
1358 | * Enables or disables the debug parameter for the query. |
||
1359 | * |
||
1360 | * @param bool $debugMode Enables debugging when set to TRUE, deactivates debugging when set to FALSE, defaults to TRUE. |
||
1361 | */ |
||
1362 | 27 | public function setDebugMode($debugMode = true) |
|
1363 | { |
||
1364 | 27 | if ($debugMode) { |
|
1365 | 27 | $this->queryParameters['debugQuery'] = 'true'; |
|
1366 | 27 | $this->queryParameters['echoParams'] = 'all'; |
|
1367 | } else { |
||
1368 | 1 | unset($this->queryParameters['debugQuery']); |
|
1369 | 1 | unset($this->queryParameters['echoParams']); |
|
1370 | } |
||
1371 | 27 | } |
|
1372 | |||
1373 | /** |
||
1374 | * Returns the link target page id. |
||
1375 | * |
||
1376 | * @return int |
||
1377 | */ |
||
1378 | 2 | public function getLinkTargetPageId() |
|
1379 | { |
||
1380 | 2 | return $this->linkTargetPageId; |
|
1381 | } |
||
1382 | |||
1383 | /** |
||
1384 | * Activates the collapsing on the configured field, if collapsing was enabled. |
||
1385 | * |
||
1386 | * @return bool |
||
1387 | */ |
||
1388 | 127 | protected function initializeCollapsingFromConfiguration() |
|
1389 | { |
||
1390 | // check collapsing |
||
1391 | 127 | if ($this->solrConfiguration->getSearchVariants()) { |
|
1392 | 4 | $collapseField = $this->solrConfiguration->getSearchVariantsField(); |
|
1393 | 4 | $this->setVariantField($collapseField); |
|
1394 | 4 | $this->setCollapsing(true); |
|
1395 | |||
1396 | 4 | return true; |
|
1397 | } |
||
1398 | |||
1399 | 123 | return false; |
|
1400 | } |
||
1401 | |||
1402 | /** |
||
1403 | * @return void |
||
1404 | */ |
||
1405 | 127 | protected function initializeFaceting() |
|
1406 | { |
||
1407 | 127 | $faceting = Faceting::fromTypoScriptConfiguration($this->solrConfiguration); |
|
1408 | 127 | $this->setFaceting($faceting); |
|
1409 | 127 | } |
|
1410 | |||
1411 | /** |
||
1412 | * @return void |
||
1413 | */ |
||
1414 | 128 | protected function initializeGrouping() |
|
1415 | { |
||
1416 | 128 | $grouping = Grouping::fromTypoScriptConfiguration($this->solrConfiguration); |
|
1417 | 128 | $this->setGrouping($grouping); |
|
1418 | 128 | } |
|
1419 | |||
1420 | /** |
||
1421 | * @return void |
||
1422 | */ |
||
1423 | 128 | protected function initializeFilters() |
|
1428 | } |
||
1429 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.