comparison sites/all/modules/custom/solrconnect/apachesolr_search.module @ 0:015d06b10d37 default tip

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