search.extender.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /**
  3. * @file
  4. * Search query extender and helper functions.
  5. */
  6. /**
  7. * Do a query on the full-text search index for a word or words.
  8. *
  9. * This function is normally only called by each module that supports the
  10. * indexed search (and thus, implements hook_update_index()).
  11. *
  12. * Results are retrieved in two logical passes. However, the two passes are
  13. * joined together into a single query. And in the case of most simple
  14. * queries the second pass is not even used.
  15. *
  16. * The first pass selects a set of all possible matches, which has the benefit
  17. * of also providing the exact result set for simple "AND" or "OR" searches.
  18. *
  19. * The second portion of the query further refines this set by verifying
  20. * advanced text conditions (such as negative or phrase matches).
  21. *
  22. * The used query object has the tag 'search_$module' and can be further
  23. * extended with hook_query_alter().
  24. */
  25. class SearchQuery extends SelectQueryExtender {
  26. /**
  27. * The search query that is used for searching.
  28. *
  29. * @var string
  30. */
  31. protected $searchExpression;
  32. /**
  33. * Type of search (search module).
  34. *
  35. * This maps to the value of the type column in search_index, and is equal
  36. * to the machine-readable name of the module that implements
  37. * hook_search_info().
  38. *
  39. * @var string
  40. */
  41. protected $type;
  42. /**
  43. * Positive and negative search keys.
  44. *
  45. * @var array
  46. */
  47. protected $keys = array('positive' => array(), 'negative' => array());
  48. /**
  49. * Indicates whether the first pass query requires complex conditions (LIKE).
  50. *
  51. * @var boolean.
  52. */
  53. protected $simple = TRUE;
  54. /**
  55. * Conditions that are used for exact searches.
  56. *
  57. * This is always used for the second pass query but not for the first pass,
  58. * unless $this->simple is FALSE.
  59. *
  60. * @var DatabaseCondition
  61. */
  62. protected $conditions;
  63. /**
  64. * Indicates how many matches for a search query are necessary.
  65. *
  66. * @var int
  67. */
  68. protected $matches = 0;
  69. /**
  70. * Array of search words.
  71. *
  72. * These words have to match against {search_index}.word.
  73. *
  74. * @var array
  75. */
  76. protected $words = array();
  77. /**
  78. * Multiplier for the normalized search score.
  79. *
  80. * This value is calculated by the first pass query and multiplied with the
  81. * actual score of a specific word to make sure that the resulting calculated
  82. * score is between 0 and 1.
  83. *
  84. * @var float
  85. */
  86. protected $normalize;
  87. /**
  88. * Indicates whether the first pass query has been executed.
  89. *
  90. * @var boolean
  91. */
  92. protected $executedFirstPass = FALSE;
  93. /**
  94. * Stores score expressions.
  95. *
  96. * @var array
  97. *
  98. * @see addScore()
  99. */
  100. protected $scores = array();
  101. /**
  102. * Stores arguments for score expressions.
  103. *
  104. * @var array
  105. */
  106. protected $scoresArguments = array();
  107. /**
  108. * Stores multipliers for score expressions.
  109. *
  110. * @var array
  111. */
  112. protected $multiply = array();
  113. /**
  114. * Whether or not search expressions were ignored.
  115. *
  116. * The maximum number of AND/OR combinations exceeded can be configured to
  117. * avoid Denial-of-Service attacks. Expressions beyond the limit are ignored.
  118. *
  119. * @var boolean
  120. */
  121. protected $expressionsIgnored = FALSE;
  122. /**
  123. * Sets up the search query expression.
  124. *
  125. * @param $query
  126. * A search query string, which can contain options.
  127. * @param $module
  128. * The search module. This maps to {search_index}.type in the database.
  129. *
  130. * @return
  131. * The SearchQuery object.
  132. */
  133. public function searchExpression($expression, $module) {
  134. $this->searchExpression = $expression;
  135. $this->type = $module;
  136. return $this;
  137. }
  138. /**
  139. * Applies a search option and removes it from the search query string.
  140. *
  141. * These options are in the form option:value,value2,value3.
  142. *
  143. * @param $option
  144. * Name of the option.
  145. * @param $column
  146. * Name of the database column to which the value should be applied.
  147. *
  148. * @return
  149. * TRUE if a value for that option was found, FALSE if not.
  150. */
  151. public function setOption($option, $column) {
  152. if ($values = search_expression_extract($this->searchExpression, $option)) {
  153. $or = db_or();
  154. foreach (explode(',', $values) as $value) {
  155. $or->condition($column, $value);
  156. }
  157. $this->condition($or);
  158. $this->searchExpression = search_expression_insert($this->searchExpression, $option);
  159. return TRUE;
  160. }
  161. return FALSE;
  162. }
  163. /**
  164. * Parses the search query into SQL conditions.
  165. *
  166. * We build two queries that match the dataset bodies.
  167. */
  168. protected function parseSearchExpression() {
  169. // Matchs words optionally prefixed by a dash. A word in this case is
  170. // something between two spaces, optionally quoted.
  171. preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->searchExpression , $keywords, PREG_SET_ORDER);
  172. if (count($keywords) == 0) {
  173. return;
  174. }
  175. // Classify tokens.
  176. $or = FALSE;
  177. $warning = '';
  178. $limit_combinations = variable_get('search_and_or_limit', 7);
  179. // The first search expression does not count as AND.
  180. $and_count = -1;
  181. $or_count = 0;
  182. foreach ($keywords as $match) {
  183. if ($or_count && $and_count + $or_count >= $limit_combinations) {
  184. // Ignore all further search expressions to prevent Denial-of-Service
  185. // attacks using a high number of AND/OR combinations.
  186. $this->expressionsIgnored = TRUE;
  187. break;
  188. }
  189. $phrase = FALSE;
  190. // Strip off phrase quotes.
  191. if ($match[2]{0} == '"') {
  192. $match[2] = substr($match[2], 1, -1);
  193. $phrase = TRUE;
  194. $this->simple = FALSE;
  195. }
  196. // Simplify keyword according to indexing rules and external
  197. // preprocessors. Use same process as during search indexing, so it
  198. // will match search index.
  199. $words = search_simplify($match[2]);
  200. // Re-explode in case simplification added more words, except when
  201. // matching a phrase.
  202. $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
  203. // Negative matches.
  204. if ($match[1] == '-') {
  205. $this->keys['negative'] = array_merge($this->keys['negative'], $words);
  206. }
  207. // OR operator: instead of a single keyword, we store an array of all
  208. // OR'd keywords.
  209. elseif ($match[2] == 'OR' && count($this->keys['positive'])) {
  210. $last = array_pop($this->keys['positive']);
  211. // Starting a new OR?
  212. if (!is_array($last)) {
  213. $last = array($last);
  214. }
  215. $this->keys['positive'][] = $last;
  216. $or = TRUE;
  217. $or_count++;
  218. continue;
  219. }
  220. // AND operator: implied, so just ignore it.
  221. elseif ($match[2] == 'AND' || $match[2] == 'and') {
  222. $warning = $match[2];
  223. continue;
  224. }
  225. // Plain keyword.
  226. else {
  227. if ($match[2] == 'or') {
  228. $warning = $match[2];
  229. }
  230. if ($or) {
  231. // Add to last element (which is an array).
  232. $this->keys['positive'][count($this->keys['positive']) - 1] = array_merge($this->keys['positive'][count($this->keys['positive']) - 1], $words);
  233. }
  234. else {
  235. $this->keys['positive'] = array_merge($this->keys['positive'], $words);
  236. $and_count++;
  237. }
  238. }
  239. $or = FALSE;
  240. }
  241. // Convert keywords into SQL statements.
  242. $this->conditions = db_and();
  243. $simple_and = FALSE;
  244. $simple_or = FALSE;
  245. // Positive matches.
  246. foreach ($this->keys['positive'] as $key) {
  247. // Group of ORed terms.
  248. if (is_array($key) && count($key)) {
  249. $simple_or = TRUE;
  250. $any = FALSE;
  251. $queryor = db_or();
  252. foreach ($key as $or) {
  253. list($num_new_scores) = $this->parseWord($or);
  254. $any |= $num_new_scores;
  255. $queryor->condition('d.data', "% $or %", 'LIKE');
  256. }
  257. if (count($queryor)) {
  258. $this->conditions->condition($queryor);
  259. // A group of OR keywords only needs to match once.
  260. $this->matches += ($any > 0);
  261. }
  262. }
  263. // Single ANDed term.
  264. else {
  265. $simple_and = TRUE;
  266. list($num_new_scores, $num_valid_words) = $this->parseWord($key);
  267. $this->conditions->condition('d.data', "% $key %", 'LIKE');
  268. if (!$num_valid_words) {
  269. $this->simple = FALSE;
  270. }
  271. // Each AND keyword needs to match at least once.
  272. $this->matches += $num_new_scores;
  273. }
  274. }
  275. if ($simple_and && $simple_or) {
  276. $this->simple = FALSE;
  277. }
  278. // Negative matches.
  279. foreach ($this->keys['negative'] as $key) {
  280. $this->conditions->condition('d.data', "% $key %", 'NOT LIKE');
  281. $this->simple = FALSE;
  282. }
  283. if ($warning == 'or') {
  284. drupal_set_message(t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'));
  285. }
  286. }
  287. /**
  288. * Helper function for parseQuery().
  289. */
  290. protected function parseWord($word) {
  291. $num_new_scores = 0;
  292. $num_valid_words = 0;
  293. // Determine the scorewords of this word/phrase.
  294. $split = explode(' ', $word);
  295. foreach ($split as $s) {
  296. $num = is_numeric($s);
  297. if ($num || drupal_strlen($s) >= variable_get('minimum_word_size', 3)) {
  298. if (!isset($this->words[$s])) {
  299. $this->words[$s] = $s;
  300. $num_new_scores++;
  301. }
  302. $num_valid_words++;
  303. }
  304. }
  305. // Return matching snippet and number of added words.
  306. return array($num_new_scores, $num_valid_words);
  307. }
  308. /**
  309. * Executes the first pass query.
  310. *
  311. * This can either be done explicitly, so that additional scores and
  312. * conditions can be applied to the second pass query, or implicitly by
  313. * addScore() or execute().
  314. *
  315. * @return
  316. * TRUE if search items exist, FALSE if not.
  317. */
  318. public function executeFirstPass() {
  319. $this->parseSearchExpression();
  320. if (count($this->words) == 0) {
  321. form_set_error('keys', format_plural(variable_get('minimum_word_size', 3), 'You must include at least one positive keyword with 1 character or more.', 'You must include at least one positive keyword with @count characters or more.'));
  322. return FALSE;
  323. }
  324. if ($this->expressionsIgnored) {
  325. drupal_set_message(t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', array('@count' => variable_get('search_and_or_limit', 7))), 'warning');
  326. }
  327. $this->executedFirstPass = TRUE;
  328. if (!empty($this->words)) {
  329. $or = db_or();
  330. foreach ($this->words as $word) {
  331. $or->condition('i.word', $word);
  332. }
  333. $this->condition($or);
  334. }
  335. // Build query for keyword normalization.
  336. $this->join('search_total', 't', 'i.word = t.word');
  337. $this
  338. ->condition('i.type', $this->type)
  339. ->groupBy('i.type')
  340. ->groupBy('i.sid')
  341. ->having('COUNT(*) >= :matches', array(':matches' => $this->matches));
  342. // Clone the query object to do the firstPass query;
  343. $first = clone $this->query;
  344. // For complex search queries, add the LIKE conditions to the first pass query.
  345. if (!$this->simple) {
  346. $first->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type');
  347. $first->condition($this->conditions);
  348. }
  349. // Calculate maximum keyword relevance, to normalize it.
  350. $first->addExpression('SUM(i.score * t.count)', 'calculated_score');
  351. $this->normalize = $first
  352. ->range(0, 1)
  353. ->orderBy('calculated_score', 'DESC')
  354. ->execute()
  355. ->fetchField();
  356. if ($this->normalize) {
  357. return TRUE;
  358. }
  359. return FALSE;
  360. }
  361. /**
  362. * Adds a custom score expression to the search query.
  363. *
  364. * Score expressions are used to order search results. If no calls to
  365. * addScore() have taken place, a default keyword relevance score will be
  366. * used. However, if at least one call to addScore() has taken place, the
  367. * keyword relevance score is not automatically added.
  368. *
  369. * Also note that if you call orderBy() directly on the query, search scores
  370. * will not automatically be used to order search results. Your orderBy()
  371. * expression can reference 'calculated_score', which will be the total
  372. * calculated score value.
  373. *
  374. * @param $score
  375. * The score expression, which should evaluate to a number between 0 and 1.
  376. * The string 'i.relevance' in a score expression will be replaced by a
  377. * measure of keyword relevance between 0 and 1.
  378. * @param $arguments
  379. * Query arguments needed to provide values to the score expression.
  380. * @param $multiply
  381. * If set, the score is multiplied with this value. However, all scores
  382. * with multipliers are then divided by the total of all multipliers, so
  383. * that overall, the normalization is maintained.
  384. *
  385. * @return object
  386. * The updated query object.
  387. */
  388. public function addScore($score, $arguments = array(), $multiply = FALSE) {
  389. if ($multiply) {
  390. $i = count($this->multiply);
  391. // Modify the score expression so it is multiplied by the multiplier,
  392. // with a divisor to renormalize.
  393. $score = "CAST(:multiply_$i AS DECIMAL) * COALESCE(( " . $score . "), 0) / CAST(:total_$i AS DECIMAL)";
  394. // Add an argument for the multiplier. The :total_$i argument is taken
  395. // care of in the execute() method, which is when the total divisor is
  396. // calculated.
  397. $arguments[':multiply_' . $i] = $multiply;
  398. $this->multiply[] = $multiply;
  399. }
  400. $this->scores[] = $score;
  401. $this->scoresArguments += $arguments;
  402. return $this;
  403. }
  404. /**
  405. * Executes the search.
  406. *
  407. * If not already done, this executes the first pass query. Then the complex
  408. * conditions are applied to the query including score expressions and
  409. * ordering.
  410. *
  411. * @return
  412. * FALSE if the first pass query returned no results, and a database result
  413. * set if there were results.
  414. */
  415. public function execute()
  416. {
  417. if (!$this->executedFirstPass) {
  418. $this->executeFirstPass();
  419. }
  420. if (!$this->normalize) {
  421. return new DatabaseStatementEmpty();
  422. }
  423. // Add conditions to query.
  424. $this->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type');
  425. $this->condition($this->conditions);
  426. if (empty($this->scores)) {
  427. // Add default score.
  428. $this->addScore('i.relevance');
  429. }
  430. if (count($this->multiply)) {
  431. // Re-normalize scores with multipliers by dividing by the total of all
  432. // multipliers. The expressions were altered in addScore(), so here just
  433. // add the arguments for the total.
  434. $i = 0;
  435. $sum = array_sum($this->multiply);
  436. foreach ($this->multiply as $total) {
  437. $this->scoresArguments[':total_' . $i] = $sum;
  438. $i++;
  439. }
  440. }
  441. // Replace the pseudo-expression 'i.relevance' with a measure of keyword
  442. // relevance in all score expressions, using string replacement. Careful
  443. // though! If you just print out a float, some locales use ',' as the
  444. // decimal separator in PHP, while SQL always uses '.'. So, make sure to
  445. // set the number format correctly.
  446. $relevance = number_format((1.0 / $this->normalize), 10, '.', '');
  447. $this->scores = str_replace('i.relevance', '(' . $relevance . ' * i.score * t.count)', $this->scores);
  448. // Add all scores together to form a query field.
  449. $this->addExpression('SUM(' . implode(' + ', $this->scores) . ')', 'calculated_score', $this->scoresArguments);
  450. // If an order has not yet been set for this query, add a default order
  451. // that sorts by the calculated sum of scores.
  452. if (count($this->getOrderBy()) == 0) {
  453. $this->orderBy('calculated_score', 'DESC');
  454. }
  455. // Add tag and useful metadata.
  456. $this
  457. ->addTag('search_' . $this->type)
  458. ->addMetaData('normalize', $this->normalize)
  459. ->fields('i', array('type', 'sid'));
  460. return $this->query->execute();
  461. }
  462. /**
  463. * Builds the default count query for SearchQuery.
  464. *
  465. * Since SearchQuery always uses GROUP BY, we can default to a subquery. We
  466. * also add the same conditions as execute() because countQuery() is called
  467. * first.
  468. */
  469. public function countQuery() {
  470. // Clone the inner query.
  471. $inner = clone $this->query;
  472. // Add conditions to query.
  473. $inner->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type');
  474. $inner->condition($this->conditions);
  475. // Remove existing fields and expressions, they are not needed for a count
  476. // query.
  477. $fields =& $inner->getFields();
  478. $fields = array();
  479. $expressions =& $inner->getExpressions();
  480. $expressions = array();
  481. // Add the sid as the only field and count them as a subquery.
  482. $count = db_select($inner->fields('i', array('sid')), NULL, array('target' => 'slave'));
  483. // Add the COUNT() expression.
  484. $count->addExpression('COUNT(*)');
  485. return $count;
  486. }
  487. }