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