@@ -, +, @@ plugin metadata and store it in the database for retreival --- Koha/Plugins.pm | 56 +++++++++++++++++++++++++++---------------- Koha/Plugins/Base.pm | 22 +++++++++++++++-- Koha/Plugins/Data.pm | 54 +++++++++++++++++++++++++++++++++++++++++ Koha/Plugins/Datum.pm | 44 ++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 22 deletions(-) create mode 100644 Koha/Plugins/Data.pm create mode 100644 Koha/Plugins/Datum.pm --- a/Koha/Plugins.pm +++ a/Koha/Plugins.pm @@ -25,11 +25,12 @@ use List::MoreUtils qw(any); 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 YAML qw(Load Dump); use C4::Context; use C4::Output; use Koha::Plugins::Methods; +use Koha::Plugins::Data; our @pluginsdir; @@ -131,29 +132,34 @@ sub GetPluginsMetadata { } )->_resultset->get_column('plugin_class'); - my @metafiles = File::Find::Rule->file()->name('PLUGIN.yml')->in( @pluginsdir ); - 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 - my $yaml_to_find = "$plugin_path/PLUGIN.yml"; - $plugin_path =~ s/$/.pm/; # Add .pm to the end - - # Find the full path to the file, it's somewhere in the list of metafiles we found when this module as loaded - my ( $meta_yaml ) = grep { $yaml_to_find eq substr( $_, -length($yaml_to_find) ) } @metafiles; - - my $plugin_metadata; - if ( -r $meta_yaml ) { # If the metafile exists and is readable, use it - $plugin_metadata = YAML::LoadFile($meta_yaml); - } else { # Fall back to loading the plugin to get the metadata if there is no PLUGIN.yml file to read - load $plugin_class; - my $plugin = $plugin_class->new({ - enable_plugins => $self->{'enable_plugins'} - }); + my $plugin_metadata = Koha::Plugins::Data->find( + { + plugin_class => $plugin_class, + plugin_key => 'metadata', + } + ); - # filter the plugin out by metadata + if ( $plugin_metadata ) { + $plugin_metadata = YAML::Load( $plugin_metadata->plugin_value ); + } + else { + load $plugin_class; + my $plugin = $plugin_class->new( + { + enable_plugins => $self->{enable_plugins} + } + ); $plugin_metadata = $plugin->get_metadata; + + Koha::Plugins::Datum->new( + { + plugin_class => $plugin_class, + plugin_key => 'metadata', + plugin_value => YAML::Dump( $plugin_metadata ), + } + ); } next @@ -193,6 +199,16 @@ sub InstallPlugins { my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} }); + # Force update plugin metadata in the database + my $metadata = $plugin->get_metadata( { skip_database => 1 } ); + my $params = { + plugin_class => $plugin_class, + plugin_key => 'metadata', + }; + my $db_metadata = Koha::Plugins::Data->find( $params ) || Koha::Plugins::Datum->new( $params ); + $db_metadata->plugin_value( YAML::Dump($metadata) ); + $db_metadata->store(); + Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete(); foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) { --- a/Koha/Plugins/Base.pm +++ a/Koha/Plugins/Base.pm @@ -19,9 +19,10 @@ package Koha::Plugins::Base; use Modern::Perl; -use Module::Pluggable require => 1; use Cwd qw(abs_path); use List::Util qw(max); +use Module::Pluggable require => 1; +use YAML qw(Load LoadFile); use base qw{Module::Bundled::Files}; @@ -177,8 +178,25 @@ sub get_template { sub get_metadata { my ( $self, $args ) = @_; + my $skip_database = $args->{skip_database}; + + my $metadata = $self->{metadata}; # Check for old style metadata in blessed hash + $metadata ||= $self::metadata; # Check for a global metadata var ( i.e. our $metadata ) + + unless ( $metadata && !$skip_database ) { # Check the database next, unless this is a plugin upgrade + $metadata = $self->retrieve_data('metadata'); + $metadata = YAML::Load( $metadata ); + } + + unless ( $metadata ) { # Check for metadata in PLUGIN.yml, if it exists + my $bundle_path = $self->bundle_path; + my $plugin_yml = "$bundle_path/PLUGIN.yml"; + if ( -r $plugin_yml ) { + $metadata = YAML::LoadFile( $plugin_yml ); + } + } + #FIXME: Why another encoding issue? For metadata containing non latin characters. - my $metadata = $self->{metadata}; $metadata->{$_} && utf8::decode($metadata->{$_}) for keys %$metadata; return $metadata; } --- a/Koha/Plugins/Data.pm +++ a/Koha/Plugins/Data.pm @@ -0,0 +1,54 @@ +package Koha::Plugins::Data; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Plugins::Datum; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Plugin::Data - Koha Plugins Data Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'PluginData'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Plugins::Datum'; +} + +1; --- a/Koha/Plugins/Datum.pm +++ a/Koha/Plugins/Datum.pm @@ -0,0 +1,44 @@ +package Koha::Plugins::Datum; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Plugin::Datum - Koha Plugin Data Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'PluginData'; +} + +1; --