search_api_solr.module 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. }
  136. /**
  137. * Returns either all dynamic field types, or a specific one.
  138. *
  139. * @param $type
  140. * If specified, the type whose definition should be returned.
  141. *
  142. * @return array
  143. * If $type was not given, an array containing all custom dynamic fields, in
  144. * the format specified by hook_search_api_solr_dynamic_field_info().
  145. * Otherwise, the definition for the given type, or NULL if it is unknown.
  146. *
  147. * @see hook_search_api_solr_dynamic_field_info().
  148. */
  149. function search_api_solr_get_dynamic_field_info($type = NULL) {
  150. $types = &drupal_static(__FUNCTION__);
  151. if (!isset($types)) {
  152. $types = module_invoke_all('search_api_solr_dynamic_field_info');
  153. $types = $types ? $types : array();
  154. drupal_alter('search_api_solr_dynamic_field_info', $types);
  155. }
  156. if (isset($type)) {
  157. return isset($types[$type]) ? $types[$type] : NULL;
  158. }
  159. return $types;
  160. }
  161. /**
  162. * Implements hook_search_api_solr_dynamic_field_info().
  163. */
  164. function search_api_solr_search_api_solr_dynamic_field_info() {
  165. return array(
  166. 'text' => array(
  167. 'prefix' => 'tm',
  168. 'always multiValued' => TRUE,
  169. ),
  170. 'tokens' => array(
  171. 'prefix' => 'tm',
  172. 'always multiValued' => TRUE,
  173. ),
  174. 'string' => array(
  175. 'prefix' => 's',
  176. 'always multiValued' => FALSE,
  177. ),
  178. 'integer' => array(
  179. 'prefix' => 'i',
  180. 'always multiValued' => FALSE,
  181. ),
  182. 'decimal' => array(
  183. 'prefix' => 'f',
  184. 'always multiValued' => FALSE,
  185. ),
  186. 'date' => array(
  187. 'prefix' => 'd',
  188. 'always multiValued' => FALSE,
  189. ),
  190. 'duration' => array(
  191. 'i',
  192. 'always multiValued' => FALSE,
  193. ),
  194. 'boolean' => array(
  195. 'b',
  196. 'always multiValued' => FALSE,
  197. ),
  198. 'uri' => array(
  199. 's',
  200. 'always multiValued' => FALSE,
  201. ),
  202. 'location' => array(
  203. 'loc',
  204. 'always multiValued' => FALSE,
  205. ),
  206. 'geohash' => array(
  207. 'geohash',
  208. 'always multiValued' => FALSE,
  209. ),
  210. );
  211. }