search_api_solr.module 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_solr_access_server_files',
  16. 'access arguments' => array(5),
  17. 'file' => 'search_api_solr.admin.inc',
  18. 'type' => MENU_LOCAL_TASK,
  19. 'weight' => -1,
  20. 'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
  21. );
  22. return $items;
  23. }
  24. /**
  25. * Implements hook_search_api_service_info().
  26. */
  27. function search_api_solr_search_api_service_info() {
  28. $variables = array(
  29. '@solr_wiki_url' => url('http://wiki.apache.org/solr/SolrQuerySyntax'),
  30. '@readme_url' => url(drupal_get_path('module', 'search_api_solr') . '/README.txt'),
  31. );
  32. $services['search_api_solr_service'] = array(
  33. 'name' => t('Solr service'),
  34. 'description' => t('<p>Index items using an Apache Solr search server.</p>
  35. <ul>
  36. <li>See <a href="@solr_wiki_url">the Solr wiki</a> for information about the "direct" parse mode.</li>
  37. <li>Will use internal Solr preprocessors, so Search API preprocessors should for the most part be deactivated.</li>
  38. <li>See the <a href="@readme_url">README.txt</a> file provided with this module for details.</li>
  39. </ul>', $variables),
  40. 'class' => 'SearchApiSolrService',
  41. );
  42. return $services;
  43. }
  44. /**
  45. * Implements hook_help().
  46. */
  47. function search_api_solr_help($path, array $arg = array()) {
  48. if ($path == 'admin/config/search/search_api') {
  49. // Included because we need the REQUIREMENT_* constants.
  50. include_once(DRUPAL_ROOT . '/includes/install.inc');
  51. module_load_include('install', 'search_api_solr');
  52. $reqs = search_api_solr_requirements('runtime');
  53. foreach ($reqs as $req) {
  54. if (isset($req['description'])) {
  55. $type = $req['severity'] == REQUIREMENT_ERROR ? 'error' : ($req['severity'] == REQUIREMENT_WARNING ? 'warning' : 'status');
  56. drupal_set_message($req['description'], $type);
  57. }
  58. }
  59. }
  60. }
  61. /**
  62. * Implements hook_cron().
  63. *
  64. * Used to execute an optimization operation on all enabled Solr servers once a
  65. * day.
  66. */
  67. function search_api_solr_cron() {
  68. if (REQUEST_TIME - variable_get('search_api_solr_last_optimize', 0) > 86400) {
  69. variable_set('search_api_solr_last_optimize', REQUEST_TIME);
  70. $conditions = array('class' => 'search_api_solr_service', 'enabled' => TRUE);
  71. foreach (search_api_server_load_multiple(FALSE, $conditions) as $server) {
  72. try {
  73. $server->getSolrConnection()->optimize(FALSE);
  74. }
  75. catch(Exception $e) {
  76. watchdog_exception('search_api_solr', $e, '%type while optimizing Solr server @server: !message in %function (line %line of %file).', array('@server' => $server->name));
  77. }
  78. }
  79. }
  80. }
  81. /**
  82. * Implements hook_flush_caches().
  83. */
  84. function search_api_solr_flush_caches() {
  85. return array('cache_search_api_solr');
  86. }
  87. /**
  88. * Implements hook_search_api_server_update().
  89. */
  90. function search_api_solr_search_api_server_update(SearchApiServer $server) {
  91. if ($server->class === 'search_api_solr_service') {
  92. $server->getSolrConnection()->clearCache();
  93. }
  94. }
  95. /**
  96. * Implements hook_views_api().
  97. */
  98. function search_api_solr_views_api() {
  99. if (module_exists('search_api_views')) {
  100. return array(
  101. 'api' => 3,
  102. );
  103. }
  104. }
  105. /**
  106. * Retrieves Solr-specific data for available data types.
  107. *
  108. * Returns the data type information for both the default Search API data types
  109. * and custom data types defined by hook_search_api_data_type_info(). Names for
  110. * default data types are not included, since they are not relevant to the Solr
  111. * service class.
  112. *
  113. * We're adding some extra Solr field information for the default search api
  114. * data types (as well as on behalf of a couple contrib field types). The
  115. * extra information we're adding is documented in
  116. * search_api_solr_hook_search_api_data_type_info(). You can use the same
  117. * additional keys in hook_search_api_data_type_info() to support custom
  118. * dynamic fields in your indexes with Solr.
  119. *
  120. * @param string|null $type
  121. * (optional) A specific type for which the information should be returned.
  122. * Defaults to returning all information.
  123. *
  124. * @return array|null
  125. * If $type was given, information about that type or NULL if it is unknown.
  126. * Otherwise, an array of all types. The format in both cases is the same as
  127. * for search_api_get_data_type_info().
  128. *
  129. * @see search_api_get_data_type_info()
  130. * @see search_api_solr_hook_search_api_data_type_info()
  131. */
  132. function search_api_solr_get_data_type_info($type = NULL) {
  133. $types = &drupal_static(__FUNCTION__);
  134. if (!isset($types)) {
  135. // Grab the stock search_api data types.
  136. $types = search_api_get_data_type_info();
  137. // Add our extras for the default search api fields.
  138. $types += array(
  139. 'text' => array(
  140. 'prefix' => 'tm',
  141. 'always multiValued' => TRUE,
  142. ),
  143. 'string' => array(
  144. 'prefix' => 's',
  145. ),
  146. 'integer' => array(
  147. 'prefix' => 'i',
  148. ),
  149. 'decimal' => array(
  150. 'prefix' => 'f',
  151. ),
  152. 'date' => array(
  153. 'prefix' => 'd',
  154. ),
  155. 'duration' => array(
  156. 'prefix' => 'i',
  157. ),
  158. 'boolean' => array(
  159. 'prefix' => 'b',
  160. ),
  161. 'uri' => array(
  162. 'prefix' => 's',
  163. ),
  164. 'tokens' => array(
  165. 'prefix' => 'tm',
  166. 'always multiValued' => TRUE,
  167. ),
  168. );
  169. // Extra data type info.
  170. $extra_types_info = array(
  171. 'location' => array(
  172. 'prefix' => 'loc',
  173. ),
  174. 'geohash' => array(
  175. 'prefix' => 'geo',
  176. ),
  177. );
  178. // For the extra types, only add our extra info if it's already been defined.
  179. foreach ($extra_types_info as $key => $info) {
  180. if (array_key_exists($key, $types)) {
  181. // Merge our extras into the data type info
  182. $types[$key] += $info;
  183. }
  184. }
  185. }
  186. // Return the info.
  187. if (isset($type)) {
  188. return isset($types[$type]) ? $types[$type] : NULL;
  189. }
  190. return $types;
  191. }
  192. /**
  193. * Retrieves a list of all config files of a server.
  194. *
  195. * @param SearchApiServer $server
  196. * The Solr server whose files should be retrieved.
  197. * @param string $dir_name
  198. * (optional) The directory that should be searched for files. Defaults to the
  199. * root config directory.
  200. *
  201. * @return array
  202. * An associative array of all config files in the given directory. The keys
  203. * are the file names, values are arrays with information about the file. The
  204. * files are returned in alphabetical order and breadth-first.
  205. *
  206. * @throws SearchApiException
  207. * If a problem occurred while retrieving the files.
  208. */
  209. function search_api_solr_server_get_files(SearchApiServer $server, $dir_name = NULL) {
  210. $response = $server->getFile($dir_name);
  211. // Search for directories and recursively merge directory files.
  212. $files_data = json_decode($response->data, TRUE);
  213. $files_list = $files_data['files'];
  214. $result = array('' => array());
  215. foreach ($files_list as $file_name => $file_info) {
  216. if (empty($file_info['directory'])) {
  217. $result[''][$file_name] = $file_info;
  218. }
  219. else {
  220. $result[$file_name] = search_api_solr_server_get_files($server, $file_name);
  221. }
  222. }
  223. ksort($result);
  224. ksort($result['']);
  225. return array_reduce($result, 'array_merge', array());
  226. }
  227. /**
  228. * @deprecated
  229. *
  230. * @see search_api_solr_access_server_files()
  231. */
  232. function search_api_access_server_files(SearchApiServer $server) {
  233. return search_api_solr_access_server_files($server);
  234. }
  235. /**
  236. * Access callback for a server's "Files" tab.
  237. *
  238. * Grants access if the user has the "administer search_api" permission and the
  239. * server is a Solr server.
  240. *
  241. * @param SearchApiServer $server
  242. * The server for which access should be tested.
  243. *
  244. * @return bool
  245. * TRUE if access should be granted, FALSE otherwise.
  246. */
  247. function search_api_solr_access_server_files(SearchApiServer $server) {
  248. if (!user_access('administer search_api')) {
  249. return FALSE;
  250. }
  251. $service_info = search_api_get_service_info($server->class);
  252. $service_class = $service_info['class'];
  253. if (empty($service_class) || !class_exists($service_class)) {
  254. // Service class not found.
  255. return FALSE;
  256. }
  257. if ($service_class == 'SearchApiSolrService' || in_array('SearchApiSolrService', class_parents($service_class))) {
  258. // It's an SearchApiSolrService based connection class.
  259. return TRUE;
  260. }
  261. return FALSE;
  262. }
  263. /**
  264. * Switches a server to use clean identifiers.
  265. *
  266. * Used as a submit callback in SearchApiSolrService::configurationForm().
  267. */
  268. function _search_api_solr_switch_to_clean_ids(array $form, array &$form_state) {
  269. $server = $form_state['server'];
  270. $server->options['clean_ids'] = TRUE;
  271. $server->save();
  272. drupal_set_message(t('The Solr server was successfully switched to use clean field identifiers.'));
  273. $count = 0;
  274. $conditions['server'] = $server->machine_name;
  275. $conditions['enabled'] = 1;
  276. foreach (search_api_index_load_multiple(FALSE, $conditions) as $index) {
  277. if (!empty($index->options['fields'])) {
  278. foreach ($index->options['fields'] as $key => $field) {
  279. if (strpos($key, ':') !== FALSE) {
  280. $index->reindex();
  281. ++$count;
  282. break;
  283. }
  284. }
  285. }
  286. }
  287. if ($count) {
  288. $msg = format_plural($count, '1 index was scheduled for re-indexing.', '@count indexes were scheduled for re-indexing.');
  289. drupal_set_message($msg);
  290. }
  291. }