1 package Koha::Z3950Responder;
3 # Copyright ByWater Solutions 2016
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 C4::Biblio qw( GetMarcFromKohaField );
23 use C4::Koha qw( GetAuthorisedValues );
25 use Net::Z3950::SimpleServer;
29 Koha::Z3950Responder - Main class for interfacing with Net::Z3950::SimpleServer
33 use Koha::Z3950Responder;
35 my $z = Koha::Z3950Responder->new( {
36 add_item_status_subfield => 1,
37 add_status_multi_subfield => 1,
39 num_to_prefetch => 20,
40 config_dir => '/home/koha/etc',
48 A daemon class that interfaces with Net::Z3950::SimpleServer to provider Z39.50/SRU
49 service. Uses a Session class for the actual functionality.
53 =head2 INSTANCE METHODS
58 add_item_status_subfield => 1
64 my ( $class, $config ) = @_;
66 my ($item_tag, $itemnumber_subfield) = GetMarcFromKohaField( "items.itemnumber" );
68 # We hardcode the strings for English so SOMETHING will work if the authorized value doesn't exist.
69 my $status_strings = {
70 AVAILABLE => 'Available',
71 CHECKED_OUT => 'Checked Out',
73 NOT_FOR_LOAN => 'Not for Loan',
75 WITHDRAWN => 'Withdrawn',
76 IN_TRANSIT => 'In Transit',
80 foreach my $val ( @{ GetAuthorisedValues( 'Z3950_STATUS' ) } ) {
81 $status_strings->{ $val->{authorised_value} } = $val->{lib};
86 item_tag => $item_tag,
87 itemnumber_subfield => $itemnumber_subfield,
88 status_strings => $status_strings,
91 # If requested, turn on debugging.
92 if ( $self->{debug} ) {
93 # Turn on single-process mode.
94 unshift @{ $self->{yaz_options} }, '-S';
96 # Turn off Yaz's built-in logging apart from fatal errors (can be turned back on if desired).
97 unshift @{ $self->{yaz_options} }, '-v', 'none,fatal';
100 # Set main config for SRU support and working directory
101 if ( $self->{config_dir} ) {
102 unshift @{ $self->{yaz_options} }, '-f', $self->{config_dir} . 'config.xml';
103 unshift @{ $self->{yaz_options} }, '-w', $self->{config_dir};
106 # Set num to prefetch if not passed
107 $self->{num_to_prefetch} //= 20;
109 $self->{server} = Net::Z3950::SimpleServer->new(
110 INIT => sub { $self->init_handler(@_) },
111 SEARCH => sub { $self->search_handler(@_) },
112 FETCH => sub { $self->fetch_handler(@_) },
113 CLOSE => sub { $self->close_handler(@_) },
116 return bless( $self, $class );
123 Start the daemon and begin serving requests. Does not return unless initialization fails or a
131 $self->{server}->launch_server( 'Koha::Z3950Responder', @{ $self->{yaz_options} } )
136 These methods are SimpleServer callbacks bound to this Z3950Responder object.
137 It's worth noting that these callbacks don't return anything; they both
138 receive and return data in the $args hashref.
142 Callback that is called when a new connection is initialized
147 # Called when the client first connects.
148 my ( $self, $args ) = @_;
150 # This holds all of the per-connection state.
152 if (C4::Context->preference('SearchEngine') eq 'Zebra') {
153 use Koha::Z3950Responder::ZebraSession;
154 $session = Koha::Z3950Responder::ZebraSession->new({
156 peer => $args->{PEER_NAME},
159 use Koha::Z3950Responder::GenericSession;
160 $session = Koha::Z3950Responder::GenericSession->new({
162 peer => $args->{PEER_NAME}
166 $args->{HANDLE} = $session;
168 $args->{IMP_NAME} = "Koha";
169 $args->{IMP_VER} = Koha::version;
172 =head3 search_handler
174 Callback that is called when a new search is performed
179 my ( $self, $args ) = @_;
181 $args->{HANDLE}->search_handler($args);
186 Callback that is called when records are requested
191 my ( $self, $args ) = @_;
193 $args->{HANDLE}->fetch_handler( $args );
198 Callback that is called when a session is terminated
203 my ( $self, $args ) = @_;
205 $args->{HANDLE}->close_handler( $args );