Bug 32997: Add REST API endpoint to list authorised values for multiple given categories
[srvgit] / Koha / Z3950Responder.pm
index c7a7bf2..3e1f1d2 100644 (file)
@@ -1,34 +1,67 @@
-#!/usr/bin/perl
-
 package Koha::Z3950Responder;
 
 # Copyright ByWater Solutions 2016
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
 use C4::Biblio qw( GetMarcFromKohaField );
 use C4::Koha qw( GetAuthorisedValues );
 
-use Koha;
-use Koha::Z3950Responder::Session;
+use Koha::Caches;
 
 use Net::Z3950::SimpleServer;
 
+=head1 NAME
+
+Koha::Z3950Responder - Main class for interfacing with Net::Z3950::SimpleServer
+
+=head1 SYNOPSIS
+
+    use Koha::Z3950Responder;
+
+    my $z = Koha::Z3950Responder->new( {
+        add_item_status_subfield => 1,
+        add_status_multi_subfield => 1,
+        debug => 0,
+        num_to_prefetch => 20,
+        config_dir => '/home/koha/etc',
+        yaz_options => [ ],
+    } );
+
+    $z->start();
+
+=head1 DESCRIPTION
+
+A daemon class that interfaces with Net::Z3950::SimpleServer to provider Z39.50/SRU
+service. Uses a Session class for the actual functionality.
+
+=head1 METHODS
+
+=head2 INSTANCE METHODS
+
+=head3 new
+
+    $self->new({
+        add_item_status_subfield => 1
+    });
+
+=cut
+
 sub new {
     my ( $class, $config ) = @_;
 
@@ -66,10 +99,18 @@ sub new {
         unshift @{ $self->{yaz_options} }, '-v', 'none,fatal';
     }
 
+    # Set main config for SRU support and working directory
+    if ( $self->{config_dir} ) {
+        unshift @{ $self->{yaz_options} }, '-f', $self->{config_dir} . 'config.xml';
+        unshift @{ $self->{yaz_options} }, '-w', $self->{config_dir};
+    }
+
+    # Set num to prefetch if not passed
+    $self->{num_to_prefetch} //= 20;
+
     $self->{server} = Net::Z3950::SimpleServer->new(
         INIT => sub { $self->init_handler(@_) },
         SEARCH => sub { $self->search_handler(@_) },
-        PRESENT => sub { $self->present_handler(@_) },
         FETCH => sub { $self->fetch_handler(@_) },
         CLOSE => sub { $self->close_handler(@_) },
     );
@@ -77,25 +118,52 @@ sub new {
     return bless( $self, $class );
 }
 
+=head3 start
+
+    $z->start();
+
+Start the daemon and begin serving requests. Does not return unless initialization fails or a
+fatal error occurs.
+
+=cut
+
 sub start {
     my ( $self ) = @_;
 
     $self->{server}->launch_server( 'Koha::Z3950Responder', @{ $self->{yaz_options} } )
 }
 
-# The rest of these methods are SimpleServer callbacks bound to this Z3950Responder object. It's
-# worth noting that these callbacks don't return anything; they both receive and return data in the
-# $args hashref.
+=head2 CALLBACKS
+
+These methods are SimpleServer callbacks bound to this Z3950Responder object.
+It's worth noting that these callbacks don't return anything; they both
+receive and return data in the $args hashref.
+
+=head3 init_handler
+
+Callback that is called when a new connection is initialized
+
+=cut
 
 sub init_handler {
     # Called when the client first connects.
     my ( $self, $args ) = @_;
 
     # This holds all of the per-connection state.
-    my $session = Koha::Z3950Responder::Session->new({
-        server => $self,
-        peer => $args->{PEER_NAME},
-    });
+    my $session;
+    if (C4::Context->preference('SearchEngine') eq 'Zebra') {
+        use Koha::Z3950Responder::ZebraSession;
+        $session = Koha::Z3950Responder::ZebraSession->new({
+            server => $self,
+            peer => $args->{PEER_NAME},
+        });
+    } else {
+        use Koha::Z3950Responder::GenericSession;
+        $session = Koha::Z3950Responder::GenericSession->new({
+            server => $self,
+            peer => $args->{PEER_NAME}
+        });
+    }
 
     $args->{HANDLE} = $session;
 
@@ -103,27 +171,42 @@ sub init_handler {
     $args->{IMP_VER} = Koha::version;
 }
 
+=head3 search_handler
+
+Callback that is called when a new search is performed
+
+=cut
+
 sub search_handler {
-    # Called when search is first sent.
     my ( $self, $args ) = @_;
 
+    my $SearchEngine = C4::Context->preference('SearchEngine');
+    # Flushing L1 to make sure the search will be processed using the correct data
+    Koha::Caches->flush_L1_caches();
+    $self->init_handler($args)
+        if $SearchEngine ne C4::Context->preference('SearchEngine');
+
     $args->{HANDLE}->search_handler($args);
 }
 
-sub present_handler {
-    # Called when a set of records is requested.
-    my ( $self, $args ) = @_;
+=head3 fetch_handler
 
-    $args->{HANDLE}->present_handler($args);
-}
+Callback that is called when records are requested
+
+=cut
 
 sub fetch_handler {
-    # Called when a given record is requested.
     my ( $self, $args ) = @_;
 
     $args->{HANDLE}->fetch_handler( $args );
 }
 
+=head3 close_handler
+
+Callback that is called when a session is terminated
+
+=cut
+
 sub close_handler {
     my ( $self, $args ) = @_;