From 798797bdf68ceda059f8063861cb5c8650b97f5a Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 19 Dec 2023 09:22:31 -0500 Subject: [PATCH] Bug 35603: Add plugin feature to allow plugins to warn if any external library modules needed are missing Some plugins require extra modules not included with Koha, but there is no simple way to let a user know when installing a plugin. This patch allows a new metadata to be added that is a list of hashes with the key module. The hash is used for future expansion to allow new critera to be added such as minimum and maximum module version. Test Plan: 1) Install any plugin 2) In the plugin metadata, add the following key: libs => [ { module => 'Business::Barcode::EAN13' } ], 3) Apply this patch 4) Restart all the things! 5) Browse to plugins-home.pl, note the error listing the missing module 6) Install Business::Barcode::EAN13 from cpan 7) Reload the page 8) Note the error is gone! Signed-off-by: Martin Renvoize --- Koha/Plugins.pm | 15 ++++++++++++++- Koha/Plugins/Base.pm | 2 +- .../prog/en/modules/plugins/plugins-home.tt | 16 ++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index 2b4c07956e6..6031e5705f0 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -22,7 +22,7 @@ use Modern::Perl; use Array::Utils qw( array_minus ); use Class::Inspector; use List::MoreUtils qw( any ); -use Module::Load::Conditional qw( can_load ); +use Module::Load::Conditional qw( can_load check_install ); use Module::Load; use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/; use Try::Tiny; @@ -243,6 +243,19 @@ sub GetPlugins { next unless $plugin->is_enabled or defined($params->{all}) && $params->{all}; + # Check metadata for required perl modules + my $libs = $plugin->get_metadata->{libs}; + my @missing_libs; + foreach my $lib (@$libs) { + unless ( check_install( module => $lib->{module} ) ) { + push( @missing_libs, $lib ); + } + } + if (@missing_libs) { + push @plugins, { error => 'missing_libs', name => $plugin_class, libs => \@missing_libs }; + next; + } + # filter the plugin out by metadata my $plugin_metadata = $plugin->get_metadata; next diff --git a/Koha/Plugins/Base.pm b/Koha/Plugins/Base.pm index 8e547fba0d6..1b3d0f0c9ef 100644 --- a/Koha/Plugins/Base.pm +++ b/Koha/Plugins/Base.pm @@ -196,7 +196,7 @@ sub get_metadata { #FIXME: Why another encoding issue? For metadata containing non latin characters. my $metadata = $self->{metadata}; - defined($metadata->{$_}) && utf8::decode($metadata->{$_}) for keys %$metadata; + defined($metadata->{$_}) && !ref $metadata->{$_} && utf8::decode($metadata->{$_}) for keys %$metadata; return $metadata; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt index b146bf2d2aa..79f3ae1d0b2 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt @@ -155,10 +155,22 @@ ERRORS [% IF ( CAN_user_plugins_configure || CAN_user_plugins_manage || CAN_user_plugins_report || CAN_user_plugins_tool ) %] - Error found whilst attempting to load plugin + [% ELSE %] - Error found whilst attempting to load plugin + [% END %] + + [% IF plugin.error == 'missing_libs' %] + This plugin requires the following missing modules to be installed: + + [% ELSE %] + Error found whilst attempting to load plugin + [% END %] + [% ELSE %] -- 2.30.2