comparison solrsearch_search.module @ 0:a2b4f67e73dc default tip

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