From a1f93e85c592e7996580a8ad33a40a3c80fbbd8b Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 16 Apr 2020 11:50:28 -0400 Subject: [PATCH] Bug 24631: Plugin metadata should be outside the main class Currently, plugin metadata is stored within the main plugin class file but this means one has to load the class to read it. We should pull this data out into a manifest/metadata file in a standard format (JSON/YAML) to allow simpler external parsing. Test Plan: 1) Apply this patch 2) Install the latest Kitchen Sink plugin: https://github.com/bywatersolutions/koha-plugin-kitchen-sink/releases/download/v2.1.34/koha-plugin-kitchen-sink-v2.1.34.kpz 3) Browse to plugins home, note that nothing appears to have changed 4) Browse to your pluginsdir, find the META.yml file for the Kitchen Sink plugin, edit some of the metadata 5) Reload the plugins home page, note your changes to META.yml appear in the plugins table! --- Koha/Plugins.pm | 54 ++++++++++++++++++- .../prog/en/modules/plugins/plugins-home.tt | 28 +++++----- plugins/plugins-home.pl | 4 +- 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index 9dc42c5155..4fc8fb0c47 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -21,9 +21,10 @@ use Modern::Perl; use Class::Inspector; use List::MoreUtils qw(any); -use Module::Load::Conditional qw(can_load); use Module::Load qw(load); +use Module::Load::Conditional qw(can_load); use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/; +use YAML qw(LoadFile); use C4::Context; use C4::Output; @@ -112,6 +113,57 @@ sub GetPlugins { return @plugins; } +sub GetPluginsMetadata { + my ( $self, $params ) = @_; + + my $method = $params->{method}; + my $req_metadata = $params->{metadata} // {}; + + my $filter = ( $method ) ? { plugin_method => $method } : undef; + + my $plugin_classes = Koha::Plugins::Methods->search( + $filter, + { columns => 'plugin_class', + distinct => 1 + } + )->_resultset->get_column('plugin_class'); + + my @plugins; + while ( my $plugin_class = $plugin_classes->next ) { + my $plugin_path = $plugin_class; + $plugin_path =~ s/::/\//g; # Take class name, transform :: to / to get path + $plugin_path =~ s/$/.pm/; # Add .pm to the end + require $plugin_path; # Require the plugin to have it's path listed in INC + $plugin_path = + $INC{$plugin_path}; # Get the full true path to the plugin from INC + my $meta_yaml = "$plugin_path/META.yml"; + + my $plugin_metadata; + + if ( -r $meta_yaml ) { + $plugin_metadata = YAML::LoadFile($meta_yaml); + } else { + load $plugin_class; + my $plugin = $plugin_class->new({ + enable_plugins => $self->{'enable_plugins'} + }); + + # filter the plugin out by metadata + $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_metadata; + + } + + return @plugins; +} + =head2 InstallPlugins Koha::Plugins::InstallPlugins() 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 1c25a22718..df399f3d66 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 @@ -80,7 +80,7 @@ [% END %] - [% UNLESS ( plugins ) %] + [% UNLESS ( plugins_metadata ) %] [% UNLESS ( method ) %]
No plugins installed
[% ELSE %] @@ -113,10 +113,10 @@ [% END %] - [% FOREACH plugin IN plugins %] + [% FOREACH metadata IN plugins_metadata %] - [% plugin.metadata.name | html %] + [% metadata.name | html %] [% IF ( plugin.is_enabled ) %] ENABLED [% ELSE %] @@ -124,25 +124,25 @@ [% END %] - [% plugin.metadata.description | html %] + [% metadata.description | html %] - [% IF ( plugin.metadata.minimum_version && koha_version < plugin.metadata.minimum_version ) %] + [% IF ( metadata.minimum_version && koha_version < metadata.minimum_version ) %]
Warning: This report was written for a newer version of Koha. Run at your own risk.
[% END %] - [% IF ( plugin.metadata.maximum_version && koha_version > plugin.metadata.maximum_version ) %] + [% IF ( metadata.maximum_version && koha_version > metadata.maximum_version ) %]
Warning: This plugin was written for an older version of Koha. Run at your own risk.
[% END %] - [% plugin.metadata.author | html %] - [% plugin.metadata.version | html %] - [% plugin.metadata.minimum_version | html %] - [% plugin.metadata.maximum_version | html %] - [% plugin.metadata.date_updated | $KohaDates %] + [% metadata.author | html %] + [% metadata.version | html %] + [% metadata.minimum_version | html %] + [% metadata.maximum_version | html %] + [% metadata.date_updated | $KohaDates %] [% IF ( CAN_user_plugins_configure || CAN_user_plugins_manage || CAN_user_plugins_report || CAN_user_plugins_tool ) %]