From bd2347fbf85083fe6204dcaaf1cc98826219eaed Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 20 May 2020 11:12:12 +0000 Subject: [PATCH] Bug 25549: Remove plugin methods for broken plugins To test: 1 - Enable plugins in the koha-conf 2 - Install the kitchen sink plugin 3 - Your staff client should be orange now :-) 4 - edit the plugin module /var/lib/koha/kohadev/plugins/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm 5 - Add a line to break compilation, like: this won't compile 6 - Restart all 7 - Your koha is now broken 8 - kshell 9 - perl misc/devel/install_plugins.pl 10 - Restart all 11 - Koha remains broken 12 - Apply patch 13 - kshell 14 - perl misc/devel/install_plugins.pl 15 - Koha now works! 16 - Koha is not orange because the plugin methods are removed Signed-off-by: Martin Renvoize Signed-off-by: Nick Clemens Signed-off-by: Katrin Fischer Signed-off-by: Jonathan Druart (cherry picked from commit 0eae966d257fa2ecc6a6506e2515691e092a701c) Signed-off-by: Lucas Gass --- Koha/Plugins.pm | 40 +++++++++++++++++++---------------- misc/devel/install_plugins.pl | 10 ++++++--- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index 13a30d96a4..6a1b7b392b 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -89,24 +89,27 @@ sub GetPlugins { # Loop through all plugins that implement at least a method while ( my $plugin_class = $plugin_classes->next ) { - load $plugin_class; - my $plugin = $plugin_class->new({ - enable_plugins => $self->{'enable_plugins'} - # loads even if plugins are disabled - # FIXME: is this for testing without bothering to mock config? - }); - - next unless $plugin->is_enabled or - defined($params->{all}) && $params->{all}; - - # filter the plugin out by metadata - my $plugin_metadata = $plugin->get_metadata; - next - if $plugin_metadata - and %$req_metadata - and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata; + if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) { + my $plugin = $plugin_class->new({ + enable_plugins => $self->{'enable_plugins'} + # loads even if plugins are disabled + # FIXME: is this for testing without bothering to mock config? + }); + + next unless $plugin->is_enabled or + defined($params->{all}) && $params->{all}; + + # filter the plugin out by metadata + my $plugin_metadata = $plugin->get_metadata; + next + if $plugin_metadata + and %$req_metadata + and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata; - push @plugins, $plugin; + push @plugins, $plugin; + } elsif ( defined($params->{errors}) && $params->{errors} ){ + push @plugins, { error => 'cannot_load', name => $plugin_class }; + } } @@ -132,13 +135,14 @@ sub InstallPlugins { my @plugin_classes = $self->plugins(); my @plugins; + # If we can reload the plugin we will add the methods back, if not they should be removed + Koha::Plugins::Methods->search()->delete(); foreach my $plugin_class (@plugin_classes) { if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) { next unless $plugin_class->isa('Koha::Plugins::Base'); my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} }); - Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete(); foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) { Koha::Plugins::Method->new( diff --git a/misc/devel/install_plugins.pl b/misc/devel/install_plugins.pl index 073ddbbb1c..0177d733b4 100755 --- a/misc/devel/install_plugins.pl +++ b/misc/devel/install_plugins.pl @@ -37,14 +37,18 @@ unless ( C4::Context->config("enable_plugins") ) { exit 1; } - my @existing_plugins = Koha::Plugins->new()->GetPlugins({ all => 1, + errors => 1, }); my $existing_plugins; for my $existing_plugin (@existing_plugins) { - $existing_plugins->{ $existing_plugin->{metadata}->{name} } = - $existing_plugin->{metadata}->{version}; + if( defined $existing_plugin->{error} ){ + print "Could not load: ".$existing_plugin->{name}." any associated method will be removed\n"; + } else { + $existing_plugins->{ $existing_plugin->{metadata}->{name} } = + $existing_plugin->{metadata}->{version}; + } } my @installed_plugins = Koha::Plugins->new()->InstallPlugins(); -- 2.39.5