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