Bug 25549: Remove plugin methods for broken plugins
authorNick Clemens <nick@bywatersolutions.com>
Wed, 20 May 2020 11:12:12 +0000 (11:12 +0000)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 22 Oct 2020 08:04:30 +0000 (10:04 +0200)
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 <martin.renvoize@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/Plugins.pm
misc/devel/install_plugins.pl

index 092c5e0..77758fb 100644 (file)
@@ -118,24 +118,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 };
+        }
 
     }
 
@@ -161,13 +164,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(
index 073ddbb..0177d73 100755 (executable)
@@ -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();