Bug 25067: (QA follow-up) Add +x on test files
[koha.git] / Koha / Z3950Responder / GenericSession.pm
blob103c627c53feccb72312a80be2ffc57f88f86385
1 package Koha::Z3950Responder::GenericSession;
3 # Copyright The National Library of Finland 2018
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use base qw( Koha::Z3950Responder::Session );
24 use Koha::Logger;
25 use Koha::SearchEngine::Search;
26 use Koha::SearchEngine::QueryBuilder;
27 use Koha::Z3950Responder::RPN;
29 =head1 NAME
31 Koha::Z3950Responder::genericSession
33 =head1 SYNOPSIS
35 Backend-agnostic session class that uses C<Koha::Session> as the base class. Utilizes
36 C<Koha::SearchEngine> for the actual functionality.
38 =head2 INSTANCE METHODS
40 =head3 start_search
42 my ($resultset, $hits) = $self->start_search( $args, $self->{server}->{num_to_prefetch} );
44 Perform a search using C<Koha::SearchEngine>'s QueryBuilder and Search.
46 =cut
48 sub start_search {
49 my ( $self, $args, $num_to_prefetch ) = @_;
51 if (!defined $self->{'attribute_mappings'}) {
52 require YAML;
53 $self->{'attribute_mappings'} = YAML::LoadFile($self->{server}->{config_dir} . 'attribute_mappings.yaml');
56 my $database = $args->{DATABASES}->[0];
57 my $builder = Koha::SearchEngine::QueryBuilder->new({ index => $database });
58 my $searcher = Koha::SearchEngine::Search->new({ index => $database });
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);
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;
72 my $resultset = {
73 query => $built_query,
74 database => $database,
75 cached_offset => 0,
76 cached_results => $marcresults,
77 hits => $hits
80 return ($resultset, $hits);
83 =head3 fetch_record
85 my $record = $self->fetch_record( $resultset, $args, $offset, $server->{num_to_prefetch} );
87 Fetch a record from SearchEngine. Caches records in session to avoid too many fetches.
89 =cut
91 sub fetch_record {
92 my ( $self, $resultset, $args, $index, $num_to_prefetch ) = @_;
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;
105 $resultset->{cached_offset} = $offset;
106 $resultset->{cached_results} = $marcresults;
108 return $resultset->{cached_results}[$offset - $resultset->{cached_offset}];