Bug 18984: Remove NORMARC support
[koha.git] / C4 / Search.pm
1 package C4::Search;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use C4::Context;
20 use C4::Biblio qw( TransformMarcToKoha GetMarcFromKohaField GetFrameworkCode GetAuthorisedValueDesc GetBiblioData );
21 use C4::Koha qw( getFacets GetVariationsOfISBN GetNormalizedUPC GetNormalizedEAN GetNormalizedOCLCNumber GetNormalizedISBN getitemtypeimagelocation );
22 use Koha::DateUtils;
23 use Koha::Libraries;
24 use Lingua::Stem;
25 use XML::Simple;
26 use C4::XSLT qw( XSLTParse4Display );
27 use C4::Reserves qw( GetReserveStatus );
28 use C4::Charset qw( SetUTF8Flag );
29 use Koha::AuthorisedValues;
30 use Koha::ItemTypes;
31 use Koha::Libraries;
32 use Koha::Logger;
33 use Koha::Patrons;
34 use Koha::RecordProcessor;
35 use URI::Escape;
36 use Business::ISBN;
37 use MARC::Record;
38 use MARC::Field;
39
40 our (@ISA, @EXPORT_OK);
41 BEGIN {
42     require Exporter;
43     @ISA    = qw(Exporter);
44     @EXPORT_OK = qw(
45       FindDuplicate
46       SimpleSearch
47       searchResults
48       getRecords
49       buildQuery
50       GetDistinctValues
51       enabled_staff_search_views
52       new_record_from_zebra
53       z3950_search_args
54       getIndexes
55     );
56 }
57
58 =head1 NAME
59
60 C4::Search - Functions for searching the Koha catalog.
61
62 =head1 SYNOPSIS
63
64 See opac/opac-search.pl or catalogue/search.pl for example of usage
65
66 =head1 DESCRIPTION
67
68 This module provides searching functions for Koha's bibliographic databases
69
70 =head1 FUNCTIONS
71
72 =cut
73
74 # make all your functions, whether exported or not;
75
76 =head2 FindDuplicate
77
78 ($biblionumber,$biblionumber,$title) = FindDuplicate($record);
79
80 This function attempts to find duplicate records using a hard-coded, fairly simplistic algorithm
81
82 =cut
83
84 sub FindDuplicate {
85     my ($record) = @_;
86     my $dbh = C4::Context->dbh;
87     my $result = TransformMarcToKoha( $record, '' );
88     my $sth;
89     my $query;
90
91     # search duplicate on ISBN, easy and fast..
92     # ... normalize first
93     if ( $result->{isbn} ) {
94         $result->{isbn} =~ s/\(.*$//;
95         $result->{isbn} =~ s/\s+$//;
96         $query = "isbn:$result->{isbn}";
97     }
98     else {
99
100         my $titleindex = 'ti,ext';
101         my $authorindex = 'au,ext';
102         my $op = 'and';
103
104         $result->{title} =~ s /\\//g;
105         $result->{title} =~ s /\"//g;
106         $result->{title} =~ s /\(//g;
107         $result->{title} =~ s /\)//g;
108
109         $query = "$titleindex:\"$result->{title}\"";
110         if   ( $result->{author} ) {
111             $result->{author} =~ s /\\//g;
112             $result->{author} =~ s /\"//g;
113             $result->{author} =~ s /\(//g;
114             $result->{author} =~ s /\)//g;
115
116             $query .= " $op $authorindex:\"$result->{author}\"";
117         }
118     }
119
120     my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
121     my ( $error, $searchresults, undef ) = $searcher->simple_search_compat($query,0,50);
122     my @results;
123     if (!defined $error) {
124         foreach my $possible_duplicate_record (@{$searchresults}) {
125             my $marcrecord = new_record_from_zebra(
126                 'biblioserver',
127                 $possible_duplicate_record
128             );
129
130             my $result = TransformMarcToKoha( $marcrecord, '' );
131
132             # FIXME :: why 2 $biblionumber ?
133             if ($result) {
134                 push @results, $result->{'biblionumber'};
135                 push @results, $result->{'title'};
136             }
137         }
138     }
139     return @results;
140 }
141
142 =head2 SimpleSearch
143
144 ( $error, $results, $total_hits ) = SimpleSearch( $query, $offset, $max_results, [@servers], [%options] );
145
146 This function provides a simple search API on the bibliographic catalog
147
148 =over 2
149
150 =item C<input arg:>
151
152     * $query can be a simple keyword or a complete CCL query
153     * @servers is optional. Defaults to biblioserver as found in koha-conf.xml
154     * $offset - If present, represents the number of records at the beginning to omit. Defaults to 0
155     * $max_results - if present, determines the maximum number of records to fetch. undef is All. defaults to undef.
156     * %options is optional. (e.g. "skip_normalize" allows you to skip changing : to = )
157
158
159 =item C<Return:>
160
161     Returns an array consisting of three elements
162     * $error is undefined unless an error is detected
163     * $results is a reference to an array of records.
164     * $total_hits is the number of hits that would have been returned with no limit
165
166     If an error is returned the two other return elements are undefined. If error itself is undefined
167     the other two elements are always defined
168
169 =item C<usage in the script:>
170
171 =back
172
173 my ( $error, $marcresults, $total_hits ) = SimpleSearch($query);
174
175 if (defined $error) {
176     $template->param(query_error => $error);
177     warn "error: ".$error;
178     output_html_with_http_headers $input, $cookie, $template->output;
179     exit;
180 }
181
182 my $hits = @{$marcresults};
183 my @results;
184
185 for my $r ( @{$marcresults} ) {
186     my $marcrecord = MARC::File::USMARC::decode($r);
187     my $biblio = TransformMarcToKoha($marcrecord,q{});
188
189     #build the iarray of hashs for the template.
190     push @results, {
191         title           => $biblio->{'title'},
192         subtitle        => $biblio->{'subtitle'},
193         biblionumber    => $biblio->{'biblionumber'},
194         author          => $biblio->{'author'},
195         publishercode   => $biblio->{'publishercode'},
196         publicationyear => $biblio->{'publicationyear'},
197         };
198
199 }
200
201 $template->param(result=>\@results);
202
203 =cut
204
205 sub SimpleSearch {
206     my ( $query, $offset, $max_results, $servers, %options )  = @_;
207
208     return ( 'No query entered', undef, undef ) unless $query;
209     # FIXME hardcoded value. See catalog/search.pl & opac-search.pl too.
210     my @servers = defined ( $servers ) ? @$servers : ( 'biblioserver' );
211     my @zoom_queries;
212     my @tmpresults;
213     my @zconns;
214     my $results = [];
215     my $total_hits = 0;
216
217     # Initialize & Search Zebra
218     for ( my $i = 0 ; $i < @servers ; $i++ ) {
219         eval {
220             $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
221             $query =~ s/:/=/g unless $options{skip_normalize};
222             $zoom_queries[$i] = ZOOM::Query::CCL2RPN->new( $query, $zconns[$i]);
223             $tmpresults[$i] = $zconns[$i]->search( $zoom_queries[$i] );
224
225             # error handling
226             my $error =
227                 $zconns[$i]->errmsg() . " ("
228               . $zconns[$i]->errcode() . ") "
229               . $zconns[$i]->addinfo() . " "
230               . $zconns[$i]->diagset();
231
232             return ( $error, undef, undef ) if $zconns[$i]->errcode();
233         };
234         if ($@) {
235
236             # caught a ZOOM::Exception
237             my $error =
238                 $@->message() . " ("
239               . $@->code() . ") "
240               . $@->addinfo() . " "
241               . $@->diagset();
242             warn $error." for query: $query";
243             return ( $error, undef, undef );
244         }
245     }
246
247     _ZOOM_event_loop(
248         \@zconns,
249         \@tmpresults,
250         sub {
251             my ($i, $size) = @_;
252             my $first_record = defined($offset) ? $offset + 1 : 1;
253             my $hits = $tmpresults[ $i - 1 ]->size();
254             $total_hits += $hits;
255             my $last_record = $hits;
256             if ( defined $max_results && $offset + $max_results < $hits ) {
257                 $last_record = $offset + $max_results;
258             }
259
260             for my $j ( $first_record .. $last_record ) {
261                 my $record = eval {
262                   $tmpresults[ $i - 1 ]->record( $j - 1 )->raw()
263                   ;    # 0 indexed
264                 };
265                 push @{$results}, $record if defined $record;
266             }
267         }
268     );
269
270     foreach my $zoom_query (@zoom_queries) {
271         $zoom_query->destroy();
272     }
273
274     return ( undef, $results, $total_hits );
275 }
276
277 =head2 getRecords
278
279 ( undef, $results_hashref, \@facets_loop ) = getRecords (
280
281         $koha_query,       $simple_query, $sort_by_ref,    $servers_ref,
282         $results_per_page, $offset,       $branches,       $itemtypes,
283         $query_type,       $scan,         $opac
284     );
285
286 The all singing, all dancing, multi-server, asynchronous, scanning,
287 searching, record nabbing, facet-building
288
289 See verbose embedded documentation.
290
291 =cut
292
293 sub getRecords {
294     my (
295         $koha_query,       $simple_query, $sort_by_ref,    $servers_ref,
296         $results_per_page, $offset,       $branches,         $itemtypes,
297         $query_type,       $scan,         $opac
298     ) = @_;
299
300     my @servers = @$servers_ref;
301     my @sort_by = @$sort_by_ref;
302     $offset = 0 if $offset < 0;
303
304     # Initialize variables for the ZOOM connection and results object
305     my @zconns;
306     my @results;
307     my $results_hashref = ();
308
309     # TODO simplify this structure ( { branchcode => $branchname } is enought) and remove this parameter
310     $branches ||= { map { $_->branchcode => { branchname => $_->branchname } } Koha::Libraries->search };
311
312     # Initialize variables for the faceted results objects
313     my $facets_counter = {};
314     my $facets_info    = {};
315     my $facets         = getFacets();
316
317     my @facets_loop;    # stores the ref to array of hashes for template facets loop
318
319     ### LOOP THROUGH THE SERVERS
320     for ( my $i = 0 ; $i < @servers ; $i++ ) {
321         $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
322
323 # perform the search, create the results objects
324 # if this is a local search, use the $koha-query, if it's a federated one, use the federated-query
325         my $query_to_use = ($servers[$i] =~ /biblioserver/) ? $koha_query : $simple_query;
326
327         Koha::Logger->get->debug($simple_query) if $scan;
328
329         # Check if we've got a query_type defined, if so, use it
330         eval {
331             if ($query_type) {
332                 if ($query_type =~ /^ccl/) {
333                     $query_to_use =~ s/\:/\=/g;    # change : to = last minute (FIXME)
334                     $results[$i] = $zconns[$i]->search(ZOOM::Query::CCL2RPN->new($query_to_use, $zconns[$i]));
335                 } elsif ($query_type =~ /^cql/) {
336                     $results[$i] = $zconns[$i]->search(ZOOM::Query::CQL->new($query_to_use, $zconns[$i]));
337                 } elsif ($query_type =~ /^pqf/) {
338                     $results[$i] = $zconns[$i]->search(ZOOM::Query::PQF->new($query_to_use, $zconns[$i]));
339                 } else {
340                     warn "Unknown query_type '$query_type'.  Results undetermined.";
341                 }
342             } elsif ($scan) {
343                     $results[$i] = $zconns[$i]->scan(  ZOOM::Query::CCL2RPN->new($query_to_use, $zconns[$i]));
344             } else {
345                     $results[$i] = $zconns[$i]->search(ZOOM::Query::CCL2RPN->new($query_to_use, $zconns[$i]));
346             }
347         };
348         if ($@) {
349             warn "WARNING: query problem with $query_to_use " . $@;
350         }
351
352         # Concatenate the sort_by limits and pass them to the results object
353         # Note: sort will override rank
354         my $sort_by;
355         foreach my $sort (@sort_by) {
356             if ( $sort eq "author_az" || $sort eq "author_asc" ) {
357                 $sort_by .= "1=1003 <i ";
358             }
359             elsif ( $sort eq "author_za" || $sort eq "author_dsc" ) {
360                 $sort_by .= "1=1003 >i ";
361             }
362             elsif ( $sort eq "popularity_asc" ) {
363                 $sort_by .= "1=9003 <i ";
364             }
365             elsif ( $sort eq "popularity_dsc" ) {
366                 $sort_by .= "1=9003 >i ";
367             }
368             elsif ( $sort eq "call_number_asc" ) {
369                 $sort_by .= "1=8007  <i ";
370             }
371             elsif ( $sort eq "call_number_dsc" ) {
372                 $sort_by .= "1=8007 >i ";
373             }
374             elsif ( $sort eq "pubdate_asc" ) {
375                 $sort_by .= "1=31 <i ";
376             }
377             elsif ( $sort eq "pubdate_dsc" ) {
378                 $sort_by .= "1=31 >i ";
379             }
380             elsif ( $sort eq "acqdate_asc" ) {
381                 $sort_by .= "1=32 <i ";
382             }
383             elsif ( $sort eq "acqdate_dsc" ) {
384                 $sort_by .= "1=32 >i ";
385             }
386             elsif ( $sort eq "title_az" || $sort eq "title_asc" ) {
387                 $sort_by .= "1=4 <i ";
388             }
389             elsif ( $sort eq "title_za" || $sort eq "title_dsc" ) {
390                 $sort_by .= "1=4 >i ";
391             }
392             else {
393                 warn "Ignoring unrecognized sort '$sort' requested" if $sort_by;
394             }
395         }
396         if ( $sort_by && !$scan && $results[$i] ) {
397             if ( $results[$i]->sort( "yaz", $sort_by ) < 0 ) {
398                 warn "WARNING sort $sort_by failed";
399             }
400         }
401     }    # finished looping through servers
402
403     # The big moment: asynchronously retrieve results from all servers
404         _ZOOM_event_loop(
405             \@zconns,
406             \@results,
407             sub {
408                 my ( $i, $size ) = @_;
409                 my $results_hash;
410
411                 # loop through the results
412                 $results_hash->{'hits'} = $size;
413                 my $times;
414                 if ( $offset + $results_per_page <= $size ) {
415                     $times = $offset + $results_per_page;
416                 }
417                 else {
418                     $times = $size;
419                 }
420
421                 for ( my $j = $offset ; $j < $times ; $j++ ) {
422                     my $record;
423
424                     ## Check if it's an index scan
425                     if ($scan) {
426                         my ( $term, $occ ) = $results[ $i - 1 ]->display_term($j);
427
428                  # here we create a minimal MARC record and hand it off to the
429                  # template just like a normal result ... perhaps not ideal, but
430                  # it works for now
431                         my $tmprecord = MARC::Record->new();
432                         $tmprecord->encoding('UTF-8');
433                         my $tmptitle;
434                         my $tmpauthor;
435
436                 # the minimal record in author/title (depending on MARC flavour)
437                         if ( C4::Context->preference("marcflavour") eq
438                             "UNIMARC" )
439                         {
440                             $tmptitle = MARC::Field->new(
441                                 '200', ' ', ' ',
442                                 a => $term,
443                                 f => $occ
444                             );
445                             $tmprecord->append_fields($tmptitle);
446                         }
447                         else {
448                             $tmptitle =
449                               MARC::Field->new( '245', ' ', ' ', a => $term, );
450                             $tmpauthor =
451                               MARC::Field->new( '100', ' ', ' ', a => $occ, );
452                             $tmprecord->append_fields($tmptitle);
453                             $tmprecord->append_fields($tmpauthor);
454                         }
455                         $results_hash->{'RECORDS'}[$j] =
456                           $tmprecord->as_usmarc();
457                     }
458
459                     # not an index scan
460                     else {
461                         $record = $results[ $i - 1 ]->record($j)->raw();
462                         # warn "RECORD $j:".$record;
463                         $results_hash->{'RECORDS'}[$j] = $record;
464                     }
465
466                 }
467                 $results_hashref->{ $servers[ $i - 1 ] } = $results_hash;
468
469                 # Fill the facets while we're looping, but only for the
470                 # biblioserver and not for a scan
471                 if ( !$scan && $servers[ $i - 1 ] =~ /biblioserver/ ) {
472                     $facets_counter = GetFacets( $results[ $i - 1 ] );
473                     $facets_info    = _get_facets_info( $facets );
474                 }
475
476                 # BUILD FACETS
477                 if ( $servers[ $i - 1 ] =~ /biblioserver/ ) {
478                     for my $link_value (
479                         sort { $a cmp $b } keys %$facets_counter
480                       )
481                     {
482                         my @this_facets_array;
483                         for my $one_facet (
484                             sort {
485                                 $facets_counter->{$link_value}
486                                   ->{$b} <=> $facets_counter->{$link_value}
487                                   ->{$a}
488                             } keys %{ $facets_counter->{$link_value} }
489                           )
490                         {
491 # Sanitize the link value : parenthesis, question and exclamation mark will cause errors with CCL
492                             my $facet_link_value = $one_facet;
493                             $facet_link_value =~ s/[()!?¡¿؟]/ /g;
494
495                             # fix the length that will display in the label,
496                             my $facet_label_value = $one_facet;
497                             my $facet_max_length  = C4::Context->preference(
498                                 'FacetLabelTruncationLength')
499                               || 20;
500                             $facet_label_value =
501                               substr( $one_facet, 0, $facet_max_length )
502                               . "..."
503                               if length($facet_label_value) >
504                                   $facet_max_length;
505
506                         # if it's a branch, label by the name, not the code,
507                             if ( $link_value =~ /branch/ ) {
508                                 if (   defined $branches
509                                     && ref($branches) eq "HASH"
510                                     && defined $branches->{$one_facet}
511                                     && ref( $branches->{$one_facet} ) eq
512                                     "HASH" )
513                                 {
514                                     $facet_label_value =
515                                       $branches->{$one_facet}
516                                       ->{'branchname'};
517                                 }
518                                 else {
519                                     $facet_label_value = "*";
520                                 }
521                             }
522
523                       # if it's a itemtype, label by the name, not the code,
524                             if ( $link_value =~ /itype/ ) {
525                                 if (   defined $itemtypes
526                                     && ref($itemtypes) eq "HASH"
527                                     && defined $itemtypes->{$one_facet}
528                                     && ref( $itemtypes->{$one_facet} ) eq
529                                     "HASH" )
530                                 {
531                                     $facet_label_value =
532                                       $itemtypes->{$one_facet}
533                                       ->{translated_description};
534                                 }
535                             }
536
537            # also, if it's a location code, use the name instead of the code
538                             if ( $link_value =~ /location/ ) {
539                                 # TODO Retrieve all authorised values at once, instead of 1 query per entry
540                                 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $one_facet });
541                                 $facet_label_value = $av->count ? $av->next->opac_description : '';
542                             }
543
544                             # also, if it's a collection code, use the name instead of the code
545                             if ( $link_value =~ /ccode/ ) {
546                                 # TODO Retrieve all authorised values at once, instead of 1 query per entry
547                                 my $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $one_facet });
548                                 $facet_label_value = $av->count ? $av->next->opac_description : '';
549                             }
550
551             # but we're down with the whole label being in the link's title.
552                             push @this_facets_array,
553                               {
554                                 facet_count =>
555                                   $facets_counter->{$link_value}
556                                   ->{$one_facet},
557                                 facet_label_value => $facet_label_value,
558                                 facet_title_value => $one_facet,
559                                 facet_link_value  => $facet_link_value,
560                                 type_link_value   => $link_value,
561                               }
562                               if ($facet_label_value);
563                         }
564
565                         push @facets_loop,
566                           {
567                             type_link_value => $link_value,
568                             type_id         => $link_value . "_id",
569                             "type_label_"
570                               . $facets_info->{$link_value}->{'label_value'} =>
571                               1,
572                             facets     => \@this_facets_array,
573                           }
574                           unless (
575                             (
576                                 $facets_info->{$link_value}->{'label_value'} =~
577                                 /Libraries/
578                             )
579                             and ( Koha::Libraries->search->count == 1 )
580                           );
581                     }
582                 }
583             }
584         );
585
586     # This sorts the facets into alphabetical order
587     if (@facets_loop) {
588         foreach my $f (@facets_loop) {
589             if( C4::Context->preference('FacetOrder') eq 'Alphabetical' ){
590                 $f->{facets} =
591                     [ sort { uc($a->{facet_label_value}) cmp uc($b->{facet_label_value}) } @{ $f->{facets} } ];
592             }
593         }
594     }
595
596     return ( undef, $results_hashref, \@facets_loop );
597 }
598
599 sub GetFacets {
600
601     my $rs = shift;
602     my $facets;
603
604     my $use_zebra_facets = C4::Context->config('use_zebra_facets') // 0;
605
606     if ( $use_zebra_facets ) {
607         $facets = _get_facets_from_zebra( $rs );
608     } else {
609         $facets = _get_facets_from_records( $rs );
610     }
611
612     return $facets;
613 }
614
615 sub _get_facets_from_records {
616
617     my $rs = shift;
618
619     my $facets_maxrecs = C4::Context->preference('maxRecordsForFacets') // 20;
620     my $facets_config  = getFacets();
621     my $facets         = {};
622     my $size           = $rs->size();
623     my $jmax           = $size > $facets_maxrecs
624                             ? $facets_maxrecs
625                             : $size;
626
627     for ( my $j = 0 ; $j < $jmax ; $j++ ) {
628
629         my $marc_record = new_record_from_zebra (
630                 'biblioserver',
631                 $rs->record( $j )->raw()
632         );
633
634         if ( ! defined $marc_record ) {
635             warn "ERROR DECODING RECORD - $@: " .
636                 $rs->record( $j )->raw();
637             next;
638         }
639
640         _get_facets_data_from_record( $marc_record, $facets_config, $facets );
641     }
642
643     return $facets;
644 }
645
646 =head2 _get_facets_data_from_record
647
648     C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
649
650 Internal function that extracts facets information from a MARC::Record object
651 and populates $facets_counter for using in getRecords.
652
653 $facets is expected to be filled with C4::Koha::getFacets output (i.e. the configured
654 facets for Zebra).
655
656 =cut
657
658 sub _get_facets_data_from_record {
659
660     my ( $marc_record, $facets, $facets_counter ) = @_;
661
662     for my $facet (@$facets) {
663
664         my @used_datas = ();
665
666         foreach my $tag ( @{ $facet->{ tags } } ) {
667
668             # tag number is the first three digits
669             my $tag_num          = substr( $tag, 0, 3 );
670             # subfields are the remainder
671             my $subfield_letters = substr( $tag, 3 );
672
673             my @fields = $marc_record->field( $tag_num );
674             foreach my $field (@fields) {
675                 # If $field->indicator(1) eq 'z', it means it is a 'see from'
676                 # field introduced because of IncludeSeeFromInSearches, so skip it
677                 next if $field->indicator(1) eq 'z';
678
679                 my $data = $field->as_string( $subfield_letters, $facet->{ sep } );
680                 $data =~ s/\s*(?<!\p{Uppercase})[.\-,;]*\s*$//;
681
682                 unless ( grep { $_ eq $data } @used_datas ) {
683                     push @used_datas, $data;
684                     $facets_counter->{ $facet->{ idx } }->{ $data }++;
685                 }
686             }
687         }
688     }
689 }
690
691 =head2 _get_facets_from_zebra
692
693     my $facets = _get_facets_from_zebra( $result_set )
694
695 Retrieves facets for a specified result set. It loops through the facets defined
696 in C4::Koha::getFacets and returns a hash with the following structure:
697
698    {  facet_idx => {
699             facet_value => count
700       },
701       ...
702    }
703
704 =cut
705
706 sub _get_facets_from_zebra {
707
708     my $rs = shift;
709
710     # save current elementSetName
711     my $elementSetName = $rs->option( 'elementSetName' );
712
713     my $facets_loop = getFacets();
714     my $facets_data  = {};
715     # loop through defined facets and fill the facets hashref
716     foreach my $facet ( @$facets_loop ) {
717
718         my $idx = $facet->{ idx };
719         my $sep = $facet->{ sep };
720         my $facet_values = _get_facet_from_result_set( $idx, $rs, $sep );
721         if ( $facet_values ) {
722             # we've actually got a result
723             $facets_data->{ $idx } = $facet_values;
724         }
725     }
726     # set elementSetName to its previous value to avoid side effects
727     $rs->option( elementSetName => $elementSetName );
728
729     return $facets_data;
730 }
731
732 =head2 _get_facet_from_result_set
733
734     my $facet_values =
735         C4::Search::_get_facet_from_result_set( $facet_idx, $result_set, $sep )
736
737 Internal function that extracts facet information for a specific index ($facet_idx) and
738 returns a hash containing facet values and count:
739
740     {
741         $facet_value => $count ,
742         ...
743     }
744
745 Warning: this function has the side effect of changing the elementSetName for the result
746 set. It is a helper function for the main loop, which takes care of backing it up for
747 restoring.
748
749 =cut
750
751 sub _get_facet_from_result_set {
752
753     my $facet_idx = shift;
754     my $rs        = shift;
755     my $sep       = shift;
756
757     my $internal_sep  = '<*>';
758     my $facetMaxCount = C4::Context->preference('FacetMaxCount') // 20;
759
760     return if ( ! defined $facet_idx || ! defined $rs );
761     # zebra's facet element, untokenized index
762     my $facet_element = 'zebra::facet::' . $facet_idx . ':0:' . $facetMaxCount;
763     # configure zebra results for retrieving the desired facet
764     $rs->option( elementSetName => $facet_element );
765     # get the facet record from result set
766     my $facet = $rs->record( 0 )->raw;
767     # if the facet has no restuls...
768     return if !defined $facet;
769     # TODO: benchmark DOM vs. SAX performance
770     my $facet_dom = XML::LibXML->load_xml(
771       string => ($facet)
772     );
773     my @terms = $facet_dom->getElementsByTagName('term');
774     return if ! @terms;
775
776     my $facets = {};
777     foreach my $term ( @terms ) {
778         my $facet_value = $term->textContent;
779         $facet_value =~ s/\s*(?<!\p{Uppercase})[.\-,;]*\s*$//;
780         $facet_value =~ s/\Q$internal_sep\E/$sep/ if defined $sep;
781         $facets->{ $facet_value } += $term->getAttribute( 'occur' );
782     }
783
784     return $facets;
785 }
786
787 =head2 _get_facets_info
788
789     my $facets_info = C4::Search::_get_facets_info( $facets )
790
791 Internal function that extracts facets information and properly builds
792 the data structure needed to render facet labels.
793
794 =cut
795
796 sub _get_facets_info {
797
798     my $facets = shift;
799
800     my $facets_info = {};
801
802     for my $facet ( @$facets ) {
803         $facets_info->{ $facet->{ idx } }->{ label_value } = $facet->{ label };
804     }
805
806     return $facets_info;
807 }
808
809 # TRUNCATION
810 sub _detect_truncation {
811     my ( $operand, $index ) = @_;
812     my ( @nontruncated, @righttruncated, @lefttruncated, @rightlefttruncated,
813         @regexpr );
814     $operand =~ s/^ //g;
815     my @wordlist = split( /\s/, $operand );
816     foreach my $word (@wordlist) {
817         if ( $word =~ s/^\*([^\*]+)\*$/$1/ ) {
818             push @rightlefttruncated, $word;
819         }
820         elsif ( $word =~ s/^\*([^\*]+)$/$1/ ) {
821             push @lefttruncated, $word;
822         }
823         elsif ( $word =~ s/^([^\*]+)\*$/$1/ ) {
824             push @righttruncated, $word;
825         }
826         elsif ( index( $word, "*" ) < 0 ) {
827             push @nontruncated, $word;
828         }
829         else {
830             push @regexpr, $word;
831         }
832     }
833     return (
834         \@nontruncated,       \@righttruncated, \@lefttruncated,
835         \@rightlefttruncated, \@regexpr
836     );
837 }
838
839 # STEMMING
840 sub _build_stemmed_operand {
841     my ($operand,$lang) = @_;
842     require Lingua::Stem::Snowball ;
843     my $stemmed_operand=q{};
844
845     # Stemmer needs language
846     return $operand unless $lang;
847
848     # If operand contains a digit, it is almost certainly an identifier, and should
849     # not be stemmed.  This is particularly relevant for ISBNs and ISSNs, which
850     # can contain the letter "X" - for example, _build_stemmend_operand would reduce
851     # "014100018X" to "x ", which for a MARC21 database would bring up irrelevant
852     # results (e.g., "23 x 29 cm." from the 300$c).  Bug 2098.
853     return $operand if $operand =~ /\d/;
854
855 # FIXME: the locale should be set based on the user's language and/or search choice
856     #warn "$lang";
857     # Make sure we only use the first two letters from the language code
858     $lang = lc(substr($lang, 0, 2));
859     # The language codes for the two variants of Norwegian will now be "nb" and "nn",
860     # none of which Lingua::Stem::Snowball can use, so we need to "translate" them
861     if ($lang eq 'nb' || $lang eq 'nn') {
862       $lang = 'no';
863     }
864     my $stemmer = Lingua::Stem::Snowball->new( lang => $lang,
865                                                encoding => "UTF-8" );
866
867     my @words = split( / /, $operand );
868     my @stems = $stemmer->stem(\@words);
869     for my $stem (@stems) {
870         $stemmed_operand .= "$stem";
871         $stemmed_operand .= "?"
872           unless ( $stem =~ /(and$|or$|not$)/ ) || ( length($stem) < 3 );
873         $stemmed_operand .= " ";
874     }
875
876     Koha::Logger->get->debug("STEMMED OPERAND: $stemmed_operand");
877     return $stemmed_operand;
878 }
879
880 # FIELD WEIGHTING
881 sub _build_weighted_query {
882
883 # FIELD WEIGHTING - This is largely experimental stuff. What I'm committing works
884 # pretty well but could work much better if we had a smarter query parser
885     my ( $operand, $stemmed_operand, $index ) = @_;
886     my $stemming      = C4::Context->preference("QueryStemming")     || 0;
887     my $weight_fields = C4::Context->preference("QueryWeightFields") || 0;
888     my $fuzzy_enabled = C4::Context->preference("QueryFuzzy")        || 0;
889     $operand =~ s/"/ /g;    # Bug 7518: searches with quotation marks don't work
890
891     my $weighted_query .= "(rk=(";    # Specifies that we're applying rank
892
893     # Keyword, or, no index specified
894     if ( ( $index eq 'kw' ) || ( !$index ) ) {
895         $weighted_query .=
896           "Title-cover,ext,r1=\"$operand\"";    # exact title-cover
897         $weighted_query .= " or ti,ext,r2=\"$operand\"";    # exact title
898         $weighted_query .= " or Title-cover,phr,r3=\"$operand\"";    # phrase title
899         $weighted_query .= " or ti,wrdl,r4=\"$operand\"";    # words in title
900           #$weighted_query .= " or any,ext,r4=$operand";               # exact any
901           #$weighted_query .=" or kw,wrdl,r5=\"$operand\"";            # word list any
902         $weighted_query .= " or wrdl,fuzzy,r8=\"$operand\""
903           if $fuzzy_enabled;    # add fuzzy, word list
904         $weighted_query .= " or wrdl,right-Truncation,r9=\"$stemmed_operand\""
905           if ( $stemming and $stemmed_operand )
906           ;                     # add stemming, right truncation
907         $weighted_query .= " or wrdl,r9=\"$operand\"";
908
909         # embedded sorting: 0 a-z; 1 z-a
910         # $weighted_query .= ") or (sort1,aut=1";
911     }
912
913     # Barcode searches should skip this process
914     elsif ( $index eq 'bc' ) {
915         $weighted_query .= "bc=\"$operand\"";
916     }
917
918     # Authority-number searches should skip this process
919     elsif ( $index eq 'an' ) {
920         $weighted_query .= "an=\"$operand\"";
921     }
922
923     # If the index is numeric, don't autoquote it.
924     elsif ( $index =~ /,st-numeric$/ ) {
925         $weighted_query .= " $index=$operand";
926     }
927
928     # If the index already has more than one qualifier, wrap the operand
929     # in quotes and pass it back (assumption is that the user knows what they
930     # are doing and won't appreciate us mucking up their query
931     elsif ( $index =~ ',' ) {
932         $weighted_query .= " $index=\"$operand\"";
933     }
934
935     #TODO: build better cases based on specific search indexes
936     else {
937         $weighted_query .= " $index,ext,r1=\"$operand\"";    # exact index
938           #$weighted_query .= " or (title-sort-az=0 or $index,startswithnt,st-word,r3=$operand #)";
939         $weighted_query .= " or $index,phr,r3=\"$operand\"";    # phrase index
940         $weighted_query .= " or $index,wrdl,r6=\"$operand\"";    # word list index
941         $weighted_query .= " or $index,wrdl,fuzzy,r8=\"$operand\""
942           if $fuzzy_enabled;    # add fuzzy, word list
943         $weighted_query .= " or $index,wrdl,rt,r9=\"$stemmed_operand\""
944           if ( $stemming and $stemmed_operand );    # add stemming, right truncation
945     }
946
947     $weighted_query .= "))";                       # close rank specification
948     return $weighted_query;
949 }
950
951 =head2 getIndexes
952
953 Return an array with available indexes.
954
955 =cut
956
957 sub getIndexes{
958     my @indexes = (
959                     # biblio indexes
960                     'ab',
961                     'Abstract',
962                     'acqdate',
963                     'allrecords',
964                     'an',
965                     'Any',
966                     'at',
967                     'arl',
968                     'arp',
969                     'au',
970                     'aub',
971                     'aud',
972                     'audience',
973                     'auo',
974                     'aut',
975                     'Author',
976                     'Author-in-order ',
977                     'Author-personal-bibliography',
978                     'Authority-Number',
979                     'authtype',
980                     'bc',
981                     'Bib-level',
982                     'biblionumber',
983                     'bio',
984                     'biography',
985                     'callnum',
986                     'cfn',
987                     'Chronological-subdivision',
988                     'cn-bib-source',
989                     'cn-bib-sort',
990                     'cn-class',
991                     'cn-item',
992                     'cn-prefix',
993                     'cn-suffix',
994                     'cpn',
995                     'Code-institution',
996                     'Conference-name',
997                     'Conference-name-heading',
998                     'Conference-name-see',
999                     'Conference-name-seealso',
1000                     'Content-type',
1001                     'Control-number',
1002                     'Control-number-identifier',
1003                     'cni',
1004                     'copydate',
1005                     'Corporate-name',
1006                     'Corporate-name-heading',
1007                     'Corporate-name-see',
1008                     'Corporate-name-seealso',
1009                     'Country-publication',
1010                     'ctype',
1011                     'curriculum',
1012                     'date-entered-on-file',
1013                     'Date-of-acquisition',
1014                     'Date-of-publication',
1015                     'Date-time-last-modified',
1016                     'Dewey-classification',
1017                     'Dissertation-information',
1018                     'diss',
1019                     'dtlm',
1020                     'EAN',
1021                     'extent',
1022                     'fic',
1023                     'fiction',
1024                     'Form-subdivision',
1025                     'format',
1026                     'Geographic-subdivision',
1027                     'he',
1028                     'Heading',
1029                     'Heading-use-main-or-added-entry',
1030                     'Heading-use-series-added-entry ',
1031                     'Heading-use-subject-added-entry',
1032                     'Host-item',
1033                     'id-other',
1034                     'ident',
1035                     'Identifier-standard',
1036                     'Illustration-code',
1037                     'Index-term-genre',
1038                     'Index-term-uncontrolled',
1039                     'Interest-age-level',
1040                     'Interest-grade-level',
1041                     'ISBN',
1042                     'isbn',
1043                     'ISSN',
1044                     'issn',
1045                     'itemtype',
1046                     'kw',
1047                     'Koha-Auth-Number',
1048                     'l-format',
1049                     'language',
1050                     'language-original',
1051                     'lc-card',
1052                     'LC-card-number',
1053                     'lcn',
1054                     'lex',
1055                     'lexile-number',
1056                     'llength',
1057                     'ln',
1058                     'ln-audio',
1059                     'ln-subtitle',
1060                     'Local-classification',
1061                     'Local-number',
1062                     'Match-heading',
1063                     'Match-heading-see-from',
1064                     'Material-type',
1065                     'mc-itemtype',
1066                     'mc-rtype',
1067                     'mus',
1068                     'name',
1069                     'Music-number',
1070                     'Name-geographic',
1071                     'Name-geographic-heading',
1072                     'Name-geographic-see',
1073                     'Name-geographic-seealso',
1074                     'nb',
1075                     'Note',
1076                     'notes',
1077                     'ns',
1078                     'nt',
1079                     'Other-control-number',
1080                     'pb',
1081                     'Personal-name',
1082                     'Personal-name-heading',
1083                     'Personal-name-see',
1084                     'Personal-name-seealso',
1085                     'pl',
1086                     'Place-publication',
1087                     'pn',
1088                     'popularity',
1089                     'pubdate',
1090                     'Publisher',
1091                     'Provider',
1092                     'pv',
1093                     'Reading-grade-level',
1094                     'Record-control-number',
1095                     'rcn',
1096                     'Record-type',
1097                     'rtype',
1098                     'se',
1099                     'See',
1100                     'See-also',
1101                     'sn',
1102                     'Stock-number',
1103                     'su',
1104                     'Subject',
1105                     'Subject-heading-thesaurus',
1106                     'Subject-name-personal',
1107                     'Subject-subdivision',
1108                     'Summary',
1109                     'Suppress',
1110                     'su-geo',
1111                     'su-na',
1112                     'su-to',
1113                     'su-ut',
1114                     'ut',
1115                     'Term-genre-form',
1116                     'Term-genre-form-heading',
1117                     'Term-genre-form-see',
1118                     'Term-genre-form-seealso',
1119                     'ti',
1120                     'Title',
1121                     'Title-cover',
1122                     'Title-series',
1123                     'Title-uniform',
1124                     'Title-uniform-heading',
1125                     'Title-uniform-see',
1126                     'Title-uniform-seealso',
1127                     'totalissues',
1128                     'yr',
1129
1130                     # items indexes
1131                     'acqsource',
1132                     'barcode',
1133                     'bc',
1134                     'branch',
1135                     'ccode',
1136                     'classification-source',
1137                     'cn-sort',
1138                     'coded-location-qualifier',
1139                     'copynumber',
1140                     'damaged',
1141                     'datelastborrowed',
1142                     'datelastseen',
1143                     'holdingbranch',
1144                     'homebranch',
1145                     'issues',
1146                     'item',
1147                     'itemnumber',
1148                     'itype',
1149                     'Local-classification',
1150                     'location',
1151                     'lost',
1152                     'materials-specified',
1153                     'mc-ccode',
1154                     'mc-itype',
1155                     'mc-loc',
1156                     'notforloan',
1157                     'Number-local-acquisition',
1158                     'onloan',
1159                     'price',
1160                     'renewals',
1161                     'replacementprice',
1162                     'replacementpricedate',
1163                     'reserves',
1164                     'restricted',
1165                     'stack',
1166                     'stocknumber',
1167                     'inv',
1168                     'uri',
1169                     'withdrawn',
1170
1171                     # subject related
1172                   );
1173
1174     return \@indexes;
1175 }
1176
1177 =head2 buildQuery
1178
1179 ( $error, $query,
1180 $simple_query, $query_cgi,
1181 $query_desc, $limit,
1182 $limit_cgi, $limit_desc,
1183 $query_type ) = buildQuery ( $operators, $operands, $indexes, $limits, $sort_by, $scan, $lang);
1184
1185 Build queries and limits in CCL, CGI, Human,
1186 handle truncation, stemming, field weighting, fuzziness, etc.
1187
1188 See verbose embedded documentation.
1189
1190
1191 =cut
1192
1193 sub buildQuery {
1194     my ( $operators, $operands, $indexes, $limits, $sort_by, $scan, $lang) = @_;
1195
1196     my $query_desc;
1197
1198     # dereference
1199     my @operators = $operators ? @$operators : ();
1200     my @indexes   = $indexes   ? @$indexes   : ();
1201     my @operands  = $operands  ? @$operands  : ();
1202     my @limits    = $limits    ? @$limits    : ();
1203     my @sort_by   = $sort_by   ? @$sort_by   : ();
1204
1205     my $stemming         = C4::Context->preference("QueryStemming")        || 0;
1206     my $auto_truncation  = C4::Context->preference("QueryAutoTruncate")    || 0;
1207     my $weight_fields    = C4::Context->preference("QueryWeightFields")    || 0;
1208     my $fuzzy_enabled    = C4::Context->preference("QueryFuzzy")           || 0;
1209
1210     my $query        = $operands[0] // "";
1211     my $simple_query = $operands[0];
1212
1213     # initialize the variables we're passing back
1214     my $query_cgi;
1215     my $query_type;
1216
1217     my $limit;
1218     my $limit_cgi;
1219     my $limit_desc;
1220
1221     my $cclq       = 0;
1222     my $cclindexes = getIndexes();
1223     if ( $query !~ /\s*(ccl=|pqf=|cql=)/ ) {
1224         while ( !$cclq && $query =~ /(?:^|\W)([\w-]+)(,[\w-]+)*[:=]/g ) {
1225             my $dx = lc($1);
1226             $cclq = grep { lc($_) eq $dx } @$cclindexes;
1227         }
1228         $query = "ccl=$query" if $cclq;
1229     }
1230
1231 # for handling ccl, cql, pqf queries in diagnostic mode, skip the rest of the steps
1232 # DIAGNOSTIC ONLY!!
1233     if ( $query =~ /^ccl=/ ) {
1234         my $q=$';
1235         # This is needed otherwise ccl= and &limit won't work together, and
1236         # this happens when selecting a subject on the opac-detail page
1237         @limits = grep {!/^$/} @limits;
1238         my $original_q = $q; # without available part
1239         unless ( grep { $_ eq 'available' } @limits ) {
1240             $q =~ s| and \( \(allrecords,AlwaysMatches=''\) and \(not-onloan-count,st-numeric >= 1\) and \(lost,st-numeric=0\) \)||;
1241             $original_q = $q;
1242         }
1243         if ( @limits ) {
1244             if ( grep { $_ eq 'available' } @limits ) {
1245                 $q .= q| and ( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) )|;
1246                 @limits = grep {!/^available$/} @limits;
1247             }
1248             $q .= ' and '.join(' and ', @limits) if @limits;
1249         }
1250         return ( undef, $q, $q, "q=ccl=".uri_escape_utf8($q), $original_q, '', '', '', 'ccl' );
1251     }
1252     if ( $query =~ /^cql=/ ) {
1253         return ( undef, $', $', "q=cql=".uri_escape_utf8($'), $', '', '', '', 'cql' );
1254     }
1255     if ( $query =~ /^pqf=/ ) {
1256         $query_desc = $';
1257         $query_cgi = "q=pqf=".uri_escape_utf8($');
1258         return ( undef, $', $', $query_cgi, $query_desc, '', '', '', 'pqf' );
1259     }
1260
1261     # pass nested queries directly
1262     # FIXME: need better handling of some of these variables in this case
1263     # Nested queries aren't handled well and this implementation is flawed and causes users to be
1264     # unable to search for anything containing () commenting out, will be rewritten for 3.4.0
1265 #    if ( $query =~ /(\(|\))/ ) {
1266 #        return (
1267 #            undef,              $query, $simple_query, $query_cgi,
1268 #            $query,             $limit, $limit_cgi,    $limit_desc,
1269 #            'ccl'
1270 #        );
1271 #    }
1272
1273 # Form-based queries are non-nested and fixed depth, so we can easily modify the incoming
1274 # query operands and indexes and add stemming, truncation, field weighting, etc.
1275 # Once we do so, we'll end up with a value in $query, just like if we had an
1276 # incoming $query from the user
1277     else {
1278         $query = ""
1279           ; # clear it out so we can populate properly with field-weighted, stemmed, etc. query
1280         my $previous_operand
1281           ;    # a flag used to keep track if there was a previous query
1282                # if there was, we can apply the current operator
1283                # for every operand
1284         for ( my $i = 0 ; $i <= @operands ; $i++ ) {
1285
1286             # COMBINE OPERANDS, INDEXES AND OPERATORS
1287             if ( ($operands[$i] // '') ne '' ) {
1288                 $operands[$i]=~s/^\s+//;
1289
1290               # A flag to determine whether or not to add the index to the query
1291                 my $indexes_set;
1292
1293 # If the user is sophisticated enough to specify an index, turn off field weighting, and stemming handling
1294                 if ( $operands[$i] =~ /\w(:|=)/ || $scan ) {
1295                     $weight_fields    = 0;
1296                     $stemming         = 0;
1297                 } else {
1298                     $operands[$i] =~ s/\?/{?}/g; # need to escape question marks
1299                 }
1300                 my $operand = $operands[$i];
1301                 my $index   = $indexes[$i] || 'kw';
1302
1303                 # Add index-specific attributes
1304
1305                 #Afaik, this 'yr' condition will only ever be met in the staff interface advanced search
1306                 #for "Publication date", since typing 'yr:YYYY' into the search box produces a CCL query,
1307                 #which is processed higher up in this sub. Other than that, year searches are typically
1308                 #handled as limits which are not processed her either.
1309
1310                 # Search ranges: Date of Publication, st-numeric
1311                 if ( $index =~ /(yr|st-numeric)/ ) {
1312                     #weight_fields/relevance search causes errors with date ranges
1313                     #In the case of YYYY-, it will only return records with a 'yr' of YYYY (not the range)
1314                     #In the case of YYYY-YYYY, it will return no results
1315                     $stemming = $auto_truncation = $weight_fields = $fuzzy_enabled = 0;
1316                 }
1317
1318                 # Date of Acquisition
1319                 elsif ( $index =~ /acqdate/ ) {
1320                     #stemming and auto_truncation would have zero impact since it already is YYYY-MM-DD format
1321                     #Weight_fields probably SHOULD be turned OFF, otherwise you'll get records floating to the
1322                       #top of the results just because they have lots of item records matching that date.
1323                     #Fuzzy actually only applies during _build_weighted_query, and is reset there anyway, so
1324                       #irrelevant here
1325                     $stemming = $auto_truncation = $weight_fields = $fuzzy_enabled = 0;
1326                 }
1327                 # ISBN,ISSN,Standard Number, don't need special treatment
1328                 elsif ( $index eq 'nb' || $index eq 'ns' || $index eq 'hi' ) {
1329                     (
1330                         $stemming,      $auto_truncation,
1331                         $weight_fields, $fuzzy_enabled
1332                     ) = ( 0, 0, 0, 0 );
1333
1334                     if ( $index eq 'nb' ) {
1335                         if ( C4::Context->preference("SearchWithISBNVariations") ) {
1336                             my @isbns = C4::Koha::GetVariationsOfISBN( $operand );
1337                             $operands[$i] = $operand =  '(nb=' . join(' OR nb=', @isbns) . ')';
1338                             $indexes[$i] = $index = 'kw';
1339                         }
1340                     }
1341                 }
1342
1343                 # Set default structure attribute (word list)
1344                 my $struct_attr = q{};
1345                 unless ( $indexes_set || $index =~ /,(st-|phr|ext|wrdl)/ || $index =~ /^(nb|ns)$/ ) {
1346                     $struct_attr = ",wrdl";
1347                 }
1348
1349                 # Some helpful index variants
1350                 my $index_plus       = $index . $struct_attr . ':';
1351                 my $index_plus_comma = $index . $struct_attr . ',';
1352
1353                 if ($auto_truncation){
1354                         unless ( $index =~ /,(st-|phr|ext)/ ) {
1355                                                 #FIXME only valid with LTR scripts
1356                                                 $operand=join(" ",map{
1357                                                                                         (index($_,"*")>0?"$_":"$_*")
1358                                                                                          }split (/\s+/,$operand));
1359                                         }
1360                                 }
1361
1362                 # Detect Truncation
1363                 my $truncated_operand = q{};
1364                 my( $nontruncated, $righttruncated, $lefttruncated,
1365                     $rightlefttruncated, $regexpr
1366                 ) = _detect_truncation( $operand, $index );
1367
1368                 Koha::Logger->get->debug(
1369                     "TRUNCATION: NON:>@$nontruncated< RIGHT:>@$righttruncated< LEFT:>@$lefttruncated< RIGHTLEFT:>@$rightlefttruncated< REGEX:>@$regexpr<");
1370
1371                 # Apply Truncation
1372                 if (
1373                     scalar(@$righttruncated) + scalar(@$lefttruncated) +
1374                     scalar(@$rightlefttruncated) > 0 )
1375                 {
1376
1377                # Don't field weight or add the index to the query, we do it here
1378                     $indexes_set = 1;
1379                     undef $weight_fields;
1380                     my $previous_truncation_operand;
1381                     if (scalar @$nontruncated) {
1382                         $truncated_operand .= "$index_plus @$nontruncated ";
1383                         $previous_truncation_operand = 1;
1384                     }
1385                     if (scalar @$righttruncated) {
1386                         $truncated_operand .= "and " if $previous_truncation_operand;
1387                         $truncated_operand .= $index_plus_comma . "rtrn:@$righttruncated ";
1388                         $previous_truncation_operand = 1;
1389                     }
1390                     if (scalar @$lefttruncated) {
1391                         $truncated_operand .= "and " if $previous_truncation_operand;
1392                         $truncated_operand .= $index_plus_comma . "ltrn:@$lefttruncated ";
1393                         $previous_truncation_operand = 1;
1394                     }
1395                     if (scalar @$rightlefttruncated) {
1396                         $truncated_operand .= "and " if $previous_truncation_operand;
1397                         $truncated_operand .= $index_plus_comma . "rltrn:@$rightlefttruncated ";
1398                         $previous_truncation_operand = 1;
1399                     }
1400                 }
1401                 $operand = $truncated_operand if $truncated_operand;
1402                 Koha::Logger->get->debug("TRUNCATED OPERAND: >$truncated_operand<");
1403
1404                 # Handle Stemming
1405                 my $stemmed_operand = q{};
1406                 $stemmed_operand = _build_stemmed_operand($operand, $lang)
1407                                                                                 if $stemming;
1408
1409                 Koha::Logger->get->debug("STEMMED OPERAND: >$stemmed_operand<");
1410
1411                 # Handle Field Weighting
1412                 my $weighted_operand = q{};
1413                 if ($weight_fields) {
1414                     $weighted_operand = _build_weighted_query( $operand, $stemmed_operand, $index );
1415                     $operand = $weighted_operand;
1416                     $indexes_set = 1;
1417                 }
1418
1419                 Koha::Logger->get->debug("FIELD WEIGHTED OPERAND: >$weighted_operand<");
1420
1421                 #Use relevance ranking when not using a weighted query (which adds relevance ranking of its own)
1422
1423                 #N.B. Truncation is mutually exclusive with Weighted Queries,
1424                 #so even if QueryWeightFields is turned on, QueryAutoTruncate will turn it off, thus
1425                 #the need for this relevance wrapper.
1426                 $operand = "(rk=($operand))" unless $weight_fields;
1427
1428                 ($query,$query_cgi,$query_desc,$previous_operand) = _build_initial_query({
1429                     query => $query,
1430                     query_cgi => $query_cgi,
1431                     query_desc => $query_desc,
1432                     operator => ($operators[ $i - 1 ]) ? $operators[ $i - 1 ] : '',
1433                     parsed_operand => $operand,
1434                     original_operand => $operands[$i] // '',
1435                     index => $index,
1436                     index_plus => $index_plus,
1437                     indexes_set => $indexes_set,
1438                     previous_operand => $previous_operand,
1439                 });
1440
1441             }    #/if $operands
1442         }    # /for
1443     }
1444     Koha::Logger->get->debug("QUERY BEFORE LIMITS: >$query<");
1445
1446     # add limits
1447     my %group_OR_limits;
1448     my $availability_limit;
1449     foreach my $this_limit (@limits) {
1450         next unless $this_limit;
1451         if ( $this_limit =~ /available/ ) {
1452 #
1453 ## 'available' is defined as (items.onloan is NULL) and (items.itemlost = 0)
1454 ## In English:
1455 ## all records not indexed in the onloan register (zebra) and all records with a value of lost equal to 0
1456             $availability_limit .=
1457 "( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) )";
1458             $limit_cgi  .= "&limit=available";
1459             $limit_desc .= "";
1460         }
1461
1462         # group_OR_limits, prefixed by mc-
1463         # OR every member of the group
1464         elsif ( $this_limit =~ /mc/ ) {
1465             my ($k,$v) = split(/:/, $this_limit,2);
1466             if ( $k !~ /mc-i(tem)?type/ ) {
1467                 # in case the mc-ccode value has complicating chars like ()'s inside it we wrap in quotes
1468                 $this_limit =~ tr/"//d;
1469                 $this_limit = $k.':"'.$v.'"';
1470             }
1471
1472             $group_OR_limits{$k} .= " or " if $group_OR_limits{$k};
1473             $limit_desc      .= " or " if $group_OR_limits{$k};
1474             $group_OR_limits{$k} .= "$this_limit";
1475             $limit_cgi       .= "&limit=" . uri_escape_utf8($this_limit);
1476             $limit_desc      .= " $this_limit";
1477         }
1478         elsif ( $this_limit =~ '^multibranchlimit:|^branch:' ) {
1479             $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1480             $limit .= " and " if $limit || $query;
1481             my $branchfield  = C4::Context->preference('SearchLimitLibrary');
1482             my @branchcodes;
1483             if(  $this_limit =~ '^multibranchlimit:' ){
1484                 my ($group_id) = ( $this_limit =~ /^multibranchlimit:(.*)$/ );
1485                 my $search_group = Koha::Library::Groups->find( $group_id );
1486                 @branchcodes  = map { $_->branchcode } $search_group->all_libraries;
1487                 @branchcodes = sort { $a cmp $b } @branchcodes;
1488             } else {
1489                 @branchcodes = ( $this_limit =~ /^branch:(.*)$/ );
1490             }
1491
1492             if (@branchcodes) {
1493                 if ( $branchfield eq "homebranch" ) {
1494                     $this_limit = sprintf "(%s)", join " or ", map { 'homebranch: ' . $_ } @branchcodes;
1495                 }
1496                 elsif ( $branchfield eq "holdingbranch" ) {
1497                     $this_limit = sprintf "(%s)", join " or ", map { 'holdingbranch: ' . $_ } @branchcodes;
1498                 }
1499                 else {
1500                     $this_limit =  sprintf "(%s or %s)",
1501                       join( " or ", map { 'homebranch: ' . $_ } @branchcodes ),
1502                       join( " or ", map { 'holdingbranch: ' . $_ } @branchcodes );
1503                 }
1504             }
1505             $limit .= "$this_limit";
1506             $limit_desc .= " $this_limit";
1507         }
1508
1509         # Regular old limits
1510         else {
1511             $limit .= " and " if $limit || $query;
1512             $limit      .= "$this_limit";
1513             $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1514             $limit_desc .= " $this_limit";
1515         }
1516     }
1517     foreach my $k (keys (%group_OR_limits)) {
1518         $limit .= " and " if ( $query || $limit );
1519         $limit .= "($group_OR_limits{$k})";
1520     }
1521     if ($availability_limit) {
1522         $limit .= " and " if ( $query || $limit );
1523         $limit .= "($availability_limit)";
1524     }
1525
1526     # Normalize the query and limit strings
1527     # This is flawed , means we can't search anything with : in it
1528     # if user wants to do ccl or cql, start the query with that
1529 #    $query =~ s/:/=/g;
1530     #NOTE: We use several several different regexps here as you can't have variable length lookback assertions
1531     $query =~ s/(?<=(ti|au|pb|su|an|kw|mc|nb|ns)):/=/g;
1532     $query =~ s/(?<=(wrdl)):/=/g;
1533     $query =~ s/(?<=(trn|phr)):/=/g;
1534     $query =~ s/(?<=(st-numeric)):/=/g;
1535     $query =~ s/(?<=(st-year)):/=/g;
1536     $query =~ s/(?<=(st-date-normalized)):/=/g;
1537
1538     # Removing warnings for later substitutions
1539     $query        //= q{};
1540     $query_desc   //= q{};
1541     $query_cgi    //= q{};
1542     $limit        //= q{};
1543     $limit_desc   //= q{};
1544     $limit_cgi    //= q{};
1545     $simple_query //= q{};
1546     $limit =~ s/:/=/g;
1547     for ( $query, $query_desc, $limit, $limit_desc ) {
1548         s/  +/ /g;    # remove extra spaces
1549         s/^ //g;     # remove any beginning spaces
1550         s/ $//g;     # remove any ending spaces
1551         s/==/=/g;    # remove double == from query
1552     }
1553     $query_cgi =~ s/^&//; # remove unnecessary & from beginning of the query cgi
1554
1555     for ($query_cgi,$simple_query) {
1556         s/"//g;
1557     }
1558     # append the limit to the query
1559     $query .= " " . $limit;
1560
1561     Koha::Logger->get->debug(
1562         sprintf "buildQuery returns\nQUERY:%s\nQUERY CGI:%s\nQUERY DESC:%s\nLIMIT:%s\nLIMIT CGI:%s\nLIMIT DESC:%s",
1563         $query, $query_cgi, $query_desc, $limit, $limit_cgi, $limit_desc );
1564
1565     return (
1566         undef,              $query, $simple_query, $query_cgi,
1567         $query_desc,        $limit, $limit_cgi,    $limit_desc,
1568         $query_type
1569     );
1570 }
1571
1572 =head2 _build_initial_query
1573
1574   ($query, $query_cgi, $query_desc, $previous_operand) = _build_initial_query($initial_query_params);
1575
1576   Build a section of the initial query containing indexes, operators, and operands.
1577
1578 =cut
1579
1580 sub _build_initial_query {
1581     my ($params) = @_;
1582
1583     my $operator = "";
1584     if ($params->{previous_operand}){
1585         #If there is a previous operand, add a supplied operator or the default 'and'
1586         $operator = ($params->{operator}) ? " ".($params->{operator})." " : ' and ';
1587     }
1588
1589     #NOTE: indexes_set is typically set when doing truncation or field weighting
1590     my $operand = ($params->{indexes_set}) ? $params->{parsed_operand} : $params->{index_plus}.$params->{parsed_operand};
1591
1592     #e.g. "kw,wrdl:test"
1593     #e.g. " and kw,wrdl:test"
1594     $params->{query} .= $operator . $operand;
1595
1596     $params->{query_cgi} .= "&op=".uri_escape_utf8($operator) if $operator;
1597     $params->{query_cgi} .= "&idx=".uri_escape_utf8($params->{index}) if $params->{index};
1598     $params->{query_cgi} .= "&q=".uri_escape_utf8($params->{original_operand}) if $params->{original_operand};
1599
1600     #e.g. " and kw,wrdl: test"
1601     $params->{query_desc} .= $operator . ( $params->{index_plus} // q{} ) . " " . ( $params->{original_operand} // q{} );
1602
1603     $params->{previous_operand} = 1 unless $params->{previous_operand}; #If there is no previous operand, mark this as one
1604
1605     return ($params->{query}, $params->{query_cgi}, $params->{query_desc}, $params->{previous_operand});
1606 }
1607
1608 =head2 searchResults
1609
1610   my @search_results = searchResults($search_context, $searchdesc, $hits, 
1611                                      $results_per_page, $offset, $scan, 
1612                                      @marcresults);
1613
1614 Format results in a form suitable for passing to the template
1615
1616 =cut
1617
1618 # IMO this subroutine is pretty messy still -- it's responsible for
1619 # building the HTML output for the template
1620 sub searchResults {
1621     my ( $search_context, $searchdesc, $hits, $results_per_page, $offset, $scan, $marcresults, $xslt_variables ) = @_;
1622     my $dbh = C4::Context->dbh;
1623     my @newresults;
1624
1625     require C4::Items;
1626
1627     $search_context->{'interface'} = 'opac' if !$search_context->{'interface'} || $search_context->{'interface'} ne 'intranet';
1628     my ($is_opac, $hidelostitems);
1629     if ($search_context->{'interface'} eq 'opac') {
1630         $hidelostitems = C4::Context->preference('hidelostitems');
1631         $is_opac       = 1;
1632     }
1633
1634     my $record_processor = Koha::RecordProcessor->new({
1635         filters => 'ViewPolicy'
1636     });
1637
1638     #Build branchnames hash
1639     my %branches = map { $_->branchcode => $_->branchname } Koha::Libraries->search({}, { order_by => 'branchname' });
1640
1641 # FIXME - We build an authorised values hash here, using the default framework
1642 # though it is possible to have different authvals for different fws.
1643
1644     my $shelflocations =
1645       { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
1646
1647     # get notforloan authorised value list (see $shelflocations  FIXME)
1648     my $av = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
1649     my $notforloan_authorised_value = $av->count ? $av->next->authorised_value : undef;
1650
1651     #Get itemtype hash
1652     my $itemtypes = Koha::ItemTypes->search_with_localization;
1653     my %itemtypes = map { $_->{itemtype} => $_ } @{ $itemtypes->unblessed };
1654
1655     #search item field code
1656     my ($itemtag, undef) = &GetMarcFromKohaField( "items.itemnumber" );
1657
1658     ## find column names of items related to MARC
1659     my %subfieldstosearch;
1660     my @columns = Koha::Database->new()->schema()->resultset('Item')->result_source->columns;
1661     for my $column ( @columns ) {
1662         my ( $tagfield, $tagsubfield ) =
1663           &GetMarcFromKohaField( "items." . $column );
1664         if ( defined $tagsubfield ) {
1665             $subfieldstosearch{$column} = $tagsubfield;
1666         }
1667     }
1668
1669     # handle which records to actually retrieve
1670     my $times; # Times is which record to process up to
1671     if ( $hits && $offset + $results_per_page <= $hits ) {
1672         $times = $offset + $results_per_page;
1673     }
1674     else {
1675         $times = $hits; # If less hits than results_per_page+offset we go to the end
1676     }
1677
1678     my $marcflavour = C4::Context->preference("marcflavour");
1679     # We get the biblionumber position in MARC
1680     my ($bibliotag,$bibliosubf)=GetMarcFromKohaField( 'biblio.biblionumber' );
1681
1682     # set stuff for XSLT processing here once, not later again for every record we retrieved
1683
1684     my $userenv = C4::Context->userenv;
1685     my $logged_in_user
1686         = ( defined $userenv and $userenv->{number} )
1687         ? Koha::Patrons->find( $userenv->{number} )
1688         : undef;
1689     my $patron_category_hide_lost_items = ($logged_in_user) ? $logged_in_user->category->hidelostitems : 0;
1690
1691     # loop through all of the records we've retrieved
1692     for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) {
1693
1694         my $marcrecord;
1695         if ($scan) {
1696             # For Scan searches we built USMARC data
1697             $marcrecord = MARC::Record->new_from_usmarc( $marcresults->[$i]);
1698         } else {
1699             # Normal search, render from Zebra's output
1700             $marcrecord = new_record_from_zebra(
1701                 'biblioserver',
1702                 $marcresults->[$i]
1703             );
1704
1705             if ( ! defined $marcrecord ) {
1706                 warn "ERROR DECODING RECORD - $@: " . $marcresults->[$i];
1707                 next;
1708             }
1709         }
1710
1711         my $fw = $scan
1712              ? undef
1713              : $bibliotag < 10
1714                ? GetFrameworkCode($marcrecord->field($bibliotag)->data)
1715                : GetFrameworkCode($marcrecord->subfield($bibliotag,$bibliosubf));
1716
1717         SetUTF8Flag($marcrecord);
1718         my $oldbiblio = TransformMarcToKoha( $marcrecord, $fw, 'no_items' );
1719         $oldbiblio->{result_number} = $i + 1;
1720
1721                 $oldbiblio->{normalized_upc}  = GetNormalizedUPC(       $marcrecord,$marcflavour);
1722                 $oldbiblio->{normalized_ean}  = GetNormalizedEAN(       $marcrecord,$marcflavour);
1723                 $oldbiblio->{normalized_oclc} = GetNormalizedOCLCNumber($marcrecord,$marcflavour);
1724         $oldbiblio->{normalized_isbn} = GetNormalizedISBN($oldbiblio->{isbn},$marcrecord,$marcflavour); # Use existing ISBN from record if we got one
1725                 $oldbiblio->{content_identifier_exists} = 1 if ($oldbiblio->{normalized_isbn} or $oldbiblio->{normalized_oclc} or $oldbiblio->{normalized_ean} or $oldbiblio->{normalized_upc});
1726
1727                 # edition information, if any
1728         $oldbiblio->{edition} = $oldbiblio->{editionstatement};
1729
1730         my $itemtype = $oldbiblio->{itemtype} ? $itemtypes{$oldbiblio->{itemtype}} : undef;
1731         # add imageurl to itemtype if there is one
1732         $oldbiblio->{imageurl} = $itemtype ? getitemtypeimagelocation( $search_context->{'interface'}, $itemtype->{imageurl} ) : q{};
1733         # Build summary if there is one (the summary is defined in the itemtypes table)
1734         $oldbiblio->{description} = $itemtype ? $itemtype->{translated_description} : q{};
1735
1736         # Pull out the items fields
1737         my @fields = $marcrecord->field($itemtag);
1738         $marcrecord->delete_fields( @fields ) unless C4::Context->preference('PassItemMarcToXSLT');
1739         my $marcflavor = C4::Context->preference("marcflavour");
1740
1741         # adding linked items that belong to host records
1742         if ( C4::Context->preference('EasyAnalyticalRecords') ) {
1743             my $analyticsfield = '773';
1744             if ($marcflavor eq 'MARC21') {
1745                 $analyticsfield = '773';
1746             } elsif ($marcflavor eq 'UNIMARC') {
1747                 $analyticsfield = '461';
1748             }
1749             foreach my $hostfield ( $marcrecord->field($analyticsfield)) {
1750                 my $hostbiblionumber = $hostfield->subfield("0");
1751                 my $linkeditemnumber = $hostfield->subfield("9");
1752                 if( $hostbiblionumber ) {
1753                     my $linkeditemmarc = C4::Items::GetMarcItem( $hostbiblionumber, $linkeditemnumber );
1754                     if ($linkeditemmarc) {
1755                         my $linkeditemfield = $linkeditemmarc->field($itemtag);
1756                         if ($linkeditemfield) {
1757                             push( @fields, $linkeditemfield );
1758                         }
1759                     }
1760                 }
1761             }
1762         }
1763
1764         # Setting item statuses for display
1765         my @available_items_loop;
1766         my @onloan_items_loop;
1767         my @other_items_loop;
1768
1769         my $available_items;
1770         my $onloan_items;
1771         my $other_items;
1772
1773         my $ordered_count         = 0;
1774         my $available_count       = 0;
1775         my $onloan_count          = 0;
1776         my $longoverdue_count     = 0;
1777         my $other_count           = 0;
1778         my $withdrawn_count        = 0;
1779         my $itemlost_count        = 0;
1780         my $hideatopac_count      = 0;
1781         my $itembinding_count     = 0;
1782         my $itemdamaged_count     = 0;
1783         my $item_in_transit_count = 0;
1784         my $can_place_holds       = 0;
1785         my $item_onhold_count     = 0;
1786         my $notforloan_count      = 0;
1787         my $items_count           = scalar(@fields);
1788         my $maxitems_pref = C4::Context->preference('maxItemsinSearchResults');
1789         my $maxitems = $maxitems_pref ? $maxitems_pref - 1 : 1;
1790         my @hiddenitems; # hidden itemnumbers based on OpacHiddenItems syspref
1791
1792         # loop through every item
1793         foreach my $field (@fields) {
1794             my $item;
1795
1796             # populate the items hash
1797             foreach my $code ( keys %subfieldstosearch ) {
1798                 $item->{$code} = $field->subfield( $subfieldstosearch{$code} );
1799             }
1800             $item->{description} = $itemtypes{ $item->{itype} }{translated_description} if $item->{itype};
1801
1802                 # OPAC hidden items
1803             if ($is_opac) {
1804                 # hidden because lost
1805                 if ($hidelostitems && $item->{itemlost}) {
1806                     $hideatopac_count++;
1807                     next;
1808                 }
1809                 # hidden based on OpacHiddenItems syspref
1810                 my @hi = C4::Items::GetHiddenItemnumbers({ items=> [ $item ], borcat => $search_context->{category} });
1811                 if (scalar @hi) {
1812                     push @hiddenitems, @hi;
1813                     $hideatopac_count++;
1814                     next;
1815                 }
1816             }
1817
1818             my $hbranch     = C4::Context->preference('StaffSearchResultsDisplayBranch');
1819             my $otherbranch = $hbranch eq 'homebranch' ? 'holdingbranch' : 'homebranch';
1820
1821             # set item's branch name, use HomeOrHoldingBranch syspref first, fall back to the other one
1822             if ($item->{$hbranch}) {
1823                 $item->{'branchname'} = $branches{$item->{$hbranch}};
1824             }
1825             elsif ($item->{$otherbranch}) {     # Last resort
1826                 $item->{'branchname'} = $branches{$item->{$otherbranch}};
1827             }
1828
1829             my $prefix =
1830                 ( $item->{$hbranch} ? $item->{$hbranch} . '--' : q{} )
1831               . ( $item->{location} ? $item->{location} : q{} )
1832               . ( $item->{itype}    ? $item->{itype}    : q{} )
1833               . ( $item->{itemcallnumber} ? $item->{itemcallnumber} : q{} );
1834 # For each grouping of items (onloan, available, unavailable), we build a key to store relevant info about that item
1835             if ( $item->{onloan}
1836                 and $logged_in_user
1837                 and !( $patron_category_hide_lost_items and $item->{itemlost} ) )
1838             {
1839                 $onloan_count++;
1840                 my $key = $prefix . $item->{onloan} . $item->{barcode};
1841                 $onloan_items->{$key}->{due_date} = $item->{onloan};
1842                 $onloan_items->{$key}->{count}++ if $item->{$hbranch};
1843                 $onloan_items->{$key}->{branchname}     = $item->{branchname};
1844                 $onloan_items->{$key}->{location}       = $shelflocations->{ $item->{location} } if $item->{location};
1845                 $onloan_items->{$key}->{itemcallnumber} = $item->{itemcallnumber};
1846                 $onloan_items->{$key}->{description}    = $item->{description};
1847                 $onloan_items->{$key}->{imageurl} =
1848                   getitemtypeimagelocation( $search_context->{'interface'}, $itemtypes{ $item->{itype} }->{imageurl} );
1849
1850                 # if something's checked out and lost, mark it as 'long overdue'
1851                 if ( $item->{itemlost} ) {
1852                     $onloan_items->{$key}->{longoverdue}++;
1853                     $longoverdue_count++;
1854                 }
1855                 else {    # can place holds as long as item isn't lost
1856                     $can_place_holds = 1;
1857                 }
1858             }
1859
1860          # items not on loan, but still unavailable ( lost, withdrawn, damaged )
1861             else {
1862
1863                 my $itemtype = C4::Context->preference("item-level_itypes")? $item->{itype}: $oldbiblio->{itemtype};
1864                 $item->{notforloan} = 1 if !$item->{notforloan} &&
1865                     $itemtype && $itemtypes{ $itemtype }->{notforloan};
1866
1867                 # item is on order
1868                 if ( $item->{notforloan} < 0 ) {
1869                     $ordered_count++;
1870                 } elsif ( $item->{notforloan} > 0 ) {
1871                     $notforloan_count++;
1872                 }
1873
1874                 # is item in transit?
1875                 my $transfertwhen = '';
1876                 my ($transfertfrom, $transfertto);
1877
1878                 # is item on the reserve shelf?
1879                 my $reservestatus = '';
1880
1881                 unless ($item->{withdrawn}
1882                         || $item->{itemlost}
1883                         || $item->{damaged}
1884                         || $item->{notforloan}
1885                         || ( C4::Context->preference('MaxSearchResultsItemsPerRecordStatusCheck')
1886                         && $items_count > C4::Context->preference('MaxSearchResultsItemsPerRecordStatusCheck') ) ) {
1887
1888                     # A couple heuristics to limit how many times
1889                     # we query the database for item transfer information, sacrificing
1890                     # accuracy in some cases for speed;
1891                     #
1892                     # 1. don't query if item has one of the other statuses
1893                     # 2. don't check transit status if the bib has
1894                     #    more than 20 items
1895                     #
1896                     # FIXME: to avoid having the query the database like this, and to make
1897                     #        the in transit status count as unavailable for search limiting,
1898                     #        should map transit status to record indexed in Zebra.
1899                     #
1900                     ($transfertwhen, $transfertfrom, $transfertto) = C4::Circulation::GetTransfers($item->{itemnumber});
1901                     $reservestatus = C4::Reserves::GetReserveStatus( $item->{itemnumber} );
1902                 }
1903
1904                 # item is withdrawn, lost, damaged, not for loan, reserved or in transit
1905                 if (   $item->{withdrawn}
1906                     || $item->{itemlost}
1907                     || $item->{damaged}
1908                     || $item->{notforloan}
1909                     || $reservestatus eq 'Waiting'
1910                     || ($transfertwhen && $transfertwhen ne ''))
1911                 {
1912                     $withdrawn_count++        if $item->{withdrawn};
1913                     $itemlost_count++        if $item->{itemlost};
1914                     $itemdamaged_count++     if $item->{damaged};
1915                     $item_in_transit_count++ if $transfertwhen && $transfertwhen ne '';
1916                     $item_onhold_count++     if $reservestatus eq 'Waiting';
1917                     $item->{status} = ($item->{withdrawn}//q{}) . "-" . ($item->{itemlost}//q{}) . "-" . ($item->{damaged}//q{}) . "-" . ($item->{notforloan}//q{});
1918
1919                     # can place a hold on a item if
1920                     # not lost nor withdrawn
1921                     # not damaged unless AllowHoldsOnDamagedItems is true
1922                     # item is either for loan or on order (notforloan < 0)
1923                     $can_place_holds = 1
1924                       if (
1925                            !$item->{itemlost}
1926                         && !$item->{withdrawn}
1927                         && ( !$item->{damaged} || C4::Context->preference('AllowHoldsOnDamagedItems') )
1928                         && ( !$item->{notforloan} || $item->{notforloan} < 0 )
1929                       );
1930
1931                     $other_count++;
1932
1933                     my $key = $prefix . $item->{status};
1934                     foreach (qw(withdrawn itemlost damaged branchname itemcallnumber)) {
1935                         $other_items->{$key}->{$_} = $item->{$_};
1936                     }
1937                     $other_items->{$key}->{intransit} = ( $transfertwhen ne '' ) ? 1 : 0;
1938                     $other_items->{$key}->{onhold} = ($reservestatus) ? 1 : 0;
1939                     $other_items->{$key}->{notforloan} = GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value) if $notforloan_authorised_value and $item->{notforloan};
1940                     $other_items->{$key}->{count}++ if $item->{$hbranch};
1941                     $other_items->{$key}->{location} = $shelflocations->{ $item->{location} } if $item->{location};
1942                     $other_items->{$key}->{description} = $item->{description};
1943                     $other_items->{$key}->{imageurl} = getitemtypeimagelocation( $search_context->{'interface'}, $itemtypes{ $item->{itype}//q{} }->{imageurl} );
1944                 }
1945                 # item is available
1946                 else {
1947                     $can_place_holds = 1;
1948                     $available_count++;
1949                     $available_items->{$prefix}->{count}++ if $item->{$hbranch};
1950                     foreach (qw(branchname itemcallnumber description)) {
1951                         $available_items->{$prefix}->{$_} = $item->{$_};
1952                     }
1953                     $available_items->{$prefix}->{location} = $shelflocations->{ $item->{location} } if $item->{location};
1954                     $available_items->{$prefix}->{imageurl} = getitemtypeimagelocation( $search_context->{'interface'}, $itemtypes{ $item->{itype}//q{} }->{imageurl} );
1955                 }
1956             }
1957         }    # notforloan, item level and biblioitem level
1958
1959         # if all items are hidden, do not show the record
1960         if ( C4::Context->preference('OpacHiddenItemsHidesRecord') && $items_count > 0 && $hideatopac_count == $items_count) {
1961             next;
1962         }
1963
1964         my ( $availableitemscount, $onloanitemscount, $otheritemscount );
1965         for my $key ( sort keys %$onloan_items ) {
1966             (++$onloanitemscount > $maxitems) and last;
1967             push @onloan_items_loop, $onloan_items->{$key};
1968         }
1969         for my $key ( sort keys %$other_items ) {
1970             (++$otheritemscount > $maxitems) and last;
1971             push @other_items_loop, $other_items->{$key};
1972         }
1973         for my $key ( sort keys %$available_items ) {
1974             (++$availableitemscount > $maxitems) and last;
1975             push @available_items_loop, $available_items->{$key}
1976         }
1977
1978         # XSLT processing of some stuff
1979         # we fetched the sysprefs already before the loop through all retrieved record!
1980         if (!$scan) {
1981             $record_processor->options({
1982                 frameworkcode => $fw,
1983                 interface     => $search_context->{'interface'}
1984             });
1985
1986             $record_processor->process($marcrecord);
1987
1988             $oldbiblio->{XSLTResultsRecord} = XSLTParse4Display(
1989                 {
1990                     biblionumber => $oldbiblio->{biblionumber},
1991                     record       => $marcrecord,
1992                     xsl_syspref  => (
1993                         $is_opac
1994                         ? 'OPACXSLTResultsDisplay'
1995                         : 'XSLTResultsDisplay'
1996                     ),
1997                     fix_amps       => 1,
1998                     hidden_items   => \@hiddenitems,
1999                     xslt_variables => $xslt_variables
2000                 }
2001             );
2002         }
2003
2004         # if biblio level itypes are used and itemtype is notforloan, it can't be reserved either
2005         if (!C4::Context->preference("item-level_itypes")) {
2006             if ($itemtype && $itemtype->{notforloan}) {
2007                 $can_place_holds = 0;
2008             }
2009         }
2010         $oldbiblio->{norequests} = 1 unless $can_place_holds;
2011         $oldbiblio->{items_count}          = $items_count;
2012         $oldbiblio->{available_items_loop} = \@available_items_loop;
2013         $oldbiblio->{onloan_items_loop}    = \@onloan_items_loop;
2014         $oldbiblio->{other_items_loop}     = \@other_items_loop;
2015         $oldbiblio->{availablecount}       = $available_count;
2016         $oldbiblio->{availableplural}      = 1 if $available_count > 1;
2017         $oldbiblio->{onloancount}          = $onloan_count;
2018         $oldbiblio->{onloanplural}         = 1 if $onloan_count > 1;
2019         $oldbiblio->{othercount}           = $other_count;
2020         $oldbiblio->{otherplural}          = 1 if $other_count > 1;
2021         $oldbiblio->{withdrawncount}        = $withdrawn_count;
2022         $oldbiblio->{itemlostcount}        = $itemlost_count;
2023         $oldbiblio->{damagedcount}         = $itemdamaged_count;
2024         $oldbiblio->{intransitcount}       = $item_in_transit_count;
2025         $oldbiblio->{onholdcount}          = $item_onhold_count;
2026         $oldbiblio->{orderedcount}         = $ordered_count;
2027         $oldbiblio->{notforloancount}      = $notforloan_count;
2028
2029         if (C4::Context->preference("AlternateHoldingsField") && $items_count == 0) {
2030             my $fieldspec = C4::Context->preference("AlternateHoldingsField");
2031             my $subfields = substr $fieldspec, 3;
2032             my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
2033             my @alternateholdingsinfo = ();
2034             my @holdingsfields = $marcrecord->field(substr $fieldspec, 0, 3);
2035             my $alternateholdingscount = 0;
2036
2037             for my $field (@holdingsfields) {
2038                 my %holding = ( holding => '' );
2039                 my $havesubfield = 0;
2040                 for my $subfield ($field->subfields()) {
2041                     if ((index $subfields, $$subfield[0]) >= 0) {
2042                         $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
2043                         $holding{'holding'} .= $$subfield[1];
2044                         $havesubfield++;
2045                     }
2046                 }
2047                 if ($havesubfield) {
2048                     push(@alternateholdingsinfo, \%holding);
2049                     $alternateholdingscount++;
2050                 }
2051             }
2052
2053             $oldbiblio->{'ALTERNATEHOLDINGS'} = \@alternateholdingsinfo;
2054             $oldbiblio->{'alternateholdings_count'} = $alternateholdingscount;
2055         }
2056
2057         $oldbiblio->{biblio_object} = Koha::Biblios->find( $oldbiblio->{biblionumber} );
2058
2059         push( @newresults, $oldbiblio );
2060     }
2061
2062     return @newresults;
2063 }
2064
2065 =head2 enabled_staff_search_views
2066
2067 %hash = enabled_staff_search_views()
2068
2069 This function returns a hash that contains three flags obtained from the system
2070 preferences, used to determine whether a particular staff search results view
2071 is enabled.
2072
2073 =over 2
2074
2075 =item C<Output arg:>
2076
2077     * $hash{can_view_MARC} is true only if the MARC view is enabled
2078     * $hash{can_view_ISBD} is true only if the ISBD view is enabled
2079     * $hash{can_view_labeledMARC} is true only if the Labeled MARC view is enabled
2080
2081 =item C<usage in the script:>
2082
2083 =back
2084
2085 $template->param ( C4::Search::enabled_staff_search_views );
2086
2087 =cut
2088
2089 sub enabled_staff_search_views
2090 {
2091         return (
2092                 can_view_MARC                   => C4::Context->preference('viewMARC'),                 # 1 if the staff search allows the MARC view
2093                 can_view_ISBD                   => C4::Context->preference('viewISBD'),                 # 1 if the staff search allows the ISBD view
2094                 can_view_labeledMARC    => C4::Context->preference('viewLabeledMARC'),  # 1 if the staff search allows the Labeled MARC view
2095         );
2096 }
2097
2098 =head2 z3950_search_args
2099
2100 $arrayref = z3950_search_args($matchpoints)
2101
2102 This function returns an array reference that contains the search parameters to be
2103 passed to the Z39.50 search script (z3950_search.pl). The array elements
2104 are hash refs whose keys are name and value, and whose values are the
2105 name of a search parameter, the value of that search parameter and the URL encoded
2106 value of that parameter.
2107
2108 The search parameter names are lccn, isbn, issn, title, author, dewey and subject.
2109
2110 The search parameter values are obtained from the bibliographic record whose
2111 data is in a hash reference in $matchpoints, as returned by Biblio::GetBiblioData().
2112
2113 If $matchpoints is a scalar, it is assumed to be an unnamed query descriptor, e.g.
2114 a general purpose search argument. In this case, the returned array contains only
2115 entry: the key is 'title' and the value is derived from $matchpoints.
2116
2117 If a search parameter value is undefined or empty, it is not included in the returned
2118 array.
2119
2120 The returned array reference may be passed directly to the template parameters.
2121
2122 =over 2
2123
2124 =item C<Output arg:>
2125
2126     * $array containing hash refs as described above
2127
2128 =item C<usage in the script:>
2129
2130 =back
2131
2132 $data = Biblio::GetBiblioData($bibno);
2133 $template->param ( MYLOOP => C4::Search::z3950_search_args($data) )
2134
2135 *OR*
2136
2137 $template->param ( MYLOOP => C4::Search::z3950_search_args($searchscalar) )
2138
2139 =cut
2140
2141 sub z3950_search_args {
2142     my $bibrec = shift;
2143
2144     my $isbn_string = ref( $bibrec ) ? $bibrec->{title} : $bibrec;
2145     my $isbn = Business::ISBN->new( $isbn_string );
2146
2147     if (defined $isbn && $isbn->is_valid)
2148     {
2149         if ( ref($bibrec) ) {
2150             $bibrec->{isbn} = $isbn_string;
2151             $bibrec->{title} = undef;
2152         } else {
2153             $bibrec = { isbn => $isbn_string };
2154         }
2155     }
2156     else {
2157         $bibrec = { title => $bibrec } if !ref $bibrec;
2158     }
2159     my $array = [];
2160     for my $field (qw/ lccn isbn issn title author dewey subject /)
2161     {
2162         push @$array, { name => $field, value => $bibrec->{$field} }
2163           if defined $bibrec->{$field};
2164     }
2165     return $array;
2166 }
2167
2168 =head2 GetDistinctValues($field);
2169
2170 C<$field> is a reference to the fields array
2171
2172 =cut
2173
2174 sub GetDistinctValues {
2175     my ($fieldname,$string)=@_;
2176     # returns a reference to a hash of references to branches...
2177     if ($fieldname=~/\./){
2178                         my ($table,$column)=split /\./, $fieldname;
2179                         my $dbh = C4::Context->dbh;
2180                         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 ");
2181                         $sth->execute;
2182                         my $elements=$sth->fetchall_arrayref({});
2183                         return $elements;
2184    }
2185    else {
2186                 $string||= qq("");
2187                 my @servers=qw<biblioserver authorityserver>;
2188                 my (@zconns,@results);
2189         for ( my $i = 0 ; $i < @servers ; $i++ ) {
2190                 $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
2191                         $results[$i] =
2192                       $zconns[$i]->scan(
2193                         ZOOM::Query::CCL2RPN->new( qq"$fieldname $string", $zconns[$i])
2194                       );
2195                 }
2196                 # The big moment: asynchronously retrieve results from all servers
2197                 my @elements;
2198         _ZOOM_event_loop(
2199             \@zconns,
2200             \@results,
2201             sub {
2202                 my ( $i, $size ) = @_;
2203                 for ( my $j = 0 ; $j < $size ; $j++ ) {
2204                     my %hashscan;
2205                     @hashscan{qw(value cnt)} =
2206                       $results[ $i - 1 ]->display_term($j);
2207                     push @elements, \%hashscan;
2208                 }
2209             }
2210         );
2211                 return \@elements;
2212    }
2213 }
2214
2215 =head2 _ZOOM_event_loop
2216
2217     _ZOOM_event_loop(\@zconns, \@results, sub {
2218         my ( $i, $size ) = @_;
2219         ....
2220     } );
2221
2222 Processes a ZOOM event loop and passes control to a closure for
2223 processing the results, and destroying the resultsets.
2224
2225 =cut
2226
2227 sub _ZOOM_event_loop {
2228     my ($zconns, $results, $callback) = @_;
2229     while ( ( my $i = ZOOM::event( $zconns ) ) != 0 ) {
2230         my $ev = $zconns->[ $i - 1 ]->last_event();
2231         if ( $ev == ZOOM::Event::ZEND ) {
2232             next unless $results->[ $i - 1 ];
2233             my $size = $results->[ $i - 1 ]->size();
2234             if ( $size > 0 ) {
2235                 $callback->($i, $size);
2236             }
2237         }
2238     }
2239
2240     foreach my $result (@$results) {
2241         $result->destroy();
2242     }
2243 }
2244
2245 =head2 new_record_from_zebra
2246
2247 Given raw data from a searchengine result set, return a MARC::Record object
2248
2249 This helper function is needed to take into account all the involved
2250 system preferences and configuration variables to properly create the
2251 MARC::Record object.
2252
2253 If we are using GRS-1, then the raw data we get from Zebra should be USMARC
2254 data. If we are using DOM, then it has to be MARCXML.
2255
2256 If we are using elasticsearch, it'll already be a MARC::Record and this
2257 function needs a new name.
2258
2259 =cut
2260
2261 sub new_record_from_zebra {
2262
2263     my $server   = shift;
2264     my $raw_data = shift;
2265     # Set the default indexing modes
2266     my $search_engine = C4::Context->preference("SearchEngine");
2267     if ($search_engine eq 'Elasticsearch') {
2268         return ref $raw_data eq 'MARC::Record' ? $raw_data : MARC::Record->new_from_xml( $raw_data, 'UTF-8' );
2269     }
2270     my $index_mode = ( $server eq 'biblioserver' )
2271                         ? C4::Context->config('zebra_bib_index_mode') // 'dom'
2272                         : C4::Context->config('zebra_auth_index_mode') // 'dom';
2273
2274     my $marc_record =  eval {
2275         if ( $index_mode eq 'dom' ) {
2276             MARC::Record->new_from_xml( $raw_data, 'UTF-8' );
2277         } else {
2278             MARC::Record->new_from_usmarc( $raw_data );
2279         }
2280     };
2281
2282     if ($@) {
2283         return;
2284     } else {
2285         return $marc_record;
2286     }
2287
2288 }
2289
2290 END { }    # module clean-up code here (global destructor)
2291
2292 1;
2293 __END__
2294
2295 =head1 AUTHOR
2296
2297 Koha Development Team <http://koha-community.org/>
2298
2299 =cut