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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use base qw( Koha::Z3950Responder::Session );
27 use Koha::SearchEngine::Search;
28 use Koha::SearchEngine::QueryBuilder;
29 use Koha::Z3950Responder::RPN;
33 Koha::Z3950Responder::genericSession
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
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.
51 my ( $self, $args, $num_to_prefetch ) = @_;
53 if (!defined $self->{'attribute_mappings'}) {
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 });
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);
70 $self->set_error($args, $self->ERR_SEARCH_FAILED, 'Search failed');
75 query => $built_query,
76 database => $database,
78 cached_results => $marcresults,
82 return ($resultset, $hits);
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.
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');
107 $resultset->{cached_offset} = $offset;
108 $resultset->{cached_results} = $marcresults;
110 return $resultset->{cached_results}[$offset - $resultset->{cached_offset}];