2 # Script to perform searching
3 # Mostly copied from search.pl, see POD there
4 use strict; # always use
6 ## STEP 1. Load things that are used in both search page and
7 # results page and decide which template to load, operations
14 use C4::Biblio; # GetBiblioData
16 use C4::Tags qw(get_tags);
17 use POSIX qw(ceil floor strftime);
18 use C4::Branch; # GetBranches
20 # create a new CGI object
21 # FIXME: no_undef_params needs to be tested
22 use CGI qw('-no_undef_params');
26 if (C4::Context->preference('BakerTaylorEnabled')) {
27 require C4::External::BakerTaylor;
28 import C4::External::BakerTaylor qw(&image_url &link_url);
32 my ($template,$borrowernumber,$cookie);
34 # decide which template to use
37 my @params = $cgi->param("limit");
39 my $build_grouped_results = C4::Context->preference('OPACGroupResults');
40 if ($cgi->param("format") =~ /(rss|atom|opensearchdescription)/) {
41 $template_name = 'opac-opensearch.tmpl';
43 elsif ($build_grouped_results) {
44 $template_name = 'opac-results-grouped.tmpl';
46 elsif ((@params>=1) || ($cgi->param("q")) || ($cgi->param('multibranchlimit')) || ($cgi->param('limit-yr')) ) {
47 $template_name = 'opac-results.tmpl';
50 $template_name = 'opac-advsearch.tmpl';
51 $template_type = 'advsearch';
54 ($template, $borrowernumber, $cookie) = get_template_and_user({
55 template_name => $template_name,
61 if ($cgi->param("format") eq 'rss2') {
62 $template->param("rss2" => 1);
64 elsif ($cgi->param("format") eq 'atom') {
65 $template->param("atom" => 1);
66 # FIXME - the timestamp is a hack - the biblio update timestamp should be used for each
67 # entry, but not sure if that's worth an extra database query for each bib
68 $template->param(timestamp => strftime("%Y-%m-%dT%H:%M:%S-00:00", gmtime));
70 elsif ($cgi->param("format") eq 'opensearchdescription') {
71 $template->param("opensearchdescription" => 1);
73 if (C4::Context->preference("marcflavour") eq "UNIMARC" ) {
74 $template->param('UNIMARC' => 1);
77 if (C4::Context->preference('BakerTaylorEnabled')) {
79 BakerTaylorEnabled => 1,
80 BakerTaylorImageURL => &image_url(),
81 BakerTaylorLinkURL => &link_url(),
82 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
85 if (C4::Context->preference('TagsEnabled')) {
86 $template->param(TagsEnabled => 1);
87 foreach (qw(TagsShowOnList TagsInputOnList)) {
88 C4::Context->preference($_) and $template->param($_ => 1);
93 # Deprecated, but preserved because it's interesting :-)
94 # The same thing can be accomplished with mod_rewrite in
98 #my $uri = $cgi->url(-base => 1);
99 #my $relative_url = $cgi->url(-relative=>1);
100 #$uri.="/".$relative_url."?";
102 #my @cgi_params_list = $cgi->param();
103 #my $url_params = $cgi->Vars;
105 #for my $each_param_set (@cgi_params_list) {
106 # $uri.= join "", map "\&$each_param_set=".$_, split("\0",$url_params->{$each_param_set}) if $url_params->{$each_param_set};
108 #warn "New URI:$uri";
109 # Only re-write a URI if there are params or if it already hasn't been re-written
110 #unless (($cgi->param('r')) || (!$cgi->param()) ) {
111 # print $cgi->redirect( -uri=>$uri."&r=1",
112 # -cookie => $cookie);
117 my $branches = GetBranches();
120 for my $branch_hash (sort keys %$branches) {
121 push @branch_loop, {value => "$branch_hash" , branchname => $branches->{$branch_hash}->{'branchname'}, };
124 my $categories = GetBranchCategories(undef,'searchdomain');
126 $template->param(branchloop => \@branch_loop, searchdomainloop => $categories);
129 my $itemtypes = GetItemTypes;
133 my $imgdir = getitemtypeimagesrc('opac');
135 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
136 my %row =( number=>$cnt++,
137 imageurl=> $itemtypes->{$thisitemtype}->{'imageurl'}?($imgdir."/".$itemtypes->{$thisitemtype}->{'imageurl'}):"",
138 code => $thisitemtype,
139 selected => $selected,
140 description => $itemtypes->{$thisitemtype}->{'description'},
143 $selected = 0 if ($selected) ;
144 push @itemtypesloop, \%row;
146 $template->param(itemtypeloop => \@itemtypesloop);
148 # # load the itypes (Called item types in the template -- just authorized values for searching)
149 # my ($itypecount,@itype_loop) = GetCcodes();
150 # $template->param(itypeloop=>\@itype_loop,);
152 # The following should only be loaded if we're bringing up the advanced search template
153 if ( $template_type eq 'advsearch' ) {
155 # load the servers (used for searching -- to do federated searching, etc.)
156 my $primary_servers_loop;# = displayPrimaryServers();
157 $template->param(outer_servers_loop => $primary_servers_loop,);
159 my $secondary_servers_loop;# = displaySecondaryServers();
160 $template->param(outer_sup_servers_loop => $secondary_servers_loop,);
162 # determine what to display next to the search boxes (ie, boolean option
163 # shouldn't appear on the first one, scan indexes should, adding a new
164 # box should only appear on the last, etc.
165 my @search_boxes_array;
166 my $search_boxes_count = C4::Context->preference("OPACAdvSearchInputCount") | 3; # FIXME: should be a syspref
167 for (my $i=1;$i<=$search_boxes_count;$i++) {
168 # if it's the first one, don't display boolean option, but show scan indexes
170 push @search_boxes_array,
176 # if it's the last one, show the 'add field' box
177 elsif ($i==$search_boxes_count) {
178 push @search_boxes_array,
185 push @search_boxes_array,
192 $template->param(uc(C4::Context->preference("marcflavour")) => 1,
194 search_boxes_loop => \@search_boxes_array);
196 # use the global setting by default
197 if ( C4::Context->preference("expandedSearchOption") == 1) {
198 $template->param( expanded_options => C4::Context->preference("expandedSearchOption") );
200 # but let the user override it
201 if ( ($cgi->param('expanded_options') == 0) || ($cgi->param('expanded_options') == 1 ) ) {
202 $template->param( expanded_options => $cgi->param('expanded_options'));
205 output_html_with_http_headers $cgi, $cookie, $template->output;
209 ### OK, if we're this far, we're performing an actual search
211 # Fetch the paramater list as a hash in scalar context:
212 # * returns paramater list as tied hash ref
213 # * we can edit the values by changing the key
214 # * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null)
215 my $params = $cgi->Vars;
217 $tag = $params->{tag} if $params->{tag};
219 # Params that can have more than one value
220 # sort by is used to sort the query
221 # in theory can have more than one but generally there's just one
223 my $default_sort_by = C4::Context->preference('OPACdefaultSortField')."_".C4::Context->preference('OPACdefaultSortOrder')
224 if (C4::Context->preference('OPACdefaultSortField') && C4::Context->preference('OPACdefaultSortOrder'));
226 @sort_by = split("\0",$params->{'sort_by'}) if $params->{'sort_by'};
227 $sort_by[0] = $default_sort_by unless $sort_by[0];
228 foreach my $sort (@sort_by) {
229 $template->param($sort => 1);
231 $template->param('sort_by' => $sort_by[0]);
233 # Use the servers defined, or just search our local catalog(default)
235 @servers = split("\0",$params->{'server'}) if $params->{'server'};
237 #FIXME: this should be handled using Context.pm
238 @servers = ("biblioserver");
239 # @servers = C4::Context->config("biblioserver");
242 # operators include boolean and proximity operators and are used
243 # to evaluate multiple operands
245 @operators = split("\0",$params->{'op'}) if $params->{'op'};
247 # indexes are query qualifiers, like 'title', 'author', etc. They
248 # can be single or multiple parameters separated by comma: kw,right-Truncation
249 my @indexes = split("\0",$params->{'idx'});
251 # if a simple index (only one) display the index used in the top search box
252 if ($indexes[0] && !$indexes[1]) {
253 $template->param("ms_".$indexes[0] => 1);
255 # an operand can be a single term, a phrase, or a complete ccl query
257 @operands = split("\0",$params->{'q'}) if $params->{'q'};
259 # if a simple search, display the value in the search box
260 if ($operands[0] && !$operands[1]) {
261 $template->param(ms_value => $operands[0]);
264 # limits are use to limit to results to a pre-defined category such as branch or language
266 @limits = split("\0",$params->{'limit'}) if $params->{'limit'};
268 if($params->{'multibranchlimit'}) {
269 push @limits, join(" or ", map { "branch: $_ "} @{GetBranchesInCategory($params->{'multibranchlimit'})}) ;
273 foreach my $limit(@limits) {
274 if ($limit =~/available/) {
278 $template->param(available => $available);
280 # append year limits if they exist
281 if ($params->{'limit-yr'}) {
282 if ($params->{'limit-yr'} =~ /\d{4}-\d{4}/) {
283 my ($yr1,$yr2) = split(/-/, $params->{'limit-yr'});
284 push @limits, "yr,st-numeric,ge=$yr1 and yr,st-numeric,le=$yr2";
286 elsif ($params->{'limit-yr'} =~ /\d{4}/) {
287 push @limits, "yr,st-numeric=$params->{'limit-yr'}";
290 #FIXME: Should return a error to the user, incorect date format specified
294 # Params that can only have one value
295 my $scan = $params->{'scan'};
296 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
297 my $results_per_page = $params->{'count'} || $count;
298 my $offset = $params->{'offset'} || 0;
299 my $page = $cgi->param('page') || 1;
300 $offset = ($page-1)*$results_per_page if $page>1;
302 my $expanded_facet = $params->{'expand'};
304 # Define some global variables
305 my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type);
309 ## I. BUILD THE QUERY
310 ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type) = buildQuery(\@operators,\@operands,\@indexes,\@limits,\@sort_by);
312 sub _input_cgi_parse ($) {
314 for my $this_cgi ( split('&',shift) ) {
315 next unless $this_cgi;
316 $this_cgi =~ /(.*)=(.*)/;
317 push @elements, { input_name => $1, input_value => $2 };
322 ## parse the query_cgi string and put it into a form suitable for <input>s
323 my @query_inputs = _input_cgi_parse($query_cgi);
324 $template->param ( QUERY_INPUTS => \@query_inputs );
326 ## parse the limit_cgi string and put it into a form suitable for <input>s
327 my @limit_inputs = _input_cgi_parse($limit_cgi);
329 # add OPAC 'hidelostitems'
330 if (C4::Context->preference('hidelostitems') == 1) {
331 # either lost ge 0 or no value in the lost register
332 $query ="($query) and ( (lost,st-numeric <= 0) or ( allrecords,AlwaysMatches='' not lost,AlwaysMatches='') )";
335 # add OPAC suppression - requires at least one item indexed with Suppress
336 if (C4::Context->preference('OpacSuppression')) {
337 $query = "($query) not Suppress=1";
340 $template->param ( LIMIT_INPUTS => \@limit_inputs );
342 ## II. DO THE SEARCH AND GET THE RESULTS
343 my $total; # the total results for the whole set
344 my $facets; # this object stores the faceted results that display on the left-hand of the results page
349 my $taglist = get_tags({term=>$tag});
350 $results_hashref->{biblioserver}->{hits} = scalar (@$taglist);
351 my @biblist = (map {GetBiblioData($_->{biblionumber})} @$taglist);
352 my @marclist = (map {$_->{marc}} @biblist );
353 $DEBUG and printf STDERR "taglist (%s biblionumber)\nmarclist (%s records)\n", scalar(@$taglist), scalar(@marclist);
354 $results_hashref->{biblioserver}->{RECORDS} = \@marclist;
355 # FIXME: tag search and standard search should work together, not exclusively
356 # FIXME: No facets for tags search.
358 elsif (C4::Context->preference('NoZebra')) {
360 ($error, $results_hashref, $facets) = NZgetRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
362 } elsif ($build_grouped_results) {
364 ($error, $results_hashref, $facets) = C4::Search::pazGetRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
368 ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
371 # use Data::Dumper; print STDERR "-" x 25, "\n", Dumper($results_hashref);
373 $template->param(query_error => $error.$@);
374 output_html_with_http_headers $cgi, $cookie, $template->output;
378 # At this point, each server has given us a result set
379 # now we build that set for template display
380 my @sup_results_array;
381 for (my $i=0;$i<=@servers;$i++) {
382 my $server = $servers[$i];
383 if ($server =~/biblioserver/) { # this is the local bibliographic server
384 $hits = $results_hashref->{$server}->{"hits"};
385 my $page = $cgi->param('page') || 0;
387 if ($build_grouped_results) {
388 foreach my $group (@{ $results_hashref->{$server}->{"GROUPS"} }) {
389 # because pazGetRecords handles retieving only the records
390 # we want as specified by $offset and $results_per_page,
391 # we need to set the offset parameter of searchResults to 0
392 my @group_results = searchResults( $query_desc, $group->{'group_count'},$results_per_page, 0,
393 @{ $group->{"RECORDS"} });
394 push @newresults, { group_label => $group->{'group_label'}, GROUP_RESULTS => \@group_results };
397 @newresults = searchResults( $query_desc,$hits,$results_per_page,$offset,@{$results_hashref->{$server}->{"RECORDS"}});
400 if (C4::Context->preference('TagsEnabled') and
401 $tag_quantity = C4::Context->preference('TagsShowOnList')) {
402 foreach (@newresults) {
403 my $bibnum = $_->{biblionumber} or next;
404 $_ ->{'TagLoop'} = get_tags({biblionumber=>$bibnum, 'sort'=>'-weight',
405 limit=>$tag_quantity });
408 foreach (@newresults) {
409 my $clean = $_->{isbn} or next;
411 $clean =~ /\b(\d{13})\b/ or
412 $clean =~ /\b(\d{10})\b/ or
413 $clean =~ /\b(\d{9}X)\b/i
417 $_ ->{'clean_isbn'} = $1;
419 $total = $total + $results_hashref->{$server}->{"hits"};
420 ## If there's just one result, redirect to the detail page
422 my $biblionumber=@newresults[0]->{biblionumber};
423 if (C4::Context->preference('BiblioDefaultView') eq 'isbd') {
424 print $cgi->redirect("/cgi-bin/koha/opac-ISBDdetail.pl?biblionumber=$biblionumber");
425 } elsif (C4::Context->preference('BiblioDefaultView') eq 'marc') {
426 print $cgi->redirect("/cgi-bin/koha/opac-MARCdetail.pl?biblionumber=$biblionumber");
428 print $cgi->redirect("/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber");
433 $template->param(total => $hits);
434 my $limit_cgi_not_availablity = $limit_cgi;
435 $limit_cgi_not_availablity =~ s/&limit=available//g;
436 $template->param(limit_cgi_not_availablity => $limit_cgi_not_availablity);
437 $template->param(limit_cgi => $limit_cgi);
438 $template->param(query_cgi => $query_cgi);
439 $template->param(query_desc => $query_desc);
440 $template->param(limit_desc => $limit_desc);
441 if ($query_desc || $limit_desc) {
442 $template->param(searchdesc => 1);
444 $template->param(stopwords_removed => "@$stopwords_removed") if $stopwords_removed;
445 $template->param(results_per_page => $results_per_page);
446 $template->param(SEARCH_RESULTS => \@newresults,
447 OPACItemsResultsDisplay => (C4::Context->preference("OPACItemsResultsDisplay") eq "itemdetails"?1:0),
449 ## Build the page numbers on the bottom of the page
451 # total number of pages there will be
452 my $pages = ceil($hits / $results_per_page);
453 # default page number
454 my $current_page_number = 1;
455 $current_page_number = ($offset / $results_per_page + 1) if $offset;
456 my $previous_page_offset = $offset - $results_per_page unless ($offset - $results_per_page <0);
457 my $next_page_offset = $offset + $results_per_page;
458 # If we're within the first 10 pages, keep it simple
459 #warn "current page:".$current_page_number;
460 if ($current_page_number < 10) {
461 # just show the first 10 pages
462 # Loop through the pages
463 my $pages_to_show = 10;
464 $pages_to_show = $pages if $pages<10;
465 for ($i=1; $i<=$pages_to_show;$i++) {
466 # the offset for this page
467 my $this_offset = (($i*$results_per_page)-$results_per_page);
468 # the page number for this page
469 my $this_page_number = $i;
470 # it should only be highlighted if it's the current page
471 my $highlight = 1 if ($this_page_number == $current_page_number);
472 # put it in the array
473 push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight, sort_by => join " ",@sort_by };
478 # now, show twenty pages, with the current one smack in the middle
480 for ($i=$current_page_number; $i<=($current_page_number + 20 );$i++) {
481 my $this_offset = ((($i-9)*$results_per_page)-$results_per_page);
482 my $this_page_number = $i-9;
483 my $highlight = 1 if ($this_page_number == $current_page_number);
484 if ($this_page_number <= $pages) {
485 push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight, sort_by => join " ",@sort_by };
490 $template->param( PAGE_NUMBERS => \@page_numbers,
491 previous_page_offset => $previous_page_offset) unless $pages < 2;
492 $template->param(next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
496 $template->param(searchdesc => 1,query_desc => $query_desc,limit_desc => $limit_desc);
498 } # end of the if local
499 # asynchronously search the authority server
500 elsif ($server =~/authorityserver/) { # this is the local authority server
501 my @inner_sup_results_array;
502 for my $sup_record ( @{$results_hashref->{$server}->{"RECORDS"}} ) {
503 my $marc_record_object = MARC::Record->new_from_usmarc($sup_record);
504 my $title_field = $marc_record_object->field(100);
505 warn "Authority Found: ".$marc_record_object->as_formatted();
506 push @inner_sup_results_array, {
507 'title' => $title_field->subfield('a'),
508 'link' => "&idx=an&q=".$marc_record_object->field('001')->as_string(),
511 my $servername = $server;
512 push @sup_results_array, { servername => $servername,
513 inner_sup_results_loop => \@inner_sup_results_array} if @inner_sup_results_array;
515 # FIXME: can add support for other targets as needed here
516 $template->param( outer_sup_results_loop => \@sup_results_array);
517 } #/end of the for loop
518 #$template->param(FEDERATED_RESULTS => \@results_array);
521 #classlist => $classlist,
524 facets_loop => $facets,
526 search_error => $error,
529 if ($query_desc || $limit_desc) {
530 $template->param(searchdesc => 1);
533 ## Now let's find out if we have any supplemental data to show the user
534 # and in the meantime, save the current query for statistical purposes, etc.
535 my $koha_spsuggest; # a flag to tell if we've got suggestions coming from Koha
536 my @koha_spsuggest; # place we store the suggestions to be returned to the template as LOOP
537 my $phrases = $query_desc;
540 if ( C4::Context->preference("kohaspsuggest") ) {
541 my ($suggest_host, $suggest_dbname, $suggest_user, $suggest_pwd) = split(':', C4::Context->preference("kohaspsuggest"));
543 my $koha_spsuggest_dbh;
544 # FIXME: this needs to be moved to Context.pm
546 $koha_spsuggest_dbh=DBI->connect("DBI:mysql:$suggest_dbname:$suggest_host","$suggest_user","$suggest_pwd");
549 warn "can't connect to spsuggest db";
552 my $koha_spsuggest_insert = "INSERT INTO phrase_log(phr_phrase,phr_resultcount,phr_ip) VALUES(?,?,?)";
553 my $koha_spsuggest_query = "SELECT display FROM distincts WHERE strcmp(soundex(suggestion), soundex(?)) = 0 order by soundex(suggestion) limit 0,5";
554 my $koha_spsuggest_sth = $koha_spsuggest_dbh->prepare($koha_spsuggest_query);
555 $koha_spsuggest_sth->execute($phrases);
556 while (my $spsuggestion = $koha_spsuggest_sth->fetchrow_array) {
557 $spsuggestion =~ s/(:|\/)//g;
559 $line{spsuggestion} = $spsuggestion;
560 push @koha_spsuggest,\%line;
564 # Now save the current query
565 $koha_spsuggest_sth=$koha_spsuggest_dbh->prepare($koha_spsuggest_insert);
566 #$koha_spsuggest_sth->execute($phrases,$results_per_page,$ipaddress);
567 $koha_spsuggest_sth->finish;
569 $template->param( koha_spsuggest => $koha_spsuggest ) unless $hits;
570 $template->param( SPELL_SUGGEST => \@koha_spsuggest,
575 warn "Kohaspsuggest failure:".$@;
579 # VI. BUILD THE TEMPLATE
580 my $content_type = $cgi->param('format') =~ /rss/ ? "application/rss+xml" :
581 $cgi->param('format') =~ /atom/ ? "application/atom+xml" :
583 output_html_with_http_headers $cgi, $cookie, $template->output, $content_type;