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>.
22 use base qw( Koha::Z3950Responder::Session );
25 use Koha::SearchEngine::Search;
26 use Koha::SearchEngine::QueryBuilder;
27 use Koha::Z3950Responder::RPN;
31 Koha::Z3950Responder::genericSession
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
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.
49 my ( $self, $args, $num_to_prefetch ) = @_;
51 if (!defined $self->{'attribute_mappings'}) {
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 });
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);
68 $self->set_error($args, $self->ERR_SEARCH_FAILED, 'Search failed');
73 query => $built_query,
74 database => $database,
76 cached_results => $marcresults,
80 return ($resultset, $hits);
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.
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');
105 $resultset->{cached_offset} = $offset;
106 $resultset->{cached_results} = $marcresults;
108 return $resultset->{cached_results}[$offset - $resultset->{cached_offset}];