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>.
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
}];