search_api_solr.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @file
  4. * Provides a Solr-based service class for the Search API.
  5. */
  6. /**
  7. * Implements hook_init().
  8. */
  9. function search_api_solr_init() {
  10. spl_autoload_register('_search_api_solr_autoload');
  11. }
  12. /**
  13. * Return path to SolrPhpClient library path, or FALSE if not found.
  14. */
  15. function _search_api_solr_solrphpclient_path() {
  16. static $path = NULL;
  17. if (!isset($path)) {
  18. $path = FALSE;
  19. // If Libraries API is installed, we first use that to try and find the
  20. // library. Otherwise we manually check a few locations.
  21. $search_dirs = array();
  22. if (function_exists('libraries_get_path')) {
  23. $dir = libraries_get_path('SolrPhpClient');
  24. // Confusingly, Libraries API 1.x will return sites/all/libraries/NAME on
  25. // failure, while Libraries API 2.x returns FALSE in that case.
  26. if ($dir) {
  27. $search_dirs[] = $dir;
  28. }
  29. }
  30. else {
  31. // Include libraries + current profile folders in searched directories.
  32. $search_dirs[] = 'sites/all/libraries/SolrPhpClient';
  33. $search_dirs[] = 'profiles/' . drupal_get_profile() . '/libraries/SolrPhpClient';
  34. }
  35. $search_dirs[] = drupal_get_path('module', 'search_api_solr') . '/SolrPhpClient';
  36. foreach ($search_dirs as $dir) {
  37. $dir = DRUPAL_ROOT . '/' . $dir;
  38. if (is_dir($dir)) {
  39. $path = $dir;
  40. break;
  41. }
  42. }
  43. }
  44. if ($path == FALSE) {
  45. throw new Exception('SolrPhpClient library not found! Please follow the instructions in search_api_solr/INSTALL.txt for installing the Solr search module.');
  46. }
  47. return $path;
  48. }
  49. /**
  50. * Autoloader for the SolrPhpClient classes.
  51. */
  52. function _search_api_solr_autoload($name) {
  53. static $lookup_cache = array();
  54. if (isset($lookup_cache[$name])) {
  55. return $lookup_cache[$name];
  56. }
  57. elseif (substr($name, 0, 11) == 'Apache_Solr') {
  58. $path = _search_api_solr_solrphpclient_path();
  59. if (file_exists($file_path = $path . '/' . str_replace('_', '/', $name) . '.php')) {
  60. require_once $file_path;
  61. $lookup_cache[$name] = TRUE;
  62. return TRUE;
  63. }
  64. }
  65. $lookup_cache[$name] = FALSE;
  66. return FALSE;
  67. }
  68. /**
  69. * Implements hook_search_api_service_info().
  70. */
  71. function search_api_solr_search_api_service_info() {
  72. $services['search_api_solr_service'] = array(
  73. 'name' => t('Solr service'),
  74. 'description' => t('<p>Index items using an Apache Solr search server.</p>' .
  75. '<ul>' . '<li>All field types are supported and indexed in a special way, with URI/String and Integer/Duration being equivalent.</li>' .
  76. '<li>See <a href="@url">the Solr wiki</a> for information about the "direct" parse mode.</li>' .
  77. '<li>Supports the search_api_facets and search_api_multi features.</li>' .
  78. '<li>Will use internal Solr preprocessors, so Search API preprocessors should for the most part be deactivated.</li>' .
  79. '<li>See the README.txt file provided with this module for details.</li>' . '</ul>',
  80. array('@url' => url('http://wiki.apache.org/solr/SolrQuerySyntax'))),
  81. 'class' => 'SearchApiSolrService',
  82. );
  83. return $services;
  84. }
  85. /**
  86. * Implements hook_help().
  87. */
  88. function search_api_solr_help($path, array $arg = array()) {
  89. if ($path == 'admin/config/search/search_api') {
  90. // Included because we need the REQUIREMENT_* constants.
  91. include_once(DRUPAL_ROOT . '/includes/install.inc');
  92. module_load_include('install', 'search_api_solr');
  93. $reqs = search_api_solr_requirements('runtime');
  94. foreach ($reqs as $req) {
  95. if (isset($req['description'])) {
  96. $type = $req['severity'] == REQUIREMENT_ERROR ? 'error' : ($req['severity'] == REQUIREMENT_WARNING ? 'warning' : 'status');
  97. drupal_set_message($req['description'], $type);
  98. }
  99. }
  100. }
  101. elseif ($path == 'admin/config/search/search_api/server/%' && !empty($arg[5])) {
  102. $server = search_api_server_load($arg[5]);
  103. if ($server && $server->enabled && $server->class == 'search_api_solr_service') {
  104. $ping = $server->ping();
  105. $type = $ping ? 'status' : 'error';
  106. if ($ping) {
  107. $msg = t('The Solr server could be reached (latency: @millisecs ms).', array('@millisecs' => $ping * 1000));
  108. }
  109. else {
  110. $msg = t('The Solr server could not be reached.');
  111. }
  112. drupal_set_message($msg, $type);
  113. }
  114. }
  115. }
  116. /**
  117. * Implements hook_cron().
  118. *
  119. * Used to execute an optimization operation on all enabled Solr servers once a
  120. * day.
  121. */
  122. function search_api_solr_cron() {
  123. if (REQUEST_TIME - variable_get('search_api_solr_last_optimize', 0) > 86400) {
  124. variable_set('search_api_solr_last_optimize', REQUEST_TIME);
  125. $conditions = array('class' => 'search_api_solr_service', 'enabled' => TRUE);
  126. foreach (search_api_server_load_multiple(FALSE, $conditions) as $server) {
  127. try {
  128. $server->getSolrConnection()->optimize(FALSE, FALSE);
  129. }
  130. catch(Exception $e) {
  131. watchdog_exception('search_api_solr', $e, '%type while optimizing Solr server @server: !message in %function (line %line of %file).', array('@server' => $server->name));
  132. }
  133. }
  134. }
  135. }