search_api_solr.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. $action = variable_get('search_api_solr_cron_action', 'spellcheck');
  69. // We treat all unknown action settings as "none". However, we turn a blind
  70. // eye for Britons and other people who can spell.
  71. if (!in_array($action, array('spellcheck', 'optimize', 'optimise'))) {
  72. return;
  73. }
  74. // 86400 seconds is one day. We use slightly less here to allow for some
  75. // variation in the request time of the cron run, so that the time of day will
  76. // (more or less) stay the same.
  77. if (REQUEST_TIME - variable_get('search_api_solr_last_optimize', 0) > 86340) {
  78. variable_set('search_api_solr_last_optimize', REQUEST_TIME);
  79. $conditions = array('class' => 'search_api_solr_service', 'enabled' => TRUE);
  80. $count = 0;
  81. foreach (search_api_server_load_multiple(FALSE, $conditions) as $server) {
  82. try {
  83. $solr = $server->getSolrConnection();
  84. if ($action != 'spellcheck') {
  85. $solr->optimize(FALSE);
  86. }
  87. else {
  88. $params['rows'] = 0;
  89. $params['spellcheck'] = 'true';
  90. $params['spellcheck.build'] = 'true';
  91. $solr->search(NULL, $params);
  92. }
  93. ++$count;
  94. }
  95. catch(SearchApiException $e) {
  96. watchdog_exception('search_api_solr', $e, '%type while optimizing Solr server @server: !message in %function (line %line of %file).', array('@server' => $server->name));
  97. }
  98. }
  99. if ($count) {
  100. $vars['@count'] = $count;
  101. if ($action != 'spellcheck') {
  102. watchdog('search_api_solr', 'Optimized @count Solr server(s).', $vars, WATCHDOG_INFO);
  103. }
  104. else {
  105. watchdog('search_api_solr', 'Rebuilt spellcheck dictionary on @count Solr server(s).', $vars, WATCHDOG_INFO);
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * Implements hook_flush_caches().
  112. */
  113. function search_api_solr_flush_caches() {
  114. return array('cache_search_api_solr');
  115. }
  116. /**
  117. * Implements hook_search_api_server_update().
  118. */
  119. function search_api_solr_search_api_server_update(SearchApiServer $server) {
  120. if ($server->class === 'search_api_solr_service') {
  121. $server->getSolrConnection()->clearCache();
  122. }
  123. }
  124. /**
  125. * Implements hook_views_api().
  126. */
  127. function search_api_solr_views_api() {
  128. if (module_exists('search_api_views')) {
  129. return array(
  130. 'api' => 3,
  131. );
  132. }
  133. }
  134. /**
  135. * Retrieves Solr-specific data for available data types.
  136. *
  137. * Returns the data type information for both the default Search API data types
  138. * and custom data types defined by hook_search_api_data_type_info(). Names for
  139. * default data types are not included, since they are not relevant to the Solr
  140. * service class.
  141. *
  142. * We're adding some extra Solr field information for the default search api
  143. * data types (as well as on behalf of a couple contrib field types). The
  144. * extra information we're adding is documented in
  145. * search_api_solr_hook_search_api_data_type_info(). You can use the same
  146. * additional keys in hook_search_api_data_type_info() to support custom
  147. * dynamic fields in your indexes with Solr.
  148. *
  149. * @param string|null $type
  150. * (optional) A specific type for which the information should be returned.
  151. * Defaults to returning all information.
  152. *
  153. * @return array|null
  154. * If $type was given, information about that type or NULL if it is unknown.
  155. * Otherwise, an array of all types. The format in both cases is the same as
  156. * for search_api_get_data_type_info().
  157. *
  158. * @see search_api_get_data_type_info()
  159. * @see search_api_solr_hook_search_api_data_type_info()
  160. */
  161. function search_api_solr_get_data_type_info($type = NULL) {
  162. $types = &drupal_static(__FUNCTION__);
  163. if (!isset($types)) {
  164. // Grab the stock search_api data types.
  165. $types = search_api_get_data_type_info();
  166. // Add our extras for the default search api fields.
  167. $types += array(
  168. 'text' => array(
  169. 'prefix' => 'tm',
  170. 'always multiValued' => TRUE,
  171. ),
  172. 'string' => array(
  173. 'prefix' => 's',
  174. ),
  175. 'integer' => array(
  176. 'prefix' => 'i',
  177. ),
  178. 'decimal' => array(
  179. 'prefix' => 'f',
  180. ),
  181. 'date' => array(
  182. 'prefix' => 'd',
  183. ),
  184. 'duration' => array(
  185. 'prefix' => 'i',
  186. ),
  187. 'boolean' => array(
  188. 'prefix' => 'b',
  189. ),
  190. 'uri' => array(
  191. 'prefix' => 's',
  192. ),
  193. 'tokens' => array(
  194. 'prefix' => 'tm',
  195. 'always multiValued' => TRUE,
  196. ),
  197. );
  198. // Extra data type info.
  199. $extra_types_info = array(
  200. 'location' => array(
  201. 'prefix' => 'loc',
  202. ),
  203. 'geohash' => array(
  204. 'prefix' => 'geo',
  205. ),
  206. );
  207. // For the extra types, only add our extra info if it's already been defined.
  208. foreach ($extra_types_info as $key => $info) {
  209. if (array_key_exists($key, $types)) {
  210. // Merge our extras into the data type info
  211. $types[$key] += $info;
  212. }
  213. }
  214. }
  215. // Return the info.
  216. if (isset($type)) {
  217. return isset($types[$type]) ? $types[$type] : NULL;
  218. }
  219. return $types;
  220. }
  221. /**
  222. * Returns a unique hash for the current site.
  223. *
  224. * This is used to identify Solr documents from different sites within a single
  225. * Solr server.
  226. *
  227. * @return string
  228. * A unique site hash, containing only alphanumeric characters.
  229. */
  230. function search_api_solr_site_hash() {
  231. // Copied from apachesolr_site_hash().
  232. if (!($hash = variable_get('search_api_solr_site_hash', FALSE))) {
  233. global $base_url;
  234. $hash = substr(base_convert(sha1(uniqid($base_url, TRUE)), 16, 36), 0, 6);
  235. variable_set('search_api_solr_site_hash', $hash);
  236. }
  237. return $hash;
  238. }
  239. /**
  240. * Retrieves a list of all config files of a server.
  241. *
  242. * @param SearchApiServer $server
  243. * The Solr server whose files should be retrieved.
  244. * @param string $dir_name
  245. * (optional) The directory that should be searched for files. Defaults to the
  246. * root config directory.
  247. *
  248. * @return array
  249. * An associative array of all config files in the given directory. The keys
  250. * are the file names, values are arrays with information about the file. The
  251. * files are returned in alphabetical order and breadth-first.
  252. *
  253. * @throws SearchApiException
  254. * If a problem occurred while retrieving the files.
  255. */
  256. function search_api_solr_server_get_files(SearchApiServer $server, $dir_name = NULL) {
  257. $response = $server->getFile($dir_name);
  258. // Search for directories and recursively merge directory files.
  259. $files_data = json_decode($response->data, TRUE);
  260. $files_list = $files_data['files'];
  261. $dir_length = strlen($dir_name) + 1;
  262. $result = array('' => array());
  263. foreach ($files_list as $file_name => $file_info) {
  264. // Annoyingly, Solr 4.7 changed the way the admin/file handler returns
  265. // the file names when listing directory contents: the returned name is now
  266. // only the base name, not the complete path from the config root directory.
  267. // We therefore have to check for this case.
  268. if ($dir_name && substr($file_name, 0, $dir_length) !== "$dir_name/") {
  269. $file_name = "$dir_name/" . $file_name;
  270. }
  271. if (empty($file_info['directory'])) {
  272. $result[''][$file_name] = $file_info;
  273. }
  274. else {
  275. $result[$file_name] = search_api_solr_server_get_files($server, $file_name);
  276. }
  277. }
  278. ksort($result);
  279. ksort($result['']);
  280. return array_reduce($result, 'array_merge', array());
  281. }
  282. /**
  283. * @deprecated
  284. *
  285. * @see search_api_solr_access_server_files()
  286. */
  287. function search_api_access_server_files(SearchApiServer $server) {
  288. return search_api_solr_access_server_files($server);
  289. }
  290. /**
  291. * Access callback for a server's "Files" tab.
  292. *
  293. * Grants access if the user has the "administer search_api" permission and the
  294. * server is a Solr server.
  295. *
  296. * @param SearchApiServer $server
  297. * The server for which access should be tested.
  298. *
  299. * @return bool
  300. * TRUE if access should be granted, FALSE otherwise.
  301. */
  302. function search_api_solr_access_server_files(SearchApiServer $server) {
  303. if (!user_access('administer search_api')) {
  304. return FALSE;
  305. }
  306. $service_info = search_api_get_service_info($server->class);
  307. $service_class = $service_info['class'];
  308. if (empty($service_class) || !class_exists($service_class)) {
  309. // Service class not found.
  310. return FALSE;
  311. }
  312. if ($service_class == 'SearchApiSolrService' || in_array('SearchApiSolrService', class_parents($service_class))) {
  313. // It's an SearchApiSolrService based connection class.
  314. return TRUE;
  315. }
  316. return FALSE;
  317. }
  318. /**
  319. * Switches a server to use clean identifiers.
  320. *
  321. * Used as a submit callback in SearchApiSolrService::configurationForm().
  322. */
  323. function _search_api_solr_switch_to_clean_ids(array $form, array &$form_state) {
  324. $server = $form_state['server'];
  325. $server->options['clean_ids'] = TRUE;
  326. $server->save();
  327. drupal_set_message(t('The Solr server was successfully switched to use clean field identifiers.'));
  328. $count = 0;
  329. $conditions['server'] = $server->machine_name;
  330. $conditions['enabled'] = 1;
  331. foreach (search_api_index_load_multiple(FALSE, $conditions) as $index) {
  332. if (!empty($index->options['fields'])) {
  333. foreach ($index->options['fields'] as $key => $field) {
  334. if (strpos($key, ':') !== FALSE) {
  335. $index->reindex();
  336. ++$count;
  337. break;
  338. }
  339. }
  340. }
  341. }
  342. if ($count) {
  343. $msg = format_plural($count, '1 index was scheduled for re-indexing.', '@count indexes were scheduled for re-indexing.');
  344. drupal_set_message($msg);
  345. }
  346. }
  347. /**
  348. * Switches a server to multi-site compatibility mode.
  349. *
  350. * Used as a submit callback in SearchApiSolrService::configurationForm().
  351. */
  352. function _search_api_solr_switch_to_site_hash(array $form, array &$form_state) {
  353. $server = $form_state['server'];
  354. try {
  355. $conditions['server'] = $server->machine_name;
  356. $indexes = search_api_index_load_multiple(FALSE, $conditions);
  357. if ($indexes) {
  358. foreach ($indexes as $index) {
  359. $index->reindex();
  360. }
  361. $msg = format_plural(count($indexes), '1 index was cleared.', '@count indexes were cleared.');
  362. $server->deleteItems('index_id:(' . implode(' ', array_keys($indexes)) . ')');
  363. drupal_set_message($msg);
  364. }
  365. }
  366. catch (SearchApiException $e) {
  367. $variables = array('@server' => $server->name);
  368. watchdog_exception('search_api_solr', $e, '%type while attempting to enable multi-site compatibility mode for Solr server @server: !message in %function (line %line of %file).', $variables);
  369. drupal_set_message(t('An error occured while attempting to enable multi-site compatibility mode for Solr server @server. Check the logs for details.', $variables), 'error');
  370. return;
  371. }
  372. $server->options['site_hash'] = TRUE;
  373. $server->save();
  374. drupal_set_message(t('The Solr server was successfully switched to multi-site compatibility mode.'));
  375. }