3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use C4::Biblio qw( TransformMarcToKoha GetMarcFromKohaField GetFrameworkCode GetAuthorisedValueDesc GetBiblioData );
21 use C4::Koha qw( getFacets GetVariationsOfISBN GetNormalizedUPC GetNormalizedEAN GetNormalizedOCLCNumber GetNormalizedISBN getitemtypeimagelocation );
24 use Koha::SearchEngine::QueryBuilder;
27 use C4::XSLT qw( XSLTParse4Display );
28 use C4::Reserves qw( GetReserveStatus );
29 use C4::Charset qw( SetUTF8Flag );
30 use Koha::AuthorisedValues;
36 use Koha::RecordProcessor;
37 use Koha::SearchFilters;
43 our (@ISA, @EXPORT_OK);
54 enabled_staff_search_views
63 C4::Search - Functions for searching the Koha catalog.
67 See opac/opac-search.pl or catalogue/search.pl for example of usage
71 This module provides searching functions for Koha's bibliographic databases
77 # make all your functions, whether exported or not;
81 ($biblionumber,$biblionumber,$title) = FindDuplicate($record);
83 This function attempts to find duplicate records using a hard-coded, fairly simplistic algorithm
89 my $dbh = C4::Context->dbh;
90 my $result = TransformMarcToKoha({ record => $record });
94 # search duplicate on ISBN, easy and fast..
96 if ( $result->{isbn} ) {
97 $result->{isbn} =~ s/\(.*$//;
98 $result->{isbn} =~ s/\s+$//;
99 $result->{isbn} =~ s/\|/OR/;
100 $query = "isbn:$result->{isbn}";
104 my $titleindex = 'ti,ext';
105 my $authorindex = 'au,ext';
108 $result->{title} =~ s /\\//g;
109 $result->{title} =~ s /\"//g;
110 $result->{title} =~ s /\(//g;
111 $result->{title} =~ s /\)//g;
113 $query = "$titleindex:\"$result->{title}\"";
114 if ( $result->{author} ) {
115 $result->{author} =~ s /\\//g;
116 $result->{author} =~ s /\"//g;
117 $result->{author} =~ s /\(//g;
118 $result->{author} =~ s /\)//g;
120 $query .= " $op $authorindex:\"$result->{author}\"";
124 my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
125 my ( $error, $searchresults, undef ) = $searcher->simple_search_compat($query,0,50);
127 if (!defined $error) {
128 foreach my $possible_duplicate_record (@{$searchresults}) {
129 my $marcrecord = new_record_from_zebra(
131 $possible_duplicate_record
134 my $result = TransformMarcToKoha({ record => $marcrecord });
136 # FIXME :: why 2 $biblionumber ?
138 push @results, $result->{'biblionumber'};
139 push @results, $result->{'title'};
148 ( $error, $results, $total_hits ) = SimpleSearch( $query, $offset, $max_results, [@servers], [%options] );
150 This function provides a simple search API on the bibliographic catalog
156 * $query can be a simple keyword or a complete CCL query
157 * @servers is optional. Defaults to biblioserver as found in koha-conf.xml
158 * $offset - If present, represents the number of records at the beginning to omit. Defaults to 0
159 * $max_results - if present, determines the maximum number of records to fetch. undef is All. defaults to undef.
160 * %options is optional. (e.g. "skip_normalize" allows you to skip changing : to = )
165 Returns an array consisting of three elements
166 * $error is undefined unless an error is detected
167 * $results is a reference to an array of records.
168 * $total_hits is the number of hits that would have been returned with no limit
170 If an error is returned the two other return elements are undefined. If error itself is undefined
171 the other two elements are always defined
173 =item C<usage in the script:>
177 my ( $error, $marcresults, $total_hits ) = SimpleSearch($query);
179 if (defined $error) {
180 $template->param(query_error => $error);
181 warn "error: ".$error;
182 output_html_with_http_headers $input, $cookie, $template->output;
186 my $hits = @{$marcresults};
189 for my $r ( @{$marcresults} ) {
190 my $marcrecord = MARC::File::USMARC::decode($r);
191 my $biblio = TransformMarcToKoha({ record => $marcrecord });
193 #build the iarray of hashs for the template.
195 title => $biblio->{'title'},
196 subtitle => $biblio->{'subtitle'},
197 biblionumber => $biblio->{'biblionumber'},
198 author => $biblio->{'author'},
199 publishercode => $biblio->{'publishercode'},
200 publicationyear => $biblio->{'publicationyear'},
205 $template->param(result=>\@results);
210 my ( $query, $offset, $max_results, $servers, %options ) = @_;
212 return ( 'No query entered', undef, undef ) unless $query;
213 # FIXME hardcoded value. See catalog/search.pl & opac-search.pl too.
214 my @servers = defined ( $servers ) ? @$servers : ( 'biblioserver' );
221 # Initialize & Search Zebra
222 for ( my $i = 0 ; $i < @servers ; $i++ ) {
224 $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
225 $query =~ s/:/=/g unless $options{skip_normalize};
226 $zoom_queries[$i] = ZOOM::Query::CCL2RPN->new( $query, $zconns[$i]);
227 $tmpresults[$i] = $zconns[$i]->search( $zoom_queries[$i] );
231 $zconns[$i]->errmsg() . " ("
232 . $zconns[$i]->errcode() . ") "
233 . $zconns[$i]->addinfo() . " "
234 . $zconns[$i]->diagset();
236 return ( $error, undef, undef ) if $zconns[$i]->errcode();
240 # caught a ZOOM::Exception
244 . $@->addinfo() . " "
246 warn $error." for query: $query";
247 return ( $error, undef, undef );
256 my $first_record = defined($offset) ? $offset + 1 : 1;
257 my $hits = $tmpresults[ $i - 1 ]->size();
258 $total_hits += $hits;
259 my $last_record = $hits;
260 if ( defined $max_results && $offset + $max_results < $hits ) {
261 $last_record = $offset + $max_results;
264 for my $j ( $first_record .. $last_record ) {
266 $tmpresults[ $i - 1 ]->record( $j - 1 )->raw()
269 push @{$results}, $record if defined $record;
274 foreach my $zoom_query (@zoom_queries) {
275 $zoom_query->destroy();
278 return ( undef, $results, $total_hits );
283 ( undef, $results_hashref, \@facets_loop ) = getRecords (
285 $koha_query, $simple_query, $sort_by_ref, $servers_ref,
286 $results_per_page, $offset, $branches, $itemtypes,
287 $query_type, $scan, $opac
290 The all singing, all dancing, multi-server, asynchronous, scanning,
291 searching, record nabbing, facet-building
293 See verbose embedded documentation.
299 $koha_query, $simple_query, $sort_by_ref, $servers_ref,
300 $results_per_page, $offset, $branches, $itemtypes,
301 $query_type, $scan, $opac
304 my @servers = @$servers_ref;
305 my @sort_by = @$sort_by_ref;
306 $offset = 0 if $offset < 0;
308 # Initialize variables for the ZOOM connection and results object
311 my $results_hashref = ();
313 # TODO simplify this structure ( { branchcode => $branchname } is enought) and remove this parameter
314 $branches ||= { map { $_->branchcode => { branchname => $_->branchname } } Koha::Libraries->search->as_list };
316 # Initialize variables for the faceted results objects
317 my $facets_counter = {};
318 my $facets_info = {};
319 my $facets = getFacets();
321 my @facets_loop; # stores the ref to array of hashes for template facets loop
323 ### LOOP THROUGH THE SERVERS
324 for ( my $i = 0 ; $i < @servers ; $i++ ) {
325 $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
327 # perform the search, create the results objects
328 # if this is a local search, use the $koha-query, if it's a federated one, use the federated-query
329 my $query_to_use = ($servers[$i] =~ /biblioserver/) ? $koha_query : $simple_query;
331 Koha::Logger->get->debug($simple_query) if $scan;
333 # Check if we've got a query_type defined, if so, use it
336 if ($query_type =~ /^ccl/) {
337 $query_to_use =~ s/\:/\=/g; # change : to = last minute (FIXME)
338 $results[$i] = $zconns[$i]->search(ZOOM::Query::CCL2RPN->new($query_to_use, $zconns[$i]));
339 } elsif ($query_type =~ /^cql/) {
340 $results[$i] = $zconns[$i]->search(ZOOM::Query::CQL->new($query_to_use, $zconns[$i]));
341 } elsif ($query_type =~ /^pqf/) {
342 $results[$i] = $zconns[$i]->search(ZOOM::Query::PQF->new($query_to_use, $zconns[$i]));
344 warn "Unknown query_type '$query_type'. Results undetermined.";
347 $results[$i] = $zconns[$i]->scan( ZOOM::Query::CCL2RPN->new($query_to_use, $zconns[$i]));
349 $results[$i] = $zconns[$i]->search(ZOOM::Query::CCL2RPN->new($query_to_use, $zconns[$i]));
353 warn "WARNING: query problem with $query_to_use " . $@;
356 # Concatenate the sort_by limits and pass them to the results object
357 # Note: sort will override rank
359 foreach my $sort (@sort_by) {
360 if ( $sort eq "author_az" || $sort eq "author_asc" ) {
361 $sort_by .= "1=1003 <i ";
363 elsif ( $sort eq "author_za" || $sort eq "author_dsc" ) {
364 $sort_by .= "1=1003 >i ";
366 elsif ( $sort eq "popularity_asc" ) {
367 $sort_by .= "1=9003,4=109 <i ";
369 elsif ( $sort eq "popularity_dsc" ) {
370 $sort_by .= "1=9003,4=109 >i ";
372 elsif ( $sort eq "call_number_asc" ) {
373 $sort_by .= "1=8007 <i ";
375 elsif ( $sort eq "call_number_dsc" ) {
376 $sort_by .= "1=8007 >i ";
378 elsif ( $sort eq "pubdate_asc" ) {
379 $sort_by .= "1=31 <i ";
381 elsif ( $sort eq "pubdate_dsc" ) {
382 $sort_by .= "1=31 >i ";
384 elsif ( $sort eq "acqdate_asc" ) {
385 $sort_by .= "1=32 <i ";
387 elsif ( $sort eq "acqdate_dsc" ) {
388 $sort_by .= "1=32 >i ";
390 elsif ( $sort eq "title_az" || $sort eq "title_asc" ) {
391 $sort_by .= "1=4 <i ";
393 elsif ( $sort eq "title_za" || $sort eq "title_dsc" ) {
394 $sort_by .= "1=4 >i ";
396 elsif ( $sort eq "biblionumber_az" || $sort eq "biblionumber_asc" ) {
397 $sort_by .= "1=12 <i ";
399 elsif ( $sort eq "biblionumber_za" || $sort eq "biblionumber_dsc" ) {
400 $sort_by .= "1=12 >i ";
403 warn "Ignoring unrecognized sort '$sort' requested" if $sort_by;
406 if ( $sort_by && !$scan && $results[$i] ) {
407 if ( $results[$i]->sort( "yaz", $sort_by ) < 0 ) {
408 warn "WARNING sort $sort_by failed";
411 } # finished looping through servers
413 # The big moment: asynchronously retrieve results from all servers
418 my ( $i, $size ) = @_;
421 # loop through the results
422 $results_hash->{'hits'} = $size;
424 if ( $offset + $results_per_page <= $size ) {
425 $times = $offset + $results_per_page;
431 for ( my $j = $offset ; $j < $times ; $j++ ) {
434 ## Check if it's an index scan
436 my ( $term, $occ ) = $results[ $i - 1 ]->display_term($j);
438 # here we create a minimal MARC record and hand it off to the
439 # template just like a normal result ... perhaps not ideal, but
441 my $tmprecord = MARC::Record->new();
442 $tmprecord->encoding('UTF-8');
446 # the minimal record in author/title (depending on MARC flavour)
447 if ( C4::Context->preference("marcflavour") eq
450 $tmptitle = MARC::Field->new(
455 $tmprecord->append_fields($tmptitle);
459 MARC::Field->new( '245', ' ', ' ', a => $term, );
461 MARC::Field->new( '100', ' ', ' ', a => $occ, );
462 $tmprecord->append_fields($tmptitle);
463 $tmprecord->append_fields($tmpauthor);
465 $results_hash->{'RECORDS'}[$j] =
466 $tmprecord->as_usmarc();
471 $record = $results[ $i - 1 ]->record($j)->raw();
472 # warn "RECORD $j:".$record;
473 $results_hash->{'RECORDS'}[$j] = $record;
477 $results_hashref->{ $servers[ $i - 1 ] } = $results_hash;
479 # Fill the facets while we're looping, but only for the
480 # biblioserver and not for a scan
481 if ( !$scan && $servers[ $i - 1 ] =~ /biblioserver/ ) {
482 $facets_counter = GetFacets( $results[ $i - 1 ] );
483 $facets_info = _get_facets_info( $facets );
487 if ( $servers[ $i - 1 ] =~ /biblioserver/ ) {
489 sort { $a cmp $b } keys %$facets_counter
492 my @this_facets_array;
495 $facets_counter->{$link_value}
496 ->{$b} <=> $facets_counter->{$link_value}
498 } keys %{ $facets_counter->{$link_value} }
501 # Sanitize the link value : parenthesis, question and exclamation mark will cause errors with CCL
502 my $facet_link_value = $one_facet;
503 $facet_link_value =~ s/[()!?¡¿؟]/ /g;
505 # fix the length that will display in the label,
506 my $facet_label_value = $one_facet;
507 my $facet_max_length = C4::Context->preference(
508 'FacetLabelTruncationLength')
511 substr( $one_facet, 0, $facet_max_length )
513 if length($facet_label_value) >
516 # if it's a branch, label by the name, not the code,
517 if ( $link_value =~ /branch/ ) {
518 if ( defined $branches
519 && ref($branches) eq "HASH"
520 && defined $branches->{$one_facet}
521 && ref( $branches->{$one_facet} ) eq
525 $branches->{$one_facet}
529 $facet_label_value = "*";
533 # if it's a itemtype, label by the name, not the code,
534 if ( $link_value =~ /itype/ ) {
535 if ( defined $itemtypes
536 && ref($itemtypes) eq "HASH"
537 && defined $itemtypes->{$one_facet}
538 && ref( $itemtypes->{$one_facet} ) eq
542 $itemtypes->{$one_facet}
543 ->{translated_description};
547 # also, if it's a location code, use the name instead of the code
548 if ( $link_value =~ /location/ ) {
549 # TODO Retrieve all authorised values at once, instead of 1 query per entry
550 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $one_facet });
551 $facet_label_value = $av->count ? $av->next->opac_description : '';
554 # also, if it's a collection code, use the name instead of the code
555 if ( $link_value =~ /ccode/ ) {
556 # TODO Retrieve all authorised values at once, instead of 1 query per entry
557 my $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $one_facet });
558 $facet_label_value = $av->count ? $av->next->opac_description : '';
561 # but we're down with the whole label being in the link's title.
562 push @this_facets_array,
565 $facets_counter->{$link_value}
567 facet_label_value => $facet_label_value,
568 facet_title_value => $one_facet,
569 facet_link_value => $facet_link_value,
570 type_link_value => $link_value,
572 if ($facet_label_value);
577 type_link_value => $link_value,
578 type_id => $link_value . "_id",
580 . $facets_info->{$link_value}->{'label_value'} =>
582 label => $facets_info->{$link_value}->{'label_value'},
583 facets => \@this_facets_array,
587 $facets_info->{$link_value}->{'label_value'} =~
590 and ( Koha::Libraries->search->count == 1 )
597 # This sorts the facets into alphabetical order
599 foreach my $f (@facets_loop) {
600 if( C4::Context->preference('FacetOrder') eq 'Alphabetical' ){
602 [ sort { uc($a->{facet_label_value}) cmp uc($b->{facet_label_value}) } @{ $f->{facets} } ];
607 return ( undef, $results_hashref, \@facets_loop );
615 my $use_zebra_facets = C4::Context->config('use_zebra_facets') // 0;
617 if ( $use_zebra_facets ) {
618 $facets = _get_facets_from_zebra( $rs );
620 $facets = _get_facets_from_records( $rs );
626 sub _get_facets_from_records {
630 my $facets_maxrecs = C4::Context->preference('maxRecordsForFacets') // 20;
631 my $facets_config = getFacets();
633 my $size = $rs->size();
634 my $jmax = $size > $facets_maxrecs
638 for ( my $j = 0 ; $j < $jmax ; $j++ ) {
640 my $marc_record = new_record_from_zebra (
642 $rs->record( $j )->raw()
645 if ( ! defined $marc_record ) {
646 warn "ERROR DECODING RECORD - $@: " .
647 $rs->record( $j )->raw();
651 _get_facets_data_from_record( $marc_record, $facets_config, $facets );
657 =head2 _get_facets_data_from_record
659 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
661 Internal function that extracts facets information from a MARC::Record object
662 and populates $facets_counter for using in getRecords.
664 $facets is expected to be filled with C4::Koha::getFacets output (i.e. the configured
669 sub _get_facets_data_from_record {
671 my ( $marc_record, $facets, $facets_counter ) = @_;
673 for my $facet (@$facets) {
677 foreach my $tag ( @{ $facet->{ tags } } ) {
679 # tag number is the first three digits
680 my $tag_num = substr( $tag, 0, 3 );
681 # subfields are the remainder
682 my $subfield_letters = substr( $tag, 3 );
684 my @fields = $marc_record->field( $tag_num );
685 foreach my $field (@fields) {
686 # If $field->indicator(1) eq 'z', it means it is a 'see from'
687 # field introduced because of IncludeSeeFromInSearches, so skip it
688 next if $field->indicator(1) eq 'z';
690 my $data = $field->as_string( $subfield_letters, $facet->{ sep } );
691 $data =~ s/\s*(?<!\p{Uppercase})[.\-,;]*\s*$//;
693 unless ( grep { $_ eq $data } @used_datas ) {
694 push @used_datas, $data;
695 $facets_counter->{ $facet->{ idx } }->{ $data }++;
702 =head2 _get_facets_from_zebra
704 my $facets = _get_facets_from_zebra( $result_set )
706 Retrieves facets for a specified result set. It loops through the facets defined
707 in C4::Koha::getFacets and returns a hash with the following structure:
717 sub _get_facets_from_zebra {
721 # save current elementSetName
722 my $elementSetName = $rs->option( 'elementSetName' );
724 my $facets_loop = getFacets();
725 my $facets_data = {};
726 # loop through defined facets and fill the facets hashref
727 foreach my $facet ( @$facets_loop ) {
729 my $idx = $facet->{ idx };
730 my $sep = $facet->{ sep };
731 my $facet_values = _get_facet_from_result_set( $idx, $rs, $sep );
732 if ( $facet_values ) {
733 # we've actually got a result
734 $facets_data->{ $idx } = $facet_values;
737 # set elementSetName to its previous value to avoid side effects
738 $rs->option( elementSetName => $elementSetName );
743 =head2 _get_facet_from_result_set
746 C4::Search::_get_facet_from_result_set( $facet_idx, $result_set, $sep )
748 Internal function that extracts facet information for a specific index ($facet_idx) and
749 returns a hash containing facet values and count:
752 $facet_value => $count ,
756 Warning: this function has the side effect of changing the elementSetName for the result
757 set. It is a helper function for the main loop, which takes care of backing it up for
762 sub _get_facet_from_result_set {
764 my $facet_idx = shift;
768 my $internal_sep = '<*>';
769 my $facetMaxCount = C4::Context->preference('FacetMaxCount') // 20;
771 return if ( ! defined $facet_idx || ! defined $rs );
772 # zebra's facet element, untokenized index
773 my $facet_element = 'zebra::facet::' . $facet_idx . ':0:' . $facetMaxCount;
774 # configure zebra results for retrieving the desired facet
775 $rs->option( elementSetName => $facet_element );
776 # get the facet record from result set
777 my $facet = $rs->record( 0 )->raw;
778 # if the facet has no restuls...
779 return if !defined $facet;
780 # TODO: benchmark DOM vs. SAX performance
781 my $facet_dom = XML::LibXML->load_xml(
784 my @terms = $facet_dom->getElementsByTagName('term');
788 foreach my $term ( @terms ) {
789 my $facet_value = $term->textContent;
790 $facet_value =~ s/\s*(?<!\p{Uppercase})[.\-,;]*\s*$//;
791 $facet_value =~ s/\Q$internal_sep\E/$sep/ if defined $sep;
792 $facets->{ $facet_value } += $term->getAttribute( 'occur' );
798 =head2 _get_facets_info
800 my $facets_info = C4::Search::_get_facets_info( $facets )
802 Internal function that extracts facets information and properly builds
803 the data structure needed to render facet labels.
807 sub _get_facets_info {
811 my $facets_info = {};
813 for my $facet ( @$facets ) {
814 $facets_info->{ $facet->{ idx } }->{ label_value } = $facet->{ label };
821 sub _detect_truncation {
822 my ( $operand, $index ) = @_;
823 my ( @nontruncated, @righttruncated, @lefttruncated, @rightlefttruncated,
826 my @wordlist = split( /\s/, $operand );
827 foreach my $word (@wordlist) {
828 if ( $word =~ s/^\*([^\*]+)\*$/$1/ ) {
829 push @rightlefttruncated, $word;
831 elsif ( $word =~ s/^\*([^\*]+)$/$1/ ) {
832 push @lefttruncated, $word;
834 elsif ( $word =~ s/^([^\*]+)\*$/$1/ ) {
835 push @righttruncated, $word;
837 elsif ( index( $word, "*" ) < 0 ) {
838 push @nontruncated, $word;
841 push @regexpr, $word;
845 \@nontruncated, \@righttruncated, \@lefttruncated,
846 \@rightlefttruncated, \@regexpr
851 sub _build_stemmed_operand {
852 my ($operand,$lang) = @_;
853 require Lingua::Stem::Snowball ;
854 my $stemmed_operand=q{};
856 # Stemmer needs language
857 return $operand unless $lang;
859 # If operand contains a digit, it is almost certainly an identifier, and should
860 # not be stemmed. This is particularly relevant for ISBNs and ISSNs, which
861 # can contain the letter "X" - for example, _build_stemmend_operand would reduce
862 # "014100018X" to "x ", which for a MARC21 database would bring up irrelevant
863 # results (e.g., "23 x 29 cm." from the 300$c). Bug 2098.
864 return $operand if $operand =~ /\d/;
866 # FIXME: the locale should be set based on the user's language and/or search choice
868 # Make sure we only use the first two letters from the language code
869 $lang = lc(substr($lang, 0, 2));
870 # The language codes for the two variants of Norwegian will now be "nb" and "nn",
871 # none of which Lingua::Stem::Snowball can use, so we need to "translate" them
872 if ($lang eq 'nb' || $lang eq 'nn') {
875 my $stemmer = Lingua::Stem::Snowball->new( lang => $lang,
876 encoding => "UTF-8" );
878 my @words = split( / /, $operand );
879 my @stems = $stemmer->stem(\@words);
880 for my $stem (@stems) {
881 $stemmed_operand .= "$stem";
882 $stemmed_operand .= "?"
883 unless ( $stem =~ /(and$|or$|not$)/ ) || ( length($stem) < 3 );
884 $stemmed_operand .= " ";
887 Koha::Logger->get->debug("STEMMED OPERAND: $stemmed_operand");
888 return $stemmed_operand;
892 sub _build_weighted_query {
894 # FIELD WEIGHTING - This is largely experimental stuff. What I'm committing works
895 # pretty well but could work much better if we had a smarter query parser
896 my ( $operand, $stemmed_operand, $index ) = @_;
897 my $stemming = C4::Context->preference("QueryStemming") || 0;
898 my $weight_fields = C4::Context->preference("QueryWeightFields") || 0;
899 my $fuzzy_enabled = C4::Context->preference("QueryFuzzy") || 0;
900 $operand =~ s/"/ /g; # Bug 7518: searches with quotation marks don't work
902 my $weighted_query = "(rk=("; # Specifies that we're applying rank
904 # Keyword, or, no index specified
905 if ( ( $index eq 'kw' ) || ( !$index ) ) {
907 "Title-cover,ext,r1=\"$operand\""; # exact title-cover
908 $weighted_query .= " or ti,ext,r2=\"$operand\""; # exact title
909 $weighted_query .= " or Title-cover,phr,r3=\"$operand\""; # phrase title
910 $weighted_query .= " or ti,wrdl,r4=\"$operand\""; # words in title
911 #$weighted_query .= " or any,ext,r4=$operand"; # exact any
912 #$weighted_query .=" or kw,wrdl,r5=\"$operand\""; # word list any
913 $weighted_query .= " or wrdl,fuzzy,r8=\"$operand\""
914 if $fuzzy_enabled; # add fuzzy, word list
915 $weighted_query .= " or wrdl,right-Truncation,r9=\"$stemmed_operand\""
916 if ( $stemming and $stemmed_operand )
917 ; # add stemming, right truncation
918 $weighted_query .= " or wrdl,r9=\"$operand\"";
920 # embedded sorting: 0 a-z; 1 z-a
921 # $weighted_query .= ") or (sort1,aut=1";
924 # Barcode searches should skip this process
925 elsif ( $index eq 'bc' ) {
926 $weighted_query .= "bc=\"$operand\"";
929 # Authority-number searches should skip this process
930 elsif ( $index eq 'an' ) {
931 $weighted_query .= "an=\"$operand\"";
934 # If the index is numeric, don't autoquote it.
935 elsif ( $index =~ /,st-numeric$/ ) {
936 $weighted_query .= " $index=$operand";
939 # If the index already has more than one qualifier, wrap the operand
940 # in quotes and pass it back (assumption is that the user knows what they
941 # are doing and won't appreciate us mucking up their query
942 elsif ( $index =~ ',' ) {
943 $weighted_query .= " $index=\"$operand\"";
946 #TODO: build better cases based on specific search indexes
948 $weighted_query .= " $index,ext,r1=\"$operand\""; # exact index
949 #$weighted_query .= " or (title-sort-az=0 or $index,startswithnt,st-word,r3=$operand #)";
950 $weighted_query .= " or $index,phr,r3=\"$operand\""; # phrase index
951 $weighted_query .= " or $index,wrdl,r6=\"$operand\""; # word list index
952 $weighted_query .= " or $index,wrdl,fuzzy,r8=\"$operand\""
953 if $fuzzy_enabled; # add fuzzy, word list
954 $weighted_query .= " or $index,wrdl,rt,r9=\"$stemmed_operand\""
955 if ( $stemming and $stemmed_operand ); # add stemming, right truncation
958 $weighted_query .= "))"; # close rank specification
959 return $weighted_query;
964 Return an array with available indexes.
988 'Author-personal-bibliography',
998 'Chronological-subdivision',
1008 'Conference-name-heading',
1009 'Conference-name-see',
1010 'Conference-name-seealso',
1014 'Control-number-identifier',
1018 'Corporate-name-heading',
1019 'Corporate-name-see',
1020 'Corporate-name-seealso',
1021 'Country-publication',
1024 'date-entered-on-file',
1025 'Date-of-acquisition',
1026 'Date-of-publication',
1027 'Date-time-last-modified',
1028 'Dewey-classification',
1029 'Dissertation-information',
1038 'Geographic-subdivision',
1041 'Heading-use-main-or-added-entry',
1042 'Heading-use-series-added-entry ',
1043 'Heading-use-subject-added-entry',
1047 'Identifier-standard',
1048 'Illustration-code',
1050 'Index-term-uncontrolled',
1051 'Interest-age-level',
1052 'Interest-grade-level',
1062 'language-original',
1072 'Local-classification',
1075 'Match-heading-see-from',
1080 'Multipart-resource-level',
1085 'Name-geographic-heading',
1086 'Name-geographic-see',
1087 'Name-geographic-seealso',
1093 'Other-control-number',
1096 'Personal-name-heading',
1097 'Personal-name-see',
1098 'Personal-name-seealso',
1100 'Place-publication',
1107 'Reading-grade-level',
1108 'Record-control-number',
1119 'Subject-heading-thesaurus',
1120 'Subject-name-personal',
1121 'Subject-subdivision',
1130 'Term-genre-form-heading',
1131 'Term-genre-form-see',
1132 'Term-genre-form-seealso',
1138 'Title-uniform-heading',
1139 'Title-uniform-see',
1140 'Title-uniform-seealso',
1150 'classification-source',
1152 'coded-location-qualifier',
1163 'Local-classification',
1166 'materials-specified',
1171 'Number-local-acquisition',
1176 'replacementpricedate',
1194 $simple_query, $query_cgi,
1195 $query_desc, $limit,
1196 $limit_cgi, $limit_desc,
1197 $query_type ) = buildQuery ( $operators, $operands, $indexes, $limits, $sort_by, $scan, $lang);
1199 Build queries and limits in CCL, CGI, Human,
1200 handle truncation, stemming, field weighting, fuzziness, etc.
1202 See verbose embedded documentation.
1208 my ( $operators, $operands, $indexes, $limits, $sort_by, $scan, $lang) = @_;
1213 my @operators = $operators ? @$operators : ();
1214 my @indexes = $indexes ? @$indexes : ();
1215 my @operands = $operands ? @$operands : ();
1216 my @limits = $limits ? @$limits : ();
1217 my @sort_by = $sort_by ? @$sort_by : ();
1219 my $stemming = C4::Context->preference("QueryStemming") || 0;
1220 my $auto_truncation = C4::Context->preference("QueryAutoTruncate") || 0;
1221 my $weight_fields = C4::Context->preference("QueryWeightFields") || 0;
1222 my $fuzzy_enabled = C4::Context->preference("QueryFuzzy") || 0;
1224 my $query = $operands[0] // "";
1225 my $simple_query = $operands[0];
1227 # initialize the variables we're passing back
1236 my $cclindexes = getIndexes();
1237 if ( $query !~ /\s*(ccl=|pqf=|cql=)/ ) {
1238 while ( !$cclq && $query =~ /(?:^|\W)([\w-]+)(,[\w-]+)*[:=]/g ) {
1240 $cclq = grep { lc($_) eq $dx } @$cclindexes;
1242 $query = "ccl=$query" if $cclq;
1246 my %group_OR_limits;
1247 my $availability_limit;
1248 foreach my $this_limit (@limits) {
1249 next unless $this_limit;
1250 if ( $this_limit =~ /available/ ) {
1252 ## 'available' is defined as (items.onloan is NULL) and (items.itemlost = 0)
1254 ## all records not indexed in the onloan register (zebra) and all records with a value of lost equal to 0
1255 $availability_limit .=
1256 "( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) )";
1257 $limit_cgi .= "&limit=available";
1261 # group_OR_limits, prefixed by mc-
1262 # OR every member of the group
1263 elsif ( $this_limit =~ /mc/ ) {
1264 my ($k,$v) = split(/:/, $this_limit,2);
1265 if ( $k !~ /mc-i(tem)?type/ ) {
1266 # in case the mc-ccode value has complicating chars like ()'s inside it we wrap in quotes
1267 $this_limit =~ tr/"//d;
1268 $this_limit = $k.':"'.$v.'"';
1271 $group_OR_limits{$k} .= " or " if $group_OR_limits{$k};
1272 $limit_desc .= " or " if $group_OR_limits{$k};
1273 $group_OR_limits{$k} .= "$this_limit";
1274 $limit_cgi .= "&limit=" . uri_escape_utf8($this_limit);
1275 $limit_desc .= " $this_limit";
1277 elsif ( $this_limit =~ '^multibranchlimit:|^branch:' ) {
1278 $limit_cgi .= "&limit=" . uri_escape_utf8($this_limit);
1279 $limit .= " and " if $limit || $query;
1280 my $branchfield = C4::Context->preference('SearchLimitLibrary');
1282 if( $this_limit =~ '^multibranchlimit:' ){
1283 my ($group_id) = ( $this_limit =~ /^multibranchlimit:(.*)$/ );
1284 my $search_group = Koha::Library::Groups->find( $group_id );
1285 @branchcodes = map { $_->branchcode } $search_group->all_libraries;
1286 @branchcodes = sort { $a cmp $b } @branchcodes;
1288 @branchcodes = ( $this_limit =~ /^branch:(.*)$/ );
1292 if ( $branchfield eq "homebranch" ) {
1293 $this_limit = sprintf "(%s)", join " or ", map { 'homebranch: ' . $_ } @branchcodes;
1295 elsif ( $branchfield eq "holdingbranch" ) {
1296 $this_limit = sprintf "(%s)", join " or ", map { 'holdingbranch: ' . $_ } @branchcodes;
1299 $this_limit = sprintf "(%s or %s)",
1300 join( " or ", map { 'homebranch: ' . $_ } @branchcodes ),
1301 join( " or ", map { 'holdingbranch: ' . $_ } @branchcodes );
1304 $limit .= "$this_limit";
1305 $limit_desc .= " $this_limit";
1306 } elsif ( $this_limit =~ '^search_filter:' ) {
1307 # Here we will get the query as a string, append to the limits, and pass through buildQuery
1308 # again to clean the terms and handle nested filters
1309 $limit_cgi .= "&limit=" . uri_escape_utf8($this_limit);
1310 my ($filter_id) = ( $this_limit =~ /^search_filter:(.*)$/ );
1311 my $search_filter = Koha::SearchFilters->find( $filter_id );
1312 next unless $search_filter;
1313 my ($expanded_lim, $query_lim) = $search_filter->expand_filter;
1314 push @$expanded_lim, $query_lim;
1315 my ( $error, undef, undef, undef, undef, $fixed_limit, undef, undef, undef ) = buildQuery ( undef, undef, undef, $expanded_lim, undef, undef, $lang);
1316 $limit .= " and " if $limit || $query;
1317 $limit .= "$fixed_limit";
1318 $limit_desc .= " $limit";
1321 # Regular old limits
1323 $limit .= " and " if $limit || $query;
1324 $limit .= "$this_limit";
1325 $limit_cgi .= "&limit=" . uri_escape_utf8($this_limit);
1326 $limit_desc .= " $this_limit";
1329 foreach my $k (keys (%group_OR_limits)) {
1330 $limit .= " and " if ( $query || $limit );
1331 $limit .= "($group_OR_limits{$k})";
1333 if ($availability_limit) {
1334 $limit .= " and " if ( $query || $limit );
1335 $limit .= "($availability_limit)";
1338 # for handling ccl, cql, pqf queries in diagnostic mode, skip the rest of the steps
1340 if ( $query =~ /^ccl=/ ) {
1342 # This is needed otherwise ccl= and &limit won't work together, and
1343 # this happens when selecting a subject on the opac-detail page
1344 my $original_q = $q; # without available part
1345 $q .= $limit if $limit;
1346 return ( undef, $q, $q, "q=ccl=".uri_escape_utf8($q), $original_q, '', '', '', 'ccl' );
1348 if ( $query =~ /^cql=/ ) {
1349 return ( undef, $', $', "q=cql=".uri_escape_utf8($'), $', '', '', '', 'cql' );
1351 if ( $query =~ /^pqf=/ ) {
1353 $query_cgi = "q=pqf=".uri_escape_utf8($');
1354 return ( undef, $', $', $query_cgi, $query_desc, '', '', '', 'pqf' );
1357 # pass nested queries directly
1358 # FIXME: need better handling of some of these variables in this case
1359 # Nested queries aren't handled well and this implementation is flawed and causes users to be
1360 # unable to search for anything containing () commenting out, will be rewritten for 3.4.0
1361 # if ( $query =~ /(\(|\))/ ) {
1363 # undef, $query, $simple_query, $query_cgi,
1364 # $query, $limit, $limit_cgi, $limit_desc,
1369 # Form-based queries are non-nested and fixed depth, so we can easily modify the incoming
1370 # query operands and indexes and add stemming, truncation, field weighting, etc.
1371 # Once we do so, we'll end up with a value in $query, just like if we had an
1372 # incoming $query from the user
1375 ; # clear it out so we can populate properly with field-weighted, stemmed, etc. query
1376 my $previous_operand
1377 ; # a flag used to keep track if there was a previous query
1378 # if there was, we can apply the current operator
1380 for ( my $i = 0 ; $i <= @operands ; $i++ ) {
1382 # COMBINE OPERANDS, INDEXES AND OPERATORS
1383 if ( ($operands[$i] // '') ne '' ) {
1384 $operands[$i]=~s/^\s+//;
1386 # A flag to determine whether or not to add the index to the query
1389 # If the user is sophisticated enough to specify an index, turn off field weighting, and stemming handling
1390 if ( $operands[$i] =~ /\w(:|=)/ || $scan ) {
1394 $operands[$i] =~ s/\?/{?}/g; # need to escape question marks
1396 my $operand = $operands[$i];
1397 my $index = $indexes[$i] || 'kw';
1399 # Add index-specific attributes
1401 #Afaik, this 'yr' condition will only ever be met in the staff interface advanced search
1402 #for "Publication date", since typing 'yr:YYYY' into the search box produces a CCL query,
1403 #which is processed higher up in this sub. Other than that, year searches are typically
1404 #handled as limits which are not processed her either.
1406 # Search ranges: Date of Publication, st-numeric
1407 if ( $index =~ /(yr|st-numeric)/ ) {
1408 #weight_fields/relevance search causes errors with date ranges
1409 #In the case of YYYY-, it will only return records with a 'yr' of YYYY (not the range)
1410 #In the case of YYYY-YYYY, it will return no results
1411 $stemming = $auto_truncation = $weight_fields = $fuzzy_enabled = 0;
1414 # Date of Acquisition
1415 elsif ( $index =~ /acqdate/ ) {
1416 #stemming and auto_truncation would have zero impact since it already is YYYY-MM-DD format
1417 #Weight_fields probably SHOULD be turned OFF, otherwise you'll get records floating to the
1418 #top of the results just because they have lots of item records matching that date.
1419 #Fuzzy actually only applies during _build_weighted_query, and is reset there anyway, so
1421 $stemming = $auto_truncation = $weight_fields = $fuzzy_enabled = 0;
1423 # ISBN,ISSN,Standard Number, don't need special treatment
1424 elsif ( $index eq 'nb' || $index eq 'ns' || $index eq 'hi' ) {
1426 $stemming, $auto_truncation,
1427 $weight_fields, $fuzzy_enabled
1430 if ( $index eq 'nb' ) {
1431 if ( C4::Context->preference("SearchWithISBNVariations") ) {
1432 my @isbns = C4::Koha::GetVariationsOfISBN( $operand );
1433 $operands[$i] = $operand = '(' . join( ' OR ', map { 'nb=' . $_ } @isbns ) . ')';
1434 $indexes[$i] = $index = 'kw';
1437 if ( $index eq 'ns' ) {
1438 if ( C4::Context->preference("SearchWithISSNVariations") ) {
1439 my @issns = C4::Koha::GetVariationsOfISSN( $operand );
1440 $operands[$i] = $operand = '(' . join( ' OR ', map { 'ns=' . $_ } @issns ) . ')';
1441 $indexes[$i] = $index = 'kw';
1446 # Set default structure attribute (word list)
1447 my $struct_attr = q{};
1448 unless ( $indexes_set || $index =~ /,(st-|phr|ext|wrdl)/ || $index =~ /^(nb|ns)$/ ) {
1449 $struct_attr = ",wrdl";
1452 # Some helpful index variants
1453 my $index_plus = $index . $struct_attr . ':';
1454 my $index_plus_comma = $index . $struct_attr . ',';
1456 if ($auto_truncation){
1457 unless ( $index =~ /,(st-|phr|ext)/ ) {
1458 #FIXME only valid with LTR scripts
1459 $operand=join(" ",map{
1460 (index($_,"*")>0?"$_":"$_*")
1461 }split (/\s+/,$operand));
1466 my $truncated_operand = q{};
1467 my( $nontruncated, $righttruncated, $lefttruncated,
1468 $rightlefttruncated, $regexpr
1469 ) = _detect_truncation( $operand, $index );
1471 Koha::Logger->get->debug(
1472 "TRUNCATION: NON:>@$nontruncated< RIGHT:>@$righttruncated< LEFT:>@$lefttruncated< RIGHTLEFT:>@$rightlefttruncated< REGEX:>@$regexpr<");
1476 scalar(@$righttruncated) + scalar(@$lefttruncated) +
1477 scalar(@$rightlefttruncated) > 0 )
1480 # Don't field weight or add the index to the query, we do it here
1482 undef $weight_fields;
1483 my $previous_truncation_operand;
1484 if (scalar @$nontruncated) {
1485 $truncated_operand .= "$index_plus @$nontruncated ";
1486 $previous_truncation_operand = 1;
1488 if (scalar @$righttruncated) {
1489 $truncated_operand .= "and " if $previous_truncation_operand;
1490 $truncated_operand .= $index_plus_comma . "rtrn:@$righttruncated ";
1491 $previous_truncation_operand = 1;
1493 if (scalar @$lefttruncated) {
1494 $truncated_operand .= "and " if $previous_truncation_operand;
1495 $truncated_operand .= $index_plus_comma . "ltrn:@$lefttruncated ";
1496 $previous_truncation_operand = 1;
1498 if (scalar @$rightlefttruncated) {
1499 $truncated_operand .= "and " if $previous_truncation_operand;
1500 $truncated_operand .= $index_plus_comma . "rltrn:@$rightlefttruncated ";
1501 $previous_truncation_operand = 1;
1504 $operand = $truncated_operand if $truncated_operand;
1505 Koha::Logger->get->debug("TRUNCATED OPERAND: >$truncated_operand<");
1508 my $stemmed_operand = q{};
1509 $stemmed_operand = _build_stemmed_operand($operand, $lang)
1512 Koha::Logger->get->debug("STEMMED OPERAND: >$stemmed_operand<");
1514 # Handle Field Weighting
1515 my $weighted_operand = q{};
1516 if ($weight_fields) {
1517 $weighted_operand = _build_weighted_query( $operand, $stemmed_operand, $index );
1518 $operand = $weighted_operand;
1522 Koha::Logger->get->debug("FIELD WEIGHTED OPERAND: >$weighted_operand<");
1524 #Use relevance ranking when not using a weighted query (which adds relevance ranking of its own)
1526 #N.B. Truncation is mutually exclusive with Weighted Queries,
1527 #so even if QueryWeightFields is turned on, QueryAutoTruncate will turn it off, thus
1528 #the need for this relevance wrapper.
1529 $operand = "(rk=($operand))" unless $weight_fields;
1531 ($query,$query_cgi,$query_desc,$previous_operand) = _build_initial_query({
1533 query_cgi => $query_cgi,
1534 query_desc => $query_desc,
1535 operator => ($operators[ $i - 1 ]) ? $operators[ $i - 1 ] : '',
1536 parsed_operand => $operand,
1537 original_operand => $operands[$i] // '',
1539 index_plus => $index_plus,
1540 indexes_set => $indexes_set,
1541 previous_operand => $previous_operand,
1547 Koha::Logger->get->debug("QUERY BEFORE LIMITS: >$query<");
1549 # Normalize the query and limit strings
1550 # This is flawed , means we can't search anything with : in it
1551 # if user wants to do ccl or cql, start the query with that
1552 # $query =~ s/:/=/g;
1553 #NOTE: We use several several different regexps here as you can't have variable length lookback assertions
1554 $query =~ s/(?<=(ti|au|pb|su|an|kw|mc|nb|ns)):/=/g;
1555 $query =~ s/(?<=(wrdl)):/=/g;
1556 $query =~ s/(?<=(trn|phr)):/=/g;
1557 $query =~ s/(?<=(st-numeric)):/=/g;
1558 $query =~ s/(?<=(st-year)):/=/g;
1559 $query =~ s/(?<=(st-date-normalized)):/=/g;
1561 # Removing warnings for later substitutions
1563 $query_desc //= q{};
1566 $limit_desc //= q{};
1568 $simple_query //= q{};
1570 for ( $query, $query_desc, $limit, $limit_desc ) {
1571 s/ +/ /g; # remove extra spaces
1572 s/^ //g; # remove any beginning spaces
1573 s/ $//g; # remove any ending spaces
1574 s/==/=/g; # remove double == from query
1576 $query_cgi =~ s/^&//; # remove unnecessary & from beginning of the query cgi
1578 for ($query_cgi,$simple_query) {
1581 # append the limit to the query
1582 $query .= " " . $limit;
1584 Koha::Logger->get->debug(
1585 sprintf "buildQuery returns\nQUERY:%s\nQUERY CGI:%s\nQUERY DESC:%s\nLIMIT:%s\nLIMIT CGI:%s\nLIMIT DESC:%s",
1586 $query, $query_cgi, $query_desc, $limit, $limit_cgi, $limit_desc );
1589 undef, $query, $simple_query, $query_cgi,
1590 $query_desc, $limit, $limit_cgi, $limit_desc,
1595 =head2 _build_initial_query
1597 ($query, $query_cgi, $query_desc, $previous_operand) = _build_initial_query($initial_query_params);
1599 Build a section of the initial query containing indexes, operators, and operands.
1603 sub _build_initial_query {
1607 if ($params->{previous_operand}){
1608 #If there is a previous operand, add a supplied operator or the default 'and'
1609 $operator = ($params->{operator}) ? ($params->{operator}) : 'AND';
1612 #NOTE: indexes_set is typically set when doing truncation or field weighting
1613 my $operand = ($params->{indexes_set}) ? $params->{parsed_operand} : $params->{index_plus}.$params->{parsed_operand};
1615 #e.g. "kw,wrdl:test"
1616 #e.g. " and kw,wrdl:test"
1617 $params->{query} .= " " . $operator . " " . $operand;
1619 $params->{query_cgi} .= "&op=".uri_escape_utf8($operator) if $operator;
1620 $params->{query_cgi} .= "&idx=".uri_escape_utf8($params->{index}) if $params->{index};
1621 $params->{query_cgi} .= "&q=".uri_escape_utf8($params->{original_operand}) if ( $params->{original_operand} ne '' );
1623 #e.g. " and kw,wrdl: test"
1624 $params->{query_desc} .= " " . $operator . " " . ( $params->{index_plus} // q{} ) . " " . ( $params->{original_operand} // q{} );
1626 $params->{previous_operand} = 1 unless $params->{previous_operand}; #If there is no previous operand, mark this as one
1628 return ($params->{query}, $params->{query_cgi}, $params->{query_desc}, $params->{previous_operand});
1631 =head2 searchResults
1633 my @search_results = searchResults($search_context, $searchdesc, $hits,
1634 $results_per_page, $offset, $scan,
1637 Format results in a form suitable for passing to the template
1641 # IMO this subroutine is pretty messy still -- it's responsible for
1642 # building the HTML output for the template
1644 my ( $search_context, $searchdesc, $hits, $results_per_page, $offset, $scan, $marcresults, $xslt_variables ) = @_;
1645 my $dbh = C4::Context->dbh;
1650 $search_context->{'interface'} = 'opac' if !$search_context->{'interface'} || $search_context->{'interface'} ne 'intranet';
1651 my ($is_opac, $hidelostitems);
1652 if ($search_context->{'interface'} eq 'opac') {
1653 $hidelostitems = C4::Context->preference('hidelostitems');
1657 my $record_processor = Koha::RecordProcessor->new({
1658 filters => 'ViewPolicy'
1661 #Build branchnames hash
1662 my %branches = map { $_->branchcode => $_->branchname } Koha::Libraries->search({}, { order_by => 'branchname' })->as_list;
1664 # FIXME - We build an authorised values hash here, using the default framework
1665 # though it is possible to have different authvals for different fws.
1667 my $shelflocations =
1668 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
1670 # get notforloan authorised value list (see $shelflocations FIXME)
1671 my $av = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
1672 my $notforloan_authorised_value = $av->count ? $av->next->authorised_value : undef;
1675 my $itemtypes = Koha::ItemTypes->search_with_localization;
1676 my %itemtypes = map { $_->{itemtype} => $_ } @{ $itemtypes->unblessed };
1678 #search item field code
1679 my ($itemtag, undef) = &GetMarcFromKohaField( "items.itemnumber" );
1681 ## find column names of items related to MARC
1682 my %subfieldstosearch;
1683 my @columns = Koha::Database->new()->schema()->resultset('Item')->result_source->columns;
1684 for my $column ( @columns ) {
1685 my ( $tagfield, $tagsubfield ) =
1686 &GetMarcFromKohaField( "items." . $column );
1687 if ( defined $tagsubfield ) {
1688 $subfieldstosearch{$column} = $tagsubfield;
1692 # handle which records to actually retrieve
1693 my $times; # Times is which record to process up to
1694 if ( $hits && $offset + $results_per_page <= $hits ) {
1695 $times = $offset + $results_per_page;
1698 $times = $hits // 0; # If less hits than results_per_page+offset we go to the end
1701 my $marcflavour = C4::Context->preference("marcflavour");
1702 # We get the biblionumber position in MARC
1703 my ($bibliotag,$bibliosubf)=GetMarcFromKohaField( 'biblio.biblionumber' );
1705 # set stuff for XSLT processing here once, not later again for every record we retrieved
1707 my $userenv = C4::Context->userenv;
1709 = ( defined $userenv and $userenv->{number} )
1710 ? Koha::Patrons->find( $userenv->{number} )
1712 my $patron_category_hide_lost_items = ($logged_in_user) ? $logged_in_user->category->hidelostitems : 0;
1714 # loop through all of the records we've retrieved
1715 for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) {
1719 # For Scan searches we built USMARC data
1720 $marcrecord = MARC::Record->new_from_usmarc( $marcresults->[$i]);
1722 # Normal search, render from Zebra's output
1723 $marcrecord = new_record_from_zebra(
1728 if ( ! defined $marcrecord ) {
1729 warn "ERROR DECODING RECORD - $@: " . $marcresults->[$i];
1737 ? GetFrameworkCode($marcrecord->field($bibliotag)->data)
1738 : GetFrameworkCode($marcrecord->subfield($bibliotag,$bibliosubf));
1740 SetUTF8Flag($marcrecord);
1741 my $oldbiblio = TransformMarcToKoha({ record => $marcrecord, limit_table => 'no_items' });
1742 $oldbiblio->{result_number} = $i + 1;
1744 $oldbiblio->{normalized_upc} = GetNormalizedUPC( $marcrecord,$marcflavour);
1745 $oldbiblio->{normalized_ean} = GetNormalizedEAN( $marcrecord,$marcflavour);
1746 $oldbiblio->{normalized_oclc} = GetNormalizedOCLCNumber($marcrecord,$marcflavour);
1747 $oldbiblio->{normalized_isbn} = GetNormalizedISBN($oldbiblio->{isbn},$marcrecord,$marcflavour); # Use existing ISBN from record if we got one
1748 $oldbiblio->{content_identifier_exists} = 1 if ($oldbiblio->{normalized_isbn} or $oldbiblio->{normalized_oclc} or $oldbiblio->{normalized_ean} or $oldbiblio->{normalized_upc});
1750 # edition information, if any
1751 $oldbiblio->{edition} = $oldbiblio->{editionstatement};
1753 my $itemtype = $oldbiblio->{itemtype} ? $itemtypes{$oldbiblio->{itemtype}} : undef;
1754 # add imageurl to itemtype if there is one
1755 $oldbiblio->{imageurl} = $itemtype ? getitemtypeimagelocation( $search_context->{'interface'}, $itemtype->{imageurl} ) : q{};
1756 # Build summary if there is one (the summary is defined in the itemtypes table)
1757 $oldbiblio->{description} = $itemtype ? $itemtype->{translated_description} : q{};
1759 # Pull out the items fields
1760 my @fields = $marcrecord->field($itemtag);
1761 $marcrecord->delete_fields( @fields ) unless C4::Context->preference('PassItemMarcToXSLT');
1762 my $marcflavor = C4::Context->preference("marcflavour");
1764 # adding linked items that belong to host records
1765 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
1766 my $analyticsfield = '773';
1767 if ($marcflavor eq 'MARC21') {
1768 $analyticsfield = '773';
1769 } elsif ($marcflavor eq 'UNIMARC') {
1770 $analyticsfield = '461';
1772 foreach my $hostfield ( $marcrecord->field($analyticsfield)) {
1773 my $hostbiblionumber = $hostfield->subfield("0");
1774 my $linkeditemnumber = $hostfield->subfield("9");
1775 if( $hostbiblionumber ) {
1776 my $linkeditemmarc = C4::Items::GetMarcItem( $hostbiblionumber, $linkeditemnumber );
1777 if ($linkeditemmarc) {
1778 my $linkeditemfield = $linkeditemmarc->field($itemtag);
1779 if ($linkeditemfield) {
1780 push( @fields, $linkeditemfield );
1787 # Setting item statuses for display
1788 my @available_items_loop;
1789 my @onloan_items_loop;
1790 my @other_items_loop;
1792 my $available_items;
1796 my $ordered_count = 0;
1797 my $available_count = 0;
1798 my $onloan_count = 0;
1799 my $longoverdue_count = 0;
1800 my $other_count = 0;
1801 my $withdrawn_count = 0;
1802 my $itemlost_count = 0;
1803 my $hideatopac_count = 0;
1804 my $itembinding_count = 0;
1805 my $itemdamaged_count = 0;
1806 my $item_in_transit_count = 0;
1807 my $item_onhold_count = 0;
1808 my $notforloan_count = 0;
1809 my $item_recalled_count = 0;
1810 my $items_count = scalar(@fields);
1811 my $maxitems_pref = C4::Context->preference('maxItemsinSearchResults');
1812 my $maxitems = $maxitems_pref ? $maxitems_pref - 1 : 1;
1813 my @hiddenitems; # hidden itemnumbers based on OpacHiddenItems syspref
1815 # loop through every item
1816 foreach my $field (@fields) {
1819 # populate the items hash
1820 foreach my $code ( keys %subfieldstosearch ) {
1821 $item->{$code} = $field->subfield( $subfieldstosearch{$code} );
1824 unless ( $item->{itemnumber} ) {
1825 warn "MARC item without itemnumber retrieved for biblio ($oldbiblio->{biblionumber})";
1829 $item->{description} = $itemtypes{ $item->{itype} }{translated_description} if $item->{itype};
1833 # hidden based on OpacHiddenItems syspref or because lost
1834 my $hi = Koha::Items->search( { itemnumber => $item->{itemnumber} } )
1835 ->filter_by_visible_in_opac({ patron => $search_context->{patron} });
1836 unless ( $hi->count ) {
1837 push @hiddenitems, $item->{itemnumber};
1838 $hideatopac_count++;
1843 my $hbranch = C4::Context->preference('StaffSearchResultsDisplayBranch');
1844 my $otherbranch = $hbranch eq 'homebranch' ? 'holdingbranch' : 'homebranch';
1846 # set item's branch name, use HomeOrHoldingBranch syspref first, fall back to the other one
1847 if ($item->{$hbranch}) {
1848 $item->{'branchname'} = $branches{$item->{$hbranch}};
1850 elsif ($item->{$otherbranch}) { # Last resort
1851 $item->{'branchname'} = $branches{$item->{$otherbranch}};
1855 ( $item->{$hbranch} ? $item->{$hbranch} . '--' : q{} )
1856 . ( $item->{location} ? $item->{location} : q{} )
1857 . ( $item->{itype} ? $item->{itype} : q{} )
1858 . ( $item->{ccode} ? $item->{ccode} : q{} )
1859 . ( $item->{itemcallnumber} ? $item->{itemcallnumber} : q{} );
1860 # For each grouping of items (onloan, available, unavailable), we build a key to store relevant info about that item
1861 if ( $item->{onloan}
1863 and !( $patron_category_hide_lost_items and $item->{itemlost} ) )
1866 my $key = $prefix . $item->{onloan} . $item->{barcode};
1867 $onloan_items->{$key}->{due_date} = $item->{onloan};
1868 $onloan_items->{$key}->{count}++ if $item->{$hbranch};
1869 $onloan_items->{$key}->{branchname} = $item->{branchname};
1870 $onloan_items->{$key}->{location} = $shelflocations->{ $item->{location} } if $item->{location};
1871 $onloan_items->{$key}->{itemcallnumber} = $item->{itemcallnumber};
1872 $onloan_items->{$key}->{description} = $item->{description};
1873 $onloan_items->{$key}->{imageurl} =
1874 getitemtypeimagelocation( $search_context->{'interface'}, $itemtypes{ $item->{itype} }->{imageurl} );
1875 $onloan_items->{$key}->{collectioncode} = GetAuthorisedValueDesc('','',$item->{ccode},'','','CCODE');
1877 # if something's checked out and lost, mark it as 'long overdue'
1878 if ( $item->{itemlost} ) {
1879 $onloan_items->{$key}->{longoverdue}++;
1880 $longoverdue_count++;
1884 # items not on loan, but still unavailable ( lost, withdrawn, damaged )
1887 my $itemtype = C4::Context->preference("item-level_itypes")? $item->{itype}: $oldbiblio->{itemtype};
1888 $item->{notforloan} = 1 if !$item->{notforloan} &&
1889 $itemtype && $itemtypes{ $itemtype }->{notforloan};
1892 if ( $item->{notforloan} < 0 ) {
1894 } elsif ( $item->{notforloan} > 0 ) {
1895 $notforloan_count++;
1898 # is item in transit?
1899 my $transfertwhen = '';
1900 my ($transfertfrom, $transfertto);
1902 # is item on the reserve shelf?
1903 my $reservestatus = '';
1905 # is item a waiting recall?
1906 my $recallstatus = '';
1908 unless ($item->{withdrawn}
1909 || $item->{itemlost}
1911 || $item->{notforloan}
1912 || ( C4::Context->preference('MaxSearchResultsItemsPerRecordStatusCheck')
1913 && $items_count > C4::Context->preference('MaxSearchResultsItemsPerRecordStatusCheck') ) ) {
1915 # A couple heuristics to limit how many times
1916 # we query the database for item transfer information, sacrificing
1917 # accuracy in some cases for speed;
1919 # 1. don't query if item has one of the other statuses
1920 # 2. don't check transit status if the bib has
1921 # more than 20 items
1923 # FIXME: to avoid having the query the database like this, and to make
1924 # the in transit status count as unavailable for search limiting,
1925 # should map transit status to record indexed in Zebra.
1927 my $item_object = Koha::Items->find($item->{itemnumber});
1928 my $transfer = defined($item_object) ? $item_object->get_transfer : undef;
1929 ( $transfertwhen, $transfertfrom, $transfertto ) =
1932 $transfer->datesent, $transfer->frombranch,
1936 $reservestatus = C4::Reserves::GetReserveStatus( $item->{itemnumber} );
1937 if ( C4::Context->preference('UseRecalls') ) {
1938 if ( Koha::Recalls->search({ item_id => $item->{itemnumber}, status => 'waiting' })->count ) {
1939 $recallstatus = 'Waiting';
1944 # item is withdrawn, lost, damaged, not for loan, reserved or in transit
1945 if ( $item->{withdrawn}
1946 || $item->{itemlost}
1948 || $item->{notforloan}
1949 || $reservestatus eq 'Waiting'
1950 || $recallstatus eq 'Waiting'
1951 || ($transfertwhen && $transfertwhen ne ''))
1953 $withdrawn_count++ if $item->{withdrawn};
1954 $itemlost_count++ if $item->{itemlost};
1955 $itemdamaged_count++ if $item->{damaged};
1956 $item_in_transit_count++ if $transfertwhen && $transfertwhen ne '';
1957 $item_onhold_count++ if $reservestatus eq 'Waiting';
1958 $item_recalled_count++ if $recallstatus eq 'Waiting';
1959 $item->{status} = ($item->{withdrawn}//q{}) . "-" . ($item->{itemlost}//q{}) . "-" . ($item->{damaged}//q{}) . "-" . ($item->{notforloan}//q{});
1963 my $key = $prefix . $item->{status};
1964 foreach (qw(withdrawn itemlost damaged branchname itemcallnumber)) {
1965 $other_items->{$key}->{$_} = $item->{$_};
1967 $other_items->{$key}->{intransit} = ( $transfertwhen ne '' ) ? 1 : 0;
1968 $other_items->{$key}->{recalled} = ($recallstatus) ? 1 : 0;
1969 $other_items->{$key}->{onhold} = ($reservestatus) ? 1 : 0;
1970 $other_items->{$key}->{notforloan} = GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value) if $notforloan_authorised_value and $item->{notforloan};
1971 $other_items->{$key}->{count}++ if $item->{$hbranch};
1972 $other_items->{$key}->{location} = $shelflocations->{ $item->{location} } if $item->{location};
1973 $other_items->{$key}->{description} = $item->{description};
1974 $other_items->{$key}->{imageurl} = getitemtypeimagelocation( $search_context->{'interface'}, $itemtypes{ $item->{itype}//q{} }->{imageurl} );
1975 $other_items->{$key}->{collectioncode} = GetAuthorisedValueDesc('','',$item->{ccode},'','','CCODE');
1980 $available_items->{$prefix}->{count}++ if $item->{$hbranch};
1981 foreach (qw(branchname itemcallnumber description)) {
1982 $available_items->{$prefix}->{$_} = $item->{$_};
1984 $available_items->{$prefix}->{location} = $shelflocations->{ $item->{location} } if $item->{location};
1985 $available_items->{$prefix}->{imageurl} = getitemtypeimagelocation( $search_context->{'interface'}, $itemtypes{ $item->{itype}//q{} }->{imageurl} );
1986 $available_items->{$prefix}->{collectioncode} = GetAuthorisedValueDesc('','',$item->{ccode},'','','CCODE');
1989 } # notforloan, item level and biblioitem level
1991 # if all items are hidden, do not show the record
1992 if ( C4::Context->preference('OpacHiddenItemsHidesRecord') && $items_count > 0 && $hideatopac_count == $items_count) {
1996 my ( $availableitemscount, $onloanitemscount, $otheritemscount );
1997 for my $key ( sort keys %$onloan_items ) {
1998 (++$onloanitemscount > $maxitems) and last;
1999 push @onloan_items_loop, $onloan_items->{$key};
2001 for my $key ( sort keys %$other_items ) {
2002 (++$otheritemscount > $maxitems) and last;
2003 push @other_items_loop, $other_items->{$key};
2005 for my $key ( sort keys %$available_items ) {
2006 (++$availableitemscount > $maxitems) and last;
2007 push @available_items_loop, $available_items->{$key}
2010 # XSLT processing of some stuff
2011 # we fetched the sysprefs already before the loop through all retrieved record!
2013 $record_processor->options({
2014 frameworkcode => $fw,
2015 interface => $search_context->{'interface'}
2018 $record_processor->process($marcrecord);
2020 $oldbiblio->{XSLTResultsRecord} = XSLTParse4Display(
2022 biblionumber => $oldbiblio->{biblionumber},
2023 record => $marcrecord,
2026 ? 'OPACXSLTResultsDisplay'
2027 : 'XSLTResultsDisplay'
2030 hidden_items => \@hiddenitems,
2031 xslt_variables => $xslt_variables,
2036 my $biblio_object = Koha::Biblios->find( $oldbiblio->{biblionumber} );
2037 $oldbiblio->{biblio_object} = $biblio_object;
2038 $oldbiblio->{coins} = eval { $biblio_object->get_coins }
2040 && C4::Context->preference('COinSinOPACResults')
2043 my $can_place_holds = 1;
2044 # if biblio level itypes are used and itemtype is notforloan, it can't be reserved either
2045 if (!C4::Context->preference("item-level_itypes")) {
2046 if ($itemtype && $itemtype->{notforloan}) {
2047 $can_place_holds = 0;
2050 $can_place_holds = $biblio_object->items->filter_by_for_hold()->count if $biblio_object;
2052 $oldbiblio->{norequests} = 1 unless $can_place_holds;
2053 $oldbiblio->{items_count} = $items_count;
2054 $oldbiblio->{available_items_loop} = \@available_items_loop;
2055 $oldbiblio->{onloan_items_loop} = \@onloan_items_loop;
2056 $oldbiblio->{other_items_loop} = \@other_items_loop;
2057 $oldbiblio->{availablecount} = $available_count;
2058 $oldbiblio->{availableplural} = 1 if $available_count > 1;
2059 $oldbiblio->{onloancount} = $onloan_count;
2060 $oldbiblio->{onloanplural} = 1 if $onloan_count > 1;
2061 $oldbiblio->{othercount} = $other_count;
2062 $oldbiblio->{otherplural} = 1 if $other_count > 1;
2063 $oldbiblio->{withdrawncount} = $withdrawn_count;
2064 $oldbiblio->{itemlostcount} = $itemlost_count;
2065 $oldbiblio->{damagedcount} = $itemdamaged_count;
2066 $oldbiblio->{intransitcount} = $item_in_transit_count;
2067 $oldbiblio->{onholdcount} = $item_onhold_count;
2068 $oldbiblio->{recalledcount} = $item_recalled_count;
2069 $oldbiblio->{orderedcount} = $ordered_count;
2070 $oldbiblio->{notforloancount} = $notforloan_count;
2072 if (C4::Context->preference("AlternateHoldingsField") && $items_count == 0) {
2073 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
2074 my $subfields = substr $fieldspec, 3;
2075 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
2076 my @alternateholdingsinfo = ();
2077 my @holdingsfields = $marcrecord->field(substr $fieldspec, 0, 3);
2079 for my $field (@holdingsfields) {
2080 my %holding = ( holding => '' );
2081 my $havesubfield = 0;
2082 for my $subfield ($field->subfields()) {
2083 if ((index $subfields, $$subfield[0]) >= 0) {
2084 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
2085 $holding{'holding'} .= $$subfield[1];
2089 if ($havesubfield) {
2090 push(@alternateholdingsinfo, \%holding);
2094 $oldbiblio->{'ALTERNATEHOLDINGS'} = \@alternateholdingsinfo;
2097 push( @newresults, $oldbiblio );
2103 =head2 enabled_staff_search_views
2105 %hash = enabled_staff_search_views()
2107 This function returns a hash that contains three flags obtained from the system
2108 preferences, used to determine whether a particular staff search results view
2113 =item C<Output arg:>
2115 * $hash{can_view_MARC} is true only if the MARC view is enabled
2116 * $hash{can_view_ISBD} is true only if the ISBD view is enabled
2117 * $hash{can_view_labeledMARC} is true only if the Labeled MARC view is enabled
2119 =item C<usage in the script:>
2123 $template->param ( C4::Search::enabled_staff_search_views );
2127 sub enabled_staff_search_views
2130 can_view_MARC => C4::Context->preference('viewMARC'), # 1 if the staff search allows the MARC view
2131 can_view_ISBD => C4::Context->preference('viewISBD'), # 1 if the staff search allows the ISBD view
2132 can_view_labeledMARC => C4::Context->preference('viewLabeledMARC'), # 1 if the staff search allows the Labeled MARC view
2136 =head2 z3950_search_args
2138 $arrayref = z3950_search_args($matchpoints)
2140 This function returns an array reference that contains the search parameters to be
2141 passed to the Z39.50 search script (z3950_search.pl). The array elements
2142 are hash refs whose keys are name and value, and whose values are the
2143 name of a search parameter, the value of that search parameter and the URL encoded
2144 value of that parameter.
2146 The search parameter names are lccn, isbn, issn, title, author, dewey and subject.
2148 The search parameter values are obtained from the bibliographic record whose
2149 data is in a hash reference in $matchpoints, as returned by Biblio::GetBiblioData().
2151 If $matchpoints is a scalar, it is assumed to be an unnamed query descriptor, e.g.
2152 a general purpose search argument. In this case, the returned array contains only
2153 entry: the key is 'title' and the value is derived from $matchpoints.
2155 If a search parameter value is undefined or empty, it is not included in the returned
2158 The returned array reference may be passed directly to the template parameters.
2162 =item C<Output arg:>
2164 * $array containing hash refs as described above
2166 =item C<usage in the script:>
2170 $data = Biblio::GetBiblioData($bibno);
2171 $template->param ( MYLOOP => C4::Search::z3950_search_args($data) )
2175 $template->param ( MYLOOP => C4::Search::z3950_search_args($searchscalar) )
2179 sub z3950_search_args {
2182 my $isbn_string = ref( $bibrec ) ? $bibrec->{title} : $bibrec;
2183 my $isbn = Business::ISBN->new( $isbn_string );
2185 if (defined $isbn && $isbn->is_valid)
2187 if ( ref($bibrec) ) {
2188 $bibrec->{isbn} = $isbn_string;
2189 $bibrec->{title} = undef;
2191 $bibrec = { isbn => $isbn_string };
2195 $bibrec = { title => $bibrec } if !ref $bibrec;
2198 for my $field (qw/ lccn isbn issn title author dewey subject /)
2200 push @$array, { name => $field, value => $bibrec->{$field} }
2201 if defined $bibrec->{$field};
2206 =head2 GetDistinctValues($field);
2208 C<$field> is a reference to the fields array
2212 sub GetDistinctValues {
2213 my ($fieldname,$string)=@_;
2214 # returns a reference to a hash of references to branches...
2215 if ($fieldname=~/\./){
2216 my ($table,$column)=split /\./, $fieldname;
2217 my $dbh = C4::Context->dbh;
2218 my $sth = $dbh->prepare("select DISTINCT($column) as value, count(*) as cnt from $table ".($string?" where $column like \"$string%\"":"")."group by value order by $column ");
2220 my $elements=$sth->fetchall_arrayref({});
2225 my @servers=qw<biblioserver authorityserver>;
2226 my (@zconns,@results);
2227 for ( my $i = 0 ; $i < @servers ; $i++ ) {
2228 $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
2231 ZOOM::Query::CCL2RPN->new( qq"$fieldname $string", $zconns[$i])
2234 # The big moment: asynchronously retrieve results from all servers
2240 my ( $i, $size ) = @_;
2241 for ( my $j = 0 ; $j < $size ; $j++ ) {
2243 @hashscan{qw(value cnt)} =
2244 $results[ $i - 1 ]->display_term($j);
2245 push @elements, \%hashscan;
2253 =head2 _ZOOM_event_loop
2255 _ZOOM_event_loop(\@zconns, \@results, sub {
2256 my ( $i, $size ) = @_;
2260 Processes a ZOOM event loop and passes control to a closure for
2261 processing the results, and destroying the resultsets.
2265 sub _ZOOM_event_loop {
2266 my ($zconns, $results, $callback) = @_;
2267 while ( ( my $i = ZOOM::event( $zconns ) ) != 0 ) {
2268 my $ev = $zconns->[ $i - 1 ]->last_event();
2269 if ( $ev == ZOOM::Event::ZEND ) {
2270 next unless $results->[ $i - 1 ];
2271 my $size = $results->[ $i - 1 ]->size();
2273 $callback->($i, $size);
2278 foreach my $result (@$results) {
2283 =head2 new_record_from_zebra
2285 Given raw data from a searchengine result set, return a MARC::Record object
2287 This helper function is needed to take into account all the involved
2288 system preferences and configuration variables to properly create the
2289 MARC::Record object.
2291 If we are using GRS-1, then the raw data we get from Zebra should be USMARC
2292 data. If we are using DOM, then it has to be MARCXML.
2294 If we are using elasticsearch, it'll already be a MARC::Record and this
2295 function needs a new name.
2299 sub new_record_from_zebra {
2302 my $raw_data = shift;
2303 # Set the default indexing modes
2304 my $search_engine = C4::Context->preference("SearchEngine");
2305 if ($search_engine eq 'Elasticsearch') {
2306 return ref $raw_data eq 'MARC::Record' ? $raw_data : MARC::Record->new_from_xml( $raw_data, 'UTF-8' );
2308 my $index_mode = ( $server eq 'biblioserver' )
2309 ? C4::Context->config('zebra_bib_index_mode') // 'dom'
2310 : C4::Context->config('zebra_auth_index_mode') // 'dom';
2312 my $marc_record = eval {
2313 if ( $index_mode eq 'dom' ) {
2314 MARC::Record->new_from_xml( $raw_data, 'UTF-8' );
2316 MARC::Record->new_from_usmarc( $raw_data );
2323 return $marc_record;
2328 END { } # module clean-up code here (global destructor)
2335 Koha Development Team <http://koha-community.org/>