From 186fc5efc93129c5895f83065a0946cf1e92017e Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 25 May 2016 13:37:49 +0200 Subject: [PATCH] Bug 16586: Koha Plugins: Limit results of GetPlugins by metadata Content-Type: text/plain; charset=utf-8 It would be helpful if we could find a plugin based on some metadata tag as returned by the plugin. This extends the use of GetPlugins that already supports searching on method. GetPlugins is used in: admin/edi_accounts.pl, plugins/plugins-home.pl and tools/stage-marc-import.pl. The changes in these three scripts are minimal and just related to parameter passing. Test t/db_dependent/Plugins.t includes another test for GetPlugins. In this regard a metadata tag has been added to t/Koha/Plugins/Test.pm. NOTE: This adjustment will also be used in a redesign for bug 15545. Test plan: Run t/db_dependent/Plugins.t. Enable pref UseKohaPlugins and config var enable_plugins. Go to plugins-home.pl. Verify that it still lists your plugins. Bonus: Check edi_accounts or stage-marc-import.pl if you have a working plugin for that. --- Koha/Plugins.pm | 35 ++++++++++++++++++++++++----------- admin/edi_accounts.pl | 4 +++- plugins/plugins-home.pl | 4 +++- t/Koha/Plugin/Test.pm | 3 ++- t/db_dependent/Plugins.t | 12 ++++++++++-- tools/stage-marc-import.pl | 4 +++- 6 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index bca7701..d3eddcf 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -45,18 +45,26 @@ sub new { return bless( $args, $class ); } -=head2 GetPlugins() +=head2 GetPlugins -This will return a list of all the available plugins of the passed type. +This will return a list of all available plugins, optionally limited by +method or metadata value. -Usage: my @plugins = C4::Plugins::GetPlugins( $method ); + my @plugins = C4::Plugins::GetPlugins({ + method => 'some_method', + metadata => { some_key => 'some_value' }, + }); + +The method and metadata parameters are optional. +Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'. +If you pass multiple keys in the metadata hash, all keys must match. -At the moment, the available types are 'report', 'tool' and 'to_marc'. =cut sub GetPlugins { - my $self = shift; - my $method = shift; + my ( $self, $params ) = @_; + my $method = $params->{method}; + my $req_metadata = $params->{metadata} // {}; my @plugin_classes = $self->plugins(); my @plugins; @@ -67,13 +75,18 @@ sub GetPlugins { my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} }); - if ($method) { - if ( $plugin->can($method) ) { - push( @plugins, $plugin ); + # Limit results by method or metadata + my $ok = 1; + next if $method && !$plugin->can($method); + my $plugin_metadata = $plugin->get_metadata; + foreach my $key ( keys %$req_metadata ) { + if( !$plugin_metadata->{$key} || + $plugin_metadata->{$key} ne $req_metadata->{$key} ) { + $ok = 0; + last; } - } else { - push( @plugins, $plugin ); } + push( @plugins, $plugin ) if $ok; } } return @plugins; diff --git a/admin/edi_accounts.pl b/admin/edi_accounts.pl index 5f963bd..e5b0694 100755 --- a/admin/edi_accounts.pl +++ b/admin/edi_accounts.pl @@ -57,7 +57,9 @@ if ( $op eq 'acct_form' ) { $template->param( plugins_enabled => $plugins_enabled ); if ( $plugins_enabled ) { - my @plugins = Koha::Plugins->new()->GetPlugins('edifact'); + my @plugins = Koha::Plugins->new()->GetPlugins({ + method => 'edifact', + }); $template->param( plugins => \@plugins ); } } diff --git a/plugins/plugins-home.pl b/plugins/plugins-home.pl index 225a560..5efd9bd 100755 --- a/plugins/plugins-home.pl +++ b/plugins/plugins-home.pl @@ -50,7 +50,9 @@ if ($plugins_enabled) { method => $method, ); - my @plugins = Koha::Plugins->new()->GetPlugins($method); + my @plugins = Koha::Plugins->new()->GetPlugins({ + method => $method, + }); $template->param( plugins => \@plugins, ); diff --git a/t/Koha/Plugin/Test.pm b/t/Koha/Plugin/Test.pm index 6a8ca78..401ce3d 100644 --- a/t/Koha/Plugin/Test.pm +++ b/t/Koha/Plugin/Test.pm @@ -1,6 +1,6 @@ package Koha::Plugin::Test; -## It's good practive to use Modern::Perl +## It's good practice to use Modern::Perl use Modern::Perl; ## Required for all plugins @@ -16,6 +16,7 @@ our $metadata = { minimum_version => '3.11', maximum_version => undef, version => $VERSION, + my_example_tag => 'find_me', }; ## This is the minimum code required for a plugin's 'new' method diff --git a/t/db_dependent/Plugins.t b/t/db_dependent/Plugins.t index 403d7c4..f918693 100755 --- a/t/db_dependent/Plugins.t +++ b/t/db_dependent/Plugins.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 22; +use Test::More tests => 23; use File::Basename; use FindBin qw($Bin); use Archive::Extract; @@ -42,9 +42,17 @@ is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' ); is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' ); is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' ); -my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins( 'report' ); +# testing GetPlugins +my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ + method => 'report' +}); my @names = map { $_->get_metadata()->{'name'} } @plugins; is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" ); +@plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ + metadata => { my_example_tag => 'find_me' }, +}); +@names = map { $_->get_metadata()->{'name'} } @plugins; +is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" ); SKIP: { my $plugins_dir = C4::Context->config("pluginsdir"); diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl index 8d94cdd..e02d1e6 100755 --- a/tools/stage-marc-import.pl +++ b/tools/stage-marc-import.pl @@ -208,7 +208,9 @@ if ($completedJobID) { if ( C4::Context->preference('UseKohaPlugins') && C4::Context->config('enable_plugins') ) { - my @plugins = Koha::Plugins->new()->GetPlugins('to_marc'); + my @plugins = Koha::Plugins->new()->GetPlugins({ + method => 'to_marc', + }); $template->param( plugins => \@plugins ); } } -- 1.7.10.4