From 37935c7284bd8424f3ad57bdcc5c79da73daa495 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 30 Oct 2023 11:14:04 -0100 Subject: [PATCH] Bug 19605: Add support for ILL backends as plugins This is designed to maintain existing functionality of loading ILL backends through the backend_dir config (default Koha/Illbackends/). A check for a plugin of a given backend name takes precedence over a backend of the same name loaded the old way through backend_dir, this means that if this happens, the backend plugin is used and NOT the backend present in backend_dir. Old backend_dir backends AND new backend plugins coexist. Test plan, k-t-d: 1) Enable ILLModule and install FreeForm, run: bash <(curl -s https://raw.githubusercontent.com/ammopt/koha-ill-dev/master/start-ill-dev.sh) 2) Install the plugin ILL backend example .kpz located at: https://github.com/ammopt/koha-ill-backend-plugin/releases/tag/1.0.0 3) koha-plack --restart kohadev 4) Visit ILL requests: /cgi-bin/koha/ill/ill-requests.pl 5) Click "+ New ILL request". Notice it lists 'FreeForm' and 'PluginBackend' 6) Click 'PluginBackend'. Enter a '123' in pubmedid, '42' in cardnumber and pick a library. Click 'Marke request' 7) Notice the request is created successfully. 8) Visit plugins: /cgi-bin/koha/plugins/plugins-home.pl 9) Click "View plugin by class". Pick "ill backend plugins". Notice the correct plugin is listed. 10) Click "configure" on the ILL backend plugin. Notice it's a normal plugin configuration page Signed-off-by: David Nind Signed-off-by: Tomas Cohen Arazi Signed-off-by: Pedro Amorim Signed-off-by: Katrin Fischer --- Koha/Illrequest.pm | 88 +++++++++++++++++++++++++++++++++------ Koha/Illrequest/Config.pm | 37 +++++++++++++++- 2 files changed, 112 insertions(+), 13 deletions(-) diff --git a/Koha/Illrequest.pm b/Koha/Illrequest.pm index 3d0214f055..db821af6df 100644 --- a/Koha/Illrequest.pm +++ b/Koha/Illrequest.pm @@ -397,6 +397,29 @@ sub status { } } +=head3 get_backend_plugin + + my $backend_plugin = $self->get_backend_plugin($backend_name); + +Returns the installed I corresponding to the given backend_id + +=cut + +sub get_backend_plugin { + my ( $self, $backend_id ) = @_; + + my @backend_plugins = Koha::Plugins->new()->GetPlugins( + { + method => 'ill_backend', + metadata => { name => $backend_id }, + all => 1, + errors => 1 + } + ); + + return $backend_plugins[0]; +} + =head3 load_backend Require "Base.pm" from the relevant ILL backend. @@ -406,8 +429,6 @@ Require "Base.pm" from the relevant ILL backend. sub load_backend { my ( $self, $backend_id ) = @_; - my @raw = qw/Koha Illbackends/; # Base Path - my $backend_name = $backend_id || $self->backend; unless ( defined $backend_name && $backend_name ne '' ) { @@ -415,13 +436,33 @@ sub load_backend { "An invalid backend ID was requested ('')"); } - my $location = join "/", @raw, $backend_name, "Base.pm"; # File to load - my $backend_class = join "::", @raw, $backend_name, "Base"; # Package name - require $location; - $self->{_my_backend} = $backend_class->new({ - config => $self->_config, - logger => Koha::Illrequest::Logger->new - }); + my $backend_plugin = $self->get_backend_plugin($backend_name); + if ($backend_plugin) { + + # New way of loading backends: Through plugins + my $backend_plugin_class = $backend_plugin->{class}; + + $self->{_my_backend} = $backend_plugin_class->new_backend( + { + config => $self->_config, + logger => Koha::Illrequest::Logger->new + } + ); + } elsif ($backend_name) { + + # Old way of loading backends: Through backend_dir config + my @raw = qw/Koha Illbackends/; # Base Path + my $location = join "/", @raw, $backend_name, "Base.pm"; # File to load + my $backend_class = join "::", @raw, $backend_name, "Base"; # Package name + require $location; + $self->{_my_backend} = $backend_class->new( + { + config => $self->_config, + logger => Koha::Illrequest::Logger->new + } + ); + } + return $self; } @@ -507,7 +548,16 @@ sub _config { sub metadata { my ( $self ) = @_; - return $self->_backend->metadata($self); + + if ( $self->_backend->can('metadata') ) { + + # Old way of loading backends: Previous expected method was 'metadata' + return $self->_backend->metadata($self); + } else { + + # New way of loading backends (as plugins): New expected method is 'backend_metadata' + return $self->_backend->backend_metadata($self); + } } =head3 _core_status_graph @@ -1042,8 +1092,22 @@ sub expand_template { my ( $self, $params ) = @_; my $backend = $self->_backend->name; # Generate path to file to load - my $backend_dir = $self->_config->backend_dir; - my $backend_tmpl = join "/", $backend_dir, $backend; + my $backend_dir; + my $backend_tmpl; + + my $backend_plugin = $self->get_backend_plugin( $self->_backend->name ); + if ($backend_plugin) { + + # New way of loading backends: Through plugins + $backend_dir = $backend_plugin->bundle_path; + $backend_tmpl = $backend_dir; + } else { + + # Old way of loading backends: Through backend_dir config + $backend_dir = $self->_config->backend_dir; + $backend_tmpl = join "/", $backend_dir, $backend; + } + my $intra_tmpl = join "/", $backend_tmpl, "intra-includes", ( $params->{method}//q{} ) . ".inc"; my $opac_tmpl = join "/", $backend_tmpl, "opac-includes", diff --git a/Koha/Illrequest/Config.pm b/Koha/Illrequest/Config.pm index fa035e1b75..2fd9eb5251 100644 --- a/Koha/Illrequest/Config.pm +++ b/Koha/Illrequest/Config.pm @@ -23,6 +23,8 @@ use File::Basename qw( basename ); use C4::Context; +use List::MoreUtils qw( uniq ); + =head1 NAME Koha::Illrequest::Config - Koha ILL Configuration Object @@ -102,6 +104,28 @@ sub backend_dir { return $self->{configuration}->{backend_directory}; } +=head3 get_installed_backend_plugins + + $backends = $config->get_installed_backend_plugins; + +Returns a list of all installed ILL backend plugins + +=cut + +sub get_installed_backend_plugins { + my ( $self, $reduce ) = @_; + + my @backend_plugins = Koha::Plugins->new()->GetPlugins( + { + method => 'ill_backend', + all => 1, + errors => 1 + } + ); + + return map { $_->{metadata}->{name} } @backend_plugins; +} + =head3 available_backends $backends = $config->available_backends; @@ -114,12 +138,23 @@ will filter those backends down to only those present in the list. sub available_backends { my ( $self, $reduce ) = @_; + + # New way of loading backends: Through plugins + my @backend_plugins_names = $self->get_installed_backend_plugins; + + # Old way of loading backends: Through backend_dir config my $backend_dir = $self->backend_dir; my @backends = (); @backends = glob "$backend_dir/*" if ( $backend_dir ); @backends = map { basename($_) } @backends; @backends = grep { $_ =~ /$reduce/ } @backends if $reduce; - return \@backends; + + # Return unique list of backend names in the event that the same backend is + # installed as a plugin AND as the old way through backend_dir + my @all_backends = ( @backends, @backend_plugins_names ); + my @all_uniq_backends = uniq(@all_backends); + + return \@all_uniq_backends; } =head3 has_branch -- 2.39.5