fca2b23b76f30c4c10d3c3113667c24e32546203
[yaffs-website] / web / core / modules / search / src / SearchQuery.php
1 <?php
2
3 namespace Drupal\search;
4
5 use Drupal\Core\Database\Query\Condition;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Database\Query\SelectExtender;
8 use Drupal\Core\Database\Query\SelectInterface;
9
10 /**
11  * Search query extender and helper functions.
12  *
13  * Performs a query on the full-text search index for a word or words.
14  *
15  * This query is used by search plugins that use the search index (not all
16  * search plugins do, as some use a different searching mechanism). It
17  * assumes you have set up a query on the {search_index} table with alias 'i',
18  * and will only work if the user is searching for at least one "positive"
19  * keyword or phrase.
20  *
21  * For efficiency, users of this query can run the prepareAndNormalize()
22  * method to figure out if there are any search results, before fully setting
23  * up and calling execute() to execute the query. The scoring expressions are
24  * not needed until the execute() step. However, it's not really necessary
25  * to do this, because this class's execute() method does that anyway.
26  *
27  * During both the prepareAndNormalize() and execute() steps, there can be
28  * problems. Call getStatus() to figure out if the query is OK or not.
29  *
30  * The query object is given the tag 'search_$type' and can be further
31  * extended with hook_query_alter().
32  */
33 class SearchQuery extends SelectExtender {
34
35   /**
36    * Indicates no positive keywords were in the search expression.
37    *
38    * Positive keywords are words that are searched for, as opposed to negative
39    * keywords, which are words that are excluded. To count as a keyword, a
40    * word must be at least
41    * \Drupal::config('search.settings')->get('index.minimum_word_size')
42    * characters.
43    *
44    * @see SearchQuery::getStatus()
45    */
46   const NO_POSITIVE_KEYWORDS = 1;
47
48   /**
49    * Indicates that part of the search expression was ignored.
50    *
51    * To prevent Denial of Service attacks, only
52    * \Drupal::config('search.settings')->get('and_or_limit') expressions
53    * (positive keywords, phrases, negative keywords) are allowed; this flag
54    * indicates that expressions existed past that limit and they were removed.
55    *
56    * @see SearchQuery::getStatus()
57    */
58   const EXPRESSIONS_IGNORED = 2;
59
60   /**
61    * Indicates that lower-case "or" was in the search expression.
62    *
63    * The word "or" in lower case was found in the search expression. This
64    * probably means someone was trying to do an OR search but used lower-case
65    * instead of upper-case.
66    *
67    * @see SearchQuery::getStatus()
68    */
69   const LOWER_CASE_OR = 4;
70
71   /**
72    * Indicates that no positive keyword matches were found.
73    *
74    * @see SearchQuery::getStatus()
75    */
76   const NO_KEYWORD_MATCHES = 8;
77
78   /**
79    * The keywords and advanced search options that are entered by the user.
80    *
81    * @var string
82    */
83   protected $searchExpression;
84
85   /**
86    * The type of search (search type).
87    *
88    * This maps to the value of the type column in search_index, and is usually
89    * equal to the machine-readable name of the plugin or the search page.
90    *
91    * @var string
92    */
93   protected $type;
94
95   /**
96    * Parsed-out positive and negative search keys.
97    *
98    * @var array
99    */
100   protected $keys = ['positive' => [], 'negative' => []];
101
102   /**
103    * Indicates whether the query conditions are simple or complex (LIKE).
104    *
105    * @var bool
106    */
107   protected $simple = TRUE;
108
109   /**
110    * Conditions that are used for exact searches.
111    *
112    * This is always used for the second step in the query, but is not part of
113    * the preparation step unless $this->simple is FALSE.
114    *
115    * @var DatabaseCondition
116    */
117   protected $conditions;
118
119   /**
120    * Indicates how many matches for a search query are necessary.
121    *
122    * @var int
123    */
124   protected $matches = 0;
125
126   /**
127    * Array of positive search words.
128    *
129    * These words have to match against {search_index}.word.
130    *
131    * @var array
132    */
133   protected $words = [];
134
135   /**
136    * Multiplier to normalize the keyword score.
137    *
138    * This value is calculated by the preparation step, and is used as a
139    * multiplier of the word scores to make sure they are between 0 and 1.
140    *
141    * @var float
142    */
143   protected $normalize = 0;
144
145   /**
146    * Indicates whether the preparation step has been executed.
147    *
148    * @var bool
149    */
150   protected $executedPrepare = FALSE;
151
152   /**
153    * A bitmap of status conditions, described in getStatus().
154    *
155    * @var int
156    *
157    * @see SearchQuery::getStatus()
158    */
159   protected $status = 0;
160
161   /**
162    * The word score expressions.
163    *
164    * @var array
165    *
166    * @see SearchQuery::addScore()
167    */
168   protected $scores = [];
169
170   /**
171    * Arguments for the score expressions.
172    *
173    * @var array
174    */
175   protected $scoresArguments = [];
176
177   /**
178    * The number of 'i.relevance' occurrences in score expressions.
179    *
180    * @var int
181    */
182   protected $relevance_count = 0;
183
184   /**
185    * Multipliers for score expressions.
186    *
187    * @var array
188    */
189   protected $multiply = [];
190
191   /**
192    * Sets the search query expression.
193    *
194    * @param string $expression
195    *   A search string, which can contain keywords and options.
196    * @param string $type
197    *   The search type. This maps to {search_index}.type in the database.
198    *
199    * @return $this
200    */
201   public function searchExpression($expression, $type) {
202     $this->searchExpression = $expression;
203     $this->type = $type;
204
205     // Add query tag.
206     $this->addTag('search_' . $type);
207
208     // Initialize conditions and status.
209     $this->conditions = new Condition('AND');
210     $this->status = 0;
211
212     return $this;
213   }
214
215   /**
216    * Parses the search query into SQL conditions.
217    *
218    * Sets up the following variables:
219    * - $this->keys
220    * - $this->words
221    * - $this->conditions
222    * - $this->simple
223    * - $this->matches
224    */
225   protected function parseSearchExpression() {
226     // Matches words optionally prefixed by a - sign. A word in this case is
227     // something between two spaces, optionally quoted.
228     preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->searchExpression, $keywords, PREG_SET_ORDER);
229
230     if (count($keywords) == 0) {
231       return;
232     }
233
234     // Classify tokens.
235     $in_or = FALSE;
236     $limit_combinations = \Drupal::config('search.settings')->get('and_or_limit');
237     // The first search expression does not count as AND.
238     $and_count = -1;
239     $or_count = 0;
240     foreach ($keywords as $match) {
241       if ($or_count && $and_count + $or_count >= $limit_combinations) {
242         // Ignore all further search expressions to prevent Denial-of-Service
243         // attacks using a high number of AND/OR combinations.
244         $this->status |= SearchQuery::EXPRESSIONS_IGNORED;
245         break;
246       }
247
248       // Strip off phrase quotes.
249       $phrase = FALSE;
250       if ($match[2]{0} == '"') {
251         $match[2] = substr($match[2], 1, -1);
252         $phrase = TRUE;
253         $this->simple = FALSE;
254       }
255
256       // Simplify keyword according to indexing rules and external
257       // preprocessors. Use same process as during search indexing, so it
258       // will match search index.
259       $words = search_simplify($match[2]);
260       // Re-explode in case simplification added more words, except when
261       // matching a phrase.
262       $words = $phrase ? [$words] : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
263       // Negative matches.
264       if ($match[1] == '-') {
265         $this->keys['negative'] = array_merge($this->keys['negative'], $words);
266       }
267       // OR operator: instead of a single keyword, we store an array of all
268       // OR'd keywords.
269       elseif ($match[2] == 'OR' && count($this->keys['positive'])) {
270         $last = array_pop($this->keys['positive']);
271         // Starting a new OR?
272         if (!is_array($last)) {
273           $last = [$last];
274         }
275         $this->keys['positive'][] = $last;
276         $in_or = TRUE;
277         $or_count++;
278         continue;
279       }
280       // AND operator: implied, so just ignore it.
281       elseif ($match[2] == 'AND' || $match[2] == 'and') {
282         continue;
283       }
284
285       // Plain keyword.
286       else {
287         if ($match[2] == 'or') {
288           // Lower-case "or" instead of "OR" is a warning condition.
289           $this->status |= SearchQuery::LOWER_CASE_OR;
290         }
291         if ($in_or) {
292           // Add to last element (which is an array).
293           $this->keys['positive'][count($this->keys['positive']) - 1] = array_merge($this->keys['positive'][count($this->keys['positive']) - 1], $words);
294         }
295         else {
296           $this->keys['positive'] = array_merge($this->keys['positive'], $words);
297           $and_count++;
298         }
299       }
300       $in_or = FALSE;
301     }
302
303     // Convert keywords into SQL statements.
304     $has_and = FALSE;
305     $has_or = FALSE;
306     // Positive matches.
307     foreach ($this->keys['positive'] as $key) {
308       // Group of ORed terms.
309       if (is_array($key) && count($key)) {
310         // If we had already found one OR, this is another one AND-ed with the
311         // first, meaning it is not a simple query.
312         if ($has_or) {
313           $this->simple = FALSE;
314         }
315         $has_or = TRUE;
316         $has_new_scores = FALSE;
317         $queryor = new Condition('OR');
318         foreach ($key as $or) {
319           list($num_new_scores) = $this->parseWord($or);
320           $has_new_scores |= $num_new_scores;
321           $queryor->condition('d.data', "% $or %", 'LIKE');
322         }
323         if (count($queryor)) {
324           $this->conditions->condition($queryor);
325           // A group of OR keywords only needs to match once.
326           $this->matches += ($has_new_scores > 0);
327         }
328       }
329       // Single ANDed term.
330       else {
331         $has_and = TRUE;
332         list($num_new_scores, $num_valid_words) = $this->parseWord($key);
333         $this->conditions->condition('d.data', "% $key %", 'LIKE');
334         if (!$num_valid_words) {
335           $this->simple = FALSE;
336         }
337         // Each AND keyword needs to match at least once.
338         $this->matches += $num_new_scores;
339       }
340     }
341     if ($has_and && $has_or) {
342       $this->simple = FALSE;
343     }
344
345     // Negative matches.
346     foreach ($this->keys['negative'] as $key) {
347       $this->conditions->condition('d.data', "% $key %", 'NOT LIKE');
348       $this->simple = FALSE;
349     }
350   }
351
352   /**
353    * Parses a word or phrase for parseQuery().
354    *
355    * Splits a phrase into words. Adds its words to $this->words, if it is not
356    * already there. Returns a list containing the number of new words found,
357    * and the total number of words in the phrase.
358    */
359   protected function parseWord($word) {
360     $num_new_scores = 0;
361     $num_valid_words = 0;
362
363     // Determine the scorewords of this word/phrase.
364     $split = explode(' ', $word);
365     foreach ($split as $s) {
366       $num = is_numeric($s);
367       if ($num || Unicode::strlen($s) >= \Drupal::config('search.settings')->get('index.minimum_word_size')) {
368         if (!isset($this->words[$s])) {
369           $this->words[$s] = $s;
370           $num_new_scores++;
371         }
372         $num_valid_words++;
373       }
374     }
375
376     // Return matching snippet and number of added words.
377     return [$num_new_scores, $num_valid_words];
378   }
379
380   /**
381    * Prepares the query and calculates the normalization factor.
382    *
383    * After the query is normalized the keywords are weighted to give the results
384    * a relevancy score. The query is ready for execution after this.
385    *
386    * Error and warning conditions can apply. Call getStatus() after calling
387    * this method to retrieve them.
388    *
389    * @return bool
390    *   TRUE if at least one keyword matched the search index; FALSE if not.
391    */
392   public function prepareAndNormalize() {
393     $this->parseSearchExpression();
394     $this->executedPrepare = TRUE;
395
396     if (count($this->words) == 0) {
397       // Although the query could proceed, there is no point in joining
398       // with other tables and attempting to normalize if there are no
399       // keywords present.
400       $this->status |= SearchQuery::NO_POSITIVE_KEYWORDS;
401       return FALSE;
402     }
403
404     // Build the basic search query: match the entered keywords.
405     $or = new Condition('OR');
406     foreach ($this->words as $word) {
407       $or->condition('i.word', $word);
408     }
409     $this->condition($or);
410
411     // Add keyword normalization information to the query.
412     $this->join('search_total', 't', 'i.word = t.word');
413     $this
414       ->condition('i.type', $this->type)
415       ->groupBy('i.type')
416       ->groupBy('i.sid');
417
418     // If the query is simple, we should have calculated the number of
419     // matching words we need to find, so impose that criterion. For non-
420     // simple queries, this condition could lead to incorrectly deciding not
421     // to continue with the full query.
422     if ($this->simple) {
423       $this->having('COUNT(*) >= :matches', [':matches' => $this->matches]);
424     }
425
426     // Clone the query object to calculate normalization.
427     $normalize_query = clone $this->query;
428
429     // For complex search queries, add the LIKE conditions; if the query is
430     // simple, we do not need them for normalization.
431     if (!$this->simple) {
432       $normalize_query->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type AND i.langcode = d.langcode');
433       if (count($this->conditions)) {
434         $normalize_query->condition($this->conditions);
435       }
436     }
437
438     // Calculate normalization, which is the max of all the search scores for
439     // positive keywords in the query. And note that the query could have other
440     // fields added to it by the user of this extension.
441     $normalize_query->addExpression('SUM(i.score * t.count)', 'calculated_score');
442     $result = $normalize_query
443       ->range(0, 1)
444       ->orderBy('calculated_score', 'DESC')
445       ->execute()
446       ->fetchObject();
447     if (isset($result->calculated_score)) {
448       $this->normalize = (float) $result->calculated_score;
449     }
450
451     if ($this->normalize) {
452       return TRUE;
453     }
454
455     // If the normalization value was zero, that indicates there were no
456     // matches to the supplied positive keywords.
457     $this->status |= SearchQuery::NO_KEYWORD_MATCHES;
458     return FALSE;
459   }
460
461   /**
462    * {@inheritdoc}
463    */
464   public function preExecute(SelectInterface $query = NULL) {
465     if (!$this->executedPrepare) {
466       $this->prepareAndNormalize();
467     }
468
469     if (!$this->normalize) {
470       return FALSE;
471     }
472
473     return parent::preExecute($query);
474   }
475
476   /**
477    * Adds a custom score expression to the search query.
478    *
479    * Score expressions are used to order search results. If no calls to
480    * addScore() have taken place, a default keyword relevance score will be
481    * used. However, if at least one call to addScore() has taken place, the
482    * keyword relevance score is not automatically added.
483    *
484    * Note that you must use this method to add ordering to your searches, and
485    * not call orderBy() directly, when using the SearchQuery extender. This is
486    * because of the two-pass system the SearchQuery class uses to normalize
487    * scores.
488    *
489    * @param string $score
490    *   The score expression, which should evaluate to a number between 0 and 1.
491    *   The string 'i.relevance' in a score expression will be replaced by a
492    *   measure of keyword relevance between 0 and 1.
493    * @param array $arguments
494    *   Query arguments needed to provide values to the score expression.
495    * @param float $multiply
496    *   If set, the score is multiplied with this value. However, all scores
497    *   with multipliers are then divided by the total of all multipliers, so
498    *   that overall, the normalization is maintained.
499    *
500    * @return $this
501    */
502   public function addScore($score, $arguments = [], $multiply = FALSE) {
503     if ($multiply) {
504       $i = count($this->multiply);
505       // Modify the score expression so it is multiplied by the multiplier,
506       // with a divisor to renormalize. Note that the ROUND here is necessary
507       // for PostgreSQL and SQLite in order to ensure that the :multiply_* and
508       // :total_* arguments are treated as a numeric type, because the
509       // PostgreSQL PDO driver sometimes puts values in as strings instead of
510       // numbers in complex expressions like this.
511       $score = "(ROUND(:multiply_$i, 4)) * COALESCE(($score), 0) / (ROUND(:total_$i, 4))";
512       // Add an argument for the multiplier. The :total_$i argument is taken
513       // care of in the execute() method, which is when the total divisor is
514       // calculated.
515       $arguments[':multiply_' . $i] = $multiply;
516       $this->multiply[] = $multiply;
517     }
518
519     // Search scoring needs a way to include a keyword relevance in the score.
520     // For historical reasons, this is done by putting 'i.relevance' into the
521     // search expression. So, use string replacement to change this to a
522     // calculated query expression, counting the number of occurrences so
523     // in the execute() method we can add arguments.
524     while (($pos = strpos($score, 'i.relevance')) !== FALSE) {
525       $pieces = explode('i.relevance', $score, 2);
526       $score = implode('((ROUND(:normalization_' . $this->relevance_count . ', 4)) * i.score * t.count)', $pieces);
527       $this->relevance_count++;
528     }
529
530     $this->scores[] = $score;
531     $this->scoresArguments += $arguments;
532
533     return $this;
534   }
535
536   /**
537    * Executes the search.
538    *
539    * The complex conditions are applied to the query including score
540    * expressions and ordering.
541    *
542    * Error and warning conditions can apply. Call getStatus() after calling
543    * this method to retrieve them.
544    *
545    * @return \Drupal\Core\Database\StatementInterface|null
546    *   A query result set containing the results of the query.
547    */
548   public function execute() {
549     if (!$this->preExecute($this)) {
550       return NULL;
551     }
552
553     // Add conditions to the query.
554     $this->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type AND i.langcode = d.langcode');
555     if (count($this->conditions)) {
556       $this->condition($this->conditions);
557     }
558
559     // Add default score (keyword relevance) if there are not any defined.
560     if (empty($this->scores)) {
561       $this->addScore('i.relevance');
562     }
563
564     if (count($this->multiply)) {
565       // Re-normalize scores with multipliers by dividing by the total of all
566       // multipliers. The expressions were altered in addScore(), so here just
567       // add the arguments for the total.
568       $sum = array_sum($this->multiply);
569       for ($i = 0; $i < count($this->multiply); $i++) {
570         $this->scoresArguments[':total_' . $i] = $sum;
571       }
572     }
573
574     // Add arguments for the keyword relevance normalization number.
575     $normalization = 1.0 / $this->normalize;
576     for ($i = 0; $i < $this->relevance_count; $i++) {
577       $this->scoresArguments[':normalization_' . $i] = $normalization;
578     }
579
580     // Add all scores together to form a query field.
581     $this->addExpression('SUM(' . implode(' + ', $this->scores) . ')', 'calculated_score', $this->scoresArguments);
582
583     // If an order has not yet been set for this query, add a default order
584     // that sorts by the calculated sum of scores.
585     if (count($this->getOrderBy()) == 0) {
586       $this->orderBy('calculated_score', 'DESC');
587     }
588
589     // Add query metadata.
590     $this
591       ->addMetaData('normalize', $this->normalize)
592       ->fields('i', ['type', 'sid']);
593     return $this->query->execute();
594   }
595
596   /**
597    * Builds the default count query for SearchQuery.
598    *
599    * Since SearchQuery always uses GROUP BY, we can default to a subquery. We
600    * also add the same conditions as execute() because countQuery() is called
601    * first.
602    */
603   public function countQuery() {
604     if (!$this->executedPrepare) {
605       $this->prepareAndNormalize();
606     }
607
608     // Clone the inner query.
609     $inner = clone $this->query;
610
611     // Add conditions to query.
612     $inner->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type');
613     if (count($this->conditions)) {
614       $inner->condition($this->conditions);
615     }
616
617     // Remove existing fields and expressions, they are not needed for a count
618     // query.
619     $fields =& $inner->getFields();
620     $fields = [];
621     $expressions =& $inner->getExpressions();
622     $expressions = [];
623
624     // Add sid as the only field and count them as a subquery.
625     $count = db_select($inner->fields('i', ['sid']), NULL, ['target' => 'replica']);
626
627     // Add the COUNT() expression.
628     $count->addExpression('COUNT(*)');
629
630     return $count;
631   }
632
633   /**
634    * Returns the query status bitmap.
635    *
636    * @return int
637    *   A bitmap indicating query status. Zero indicates there were no problems.
638    *   A non-zero value is a combination of one or more of the following flags:
639    *   - SearchQuery::NO_POSITIVE_KEYWORDS
640    *   - SearchQuery::EXPRESSIONS_IGNORED
641    *   - SearchQuery::LOWER_CASE_OR
642    *   - SearchQuery::NO_KEYWORD_MATCHES
643    */
644   public function getStatus() {
645     return $this->status;
646   }
647
648 }