apachesolr_search.module

Tracking 6.x-3.x branch
  1. drupal
    1. 5 contributions/apachesolr/apachesolr_search.module
    2. 6 contributions/apachesolr/apachesolr_search.module
    3. 7 contributions/apachesolr/apachesolr_search.module

Provides a content search implementation for node content for use with the Apache Solr search application.

Functions & methods

NameDescription
apachesolr_search_add_boost_params
apachesolr_search_add_spellcheck_params
apachesolr_search_apachesolr_default_environmentImplements hook_apachesolr_default_environment()
apachesolr_search_apachesolr_entity_info_alterImplements http://drupal.org that can give a good example of custom entity indexing such as apachesolr_user, apachesolr_term" class="local">hook_apachesolr_entity_info_alter().
apachesolr_search_apachesolr_environment_deleteImplements hook_apachesolr_environment_delete().
apachesolr_search_basic_params
apachesolr_search_blockImplements hook_block().
apachesolr_search_block_configureImplements hook_block_configure().
apachesolr_search_block_infoRetrieve all possible MLT blocks
apachesolr_search_block_saveImplements hook_block_save().
apachesolr_search_block_viewVew a specific block according to the delta identifier
apachesolr_search_build_spellchecksubmit function for the delete_index form.
apachesolr_search_conditionsThe default conditions callback.
apachesolr_search_conditions_default
apachesolr_search_default_search_pageGet or set the default search page id for the current page.
apachesolr_search_facetapi_realm_settings_form_submitForm submission handler for facetapi_realm_settings_form().
apachesolr_search_form_apachesolr_delete_index_confirm_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_block_admin_display_form_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_facetapi_realm_settings_form_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_search_admin_settings_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_search_block_form_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_search_theme_form_alterImplements hook_form_[form_id]_alter().
apachesolr_search_get_search_suggestionsRetrieve all of the suggestions that were given after a certain search
apachesolr_search_get_taxonomy_term_titleUsed as a callback function to generate a title for the taxonomy term depending on the input in the configuration screen
apachesolr_search_get_user_titleUsed as a callback function to generate a title for a user name depending on the input in the configuration screen
apachesolr_search_get_value_titleUsed as a callback function to generate a title for a node/page depending on the input in the configuration screen
apachesolr_search_highlighting_paramsAdd highlighting settings to the search params.
apachesolr_search_initImplements hook_init().
apachesolr_search_load_all_mlt_blocks
apachesolr_search_load_all_search_pagesReturn all the saved search pages
apachesolr_search_load_all_search_typesFunction that loads all the search types
apachesolr_search_menuImplements hook_menu().
apachesolr_search_menu_alter
apachesolr_search_mlt_block_load
apachesolr_search_mlt_suggestionsPerforms a moreLikeThis query using the settings and retrieves documents.
apachesolr_search_node_resultCallback function for node search results.
apachesolr_search_pager_default_initializeBackport of pager_default_initialize from Drupal 7.
apachesolr_search_pager_find_pageBackport of pager_find_page from Drupal 7.
apachesolr_search_page_browseHandle browse results for empty searches.
apachesolr_search_page_cloneFunction that clones a search page
apachesolr_search_page_existsReturns whether a search page exists.
apachesolr_search_page_loadLoad a search page
apachesolr_search_page_save
apachesolr_search_preprocess_search_resultTemplate preprocess for apachesolr search results.
apachesolr_search_preprocess_search_results@todo Make sure the paging works for Drupal 6
apachesolr_search_process_response@todo Make sure the paging works for Drupal 6
apachesolr_search_runExecute a search results based on keyword, filter, and sort strings.
apachesolr_search_run_emptyExecute a search with zero results rows so as to populate facets.
apachesolr_search_searchImplements of hook_search().
apachesolr_search_search_box_form_submitProcess a block search form submission.
apachesolr_search_search_executeHelper function to retrieve results
apachesolr_search_search_pageThe default search page
apachesolr_search_search_page_customMimics apachesolr_search_search_page() but is used for custom search pages We prefer to keep them seperate so we are not dependent from core search when someone tries to disable the core search
apachesolr_search_search_results
apachesolr_search_themeImplements hook_theme().
apachesolr_search_theme_registry_alterImplements hook_theme_registry_alter().
theme_apachesolr_search_browse_blocksShows a groups of blocks for starting a search from a filter.
theme_apachesolr_search_mlt_recommendation_block
theme_apachesolr_search_noresultsBrief message to display when no results match the query.
theme_apachesolr_search_snippetsTheme the highlighted snippet text for a search entry.
theme_apachesolr_search_suggestionsDefault theme function for spelling suggestions.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides a content search implementation for node content for use with the
  5. * Apache Solr search application.
  6. */
  7. /**
  8. * Implements hook_init().
  9. *
  10. * Checks if we should run an empty facet query so the facet blocks can be
  11. * displayed.
  12. */
  13. function apachesolr_search_init() {
  14. // Useless without facetapi
  15. if (!module_exists('facetapi')) {
  16. return;
  17. }
  18. // Using a simple query we will figure out if we have to execute this snippet
  19. // on every page or exit as fast as possible.
  20. $query = "SELECT count(env_id)
  21. FROM {apachesolr_environment_variable}
  22. WHERE name = 'apachesolr_search_show_facets'";
  23. $count = db_result(db_query($query));
  24. if ($count == 0) {
  25. return;
  26. }
  27. // Load the default search page, we only support facets to link to this
  28. // search page due to complexity and slow downs
  29. $search_page_id = apachesolr_search_default_search_page();
  30. $search_page = apachesolr_search_page_load($search_page_id);
  31. // Do not continue if our search page is not valid
  32. if (empty($search_page)) {
  33. return;
  34. }
  35. $show_facets = apachesolr_environment_variable_get($search_page['env_id'], 'apachesolr_search_show_facets', 0);
  36. if ($show_facets) {
  37. // Converts current path to lowercase for case insensitive matching.
  38. $paths = array();
  39. $paths[] = drupal_strtolower(drupal_get_path_alias($_GET['q']));
  40. $paths[] = drupal_strtolower($_GET['q']);
  41. $facet_pages = apachesolr_environment_variable_get($search_page['env_id'], 'apachesolr_search_facet_pages', '');
  42. // Iterates over each environment to check if an empty query should be run.
  43. if (!empty($facet_pages)) {
  44. // Compares path with settings, runs query if there is a match.
  45. $patterns = drupal_strtolower($facet_pages);
  46. foreach ($paths as $path) {
  47. if (drupal_match_path($path, $patterns)) {
  48. try {
  49. if (!empty($search_page['search_path'])) {
  50. $solr = apachesolr_get_solr($search_page['env_id']);
  51. // Initializes params for empty query.
  52. $params = array(
  53. 'spellcheck' => 'false',
  54. 'fq' => array(),
  55. 'rows' => 1,
  56. );
  57. apachesolr_search_run_empty('apachesolr', $params, $search_page['search_path'], $solr);
  58. }
  59. }
  60. catch (Exception $e) {
  61. watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * Implements hook_menu().
  70. */
  71. function apachesolr_search_menu() {
  72. $base_path = 'admin/settings/apachesolr/';
  73. $items[$base_path . 'search-pages'] = array(
  74. 'title' => 'Pages/Blocks',
  75. 'description' => 'Configure search pages',
  76. 'page callback' => 'apachesolr_search_page_list_all',
  77. 'access arguments' => array('administer search'),
  78. 'type' => MENU_LOCAL_TASK,
  79. 'file' => 'apachesolr_search.admin.inc',
  80. );
  81. $items[$base_path . 'search-pages/add'] = array(
  82. 'title' => 'Add search page',
  83. 'page callback' => 'drupal_get_form',
  84. 'page arguments' => array('apachesolr_search_page_settings_form'),
  85. 'access arguments' => array('administer search'),
  86. 'weight' => 1,
  87. 'file' => 'apachesolr_search.admin.inc',
  88. );
  89. $items[$base_path . 'search-pages/%apachesolr_search_page/edit'] = array(
  90. 'title' => 'Edit search page',
  91. 'page callback' => 'drupal_get_form',
  92. 'page arguments' => array('apachesolr_search_page_settings_form', 4),
  93. 'access arguments' => array('administer search'),
  94. 'file' => 'apachesolr_search.admin.inc',
  95. );
  96. $items[$base_path . 'search-pages/%apachesolr_search_page/delete'] = array(
  97. 'title' => 'Delete search page',
  98. 'page callback' => 'drupal_get_form',
  99. 'page arguments' => array('apachesolr_search_delete_search_page_confirm', 4),
  100. 'access arguments' => array('administer search'),
  101. 'file' => 'apachesolr_search.admin.inc',
  102. );
  103. $items[$base_path . 'search-pages/%apachesolr_search_page/clone'] = array(
  104. 'title' => 'Clone search page',
  105. 'page callback' => 'drupal_get_form',
  106. 'page arguments' => array('apachesolr_search_clone_search_page_confirm', 4),
  107. 'access arguments' => array('administer search'),
  108. 'file' => 'apachesolr_search.admin.inc',
  109. );
  110. $items[$base_path . 'search-pages/addblock'] = array(
  111. 'title' => 'Add search block "More Like This"',
  112. 'page callback' => 'drupal_get_form',
  113. 'page arguments' => array('apachesolr_search_mlt_add_block_form'),
  114. 'access arguments' => array('administer search'),
  115. 'weight' => 2,
  116. 'file' => 'apachesolr_search.admin.inc',
  117. );
  118. $items[$base_path . 'search-pages/block/%apachesolr_search_mlt_block/delete'] = array(
  119. 'page callback' => 'drupal_get_form',
  120. 'page arguments' => array('apachesolr_search_mlt_delete_block_form', 5),
  121. 'access arguments' => array('administer search'),
  122. 'file' => 'apachesolr_search.admin.inc',
  123. 'type' => MENU_CALLBACK,
  124. );
  125. // Environment specific settings
  126. $settings_path = 'settings/';
  127. $items[$base_path . $settings_path . '%apachesolr_environment/bias'] = array(
  128. 'title' => 'Bias',
  129. 'page callback' => 'apachesolr_bias_settings_page',
  130. 'page arguments' => array(4),
  131. 'access arguments' => array('administer search'),
  132. 'weight' => 4,
  133. 'type' => MENU_LOCAL_TASK,
  134. 'file' => 'apachesolr_search.admin.inc',
  135. );
  136. return $items;
  137. }
  138. function apachesolr_search_menu_alter(&$items) {
  139. // Gets default search information.
  140. $search_types = apachesolr_search_load_all_search_types();
  141. $search_pages = apachesolr_search_load_all_search_pages();
  142. // Iterates over search pages, builds menu items.
  143. foreach ($search_pages as $search_page) {
  144. // Validate the environment ID in case of import or missed deletion.
  145. $environment = apachesolr_environment_load($search_page['env_id']);
  146. if (!$environment) {
  147. continue;
  148. }
  149. $search_page = apachesolr_search_page_load($search_page['page_id']);
  150. if (!$search_page) {
  151. continue;
  152. }
  153. // Parses search path into it's various parts, builds menu items dependent
  154. // on whether %keys is in the path.
  155. $parts = explode('/', $search_page['search_path']);
  156. $keys_pos = count($parts);
  157. // Tests whether we are simulating a core search tab.
  158. $core_search = ($parts[0] == 'search');
  159. $taxonomy_search = ($search_page['search_path'] == 'taxonomy/term/%');
  160. $position = array_search('%', $parts);
  161. $page_title = isset($search_page['page_title']) ? $search_page['page_title'] : 'Search Results';
  162. // Replace possible tokens [term:tid], [node:nid], [user:uid] with their
  163. // menu-specific variant
  164. $items[$search_page['search_path']] = array(
  165. 'title' => $page_title,
  166. 'page callback' => 'apachesolr_search_custom_page',
  167. 'page arguments' => array($search_page['page_id'], '', $position),
  168. 'access arguments' => array('search content'),
  169. 'type' => ($core_search) ? MENU_LOCAL_TASK : MENU_SUGGESTED_ITEM,
  170. 'file' => 'apachesolr_search.pages.inc',
  171. 'file path' => drupal_get_path('module', 'apachesolr_search'),
  172. );
  173. $items[$search_page['search_path'] . '/%menu_tail'] = array(
  174. 'title' => $page_title,
  175. 'load arguments' => array('%map', '%index'),
  176. 'page callback' => 'apachesolr_search_custom_page',
  177. 'page arguments' => array($search_page['page_id'], $keys_pos, $position),
  178. 'access arguments' => array('search content'),
  179. 'type' => ($taxonomy_search) ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
  180. 'file' => 'apachesolr_search.pages.inc',
  181. 'file path' => drupal_get_path('module', 'apachesolr_search'),
  182. );
  183. // If title has a certain callback for the selected type we use it
  184. $search_type_id = !empty($search_page['settings']['apachesolr_search_search_type']) ? $search_page['settings']['apachesolr_search_search_type'] : FALSE;
  185. $search_type = !empty($search_types[$search_type_id]) ? $search_types[$search_type_id] : FALSE;
  186. if ($search_type && !empty($position)) {
  187. $title_callback = $search_type['title callback'];
  188. $items[$search_page['search_path']]['title callback'] = $title_callback;
  189. $items[$search_page['search_path']]['title arguments'] = array($search_page['page_id'], $position);
  190. $items[$search_page['search_path'] . '/%menu_tail']['title callback'] = $title_callback;
  191. $items[$search_page['search_path'] . '/%menu_tail']['title arguments'] = array($search_page['page_id'], $position);
  192. }
  193. // If we have additional searches in the search/* path
  194. /*if ($core_search) {
  195. $items[$search_page['search_path'] . '/%menu_tail']['tab_root'] = 'search/' . $default_info['path'] . '/%';
  196. $items[$search_page['search_path'] . '/%menu_tail']['tab_parent'] = 'search/' . $default_info['path'];
  197. }*/
  198. if ($taxonomy_search) {
  199. unset($items['taxonomy/term/%taxonomy_term']);
  200. unset($items['taxonomy/term/%taxonomy_term/view']);
  201. }
  202. }
  203. }
  204. /**
  205. * Function that loads all the search types
  206. *
  207. * @return array $search_types
  208. */
  209. function apachesolr_search_load_all_search_types() {
  210. static $search_types;
  211. if (isset($search_types)) {
  212. return $search_types;
  213. }
  214. // Use cache_get to avoid DB when using memcache, etc.
  215. $cache = cache_get('apachesolr_search:search_types', 'cache_apachesolr');
  216. if (isset($cache->data)) {
  217. $search_types = $cache->data;
  218. }
  219. else {
  220. $search_types = array(
  221. 'tid' => array(
  222. 'name' => apachesolr_field_name_map('tid'),
  223. 'default menu' => 'taxonomy/term/%',
  224. 'title callback' => 'apachesolr_search_get_taxonomy_term_title',
  225. ),
  226. 'is_uid' => array(
  227. 'name' => apachesolr_field_name_map('is_uid'),
  228. 'default menu' => 'user/%/search',
  229. 'title callback' => 'apachesolr_search_get_user_title',
  230. ),
  231. 'bundle' => array(
  232. 'name' => apachesolr_field_name_map('bundle'),
  233. 'default menu' => 'search/type/%',
  234. 'title callback' => 'apachesolr_search_get_value_title',
  235. ),
  236. 'ss_language' => array(
  237. 'name' => apachesolr_field_name_map('ss_language'),
  238. 'default menu' => 'search/language/%',
  239. 'title callback' => 'apachesolr_search_get_value_title',
  240. ),
  241. );
  242. drupal_alter('apachesolr_search_types', $search_types);
  243. cache_set('apachesolr_search:search_types', $search_types, 'cache_apachesolr');
  244. }
  245. return $search_types;
  246. }
  247. /**
  248. * Used as a callback function to generate a title for the taxonomy term
  249. * depending on the input in the configuration screen
  250. * @param integer $search_page_id
  251. * @param integer $value
  252. * @return String
  253. */
  254. function apachesolr_search_get_taxonomy_term_title($search_page_id = NULL, $value = NULL) {
  255. $page_title = 'Search results for %value';
  256. if (isset($value) && isset($search_page_id)) {
  257. $search_page = apachesolr_search_page_load($search_page_id);
  258. $page_title = str_replace('%value', '!value', $search_page['page_title']);
  259. $term = taxonomy_get_term($value);
  260. if (!$term) {
  261. return;
  262. }
  263. $title = $term->name;
  264. }
  265. return t($page_title, array('!value' => $title));
  266. }
  267. /**
  268. * Used as a callback function to generate a title for a user name depending
  269. * on the input in the configuration screen
  270. * @param integer $search_page_id
  271. * @param integer $value
  272. * @return String
  273. */
  274. function apachesolr_search_get_user_title($search_page_id = NULL, $value = NULL) {
  275. $page_title = 'Search results for %value';
  276. $title = '';
  277. if (isset($value) && isset($search_page_id)) {
  278. $search_page = apachesolr_search_page_load($search_page_id);
  279. $page_title = str_replace('%value', '!value', $search_page['page_title']);
  280. $user = user_load($value);
  281. if (!$user) {
  282. return;
  283. }
  284. $title = $user->name;
  285. }
  286. return t($page_title, array('!value' => $title));
  287. }
  288. /**
  289. * Used as a callback function to generate a title for a node/page depending
  290. * on the input in the configuration screen
  291. * @param integer $search_page_id
  292. * @param integer $value
  293. * @return String
  294. */
  295. function apachesolr_search_get_value_title($search_page_id = NULL, $value = NULL) {
  296. $page_title = 'Search results for %value';
  297. if (isset($value) && isset($search_page_id)) {
  298. $search_page = apachesolr_search_page_load($search_page_id);
  299. $page_title = str_replace('%value', '!value', $search_page['page_title']);
  300. $title = $value;
  301. }
  302. return t($page_title, array('!value' => $title));
  303. }
  304. /**
  305. * Get or set the default search page id for the current page.
  306. */
  307. function apachesolr_search_default_search_page($page_id = NULL, $reset = FALSE) {
  308. static $default_page_id = NULL;
  309. if ($reset) {
  310. $default_page_id = array();
  311. }
  312. if (isset($page_id)) {
  313. $default_page_id = $page_id;
  314. }
  315. if (empty($default_page_id)) {
  316. $default_page_id = variable_get('apachesolr_search_default_search_page', 'core_search');
  317. }
  318. return $default_page_id;
  319. }
  320. /**
  321. * Implements hook_apachesolr_default_environment()
  322. *
  323. * Make sure the core search page is using the default environment.
  324. */
  325. function apachesolr_search_apachesolr_default_environment($env_id, $old_env_id) {
  326. $page = apachesolr_search_page_load('core_search');
  327. if ($page && $page['env_id'] != $env_id) {
  328. $page['env_id'] = $env_id;
  329. apachesolr_search_page_save($page);
  330. }
  331. }
  332. /**
  333. * Load a search page
  334. * @param string $page_id
  335. * @return array
  336. */
  337. function apachesolr_search_page_load($page_id, $reset = FALSE) {
  338. $search_pages = apachesolr_search_load_all_search_pages($reset);
  339. if (!empty($search_pages[$page_id])) {
  340. return $search_pages[$page_id];
  341. }
  342. return FALSE;
  343. }
  344. function apachesolr_search_page_save($search_page) {
  345. if (!empty($search_page) && db_table_exists('apachesolr_search_page')) {
  346. $query = "INSERT INTO {apachesolr_search_page} (page_id, label, description, env_id, search_path, page_title, settings)
  347. VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')
  348. ON DUPLICATE KEY UPDATE page_id = '%s', label = '%s', description = '%s', env_id = '%s', search_path = '%s', page_title = '%s', settings = '%s'";
  349. $arguments = array(
  350. $search_page['page_id'],
  351. $search_page['label'],
  352. $search_page['description'],
  353. $search_page['env_id'],
  354. $search_page['search_path'],
  355. $search_page['page_title'],
  356. serialize($search_page['settings']),
  357. $search_page['page_id'],
  358. $search_page['label'],
  359. $search_page['description'],
  360. $search_page['env_id'],
  361. $search_page['search_path'],
  362. $search_page['page_title'],
  363. serialize($search_page['settings']),
  364. );
  365. db_query($query, $arguments);
  366. }
  367. }
  368. /**
  369. * Function that clones a search page
  370. *
  371. * @param $page_id
  372. * The page identifier it needs to clone.
  373. *
  374. */
  375. function apachesolr_search_page_clone($page_id) {
  376. $search_page = apachesolr_search_page_load($page_id);
  377. // Get all search_pages
  378. $search_pages = apachesolr_search_load_all_search_pages();
  379. // Create an unique ID
  380. $new_search_page_id = apachesolr_create_unique_id($search_pages, $search_page['page_id']);
  381. // Set this id to the new search page
  382. $search_page['page_id'] = $new_search_page_id;
  383. $search_page['label'] = $search_page['label'] . ' [cloned]';
  384. // All cloned search pages should be removable
  385. if (isset($search_page['settings']['apachesolr_search_not_removable'])) {
  386. unset($search_page['settings']['apachesolr_search_not_removable']);
  387. }
  388. // Save our new search page in the database
  389. apachesolr_search_page_save($search_page);
  390. }
  391. /**
  392. * Implements hook_block().
  393. */
  394. function apachesolr_search_block($op = 'list', $delta = 0, $edit = array()) {
  395. switch ($op) {
  396. case 'list' :
  397. return apachesolr_search_block_info();
  398. break;
  399. case 'view' :
  400. return apachesolr_search_block_view($delta);
  401. break;
  402. case 'configure' :
  403. return apachesolr_search_block_configure($delta);
  404. break;
  405. case 'save' :
  406. return apachesolr_search_block_save($delta, $edit);
  407. break;
  408. }
  409. }
  410. /**
  411. * Retrieve all possible MLT blocks
  412. */
  413. function apachesolr_search_block_info() {
  414. // Get all of the moreLikeThis blocks that the user has created
  415. $blocks = apachesolr_search_load_all_mlt_blocks();
  416. foreach ($blocks as $delta => $settings) {
  417. $blocks[$delta] += array(
  418. 'info' => t('Apache Solr recommendations: !name', array('!name' => $settings['name'])));
  419. }
  420. // Add the sort block.
  421. $blocks['sort'] = array(
  422. 'info' => t('Apache Solr Core: Sorting'),
  423. );
  424. return $blocks;
  425. }
  426. /**
  427. * Vew a specific block according to the delta identifier
  428. * @param string $delta
  429. * @return string
  430. */
  431. function apachesolr_search_block_view($delta = '') {
  432. if ($delta != 'sort' && ($node = menu_get_object()) && (!arg(2) || arg(2) == 'view')) {
  433. $suggestions = array();
  434. // Determine whether the user can view the current node. Probably not necessary.
  435. $block = apachesolr_search_mlt_block_load($delta);
  436. if ($block && node_access('view', $node)) {
  437. // Get our specific environment for the MLT block
  438. $env_id = (!empty($block['mlt_env_id'])) ? $block['mlt_env_id'] : '';
  439. try {
  440. $solr = apachesolr_get_solr($env_id);
  441. $docs = apachesolr_search_mlt_suggestions($block, apachesolr_document_id($node->nid), $solr);
  442. if (!empty($docs)) {
  443. $suggestions['subject'] = check_plain($block['name']);
  444. $suggestions['content'] = theme('apachesolr_search_mlt_recommendation_block', $docs, $delta);
  445. }
  446. }
  447. catch (Exception $e) {
  448. watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
  449. }
  450. }
  451. return $suggestions;
  452. }
  453. else {
  454. $environments = apachesolr_load_all_environments();
  455. foreach ($environments as $env_id => $environment) {
  456. if (apachesolr_has_searched($env_id) && !apachesolr_suppress_blocks($env_id) && $delta == 'sort') {
  457. $response = NULL;
  458. $query = apachesolr_current_query($env_id);
  459. if ($query) {
  460. // Get the query and response. Without these no blocks make sense.
  461. $response = apachesolr_static_response_cache($query->getSearcher());
  462. }
  463. if (empty($response) || ($response->response->numFound < 2)) {
  464. return;
  465. }
  466. $sorts = $query->getAvailableSorts();
  467. // Get the current sort as an array.
  468. $solrsort = $query->getSolrsort();
  469. $sort_links = array();
  470. $path = $query->getPath();
  471. $new_query = clone $query;
  472. $toggle = array('asc' => 'desc', 'desc' => 'asc');
  473. foreach ($sorts as $name => $sort) {
  474. $active = $solrsort['#name'] == $name;
  475. if ($name == 'score') {
  476. $direction = '';
  477. $new_direction = 'desc'; // We only sort by descending score.
  478. }
  479. elseif ($active) {
  480. $direction = $toggle[$solrsort['#direction']];
  481. $new_direction = $toggle[$solrsort['#direction']];
  482. }
  483. else {
  484. $direction = '';
  485. $new_direction = $sort['default'];
  486. }
  487. $new_query->setSolrsort($name, $new_direction);
  488. $sort_links[$name] = array(
  489. 'text' => $sort['title'],
  490. 'path' => $path,
  491. 'options' => array('query' => $new_query->getSolrsortUrlQuery()),
  492. 'active' => $active,
  493. 'direction' => $direction,
  494. );
  495. }
  496. foreach ($sort_links as $name => $link) {
  497. $themed_links[$name] = theme('apachesolr_sort_link', $link);
  498. }
  499. $output = theme('apachesolr_sort_list', array('items' => $themed_links));
  500. return array(
  501. 'subject' => t('Sort by'),
  502. 'content' => $output,
  503. );
  504. }
  505. }
  506. }
  507. }
  508. /**
  509. * Implements hook_form_[form_id]_alter().
  510. */
  511. function apachesolr_search_form_block_admin_display_form_alter(&$form) {
  512. foreach ($form as $key => $block) {
  513. if (isset($block['delta']['#value'])) {
  514. if ((strpos($key, "apachesolr_search_mlt-") === 0) && $block['module']['#value'] == 'apachesolr_search') {
  515. $form[$key]['delete']['#value'] = l(t('Delete'), 'admin/settings/apachesolr/search-pages/block/' . $block['delta']['#value'] . '/delete');
  516. }
  517. }
  518. }
  519. }
  520. /**
  521. * Implements hook_block_configure().
  522. */
  523. function apachesolr_search_block_configure($delta = '') {
  524. if ($delta != 'sort') {
  525. require_once(drupal_get_path('module', 'apachesolr') . '/apachesolr_search.admin.inc');
  526. return apachesolr_search_mlt_block_form($delta);
  527. }
  528. }
  529. /**
  530. * Implements hook_block_save().
  531. */
  532. function apachesolr_search_block_save($delta = '', $edit = array()) {
  533. if ($delta != 'sort') {
  534. require_once(drupal_get_path('module', 'apachesolr') . '/apachesolr_search.admin.inc');
  535. apachesolr_search_mlt_save_block($edit, $delta);
  536. }
  537. }
  538. /**
  539. * Return all the saved search pages
  540. * @return array $search_pages
  541. * Array of all search pages
  542. */
  543. function apachesolr_search_load_all_search_pages($reset = FALSE) {
  544. static $search_pages = array();
  545. if ($reset) {
  546. $search_pages = array();
  547. if (module_exists('ctools')) {
  548. ctools_include('export');
  549. ctools_export_load_object_reset('apachesolr_search_page');
  550. }
  551. }
  552. if (!empty($search_pages)) {
  553. return $search_pages;
  554. }
  555. // Get all search_pages and their id
  556. if (db_table_exists('apachesolr_search_page')) {
  557. // If ctools module is enabled, add search pages from code, e.g. from a
  558. // feature module.
  559. if (module_exists('ctools')) {
  560. ctools_include('export');
  561. $defaults = ctools_export_load_object('apachesolr_search_page', 'all');
  562. foreach ($defaults as $page_id => $default) {
  563. if (!isset($search_pages[$page_id])) {
  564. $search_pages[$page_id] = (array) $default;
  565. }
  566. }
  567. }
  568. // Get all search_pages and their id
  569. $search_pages_db = array();
  570. $search_pages_db_results = db_query('SELECT * FROM {apachesolr_search_page}');
  571. while($search_pages_db_result = db_fetch_array($search_pages_db_results)) {
  572. $search_pages_db[$search_pages_db_result['page_id']] = $search_pages_db_result;
  573. }
  574. $search_pages = $search_pages + $search_pages_db;
  575. // Ensure that the core search page uses the default environment. In some
  576. // instances, for example when unit testing, this search page isn't defined.
  577. if (isset($search_pages['core_search'])) {
  578. $search_pages['core_search']['env_id'] = apachesolr_default_environment();
  579. }
  580. // convert settings to an array
  581. foreach ($search_pages as $id => $search_page) {
  582. if (is_string($search_pages[$id]['settings'])) {
  583. $search_pages[$id]['settings'] = unserialize($search_pages[$id]['settings']);
  584. }
  585. }
  586. }
  587. return $search_pages;
  588. }
  589. function apachesolr_search_load_all_mlt_blocks() {
  590. $search_blocks = variable_get('apachesolr_search_mlt_blocks', array());
  591. return $search_blocks;
  592. }
  593. function apachesolr_search_mlt_block_load($block_id) {
  594. $search_blocks = variable_get('apachesolr_search_mlt_blocks', array());
  595. return isset($search_blocks[$block_id]) ? $search_blocks[$block_id] : FALSE;
  596. }
  597. /**
  598. * Performs a moreLikeThis query using the settings and retrieves documents.
  599. *
  600. * @param $settings
  601. * An array of settings.
  602. * @param $id
  603. * The Solr ID of the document for which you want related content.
  604. * For a node that is apachesolr_document_id($node->nid)
  605. * @param $solr
  606. * The solr environment you want to query against
  607. *
  608. * @return An array of response documents, or NULL
  609. */
  610. function apachesolr_search_mlt_suggestions($settings, $id, $solr = NULL) {
  611. try {
  612. $fields = array(
  613. 'mlt_mintf' => 'mlt.mintf',
  614. 'mlt_mindf' => 'mlt.mindf',
  615. 'mlt_minwl' => 'mlt.minwl',
  616. 'mlt_maxwl' => 'mlt.maxwl',
  617. 'mlt_maxqt' => 'mlt.maxqt',
  618. 'mlt_boost' => 'mlt.boost',
  619. 'mlt_qf' => 'mlt.qf',
  620. );
  621. $params = array(
  622. 'q' => 'id:' . $id,
  623. 'qt' => 'mlt',
  624. 'fl' => array('entity_id', 'entity_type', 'label', 'path', 'url'),
  625. 'mlt.fl' => $settings['mlt_fl'],
  626. 'start' => 0,
  627. 'rows' => $settings['num_results'],
  628. );
  629. // We can optionally specify a Solr object.
  630. $query = apachesolr_drupal_query('apachesolr_mlt', $params, '', '', $solr);
  631. foreach ($fields as $form_key => $name) {
  632. if (!empty($settings[$form_key])) {
  633. $query->addParam($name, $settings[$form_key]);
  634. }
  635. }
  636. $type_filters = array();
  637. if (is_array($settings['mlt_type_filters']) && !empty($settings['mlt_type_filters'])) {
  638. $query->addFilter('bundle', '(' . implode(' OR ', $settings['mlt_type_filters']) . ') ');
  639. }
  640. if ($custom_filters = $settings['mlt_custom_filters']) {
  641. // @todo - fix the settings form to take a comma-delimited set of filters.
  642. $query->addFilter('', $custom_filters);
  643. }
  644. // This hook allows modules to modify the query object.
  645. drupal_alter('apachesolr_query', $query);
  646. if ($query->abort_search) {
  647. return;
  648. }
  649. $response = $query->search();
  650. if (isset($response->response->docs)) {
  651. return (array) $response->response->docs;
  652. }
  653. }
  654. catch (Exception $e) {
  655. watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
  656. }
  657. }
  658. function theme_apachesolr_search_mlt_recommendation_block($vars) {
  659. $docs = $vars;
  660. $links = array();
  661. foreach ($docs as $result) {
  662. // Suitable for single-site mode. Label is already safe.
  663. $links[] = l($result->label, $result->path, array('html' => TRUE));
  664. }
  665. $links = theme('item_list', $links);
  666. return $links;
  667. }
  668. /**
  669. * Implements of hook_search().
  670. */
  671. function apachesolr_search_search($op = 'search', $keys = NULL) {
  672. module_load_include('inc', 'apachesolr', 'apachesolr.index');
  673. switch ($op) {
  674. case 'reset':
  675. $env_id = apachesolr_default_environment();
  676. apachesolr_index_mark_for_reindex($env_id);
  677. return;
  678. case 'status':
  679. $env_id = apachesolr_default_environment();
  680. return apachesolr_index_status($env_id);
  681. /* case 'search':
  682. $conditions_callback = variable_get('apachesolr_search_conditions_callback', 'apachesolr_search_conditions');
  683. $conditions = call_user_func($conditions_callback);
  684. try {
  685. $results = apachesolr_search_search_execute($keys, $conditions);
  686. return $results;
  687. }
  688. catch (Exception $e) {
  689. watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
  690. apachesolr_failure(t('Solr search'), $keys);
  691. }
  692. break;
  693. */
  694. } // switch
  695. }
  696. /**
  697. * Implements hook_form_[form_id]_alter().
  698. *
  699. * This adds the 0 option to the search admin form.
  700. */
  701. function apachesolr_search_form_search_admin_settings_alter(&$form, $form_state) {
  702. $form['indexing_throttle']['search_cron_limit']['#options']['0'] = '0';
  703. ksort($form['indexing_throttle']['search_cron_limit']['#options']);
  704. }
  705. /**
  706. * Implements hook_form_[form_id]_alter().
  707. */
  708. function apachesolr_search_form_search_theme_form_alter(&$form, $form_state) {
  709. apachesolr_search_form_search_block_form_alter($form, $form_state);
  710. }
  711. /**
  712. * Implements hook_form_[form_id]_alter().
  713. */
  714. function apachesolr_search_form_search_block_form_alter(&$form, &$form_state) {
  715. $search_pages = apachesolr_search_load_all_search_pages();
  716. $redirect_to = variable_get('apachesolr_search_default_search_page', 'drupal_core');
  717. if ($redirect_to == 'drupal_core') {
  718. return;
  719. }
  720. if (!empty($search_pages[$redirect_to])) {
  721. $form['#action'] = url($search_pages[$redirect_to]['search_path']);
  722. if (!isset($form['#submit'])) {
  723. $form['#submit'] = array('apachesolr_search_search_box_form_submit');
  724. }
  725. else {
  726. $key = array_search('search_box_form_submit', $form['#submit']);
  727. if ($key !== FALSE) {
  728. // Replace the search module's function.
  729. $form['#submit'][$key] = 'apachesolr_search_search_box_form_submit';
  730. }
  731. }
  732. }
  733. }
  734. /**
  735. * Process a block search form submission.
  736. *
  737. * @see search_box_form_submit()
  738. */
  739. function apachesolr_search_search_box_form_submit($form, &$form_state) {
  740. // The search form relies on control of the redirect destination for its
  741. // functionality, so we override any static destination set in the request,
  742. // for example by drupal_access_denied() or drupal_not_found()
  743. // (see http://drupal.org/node/292565).
  744. if (isset($_REQUEST['destination'])) {
  745. unset($_REQUEST['destination']);
  746. }
  747. if (isset($_REQUEST['edit']['destination'])) {
  748. unset($_REQUEST['edit']['destination']);
  749. }
  750. $form_id = $form['form_id']['#value'];
  751. $keys = $form_state['values'][$form_id];
  752. // Replace keys with their rawurlencoded value
  753. $keys = str_replace("/","%2f", $keys);
  754. // Handle Apache webserver clean URL quirks.
  755. if (variable_get('clean_url', '0')) {
  756. $keys = str_replace('+', '%2B', $keys);
  757. }
  758. $path = $form['#action'];
  759. // Remove base_path
  760. $base_path = '#^' . base_path() . '#';
  761. if (preg_match($base_path, $path)) {
  762. $path = preg_replace($base_path, '', $path);
  763. }
  764. // Set our path to the correct search page
  765. $form_state['redirect'] = $path . '/' .trim($keys);
  766. }
  767. /**
  768. * Helper function to retrieve results
  769. * @param $keys
  770. * The keys that are available after the path that is defined in
  771. * hook_search_info
  772. * @param $conditions
  773. * Conditions that are coming from apachesolr_search_conditions
  774. */
  775. function apachesolr_search_search_execute($keys = NULL, $conditions = NULL) {
  776. $search_page = apachesolr_search_page_load('core_search');
  777. $results = apachesolr_search_search_results($keys, $conditions, $search_page);
  778. return $results;
  779. }
  780. /**
  781. * The default conditions callback.
  782. */
  783. function apachesolr_search_conditions() {
  784. //get default conditions from the core_search
  785. $search_page = apachesolr_search_page_load('core_search');
  786. $conditions = apachesolr_search_conditions_default($search_page);
  787. return $conditions;
  788. }
  789. /**
  790. * The default search page
  791. * @param $results
  792. * The results that came from apache solr
  793. */
  794. function apachesolr_search_search_page($results) {
  795. $search_page = apachesolr_search_page_load('core_search');
  796. $build = apachesolr_search_search_page_custom($results, $search_page);
  797. return $build;
  798. }
  799. /**
  800. * Mimics apachesolr_search_search_page() but is used for custom search pages
  801. * We prefer to keep them seperate so we are not dependent from core search
  802. * when someone tries to disable the core search
  803. * @param $results
  804. * The results that came from apache solr
  805. * @param $build
  806. * the build array from where this function was called. Good to append output
  807. * to the build array
  808. * @param $search_page
  809. * the search page that is requesting an output
  810. */
  811. function apachesolr_search_search_page_custom($results, $search_page, $build = array()) {
  812. if (!empty($search_page['settings']['apachesolr_search_spellcheck'])) {
  813. // Retrieve suggestion
  814. $suggestions = apachesolr_search_get_search_suggestions($search_page['env_id']);
  815. if ($search_page && !empty($suggestions)) {
  816. $build['suggestions'] = theme('apachesolr_search_suggestions', array(l($suggestions[0], $search_page['search_path'] . '/' . $suggestions[0])));
  817. }
  818. }
  819. // Retrieve expected results from searching
  820. if (!empty($results['apachesolr_search_browse'])) {
  821. // Show facet browsing blocks.
  822. $build['search_results'] = apachesolr_search_page_browse($results['apachesolr_search_browse'], $search_page['env_id']);
  823. }
  824. elseif ($results) {
  825. $build['search_results'] = theme('search_results', $results, 'apachesolr_search', $search_page);
  826. }
  827. else {
  828. // Give the user some custom help text.
  829. $build['search_results'] = theme('apachesolr_search_noresults');
  830. }
  831. // Allows modules to alter the render array before returning.
  832. drupal_alter('apachesolr_search_page', $build, $search_page);
  833. return $build;
  834. }
  835. /*
  836. * Executes search depending on the conditions given.
  837. * See apachesolr_search.pages.inc for another use of this function
  838. */
  839. function apachesolr_search_search_results($keys = NULL, $conditions = NULL, $search_page = NULL) {
  840. $params = array();
  841. $results = array();
  842. // Process the search form. Note that if there is $_POST data,
  843. // search_form_submit() will cause a redirect to search/[module path]/[keys],
  844. // which will get us back to this page callback. In other words, the search
  845. // form submits with POST but redirects to GET. This way we can keep
  846. // the search query URL clean as a whistle.
  847. if (empty($_POST['form_id'])
  848. || ($_POST['form_id'] != 'apachesolr_search_custom_page_search_form')
  849. && ($_POST['form_id'] != 'search_form')
  850. && ($_POST['form_id'] != 'search_block_form') ) {
  851. // Check input variables
  852. if (empty($search_page)) {
  853. $search_page = apachesolr_search_page_load('core_search');
  854. // Verify if it actually loaded
  855. if (empty($search_page)) {
  856. // Something must have been really messed up.
  857. apachesolr_failure(t('Solr search'), $keys);
  858. return array();
  859. }
  860. }
  861. if (empty($conditions)) {
  862. $conditions = apachesolr_search_conditions_default($search_page);
  863. }
  864. // Sort options from the conditions array.
  865. // @see apachesolr_search_conditions_default()
  866. // See This condition callback to find out how.
  867. $solrsort = isset($conditions['apachesolr_search_sort']) ? $conditions['apachesolr_search_sort'] : '';
  868. // What to do when we have an initial empty search
  869. $empty_search_behavior = isset($search_page['settings']['apachesolr_search_browse']) ? $search_page['settings']['apachesolr_search_browse'] : '';
  870. $params = array();
  871. $results = array();
  872. try {
  873. $solr = apachesolr_get_solr($search_page['env_id']);
  874. // Default parameters
  875. $params['fq'] = isset($conditions['fq']) ? $conditions['fq'] : array();
  876. $params['rows'] = $search_page['settings']['apachesolr_search_per_page'];
  877. if (empty($search_page['settings']['apachesolr_search_spellcheck'])) {
  878. // Spellcheck needs to have a string as false/true
  879. $params['spellcheck'] = 'false';
  880. }
  881. else {
  882. $params['spellcheck'] = 'true';
  883. }
  884. // Empty text Behavior
  885. if (!$keys && !isset($conditions['f']) && ($empty_search_behavior == 'browse' || $empty_search_behavior == 'blocks')) {
  886. // Pass empty search behavior as string on to apachesolr_search_search_page()
  887. // Hardcoded apachesolr name since we rely on this for the facets
  888. apachesolr_search_run_empty('apachesolr', $params, $search_page['search_path'], $solr);
  889. $results['apachesolr_search_browse'] = $empty_search_behavior;
  890. if ($empty_search_behavior == 'browse') {
  891. // Hide sidebar blocks for content-area browsing instead.
  892. apachesolr_suppress_blocks($search_page['env_id'], TRUE);
  893. }
  894. }
  895. // Full text behavior. Triggers with a text search or a facet
  896. elseif (($keys || isset($conditions['f'])) || ($empty_search_behavior == 'results')) {
  897. $params['q'] = $keys;
  898. // Hardcoded apachesolr name since we rely on this for the facets
  899. $page = isset($_GET['page']) ? $_GET['page'] : '';
  900. $page = check_plain($page);
  901. $results = apachesolr_search_run('apachesolr', $params, $solrsort, $search_page['search_path'], $page, $solr);
  902. }
  903. }
  904. catch (Exception $e) {
  905. watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
  906. apachesolr_failure(t('Solr search'), $keys);
  907. }
  908. }
  909. return $results;
  910. }
  911. function apachesolr_search_conditions_default($search_page) {
  912. $conditions = array();
  913. $search_type = isset($search_page['settings']['apachesolr_search_search_type']) ? $search_page['settings']['apachesolr_search_search_type'] : '';
  914. $allow_user_input = isset($search_page['settings']['apachesolr_search_allow_user_input']) ? $search_page['settings']['apachesolr_search_allow_user_input'] : FALSE;
  915. $path_replacer = isset($search_page['settings']['apachesolr_search_path_replacer']) ? $search_page['settings']['apachesolr_search_path_replacer'] : '';
  916. $set_custom_filter = isset($search_page['settings']['apachesolr_search_custom_enable']) ? $search_page['settings']['apachesolr_search_custom_enable'] : '';
  917. $search_page_fq = !empty($search_page['settings']['fq']) ? $search_page['settings']['fq'] : '';
  918. $conditions['fq'] = array();
  919. // We only allow this to happen if the search page explicitely allows it
  920. if ($allow_user_input) {
  921. // Get the filterQueries from the url
  922. if (!empty($_GET['fq']) && is_array($_GET['fq'])) {
  923. // Reset the array so that we have one level lower to go through
  924. $conditions['fq'] = $_GET['fq'];
  925. }
  926. foreach($conditions['fq'] as $condition_id => $condition) {
  927. // If the user input does not pass our validation we do not allow
  928. // it to query solr
  929. $test_query = apachesolr_drupal_subquery('Test');
  930. if (!$test_query->validFilterValue($condition)) {
  931. unset($conditions['fq'][$condition_id]);
  932. }
  933. }
  934. }
  935. // Custom filters added in search pages
  936. if (!empty($search_page_fq) && !empty($set_custom_filter)) {
  937. if (!empty($path_replacer)) {
  938. // If the manual filter has a % in it, replace it with $value
  939. $conditions['fq'][] = str_replace('%', $path_replacer, $search_page_fq);
  940. }
  941. else {
  942. // Put the complete filter in the filter query
  943. $conditions['fq'][] = $search_page_fq;
  944. }
  945. }
  946. // Search type filters (such as taxonomy)
  947. if (!empty($path_replacer) && !empty($search_type) && $search_type != 'custom') {
  948. $conditions['fq'][] = $search_type . ':' . $path_replacer;
  949. }
  950. // We may also have filters added by facet API module. The 'f'
  951. // is determined by variable FacetapiUrlProcessor::$filterKey. Hard
  952. // coded here to avoid extra class loading.
  953. if (!empty($_GET['f']) && is_array($_GET['f'])) {
  954. if (module_exists('facetapi')) {
  955. $conditions['f'] = $_GET['f'];
  956. }
  957. }
  958. // Add the sort from the page to our conditions
  959. $sort = isset($_GET['solrsort']) ? $_GET['solrsort'] : '';
  960. $conditions['apachesolr_search_sort'] = $sort;
  961. return $conditions;
  962. }
  963. /**
  964. * Handle browse results for empty searches.
  965. */
  966. function apachesolr_search_page_browse($empty_search_behavior, $env_id) {
  967. $blocks = array();
  968. // Switch in case we come up with new flags.
  969. switch ($empty_search_behavior) {
  970. case 'browse':
  971. if (module_exists('facetapi') && $query = apachesolr_current_query($env_id)) {
  972. module_load_include('inc', 'facetapi', 'facetapi.block');
  973. // Get facet render elements.
  974. $searcher = $query->getSearcher();
  975. $elements = facetapi_build_realm($searcher, 'block');
  976. foreach (element_children($elements) as $key) {
  977. $delta = "facetapi_{$key}";
  978. // @todo: order/filter these pseudo-blocks according to block.module weight, visibility (see 7.x-1beta4)
  979. $block = new stdClass();
  980. $block->visibility = TRUE;
  981. $block->enabled = TRUE;
  982. $block->module = 'facetapi';
  983. $block->subject = theme('facetapi_title', array('title' => $elements[$key]['#title']));
  984. //$build[$delta] = $elements[$key];
  985. $block->region = NULL;
  986. $block->delta = 'apachesolr-' . $key;
  987. $block->content = $elements[$key][$key];
  988. if (!empty($block->content)) {
  989. $blocks[$delta] = $block;
  990. }
  991. }
  992. }
  993. break;
  994. }
  995. return theme('apachesolr_search_browse_blocks', $blocks);
  996. }
  997. /**
  998. * Shows a groups of blocks for starting a search from a filter.
  999. */
  1000. function theme_apachesolr_search_browse_blocks($blocks) {
  1001. $result = "<div class='apachesolr_browse_block'><h2>" . t('Browse available categories') .'</h2>';
  1002. $result .= '<p>' . t('Pick a category to launch a search.') . '</p>';
  1003. foreach ($blocks as $facet_field => $block) {
  1004. $result .= theme('block', $block);
  1005. }
  1006. $result .= '</div>';
  1007. return $result;
  1008. }
  1009. /**
  1010. * Execute a search with zero results rows so as to populate facets.
  1011. */
  1012. function apachesolr_search_run_empty($name, array $params = array(), $base_path = '', $solr) {
  1013. $query = apachesolr_drupal_query($name, $params, '', $base_path, $solr);
  1014. $query->addParam('rows', '0');
  1015. $solr_id = $query->solr('getId');
  1016. list($final_query, $response) = apachesolr_do_query($query);
  1017. apachesolr_has_searched($solr_id, TRUE);
  1018. }
  1019. /**
  1020. * Execute a search results based on keyword, filter, and sort strings.
  1021. *
  1022. * @param $name
  1023. * @param $params
  1024. * Array - 'q' is the keywords to search.
  1025. * @param $solrsort
  1026. * @param $base_path
  1027. * For constructing filter and sort links. Leave empty unless the links need to point somewhere
  1028. * other than the base path of the current request.
  1029. * @param integer $page
  1030. * For pagination.
  1031. * @param DrupalApacheSolrServiceInterface $solr
  1032. * The solr server resource to execute the search on.
  1033. *
  1034. * @return stdClass $response
  1035. *
  1036. * @throws Exception
  1037. */
  1038. function apachesolr_search_run($name, array $params = array(), $solrsort = '', $base_path = '', $page = 0, DrupalApacheSolrServiceInterface $solr = NULL) {
  1039. // Merge the default params into the params sent in.
  1040. $params += apachesolr_search_basic_params();
  1041. // This is the object that knows about the query coming from the user.
  1042. $query = apachesolr_drupal_query($name, $params, $solrsort, $base_path, $solr);
  1043. if ($query->getParam('q')) {
  1044. apachesolr_search_add_spellcheck_params($query);
  1045. }
  1046. // Add the paging parameters
  1047. $query->page = $page;
  1048. apachesolr_search_add_boost_params($query);
  1049. if ($query->getParam('q')) {
  1050. apachesolr_search_highlighting_params($query);
  1051. if (!$query->getParam('hl.fl')) {
  1052. $qf = array();
  1053. foreach ($query->getParam('qf') as $field) {
  1054. // Truncate off any boost so we get the simple field name.
  1055. $parts = explode('^', $field, 2);
  1056. $qf[$parts[0]] = TRUE;
  1057. }
  1058. foreach (array('content', 'ts_comments') as $field) {
  1059. if (isset($qf[$field])) {
  1060. $query->addParam('hl.fl', $field);
  1061. }
  1062. }
  1063. }
  1064. }
  1065. else {
  1066. // No highlighting, use the teaser as a snippet.
  1067. $query->addParam('fl', 'teaser');
  1068. }
  1069. list($final_query, $response) = apachesolr_do_query($query, $page);
  1070. $env_id = $query->solr('getId');
  1071. apachesolr_has_searched($env_id, TRUE);
  1072. $process_response_callback = apachesolr_environment_variable_get($env_id, 'process_response_callback', 'apachesolr_search_process_response');
  1073. if (function_exists($process_response_callback)) {
  1074. return call_user_func($process_response_callback, $response, $final_query);
  1075. }
  1076. else {
  1077. return apachesolr_search_process_response($response, $final_query);
  1078. }
  1079. }
  1080. function apachesolr_search_basic_params(DrupalSolrQueryInterface $query = NULL) {
  1081. $params = array(
  1082. 'fl' => array(
  1083. 'id',
  1084. 'entity_id',
  1085. 'entity_type',
  1086. 'bundle',
  1087. 'bundle_name',
  1088. 'label',
  1089. 'is_comment_count',
  1090. 'ds_created',
  1091. 'ds_changed',
  1092. 'score',
  1093. 'path',
  1094. 'url',
  1095. 'is_uid',
  1096. 'tos_name',
  1097. ),
  1098. 'rows' => 10,
  1099. );
  1100. if ($query) {
  1101. $query->addParams($params);
  1102. }
  1103. return $params;
  1104. }
  1105. /**
  1106. * Add highlighting settings to the search params.
  1107. *
  1108. * These settings are set in solrconfig.xml.
  1109. * See the defaults there.
  1110. * If you wish to override them, you can via settings.php or drush
  1111. */
  1112. function apachesolr_search_highlighting_params(DrupalSolrQueryInterface $query = NULL) {
  1113. $params['hl'] = variable_get('apachesolr_hl_active', NULL);
  1114. $params['hl.fragsize']= variable_get('apachesolr_hl_textsnippetlength', NULL);
  1115. $params['hl.simple.pre'] = variable_get('apachesolr_hl_pretag', NULL);
  1116. $params['hl.simple.post'] = variable_get('apachesolr_hl_posttag', NULL);
  1117. $params['hl.snippets'] = variable_get('apachesolr_hl_numsnippets', NULL);
  1118. // This should be an array of possible field names.
  1119. $params['hl.fl'] = variable_get('apachesolr_hl_fieldtohighlight', NULL);
  1120. $params = array_filter($params);
  1121. if ($query) {
  1122. $query->addParams($params);
  1123. }
  1124. return $params;
  1125. }
  1126. function apachesolr_search_add_spellcheck_params(DrupalSolrQueryInterface $query) {
  1127. $params = array();
  1128. // Add new parameter to the search request
  1129. $params['spellcheck.q'] = $query->getParam('q');
  1130. $params['spellcheck'] = 'true';
  1131. $query->addParams($params);
  1132. }
  1133. function apachesolr_search_add_boost_params(DrupalSolrQueryInterface $query) {
  1134. $env_id = $query->solr('getId');
  1135. $params = array();
  1136. $defaults = array(
  1137. 'content' => '1.0',
  1138. 'ts_comments' => '0.5',
  1139. 'tos_content_extra' => '0.1',
  1140. 'label' => '5.0',
  1141. 'tos_name' => '3.0',
  1142. 'taxonomy_names' => '2.0',
  1143. 'tags_h1' => '5.0',
  1144. 'tags_h2_h3' => '3.0',
  1145. 'tags_h4_h5_h6' => '2.0',
  1146. 'tags_inline' => '1.0',
  1147. 'tags_a' => '0',
  1148. );
  1149. $qf = apachesolr_environment_variable_get($env_id, 'field_bias', $defaults);
  1150. $fields = $query->solr('getFields');
  1151. if ($qf && $fields) {
  1152. foreach ($fields as $field_name => $field) {
  1153. if (!empty($qf[$field_name])) {
  1154. $prefix = substr($field_name, 0, 3);
  1155. if ($field_name == 'content' || $prefix == 'ts_' || $prefix == 'tm_') {
  1156. // Normed fields tend to have a lower score. Multiplying by 40 is
  1157. // a rough attempt to bring the score in line with fields that are
  1158. // not normed.
  1159. $qf[$field_name] *= 40.0;
  1160. }
  1161. $params['qf'][$field_name] = $field_name . '^' . $qf[$field_name];
  1162. }
  1163. }
  1164. }
  1165. $date_settings = apachesolr_environment_variable_get($env_id, 'apachesolr_search_date_boost', '0:0');
  1166. $comment_settings = apachesolr_environment_variable_get($env_id, 'apachesolr_search_comment_boost', '0:0');
  1167. $changed_settings = apachesolr_environment_variable_get($env_id, 'apachesolr_search_changed_boost', '0:0');
  1168. $sticky_boost = apachesolr_environment_variable_get($env_id, 'apachesolr_search_sticky_boost', '0');
  1169. $promote_boost = apachesolr_environment_variable_get($env_id, 'apachesolr_search_promote_boost', '0');
  1170. // For the boost functions for the created timestamp, etc we use the
  1171. // standard date-biasing function, as suggested (but steeper) at
  1172. // http://wiki.apache.org/solr/SolrRelevancyFAQ#How_can_I_boost_the_score_of_newer_documents
  1173. // ms() returns the time difference in ms between now and the date
  1174. // The function is thus: $ab/(ms(NOW,date)*$steepness + $ab).
  1175. list($date_steepness, $date_boost) = explode(':', $date_settings);
  1176. if ($date_boost) {
  1177. $ab = 4 / $date_steepness;
  1178. $params['bf'][] = "recip(ms(NOW,ds_created),3.16e-11,$ab,$ab)^$date_boost";
  1179. }
  1180. // Boost on comment count.
  1181. list($comment_steepness, $comment_boost) = explode(':', $comment_settings);
  1182. if ($comment_boost) {
  1183. $params['bf'][] = "recip(div(1,max(is_comment_count,1)),$comment_steepness,10,10)^$comment_boost";
  1184. }
  1185. // Boost for a more recent comment or node edit.
  1186. list($changed_steepness, $changed_boost) = explode(':', $changed_settings);
  1187. if ($changed_boost) {
  1188. $ab = 4 / $changed_steepness;
  1189. $params['bf'][] = "recip(ms(NOW,ds_created),3.16e-11,$ab,$ab)^$changed_boost";
  1190. }
  1191. // Boost for nodes with sticky bit set.
  1192. if ($sticky_boost) {
  1193. $params['bq'][] = "bs_sticky:true^$sticky_boost";
  1194. }
  1195. // Boost for nodes with promoted bit set.
  1196. if ($promote_boost) {
  1197. $params['bq'][] = "bs_promote:true^$promote_boost";
  1198. }
  1199. // Modify the weight of results according to the node types.
  1200. $type_boosts = apachesolr_environment_variable_get($env_id, 'apachesolr_search_type_boosts', array());
  1201. if (!empty($type_boosts)) {
  1202. foreach ($type_boosts as $type => $boost) {
  1203. // Only add a param if the boost is != 0 (i.e. > "Normal").
  1204. if ($boost) {
  1205. $params['bq'][] = "bundle:$type^$boost";
  1206. }
  1207. }
  1208. }
  1209. $query->addParams($params);
  1210. }
  1211. /**
  1212. *
  1213. * @param type $response
  1214. * @param DrupalSolrQueryInterface $query
  1215. * @return array
  1216. * @todo Make sure the paging works for Drupal 6
  1217. */
  1218. function apachesolr_search_process_response($response, DrupalSolrQueryInterface $query) {
  1219. $results = array();
  1220. // We default to getting snippets from the body content and comments.
  1221. $hl_fl = $query->getParam('hl.fl');
  1222. if (!$hl_fl) {
  1223. $hl_fl = array('content', 'ts_comments');
  1224. }
  1225. $total = $response->response->numFound;
  1226. apachesolr_search_pager_default_initialize($total, $query->getParam('rows'));
  1227. if ($total > 0) {
  1228. $fl = $query->getParam('fl');
  1229. // 'id' and 'entity_type' are the only required fields in the schema, and
  1230. // 'score' is generated by solr.
  1231. foreach ($response->response->docs as $doc) {
  1232. $extra = array();
  1233. // Allow modules to alter each document and its extra information.
  1234. drupal_alter('apachesolr_search_result', $doc, $extra, $query);
  1235. // Start with an empty snippets array.
  1236. $snippets = array();
  1237. // Find the nicest available snippet.
  1238. foreach ($hl_fl as $hl_param) {
  1239. if (isset($response->highlighting->{$doc->id}->$hl_param)) {
  1240. // Merge arrays preserving keys.
  1241. foreach ($response->highlighting->{$doc->id}->$hl_param as $values) {
  1242. $snippets[$hl_param] = $values;
  1243. }
  1244. }
  1245. }
  1246. // If there's no snippet at this point, add the teaser.
  1247. if (!$snippets) {
  1248. if (isset($doc->teaser)) {
  1249. $snippets[] = truncate_utf8($doc->teaser, 256, TRUE);
  1250. }
  1251. }
  1252. $hooks = array();
  1253. $bundle = !empty($doc->bundle) ? $doc->bundle : NULL;
  1254. if ($bundle) {
  1255. // Add a bundle specific theming hook if it has been defined
  1256. // in hook_theme
  1257. $hooks[] = 'apachesolr_search_snippets__' . $bundle;
  1258. }
  1259. // Add a general theming hook for snippets
  1260. $hooks[] = 'apachesolr_search_snippets';
  1261. $snippet = theme($hooks, array('doc' => $doc, 'snippets' => $snippets));
  1262. if (!isset($doc->content)) {
  1263. $doc->content = $snippet;
  1264. }
  1265. // Normalize common dates so that we can use Drupal's normal date and
  1266. // time handling.
  1267. if (isset($doc->ds_created)) {
  1268. $doc->created = strtotime($doc->ds_created);
  1269. }
  1270. else {
  1271. $doc->created = NULL;
  1272. }
  1273. if (isset($doc->ds_changed)) {
  1274. $doc->changed = strtotime($doc->ds_changed);
  1275. }
  1276. else {
  1277. $doc->changed = NULL;
  1278. }
  1279. if (isset($doc->tos_name)) {
  1280. $doc->name = $doc->tos_name;
  1281. }
  1282. else {
  1283. $doc->name = NULL;
  1284. }
  1285. // Set all expected fields from fl to NULL if they are missing so
  1286. // as to prevent Notice: Undefined property.
  1287. $fl = array_merge($fl, array('path', 'label', 'score'));
  1288. foreach ($fl as $field) {
  1289. if (!isset($doc->{$field})) {
  1290. $doc->{$field} = NULL;
  1291. }
  1292. }
  1293. $fields = (array) $doc;
  1294. // a path is not a requirement of entity (see entity_uri() ), so we check if we
  1295. // can show it and fallback to the main page of the site if we don't
  1296. // have it.
  1297. if (!isset($doc->url)) {
  1298. $path = '';
  1299. }
  1300. else {
  1301. $path = $doc->url;
  1302. }
  1303. $result = array(
  1304. // link is a required field, so handle it centrally.
  1305. 'link' => $path,
  1306. // template_preprocess_search_result() runs check_plain() on the title
  1307. // again. Decode to correct the display.
  1308. 'title' => htmlspecialchars_decode($doc->label, ENT_QUOTES),
  1309. // These values are not required by the search module but are provided
  1310. // to give entity callbacks and themers more flexibility.
  1311. 'score' => $doc->score,
  1312. 'snippets' => $snippets,
  1313. 'snippet' => $snippet,
  1314. 'fields' => $fields,
  1315. 'entity_type' => $doc->entity_type,
  1316. 'bundle' => $bundle,
  1317. );
  1318. // Call entity-type-specific callbacks for extra handling.
  1319. $function = apachesolr_entity_get_callback($doc->entity_type, 'result callback', $bundle);
  1320. if (is_callable($function)) {
  1321. $function($doc, $result, $extra);
  1322. }
  1323. $result['extra'] = $extra;
  1324. $results[] = $result;
  1325. }
  1326. }
  1327. // Hook to allow modifications of the retrieved results
  1328. foreach (module_implements('apachesolr_process_results') as $module) {
  1329. $function = $module . '_apachesolr_process_results';
  1330. $function($results, $query);
  1331. }
  1332. return $results;
  1333. }
  1334. /**
  1335. * Retrieve all of the suggestions that were given after a certain search
  1336. * @return array()
  1337. */
  1338. function apachesolr_search_get_search_suggestions($env_id) {
  1339. $suggestions_output = array();
  1340. if (apachesolr_has_searched($env_id)) {
  1341. $query = apachesolr_current_query($env_id);
  1342. $keyword = $query->getParam('q');
  1343. $searcher = $query->getSearcher();
  1344. $response = apachesolr_static_response_cache($searcher);
  1345. // Get spellchecker suggestions into an array.
  1346. if (!empty($response->spellcheck->suggestions)) {
  1347. $suggestions = get_object_vars($response->spellcheck->suggestions);
  1348. if ($suggestions) {
  1349. $replacements = array();
  1350. // Get the original query and retrieve all words with suggestions.
  1351. foreach ($suggestions as $word => $value) {
  1352. $replacements[$word] = $value->suggestion[0];
  1353. }
  1354. // Replace the keyword with the suggested keyword.
  1355. $suggested_keyword = strtr($keyword, $replacements);
  1356. // Show only if suggestion is different than current query.
  1357. if ($keyword != $suggested_keyword) {
  1358. $suggestions_output[] = $suggested_keyword;
  1359. }
  1360. }
  1361. }
  1362. }
  1363. return $suggestions_output;
  1364. }
  1365. /**
  1366. * Implements hook_apachesolr_entity_info_alter().
  1367. */
  1368. function apachesolr_search_apachesolr_entity_info_alter(&$entity_info) {
  1369. // Now set those values that we know. Other modules can do so
  1370. // for specific content types.
  1371. $entity_info['node']['result callback'] = 'apachesolr_search_node_result';
  1372. }
  1373. /**
  1374. * Callback function for node search results.
  1375. *
  1376. * @param stdClass $doc
  1377. * The result document from Apache Solr.
  1378. * @param array $result
  1379. * The result array for this record to which to add.
  1380. */
  1381. function apachesolr_search_node_result($doc, &$result, &$extra) {
  1382. $doc->uid = $doc->is_uid;
  1383. $result += array(
  1384. 'type' => $doc->bundle,
  1385. 'user' => theme('username', $doc),
  1386. 'date' => isset($doc->changed) ? $doc->changed : 0,
  1387. 'node' => $doc,
  1388. 'uid' => $doc->is_uid,
  1389. );
  1390. if (isset($doc->is_comment_count)) {
  1391. $extra['comments'] = format_plural($doc->is_comment_count, '1 comment', '@count comments');
  1392. }
  1393. }
  1394. /**
  1395. * Returns whether a search page exists.
  1396. */
  1397. function apachesolr_search_page_exists($page_id) {
  1398. if (db_table_exists('apachesolr_search_page')) {
  1399. $page = db_result(db_query("SELECT 1 FROM {apachesolr_search_page} WHERE page_id = '%s'", array($page_id)));
  1400. }
  1401. return 0;
  1402. }
  1403. /**
  1404. * Template preprocess for apachesolr search results.
  1405. *
  1406. * We need to add additional entity/bundle-based templates
  1407. */
  1408. function apachesolr_search_preprocess_search_result(&$variables) {
  1409. // If this search result is coming from our module, we want to improve the
  1410. // template potential to make life easier for themers.
  1411. if ($variables['type'] == 'apachesolr_search') {
  1412. $result = $variables['result'];
  1413. if (!empty($result['entity_type'])) {
  1414. $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['type'] . '__' . $result['entity_type'];
  1415. if (!empty($result['bundle'])) {
  1416. $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['type'] . '__' . $result['entity_type'] . '__' . $result['bundle'];
  1417. }
  1418. }
  1419. }
  1420. }
  1421. /**
  1422. *
  1423. * @param type $variables
  1424. * @return type
  1425. * @todo Make sure the paging works for Drupal 6
  1426. */
  1427. function apachesolr_search_preprocess_search_results(&$variables) {
  1428. // Initialize variables
  1429. $env_id = NULL;
  1430. // If this is a solr search, expose more data to themes to play with.
  1431. if ($variables['type'] == 'apachesolr_search') {
  1432. // Fetch our current query
  1433. if (!empty($variables['search_page']['env_id'])) {
  1434. $env_id = $variables['search_page']['env_id'];
  1435. }
  1436. $query = apachesolr_current_query($env_id);
  1437. if ($query) {
  1438. $variables['query'] = $query;
  1439. $variables['response'] = apachesolr_static_response_cache($query->getSearcher());
  1440. }
  1441. if (empty($variables['response'])) {
  1442. $variables['description'] = '';
  1443. return;
  1444. }
  1445. $total = $variables['response']->response->numFound;
  1446. $params = $variables['query']->getParams();
  1447. $variables['description'] = t('Showing items @start through @end of @total.', array(
  1448. '@start' => $params['start'] + 1,
  1449. '@end' => $params['start'] + $params['rows'] - 1,
  1450. '@total' => $total,
  1451. ));
  1452. // Redefine the pager if it was missing
  1453. //pager_default_initialize($total, $params['rows']);
  1454. $variables['pager'] = theme('pager');
  1455. // Add template hints for environments
  1456. if (!empty($env_id)) {
  1457. $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['type'] . '__' . $env_id;
  1458. // Add template hints for search pages
  1459. if (!empty($variables['search_page']['page_id'])) {
  1460. $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['type'] . '__' . $variables['search_page']['page_id'];
  1461. // Add template hints for both
  1462. $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['type'] . '__' . $env_id . '__' . $variables['search_page']['page_id'];
  1463. }
  1464. }
  1465. }
  1466. }
  1467. /**
  1468. * Implements hook_apachesolr_environment_delete().
  1469. */
  1470. function apachesolr_search_apachesolr_environment_delete($server) {
  1471. if (db_table_exists('apachesolr_search_page')) {
  1472. $query = "UPDATE {apachesolr_search_page} SET env_id = '' WHERE env_id = '%s'";
  1473. db_query($query, array($server['env_id']));
  1474. apachesolr_environment_variable_del($server['env_id'], 'apachesolr_search_show_facets');
  1475. apachesolr_environment_variable_del($server['env_id'], 'apachesolr_search_facet_pages');
  1476. menu_rebuild();
  1477. }
  1478. }
  1479. /**
  1480. * Default theme function for spelling suggestions.
  1481. */
  1482. function theme_apachesolr_search_suggestions($variables) {
  1483. $output = '<div class="spelling-suggestions">';
  1484. $output .= '<dl class="form-item"><dt><strong>' . t('Did you mean') . '</strong></dt>';
  1485. foreach ((array) $variables as $link) {
  1486. $output .= '<dd>' . $link . '</dd>';
  1487. }
  1488. $output .= '</dl></div>';
  1489. return $output;
  1490. }
  1491. /**
  1492. * Implements hook_form_[form_id]_alter().
  1493. *
  1494. * Rebuild (empty) the spellcheck dictionary when the index is deleted..
  1495. */
  1496. function apachesolr_search_form_apachesolr_delete_index_confirm_alter(&$form, $form_state) {
  1497. $form['submit']['#submit'][] = 'apachesolr_search_build_spellcheck';
  1498. }
  1499. /**
  1500. * submit function for the delete_index form.
  1501. *
  1502. */
  1503. function apachesolr_search_build_spellcheck($form, &$form_state) {
  1504. try {
  1505. $solr = apachesolr_get_solr();
  1506. $params['spellcheck'] = 'true';
  1507. $params['spellcheck.build'] = 'true';
  1508. $response = $solr->search('solr', 0, 0, $params);
  1509. }
  1510. catch (Exception $e) {
  1511. watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
  1512. }
  1513. }
  1514. /**
  1515. * Implements hook_form_[form_id]_alter().
  1516. *
  1517. * Adds settings to show facet blocks on non-search pages.
  1518. */
  1519. function apachesolr_search_form_facetapi_realm_settings_form_alter(&$form, &$form_state) {
  1520. if ('apachesolr' == $form['#facetapi']['adapter']->getId() && 'block' == $form['#facetapi']['realm']['name']) {
  1521. // Gets the environment ID from the searcher, stores in #facetapi property.
  1522. $env_id = ltrim(strstr($form['#facetapi']['adapter']->getSearcher(), '@'), '@');
  1523. $show_facets = apachesolr_environment_variable_get($env_id, 'apachesolr_search_show_facets', 0);
  1524. $facet_pages = apachesolr_environment_variable_get($env_id, 'apachesolr_search_facet_pages', '');
  1525. $form['#facetapi']['env_id'] = $env_id;
  1526. $form['apachesolr_search_show_facets'] = array(
  1527. '#type' => 'checkbox',
  1528. '#title' => t('Show facets on non-search pages.'),
  1529. '#default_value' => $show_facets,
  1530. '#weight' => '-10',
  1531. );
  1532. $form['apachesolr_search_facet_pages'] = array(
  1533. '#title' => t('Non-search paths'),
  1534. '#type' => 'textarea',
  1535. '#default_value' => $facet_pages,
  1536. '#weight' => '-10',
  1537. '#dependency' => array(
  1538. 'edit-apachesolr-search-show-facets' => array(1),
  1539. ),
  1540. );
  1541. $form['#submit'][] = 'apachesolr_search_facetapi_realm_settings_form_submit';
  1542. }
  1543. }
  1544. /**
  1545. * Form submission handler for facetapi_realm_settings_form().
  1546. */
  1547. function apachesolr_search_facetapi_realm_settings_form_submit(&$form, &$form_state) {
  1548. $env_id = $form['#facetapi']['env_id'];
  1549. // Adds the settings to the array keyed by environment ID, saves variables.
  1550. $show_facets = $form_state['values']['apachesolr_search_show_facets'];
  1551. $facet_pages = $form_state['values']['apachesolr_search_facet_pages'];
  1552. if ($show_facets) {
  1553. apachesolr_environment_variable_set($env_id, 'apachesolr_search_show_facets', $show_facets);
  1554. }
  1555. else {
  1556. // Due to performance reasons, we delete it from the vars so that our init
  1557. // process can react on environments that hae it set and not unset.
  1558. // See apachesolr_search_init().
  1559. apachesolr_environment_variable_del($env_id, 'apachesolr_search_show_facets');
  1560. }
  1561. apachesolr_environment_variable_set($env_id, 'apachesolr_search_facet_pages', $facet_pages);
  1562. }
  1563. /**
  1564. * Implements hook_theme().
  1565. */
  1566. function apachesolr_search_theme() {
  1567. return array(
  1568. /**
  1569. * Shows the facets in blocks in the search result area
  1570. */
  1571. 'apachesolr_search_browse_blocks' => array(
  1572. 'render element' => 'content',
  1573. ),
  1574. /**
  1575. * Shows the search snippet
  1576. */
  1577. 'apachesolr_search_snippets' => array(
  1578. 'arguments' => array('doc' => NULL, 'snippets' => array()),
  1579. ),
  1580. /**
  1581. * Shows a message when the search does not return any result
  1582. */
  1583. 'apachesolr_search_noresults' => array(
  1584. 'arguments' => array(),
  1585. ),
  1586. /**
  1587. * Shows a list of suggestions
  1588. */
  1589. 'apachesolr_search_suggestions' => array(
  1590. 'arguments' => array('links' => NULL),
  1591. ),
  1592. /**
  1593. * Shows a list of results (docs) in content recommendation block
  1594. */
  1595. 'apachesolr_search_mlt_recommendation_block' => array(
  1596. 'arguments' => array('docs' => NULL, 'delta' => NULL),
  1597. ),
  1598. );
  1599. }
  1600. /**
  1601. * Implements hook_theme_registry_alter().
  1602. */
  1603. function apachesolr_search_theme_registry_alter(&$theme_registry) {
  1604. if (isset($theme_registry['search_results'])) {
  1605. $theme_registry['search_results']['arguments']['search_page'] = NULL;
  1606. }
  1607. }
  1608. /**
  1609. * Theme the highlighted snippet text for a search entry.
  1610. *
  1611. * @param array $vars
  1612. *
  1613. */
  1614. function theme_apachesolr_search_snippets($vars) {
  1615. $result = '';
  1616. if (is_array($vars['snippets'])) {
  1617. $snippets = $vars['snippets'];
  1618. if (isset($snippets['content'])) {
  1619. $result .= $snippets['content'];
  1620. unset($snippets['content']);
  1621. }
  1622. if (isset($snippets['teaser'])) {
  1623. $result .= (strlen($result) > 0) ? ' ... ' : '';
  1624. $result .= $snippets['teaser'];
  1625. unset($snippets['teaser']);
  1626. }
  1627. if (count($snippets)) {
  1628. $result .= (strlen($result) > 0) ? ' ... ' : '';
  1629. $result .= implode(' ... ', $snippets);
  1630. }
  1631. }
  1632. return $result . ' ...';
  1633. }
  1634. /**
  1635. * Brief message to display when no results match the query.
  1636. *
  1637. * @see search_help()
  1638. */
  1639. function theme_apachesolr_search_noresults() {
  1640. return t('<ul>
  1641. <li>Check if your spelling is correct, or try removing filters.</li>
  1642. <li>Remove quotes around phrases to match each word individually: <em>"blue drop"</em> will match less than <em>blue drop</em>.</li>
  1643. <li>You can require or exclude terms using + and -: <em>big +blue drop</em> will require a match on <em>blue</em> while <em>big blue -drop</em> will exclude results that contain <em>drop</em>.</li>
  1644. </ul>');
  1645. }
  1646. /**
  1647. * Backport of pager_default_initialize from Drupal 7.
  1648. */
  1649. function apachesolr_search_pager_default_initialize($total, $limit, $element = 0) {
  1650. global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
  1651. $page = apachesolr_search_pager_find_page($element);
  1652. // We calculate the total of pages as ceil(items / limit).
  1653. $pager_total_items[$element] = $total;
  1654. $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  1655. $pager_page_array[$element] = max(0, min($page, ((int) $pager_total[$element]) - 1));
  1656. $pager_limits[$element] = $limit;
  1657. return $pager_page_array[$element];
  1658. }
  1659. /**
  1660. * Backport of pager_find_page from Drupal 7.
  1661. */
  1662. function apachesolr_search_pager_find_page($element = 0) {
  1663. $page = isset($_GET['page']) ? $_GET['page'] : '';
  1664. $page_array = explode(',', $page);
  1665. if (!isset($page_array[$element])) {
  1666. $page_array[$element] = 0;
  1667. }
  1668. return (int) $page_array[$element];
  1669. }