search_api_solr.module 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @file
  4. * Provides a Solr-based service class for the Search API.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function search_api_solr_menu() {
  10. $items['admin/config/search/search_api/server/%search_api_server/files'] = array(
  11. 'title' => 'Files',
  12. 'description' => 'View Solr configuration files.',
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('search_api_solr_solr_config_form', 5),
  15. 'access callback' => 'search_api_access_server_files',
  16. 'access arguments' => array(5),
  17. 'file' => 'search_api_solr.admin.inc',
  18. 'type' => MENU_LOCAL_TASK,
  19. 'weight' => -1,
  20. );
  21. return $items;
  22. }
  23. /**
  24. * Implements hook_search_api_service_info().
  25. */
  26. function search_api_solr_search_api_service_info() {
  27. $services['search_api_solr_service'] = array(
  28. 'name' => t('Solr service'),
  29. 'description' => t('<p>Index items using an Apache Solr search server.</p>' .
  30. '<ul>' . '<li>All field types are supported and indexed in a special way, with URI/String and Integer/Duration being equivalent.</li>' .
  31. '<li>See <a href="@url">the Solr wiki</a> for information about the "direct" parse mode.</li>' .
  32. '<li>Supports the search_api_facets and search_api_multi features.</li>' .
  33. '<li>Will use internal Solr preprocessors, so Search API preprocessors should for the most part be deactivated.</li>' .
  34. '<li>See the README.txt file provided with this module for details.</li>' . '</ul>',
  35. array('@url' => url('http://wiki.apache.org/solr/SolrQuerySyntax'))),
  36. 'class' => 'SearchApiSolrService',
  37. );
  38. return $services;
  39. }
  40. /**
  41. * Implements hook_help().
  42. */
  43. function search_api_solr_help($path, array $arg = array()) {
  44. if ($path == 'admin/config/search/search_api') {
  45. // Included because we need the REQUIREMENT_* constants.
  46. include_once(DRUPAL_ROOT . '/includes/install.inc');
  47. module_load_include('install', 'search_api_solr');
  48. $reqs = search_api_solr_requirements('runtime');
  49. foreach ($reqs as $req) {
  50. if (isset($req['description'])) {
  51. $type = $req['severity'] == REQUIREMENT_ERROR ? 'error' : ($req['severity'] == REQUIREMENT_WARNING ? 'warning' : 'status');
  52. drupal_set_message($req['description'], $type);
  53. }
  54. }
  55. }
  56. elseif ($path == 'admin/config/search/search_api/server/%' && !empty($arg[5])) {
  57. $server = search_api_server_load($arg[5]);
  58. if ($server && $server->enabled && $server->class == 'search_api_solr_service') {
  59. $ping = $server->ping();
  60. $type = $ping ? 'status' : 'error';
  61. if ($ping) {
  62. $msg = t('The Solr server could be reached (latency: @millisecs ms).', array('@millisecs' => $ping * 1000));
  63. }
  64. else {
  65. $msg = t('The Solr server could not be reached.');
  66. }
  67. drupal_set_message($msg, $type);
  68. }
  69. }
  70. }
  71. /**
  72. * Implements hook_cron().
  73. *
  74. * Used to execute an optimization operation on all enabled Solr servers once a
  75. * day.
  76. */
  77. function search_api_solr_cron() {
  78. if (REQUEST_TIME - variable_get('search_api_solr_last_optimize', 0) > 86400) {
  79. variable_set('search_api_solr_last_optimize', REQUEST_TIME);
  80. $conditions = array('class' => 'search_api_solr_service', 'enabled' => TRUE);
  81. foreach (search_api_server_load_multiple(FALSE, $conditions) as $server) {
  82. try {
  83. $server->getSolrConnection()->optimize(FALSE);
  84. }
  85. catch(Exception $e) {
  86. watchdog_exception('search_api_solr', $e, '%type while optimizing Solr server @server: !message in %function (line %line of %file).', array('@server' => $server->name));
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * Implements hook_flush_caches().
  93. */
  94. function search_api_solr_flush_caches() {
  95. return array('cache_search_api_solr');
  96. }
  97. /**
  98. * Implements hook_search_api_server_update().
  99. */
  100. function search_api_solr_search_api_server_update(SearchApiServer $server) {
  101. if ($server->class === 'search_api_solr_service') {
  102. $server->getSolrConnection()->clearCache();
  103. }
  104. }
  105. /**
  106. * Retrieves a list of all config files of a server.
  107. *
  108. * @param SearchApiServer $server
  109. * The Solr server whose files should be retrieved.
  110. * @param string $dir_name
  111. * (optional) The directory that should be searched for files. Defaults to the
  112. * root config directory.
  113. *
  114. * @return array
  115. * An associative array of all config files in the given directory. The keys
  116. * are the file names, values are arrays with information about the file. The
  117. * files are returned in alphabetical order and breadth-first.
  118. *
  119. * @throws SearchApiException
  120. * If a problem occurred while retrieving the files.
  121. */
  122. function search_api_solr_server_get_files(SearchApiServer $server, $dir_name = NULL) {
  123. $response = $server->getFile($dir_name);
  124. // Search for directories and recursively merge directory files.
  125. $files_data = json_decode($response->data, TRUE);
  126. $files_list = $files_data['files'];
  127. $result = array('' => array());
  128. foreach ($files_list as $file_name => $file_info) {
  129. if (empty($file_info['directory'])) {
  130. $result[''][$file_name] = $file_info;
  131. }
  132. else {
  133. $result[$file_name] = search_api_solr_server_get_files($server, $file_name);
  134. }
  135. }
  136. ksort($result);
  137. ksort($result['']);
  138. return array_reduce($result, 'array_merge', array());
  139. }
  140. /**
  141. * Access callback for a server's "Files" tab.
  142. *
  143. * Grants access if the user has the "administer search_api" permission and the
  144. * server is a Solr server.
  145. *
  146. * @param SearchApiServer $server
  147. * The server for which access should be tested.
  148. *
  149. * @return bool
  150. * TRUE if access should be granted, FALSE otherwise.
  151. */
  152. function search_api_access_server_files(SearchApiServer $server) {
  153. if (!user_access('administer search_api')) {
  154. return FALSE;
  155. }
  156. $service_info = search_api_get_service_info($server->class);
  157. $service_class = $service_info['class'];
  158. if (empty($service_class) || !class_exists($service_class)) {
  159. // Service class not found.
  160. return FALSE;
  161. }
  162. if ($service_class == 'SearchApiSolrService' || in_array('SearchApiSolrService', class_parents($service_class))) {
  163. // It's an SearchApiSolrService based connection class.
  164. return TRUE;
  165. }
  166. return FALSE;
  167. }
  168. /**
  169. * Switches a server to use clean identifiers.
  170. *
  171. * Used as a submit callback in SearchApiSolrService::configurationForm().
  172. */
  173. function _search_api_solr_switch_to_clean_ids(array $form, array &$form_state) {
  174. $server = $form_state['server'];
  175. $server->options['clean_ids'] = TRUE;
  176. $server->save();
  177. drupal_set_message(t('The Solr server was successfully switched to use clean field identifiers.'));
  178. $count = 0;
  179. $conditions['server'] = $server->machine_name;
  180. $conditions['enabled'] = 1;
  181. foreach (search_api_index_load_multiple(FALSE, $conditions) as $index) {
  182. if (!empty($index->options['fields'])) {
  183. foreach ($index->options['fields'] as $key => $field) {
  184. if (strpos($key, ':') !== FALSE) {
  185. $index->reindex();
  186. ++$count;
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. if ($count) {
  193. $msg = format_plural($count, '1 index was scheduled for re-indexing.', '@count indexes were scheduled for re-indexing.');
  194. drupal_set_message($msg);
  195. }
  196. }