search_api_multi.module 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Implements hook_views_api().
  4. */
  5. function search_api_multi_views_api() {
  6. if (module_exists('search_api_views')) {
  7. return array(
  8. 'api' => '3.0-alpha1',
  9. 'path' => drupal_get_path('module', 'search_api_multi') . '/views',
  10. );
  11. }
  12. }
  13. /**
  14. * Implements hook_search_api_server_enabled().
  15. */
  16. function search_api_multi_search_api_server_enabled(array $servers) {
  17. if (!module_exists('search_api_views')) {
  18. return;
  19. }
  20. foreach ($servers as $server) {
  21. if ($server->supportsFeature('search_api_multi')) {
  22. // Make the new server(s) available for views.
  23. views_invalidate_cache();
  24. break;
  25. }
  26. }
  27. }
  28. /**
  29. * Implements hook_search_api_server_update().
  30. */
  31. function search_api_multi_search_api_server_update(SearchApiServer $server) {
  32. if (module_exists('search_api_views') && $server->supportsFeature('search_api_multi') && !$server->enabled && $server->original->enabled) {
  33. _search_api_multi_server_unavailable($server);
  34. }
  35. }
  36. /**
  37. * Implements hook_search_api_server_delete().
  38. */
  39. function search_api_multi_search_api_server_delete(SearchApiServer $server) {
  40. if (module_exists('search_api_views') && $server->supportsFeature('search_api_multi')) {
  41. _search_api_multi_server_unavailable($server);
  42. }
  43. }
  44. /**
  45. * Function for reacting to a disabled or deleted search server.
  46. */
  47. function _search_api_multi_server_unavailable(SearchApiServer $server) {
  48. $names = array();
  49. $table = 'search_api_server_' . $server->machine_name;
  50. foreach (views_get_all_views() as $name => $view) {
  51. if (empty($view->disabled) && $view->base_table == $table) {
  52. $names[] = $name;
  53. // @todo: if ($server_deleted) $view->delete()?
  54. }
  55. }
  56. if ($names) {
  57. views_invalidate_cache();
  58. drupal_set_message(t('The following views were using the server %name: @views. You should disable or delete them.', array('%name' => $server->name, '@views' => implode(', ', $names))), 'warning');
  59. }
  60. }
  61. /**
  62. * Creates a multi-index search query on a specified search server.
  63. *
  64. * @param $id
  65. * The ID or machine name of the index to execute the search on.
  66. * @param array $options
  67. * Associative array of options configuring this query. Recognized options
  68. * are:
  69. * - conjunction: The type of conjunction to use for this query - either
  70. * 'AND' or 'OR'. 'AND' by default. This only influences the search keys,
  71. * filters will always use AND by default.
  72. * - 'parse mode': The mode with which to parse the $keys variable, if it
  73. * is set and not already an array. See SearchApiMultiQuery::parseModes() for
  74. * recognized parse modes.
  75. * - languages: The languages to search for, as an array of language IDs.
  76. * If not specified, all languages will be searched. Language-neutral
  77. * content (LANGUAGE_NONE) is always searched.
  78. * - offset: The position of the first returned search results relative to
  79. * the whole result on the server.
  80. * - limit: The maximum number of search results to return. -1 means no
  81. * limit.
  82. * - 'filter class': Can be used to change the SearchApiQueryFilterInterface
  83. * implementation to use.
  84. * - 'search id': A string that will be used as the identifier when storing
  85. * this search in the static cache.
  86. * All options are optional.
  87. *
  88. * @return SearchApiMultiQueryInterface
  89. * An object for searching on the specified server.
  90. */
  91. function search_api_multi_query($id, array $options = array()) {
  92. $server = search_api_server_load($id);
  93. if (!$server) {
  94. throw new SearchApiException(t('Unknown server with ID @id.', array('@id' => $id)));
  95. }
  96. if (!$server->supportsFeature('search_api_multi')) {
  97. throw new SearchApiException(t("The search server @name doesn't support multi-index searches.", array('@name' => $server->name)));
  98. }
  99. return $server->queryMultiple($options);
  100. }
  101. /**
  102. * Static store for the multi-index searches executed on the current page. Can
  103. * either be used to store an executed search, or to retrieve a previously
  104. * stored search.
  105. *
  106. * @param $search_id
  107. * For pages displaying multiple searches, an optional ID identifying the
  108. * search in questions. When storing a search, this is filled automatically,
  109. * unless it is manually set.
  110. * @param SearchApiMultiQuery $query
  111. * When storing an executed search, the query that was executed. NULL
  112. * otherwise.
  113. * @param array $results
  114. * When storing an executed search, the returned results as specified by
  115. * SearchApiMultiQueryInterface::execute(). An empty array, otherwise.
  116. *
  117. * @return array
  118. * If a search with the specified ID was executed, an array containing
  119. * ($query, $results) as used in this function's parameters. If $search_id is
  120. * NULL, an array of all executed searches will be returned, keyed by ID.
  121. */
  122. function search_api_multi_current_search($search_id = NULL, SearchApiMultiQuery $query = NULL, array $results = array()) {
  123. $searches = &drupal_static(__FUNCTION__, array());
  124. if (isset($query)) {
  125. if (!isset($search_id)) {
  126. $search_id = $query->getOption('search id');
  127. }
  128. $base = $search_id;
  129. $i = 0;
  130. while (isset($searches[$search_id])) {
  131. $search_id = $base . '-' . ++$i;
  132. }
  133. $searches[$search_id] = array($query, $results);
  134. }
  135. if (isset($searches[$search_id])) {
  136. return $searches[$search_id];
  137. }
  138. return $searches;
  139. }