Bug 24083: (follow-up) Fix params to AddRenewal
[koha.git] / Koha / Z3950Responder.pm
1 package Koha::Z3950Responder;
2
3 # Copyright ByWater Solutions 2016
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use C4::Biblio qw( GetMarcFromKohaField );
23 use C4::Koha qw( GetAuthorisedValues );
24
25 use Net::Z3950::SimpleServer;
26
27 =head1 NAME
28
29 Koha::Z3950Responder - Main class for interfacing with Net::Z3950::SimpleServer
30
31 =head1 SYNOPSIS
32
33     use Koha::Z3950Responder;
34
35     my $z = Koha::Z3950Responder->new( {
36         add_item_status_subfield => 1,
37         add_status_multi_subfield => 1,
38         debug => 0,
39         num_to_prefetch => 20,
40         config_dir => '/home/koha/etc',
41         yaz_options => [ ],
42     } );
43
44     $z->start();
45
46 =head1 DESCRIPTION
47
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.
50
51 =head1 METHODS
52
53 =head2 INSTANCE METHODS
54
55 =head3 new
56
57     $self->new({
58         add_item_status_subfield => 1
59     });
60
61 =cut
62
63 sub new {
64     my ( $class, $config ) = @_;
65
66     my ($item_tag, $itemnumber_subfield) = GetMarcFromKohaField( "items.itemnumber" );
67
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',
72         LOST => 'Lost',
73         NOT_FOR_LOAN => 'Not for Loan',
74         DAMAGED => 'Damaged',
75         WITHDRAWN => 'Withdrawn',
76         IN_TRANSIT => 'In Transit',
77         ON_HOLD => 'On Hold',
78     };
79
80     foreach my $val ( @{ GetAuthorisedValues( 'Z3950_STATUS' ) } ) {
81         $status_strings->{ $val->{authorised_value} } = $val->{lib};
82     }
83
84     my $self = {
85         %$config,
86         item_tag => $item_tag,
87         itemnumber_subfield => $itemnumber_subfield,
88         status_strings => $status_strings,
89     };
90
91     # If requested, turn on debugging.
92     if ( $self->{debug} ) {
93         # Turn on single-process mode.
94         unshift @{ $self->{yaz_options} }, '-S';
95     } else {
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';
98     }
99
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};
104     }
105
106     # Set num to prefetch if not passed
107     $self->{num_to_prefetch} //= 20;
108
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(@_) },
114     );
115
116     return bless( $self, $class );
117 }
118
119 =head3 start
120
121     $z->start();
122
123 Start the daemon and begin serving requests. Does not return unless initialization fails or a
124 fatal error occurs.
125
126 =cut
127
128 sub start {
129     my ( $self ) = @_;
130
131     $self->{server}->launch_server( 'Koha::Z3950Responder', @{ $self->{yaz_options} } )
132 }
133
134 =head2 CALLBACKS
135
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.
139
140 =head3 init_handler
141
142 Callback that is called when a new connection is initialized
143
144 =cut
145
146 sub init_handler {
147     # Called when the client first connects.
148     my ( $self, $args ) = @_;
149
150     # This holds all of the per-connection state.
151     my $session;
152     if (C4::Context->preference('SearchEngine') eq 'Zebra') {
153         use Koha::Z3950Responder::ZebraSession;
154         $session = Koha::Z3950Responder::ZebraSession->new({
155             server => $self,
156             peer => $args->{PEER_NAME},
157         });
158     } else {
159         use Koha::Z3950Responder::GenericSession;
160         $session = Koha::Z3950Responder::GenericSession->new({
161             server => $self,
162             peer => $args->{PEER_NAME}
163         });
164     }
165
166     $args->{HANDLE} = $session;
167
168     $args->{IMP_NAME} = "Koha";
169     $args->{IMP_VER} = Koha::version;
170 }
171
172 =head3 search_handler
173
174 Callback that is called when a new search is performed
175
176 =cut
177
178 sub search_handler {
179     my ( $self, $args ) = @_;
180
181     $args->{HANDLE}->search_handler($args);
182 }
183
184 =head3 fetch_handler
185
186 Callback that is called when records are requested
187
188 =cut
189
190 sub fetch_handler {
191     my ( $self, $args ) = @_;
192
193     $args->{HANDLE}->fetch_handler( $args );
194 }
195
196 =head3 close_handler
197
198 Callback that is called when a session is terminated
199
200 =cut
201
202 sub close_handler {
203     my ( $self, $args ) = @_;
204
205     $args->{HANDLE}->close_handler( $args );
206 }
207
208 1;