Bug 29059: Keep non-repeatable attribute from patron to preserve when merging
[koha.git] / Koha / Z3950Responder / GenericSession.pm
1 package Koha::Z3950Responder::GenericSession;
2
3 # Copyright The National Library of Finland 2018
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use base qw( Koha::Z3950Responder::Session );
23
24 use Koha::Logger;
25 use Koha::SearchEngine::Search;
26 use Koha::SearchEngine::QueryBuilder;
27 use Koha::Z3950Responder::RPN;
28
29 =head1 NAME
30
31 Koha::Z3950Responder::genericSession
32
33 =head1 SYNOPSIS
34
35 Backend-agnostic session class that uses C<Koha::Session> as the base class. Utilizes
36 C<Koha::SearchEngine> for the actual functionality.
37
38 =head2 INSTANCE METHODS
39
40 =head3 start_search
41
42     my ($resultset, $hits) = $self->start_search( $args, $self->{server}->{num_to_prefetch} );
43
44 Perform a search using C<Koha::SearchEngine>'s QueryBuilder and Search.
45
46 =cut
47
48 sub start_search {
49     my ( $self, $args, $num_to_prefetch ) = @_;
50
51     if (!defined $self->{'attribute_mappings'}) {
52         require YAML::XS;
53         $self->{'attribute_mappings'} = YAML::XS::LoadFile($self->{server}->{config_dir} . 'attribute_mappings.yaml');
54     }
55
56     my $database = $args->{DATABASES}->[0];
57     my $builder = Koha::SearchEngine::QueryBuilder->new({ index => $database });
58     my $searcher = Koha::SearchEngine::Search->new({ index => $database });
59
60     my $built_query;
61     my $query = $args->{RPN}->{'query'}->to_koha($self->{'attribute_mappings'}->{$database});
62     $self->log_debug("    parsed search: $query");
63     my @operands = $query;
64     (undef, $built_query) = $builder->build_query_compat( undef, \@operands, undef, undef, undef, 0);
65
66     my ($error, $marcresults, $hits ) = $searcher->simple_search_compat($built_query, 0, $num_to_prefetch);
67     if (defined $error) {
68         $self->set_error($args, $self->ERR_SEARCH_FAILED, 'Search failed');
69         return;
70     }
71
72     my $resultset = {
73         query => $built_query,
74         database => $database,
75         cached_offset => 0,
76         cached_results => $marcresults,
77         hits => $hits
78     };
79
80     return ($resultset, $hits);
81 }
82
83 =head3 fetch_record
84
85     my $record = $self->fetch_record( $resultset, $args, $offset, $server->{num_to_prefetch} );
86
87 Fetch a record from SearchEngine. Caches records in session to avoid too many fetches.
88
89 =cut
90
91 sub fetch_record {
92     my ( $self, $resultset, $args, $index, $num_to_prefetch ) = @_;
93
94     # Fetch more records if necessary
95     my $offset = $args->{OFFSET} - 1;
96     if ($offset < $resultset->{cached_offset} || $offset >= $resultset->{cached_offset} + $num_to_prefetch) {
97         $self->log_debug("    fetch uncached, fetching $num_to_prefetch records starting at $offset");
98         my $searcher = Koha::SearchEngine::Search->new({ index => $resultset->{'database'} });
99         my ($error, $marcresults, $num_hits ) = $searcher->simple_search_compat($resultset->{'query'}, $offset, $num_to_prefetch);
100         if (defined $error) {
101             $self->set_error($args, $self->ERR_TEMPORARY_ERROR, 'Fetch failed');
102             return;
103         }
104
105         $resultset->{cached_offset} = $offset;
106         $resultset->{cached_results} = $marcresults;
107     }
108     return $resultset->{cached_results}[$offset - $resultset->{cached_offset}];
109 }
110
111 1;