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