apachesolr_search.module 64 KB

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