query.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiViewsQuery.
  5. */
  6. /**
  7. * Views query class using a Search API index as the data source.
  8. */
  9. class SearchApiViewsQuery extends views_plugin_query {
  10. /**
  11. * Number of results to display.
  12. *
  13. * @var int
  14. */
  15. protected $limit;
  16. /**
  17. * Offset of first displayed result.
  18. *
  19. * @var int
  20. */
  21. protected $offset;
  22. /**
  23. * The index this view accesses.
  24. *
  25. * @var SearchApiIndex
  26. */
  27. protected $index;
  28. /**
  29. * The query that will be executed.
  30. *
  31. * @var SearchApiQueryInterface
  32. */
  33. protected $query;
  34. /**
  35. * The results returned by the query, after it was executed.
  36. *
  37. * @var array
  38. */
  39. protected $search_api_results = array();
  40. /**
  41. * Array of all encountered errors.
  42. *
  43. * Each of these is fatal, meaning that a non-empty $errors property will
  44. * result in an empty result being returned.
  45. *
  46. * @var array
  47. */
  48. protected $errors;
  49. /**
  50. * Whether to abort the search instead of executing it.
  51. *
  52. * @var bool
  53. */
  54. protected $abort = FALSE;
  55. /**
  56. * The names of all fields whose value is required by a handler.
  57. *
  58. * The format follows the same as Search API field identifiers (parent:child).
  59. *
  60. * @var array
  61. */
  62. protected $fields;
  63. /**
  64. * The query's sub-filters representing the different Views filter groups.
  65. *
  66. * @var array
  67. */
  68. protected $filters = array();
  69. /**
  70. * The conjunction with which multiple filter groups are combined.
  71. *
  72. * @var string
  73. */
  74. public $group_operator = 'AND';
  75. /**
  76. * Create the basic query object and fill with default values.
  77. */
  78. public function init($base_table, $base_field, $options) {
  79. try {
  80. $this->errors = array();
  81. parent::init($base_table, $base_field, $options);
  82. $this->fields = array();
  83. if (substr($base_table, 0, 17) == 'search_api_index_') {
  84. $id = substr($base_table, 17);
  85. $this->index = search_api_index_load($id);
  86. $this->query = $this->index->query(array(
  87. 'parse mode' => $this->options['parse_mode'],
  88. ));
  89. }
  90. }
  91. catch (Exception $e) {
  92. $this->errors[] = $e->getMessage();
  93. }
  94. }
  95. /**
  96. * Add a field that should be retrieved from the results by this view.
  97. *
  98. * @param $field
  99. * The field's identifier, as used by the Search API. E.g., "title" for a
  100. * node's title, "author:name" for a node's author's name.
  101. *
  102. * @return SearchApiViewsQuery
  103. * The called object.
  104. */
  105. public function addField($field) {
  106. $this->fields[$field] = TRUE;
  107. return $field;
  108. }
  109. /**
  110. * Add a sort to the query.
  111. *
  112. * @param $selector
  113. * The field to sort on. All indexed fields of the index are valid values.
  114. * In addition, the special fields 'search_api_relevance' (sort by
  115. * relevance) and 'search_api_id' (sort by item id) may be used.
  116. * @param $order
  117. * The order to sort items in - either 'ASC' or 'DESC'. Defaults to 'ASC'.
  118. */
  119. public function add_selector_orderby($selector, $order = 'ASC') {
  120. $this->query->sort($selector, $order);
  121. }
  122. /**
  123. * Defines the options used by this query plugin.
  124. *
  125. * Adds some access options.
  126. */
  127. public function option_definition() {
  128. return parent::option_definition() + array(
  129. 'search_api_bypass_access' => array(
  130. 'default' => FALSE,
  131. ),
  132. 'entity_access' => array(
  133. 'default' => FALSE,
  134. ),
  135. 'parse_mode' => array(
  136. 'default' => 'terms',
  137. ),
  138. );
  139. }
  140. /**
  141. * Add settings for the UI.
  142. *
  143. * Adds an option for bypassing access checks.
  144. */
  145. public function options_form(&$form, &$form_state) {
  146. parent::options_form($form, $form_state);
  147. $form['search_api_bypass_access'] = array(
  148. '#type' => 'checkbox',
  149. '#title' => t('Bypass access checks'),
  150. '#description' => t('If the underlying search index has access checks enabled, this option allows to disable them for this view.'),
  151. '#default_value' => $this->options['search_api_bypass_access'],
  152. );
  153. if ($this->index->getEntityType()) {
  154. $form['entity_access'] = array(
  155. '#type' => 'checkbox',
  156. '#title' => t('Additional access checks on result entities'),
  157. '#description' => t("Execute an access check for all result entities. This prevents users from seeing inappropriate content when the index contains stale data, or doesn't provide access checks. However, result counts, paging and other things won't work correctly if results are eliminated in this way, so only use this as a last ressort (and in addition to other checks, if possible)."),
  158. '#default_value' => $this->options['entity_access'],
  159. );
  160. }
  161. $form['parse_mode'] = array(
  162. '#type' => 'select',
  163. '#title' => t('Parse mode'),
  164. '#description' => t('Choose how the search keys will be parsed.'),
  165. '#options' => array(),
  166. '#default_value' => $this->options['parse_mode'],
  167. );
  168. foreach ($this->query->parseModes() as $key => $mode) {
  169. $form['parse_mode']['#options'][$key] = $mode['name'];
  170. if (!empty($mode['description'])) {
  171. $states['visible'][':input[name="query[options][parse_mode]"]']['value'] = $key;
  172. $form["parse_mode_{$key}_description"] = array(
  173. '#type' => 'item',
  174. '#title' => $mode['name'],
  175. '#description' => $mode['description'],
  176. '#states' => $states,
  177. );
  178. }
  179. }
  180. }
  181. /**
  182. * Builds the necessary info to execute the query.
  183. */
  184. public function build(&$view) {
  185. $this->view = $view;
  186. // Setup the nested filter structure for this query.
  187. if (!empty($this->where)) {
  188. // If the different groups are combined with the OR operator, we have to
  189. // add a new OR filter to the query to which the filters for the groups
  190. // will be added.
  191. if ($this->group_operator === 'OR') {
  192. $base = $this->query->createFilter('OR');
  193. $this->query->filter($base);
  194. }
  195. else {
  196. $base = $this->query;
  197. }
  198. // Add a nested filter for each filter group, with its set conjunction.
  199. foreach ($this->where as $group_id => $group) {
  200. if (!empty($group['conditions']) || !empty($group['filters'])) {
  201. $group += array('type' => 'AND');
  202. // For filters without a group, we want to always add them directly to
  203. // the query.
  204. $filter = ($group_id === '') ? $this->query : $this->query->createFilter($group['type']);
  205. if (!empty($group['conditions'])) {
  206. foreach ($group['conditions'] as $condition) {
  207. list($field, $value, $operator) = $condition;
  208. $filter->condition($field, $value, $operator);
  209. }
  210. }
  211. if (!empty($group['filters'])) {
  212. foreach ($group['filters'] as $nested_filter) {
  213. $filter->filter($nested_filter);
  214. }
  215. }
  216. // If no group was given, the filters were already set on the query.
  217. if ($group_id !== '') {
  218. $base->filter($filter);
  219. }
  220. }
  221. }
  222. }
  223. // Initialize the pager and let it modify the query to add limits.
  224. $view->init_pager();
  225. $this->pager->query();
  226. // Set the search ID, if it was not already set.
  227. if ($this->query->getOption('search id') == get_class($this->query)) {
  228. $this->query->setOption('search id', 'search_api_views:' . $view->name . ':' . $view->current_display);
  229. }
  230. // Add the "search_api_bypass_access" option to the query, if desired.
  231. if (!empty($this->options['search_api_bypass_access'])) {
  232. $this->query->setOption('search_api_bypass_access', TRUE);
  233. }
  234. // If the View and the Panel conspire to provide an overridden path then
  235. // pass that through as the base path.
  236. if (!empty($this->view->override_path) && strpos(current_path(), $this->view->override_path) !== 0) {
  237. $this->query->setOption('search_api_base_path', $this->view->override_path);
  238. }
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. public function alter(&$view) {
  244. parent::alter($view);
  245. drupal_alter('search_api_views_query', $view, $this);
  246. }
  247. /**
  248. * Executes the query and fills the associated view object with according
  249. * values.
  250. *
  251. * Values to set: $view->result, $view->total_rows, $view->execute_time,
  252. * $view->pager['current_page'].
  253. */
  254. public function execute(&$view) {
  255. if ($this->errors || $this->abort) {
  256. if (error_displayable()) {
  257. foreach ($this->errors as $msg) {
  258. drupal_set_message(check_plain($msg), 'error');
  259. }
  260. }
  261. $view->result = array();
  262. $view->total_rows = 0;
  263. $view->execute_time = 0;
  264. return;
  265. }
  266. // Calculate the "skip result count" option, if it wasn't already set to
  267. // FALSE.
  268. $skip_result_count = $this->query->getOption('skip result count', TRUE);
  269. if ($skip_result_count) {
  270. $skip_result_count = !$this->pager->use_count_query() && empty($view->get_total_rows);
  271. $this->query->setOption('skip result count', $skip_result_count);
  272. }
  273. try {
  274. // Trigger pager pre_execute().
  275. $this->pager->pre_execute($this->query);
  276. // Views passes sometimes NULL and sometimes the integer 0 for "All" in a
  277. // pager. If set to 0 items, a string "0" is passed. Therefore, we unset
  278. // the limit if an empty value OTHER than a string "0" was passed.
  279. if (!$this->limit && $this->limit !== '0') {
  280. $this->limit = NULL;
  281. }
  282. // Set the range. (We always set this, as there might even be an offset if
  283. // all items are shown.)
  284. $this->query->range($this->offset, $this->limit);
  285. $start = microtime(TRUE);
  286. // Execute the search.
  287. $results = $this->query->execute();
  288. $this->search_api_results = $results;
  289. // Store the results.
  290. if (!$skip_result_count) {
  291. $this->pager->total_items = $view->total_rows = $results['result count'];
  292. if (!empty($this->pager->options['offset'])) {
  293. $this->pager->total_items -= $this->pager->options['offset'];
  294. }
  295. $this->pager->update_page_info();
  296. }
  297. $view->result = array();
  298. if (!empty($results['results'])) {
  299. $this->addResults($results['results'], $view);
  300. }
  301. // We shouldn't use $results['performance']['complete'] here, since
  302. // extracting the results probably takes considerable time as well.
  303. $view->execute_time = microtime(TRUE) - $start;
  304. // Trigger pager post_execute().
  305. $this->pager->post_execute($view->result);
  306. }
  307. catch (Exception $e) {
  308. $this->errors[] = $e->getMessage();
  309. // Recursion to get the same error behaviour as above.
  310. $this->execute($view);
  311. }
  312. }
  313. /**
  314. * Aborts this search query.
  315. *
  316. * Used by handlers to flag a fatal error which shouldn't be displayed but
  317. * still lead to the view returning empty and the search not being executed.
  318. *
  319. * @param string|null $msg
  320. * Optionally, a translated, unescaped error message to display.
  321. */
  322. public function abort($msg = NULL) {
  323. if ($msg) {
  324. $this->errors[] = $msg;
  325. }
  326. $this->abort = TRUE;
  327. }
  328. /**
  329. * Helper function for adding results to a view in the format expected by the
  330. * view.
  331. */
  332. protected function addResults(array $results, $view) {
  333. $rows = array();
  334. $missing = array();
  335. $items = array();
  336. // First off, we try to gather as much field values as possible without
  337. // loading any items.
  338. foreach ($results as $id => $result) {
  339. if (!empty($this->options['entity_access']) && ($entity_type = $this->index->getEntityType())) {
  340. $entity = entity_load($entity_type, array($id));
  341. if (!entity_access('view', $entity_type, $entity[$id])) {
  342. continue;
  343. }
  344. }
  345. $row = array();
  346. // Include the loaded item for this result row, if present, or the item
  347. // ID.
  348. if (!empty($result['entity'])) {
  349. $row['entity'] = $result['entity'];
  350. }
  351. else {
  352. $row['entity'] = $id;
  353. }
  354. $row['_entity_properties']['search_api_relevance'] = $result['score'];
  355. $row['_entity_properties']['search_api_excerpt'] = empty($result['excerpt']) ? '' : $result['excerpt'];
  356. // Gather any fields from the search results.
  357. if (!empty($result['fields'])) {
  358. $row['_entity_properties'] += $result['fields'];
  359. }
  360. // Check whether we need to extract any properties from the result item.
  361. $missing_fields = array_diff_key($this->fields, $row);
  362. if ($missing_fields) {
  363. $missing[$id] = $missing_fields;
  364. if (is_object($row['entity'])) {
  365. $items[$id] = $row['entity'];
  366. }
  367. else {
  368. $ids[] = $id;
  369. }
  370. }
  371. // Save the row values for adding them to the Views result afterwards.
  372. $rows[$id] = (object) $row;
  373. }
  374. // Load items of those rows which haven't got all field values, yet.
  375. if (!empty($ids)) {
  376. $items += $this->index->loadItems($ids);
  377. // $items now includes loaded items, and those already passed in the
  378. // search results.
  379. foreach ($items as $id => $item) {
  380. // Extract item properties.
  381. $wrapper = $this->index->entityWrapper($item, FALSE);
  382. $rows[$id]->_entity_properties += $this->extractFields($wrapper, $missing[$id]);
  383. $rows[$id]->entity = $item;
  384. }
  385. }
  386. // Finally, add all rows to the Views result set.
  387. $view->result = array_values($rows);
  388. }
  389. /**
  390. * Helper function for extracting all necessary fields from a result item.
  391. *
  392. * Usually, this method isn't needed anymore as the properties are now
  393. * extracted by the field handlers themselves.
  394. */
  395. protected function extractFields(EntityMetadataWrapper $wrapper, array $all_fields) {
  396. $fields = array();
  397. foreach ($all_fields as $key => $true) {
  398. $fields[$key]['type'] = 'string';
  399. }
  400. $fields = search_api_extract_fields($wrapper, $fields, array('sanitized' => TRUE));
  401. $ret = array();
  402. foreach ($all_fields as $key => $true) {
  403. $ret[$key] = isset($fields[$key]['value']) ? $fields[$key]['value'] : '';
  404. }
  405. return $ret;
  406. }
  407. /**
  408. * Returns the according entity objects for the given query results.
  409. *
  410. * This is necessary to support generic entity handlers and plugins with this
  411. * query backend.
  412. *
  413. * If the current query isn't based on an entity type, the method will return
  414. * an empty array.
  415. */
  416. public function get_result_entities($results, $relationship = NULL, $field = NULL) {
  417. list($type, $wrappers) = $this->get_result_wrappers($results, $relationship, $field);
  418. $return = array();
  419. foreach ($wrappers as $i => $wrapper) {
  420. try {
  421. // Get the entity ID beforehand for possible watchdog messages.
  422. $id = $wrapper->value(array('identifier' => TRUE));
  423. // Only add results that exist.
  424. if ($entity = $wrapper->value()) {
  425. $return[$i] = $entity;
  426. }
  427. else {
  428. watchdog('search_api_views', 'The search index returned a reference to an entity with ID @id, which does not exist in the database. Your index may be out of sync and should be rebuilt.', array('@id' => $id), WATCHDOG_ERROR);
  429. }
  430. }
  431. catch (EntityMetadataWrapperException $e) {
  432. watchdog_exception('search_api_views', $e, "%type while trying to load search result entity with ID @id: !message in %function (line %line of %file).", array('@id' => $id), WATCHDOG_ERROR);
  433. }
  434. }
  435. return array($type, $return);
  436. }
  437. /**
  438. * Returns the according metadata wrappers for the given query results.
  439. *
  440. * This is necessary to support generic entity handlers and plugins with this
  441. * query backend.
  442. */
  443. public function get_result_wrappers($results, $relationship = NULL, $field = NULL) {
  444. $entity_type = $this->index->getEntityType();
  445. $wrappers = array();
  446. $load_entities = array();
  447. foreach ($results as $row_index => $row) {
  448. if ($entity_type && isset($row->entity)) {
  449. // If this entity isn't load, register it for pre-loading.
  450. if (!is_object($row->entity)) {
  451. $load_entities[$row->entity] = $row_index;
  452. }
  453. $wrappers[$row_index] = $this->index->entityWrapper($row->entity);
  454. }
  455. }
  456. // If the results are entities, we pre-load them to make use of a multiple
  457. // load. (Otherwise, each result would be loaded individually.)
  458. if (!empty($load_entities)) {
  459. $entities = entity_load($entity_type, array_keys($load_entities));
  460. foreach ($entities as $entity_id => $entity) {
  461. $wrappers[$load_entities[$entity_id]] = $this->index->entityWrapper($entity);
  462. }
  463. }
  464. // Apply the relationship, if necessary.
  465. $type = $entity_type ? $entity_type : $this->index->item_type;
  466. $selector_suffix = '';
  467. if ($field && ($pos = strrpos($field, ':'))) {
  468. $selector_suffix = substr($field, 0, $pos);
  469. }
  470. if ($selector_suffix || ($relationship && !empty($this->view->relationship[$relationship]))) {
  471. // Use EntityFieldHandlerHelper to compute the correct data selector for
  472. // the relationship.
  473. $handler = (object) array(
  474. 'view' => $this->view,
  475. 'relationship' => $relationship,
  476. 'real_field' => '',
  477. );
  478. $selector = EntityFieldHandlerHelper::construct_property_selector($handler);
  479. $selector .= ($selector ? ':' : '') . $selector_suffix;
  480. list($type, $wrappers) = EntityFieldHandlerHelper::extract_property_multiple($wrappers, $selector);
  481. }
  482. return array($type, $wrappers);
  483. }
  484. /**
  485. * API function for accessing the raw Search API query object.
  486. *
  487. * @return SearchApiQueryInterface
  488. * The search query object used internally by this handler.
  489. */
  490. public function getSearchApiQuery() {
  491. return $this->query;
  492. }
  493. /**
  494. * API function for accessing the raw Search API results.
  495. *
  496. * @return array
  497. * An associative array containing the search results, as specified by
  498. * SearchApiQueryInterface::execute().
  499. */
  500. public function getSearchApiResults() {
  501. return $this->search_api_results;
  502. }
  503. //
  504. // Query interface methods (proxy to $this->query)
  505. //
  506. public function createFilter($conjunction = 'AND', $tags = array()) {
  507. if (!$this->errors) {
  508. return $this->query->createFilter($conjunction, $tags);
  509. }
  510. }
  511. public function keys($keys = NULL) {
  512. if (!$this->errors) {
  513. $this->query->keys($keys);
  514. }
  515. return $this;
  516. }
  517. public function fields(array $fields) {
  518. if (!$this->errors) {
  519. $this->query->fields($fields);
  520. }
  521. return $this;
  522. }
  523. /**
  524. * Adds a nested filter to the search query object.
  525. *
  526. * If $group is given, the filter is added to the relevant filter group
  527. * instead.
  528. */
  529. public function filter(SearchApiQueryFilterInterface $filter, $group = NULL) {
  530. if (!$this->errors) {
  531. $this->where[$group]['filters'][] = $filter;
  532. }
  533. return $this;
  534. }
  535. /**
  536. * Set a condition on the search query object.
  537. *
  538. * If $group is given, the condition is added to the relevant filter group
  539. * instead.
  540. */
  541. public function condition($field, $value, $operator = '=', $group = NULL) {
  542. if (!$this->errors) {
  543. $this->where[$group]['conditions'][] = array($field, $value, $operator);
  544. }
  545. return $this;
  546. }
  547. public function sort($field, $order = 'ASC') {
  548. if (!$this->errors) {
  549. $this->query->sort($field, $order);
  550. }
  551. return $this;
  552. }
  553. public function range($offset = NULL, $limit = NULL) {
  554. if (!$this->errors) {
  555. $this->query->range($offset, $limit);
  556. }
  557. return $this;
  558. }
  559. public function getIndex() {
  560. return $this->index;
  561. }
  562. public function &getKeys() {
  563. if (!$this->errors) {
  564. return $this->query->getKeys();
  565. }
  566. $ret = NULL;
  567. return $ret;
  568. }
  569. public function getOriginalKeys() {
  570. if (!$this->errors) {
  571. return $this->query->getOriginalKeys();
  572. }
  573. }
  574. public function &getFields() {
  575. if (!$this->errors) {
  576. return $this->query->getFields();
  577. }
  578. $ret = NULL;
  579. return $ret;
  580. }
  581. public function getFilter() {
  582. if (!$this->errors) {
  583. return $this->query->getFilter();
  584. }
  585. }
  586. public function &getSort() {
  587. if (!$this->errors) {
  588. return $this->query->getSort();
  589. }
  590. $ret = NULL;
  591. return $ret;
  592. }
  593. public function getOption($name, $default = NULL) {
  594. if (!$this->errors) {
  595. return $this->query->getOption($name, $default);
  596. }
  597. return $default;
  598. }
  599. public function setOption($name, $value) {
  600. if (!$this->errors) {
  601. return $this->query->setOption($name, $value);
  602. }
  603. return NULL;
  604. }
  605. public function &getOptions() {
  606. if (!$this->errors) {
  607. return $this->query->getOptions();
  608. }
  609. $ret = NULL;
  610. return $ret;
  611. }
  612. }