3 # Copyright 2012 Kyle Hall
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Array::Utils qw( array_minus );
24 use List::MoreUtils qw( any );
25 use Module::Load::Conditional qw( can_load );
27 use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/;
33 use Koha::Cache::Memory::Lite;
34 use Koha::Exceptions::Plugin;
35 use Koha::Plugins::Methods;
37 use constant ENABLED_PLUGINS_CACHE_KEY => 'enabled_plugins';
40 my $pluginsdir = C4::Context->config("pluginsdir");
41 my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
42 push @INC, array_minus(@pluginsdir, @INC) ;
43 pop @INC if $INC[-1] eq '.';
48 Koha::Plugins - Module for loading and managing plugins.
57 my ( $class, $args ) = @_;
59 return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
61 $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
63 return bless( $args, $class );
68 Calls a plugin method for all enabled plugins
70 @responses = Koha::Plugins->call($method, @args)
72 Note: Pass your arguments as refs, when you want subsequent plugins to use the value
73 updated by preceding plugins, provided that these plugins support that.
78 my ($class, $method, @args) = @_;
80 return unless C4::Context->config('enable_plugins');
83 my @plugins = $class->get_enabled_plugins();
84 @plugins = grep { $_->can($method) } @plugins;
86 # TODO: Remove warn when after_hold_create is removed from the codebase
87 warn "after_hold_create is deprecated and will be removed soon. Contact the following plugin's authors: " . join( ', ', map {$_->{metadata}->{name}} @plugins)
88 if $method eq 'after_hold_create' and @plugins;
90 foreach my $plugin (@plugins) {
91 my $response = eval { $plugin->$method(@args) };
93 warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
97 push @responses, $response;
103 =head2 get_enabled_plugins
105 Returns a list of enabled plugins.
107 @plugins = Koha::Plugins->get_enabled_plugins();
111 sub get_enabled_plugins {
114 return unless C4::Context->config('enable_plugins');
116 my $enabled_plugins = Koha::Cache::Memory::Lite->get_from_cache(ENABLED_PLUGINS_CACHE_KEY);
117 unless ($enabled_plugins) {
118 $enabled_plugins = [];
119 my $rs = Koha::Database->schema->resultset('PluginData');
120 $rs = $rs->search({ plugin_key => '__ENABLED__', plugin_value => 1 });
121 my @plugin_classes = $rs->get_column('plugin_class')->all();
122 foreach my $plugin_class (@plugin_classes) {
123 unless (can_load(modules => { $plugin_class => undef }, nocache => 1)) {
124 warn "Failed to load $plugin_class: $Module::Load::Conditional::ERROR";
128 my $plugin = eval { $plugin_class->new() };
129 if ($@ || !$plugin) {
130 warn "Failed to instantiate plugin $plugin_class: $@";
134 push @$enabled_plugins, $plugin;
136 Koha::Cache::Memory::Lite->set_in_cache(ENABLED_PLUGINS_CACHE_KEY, $enabled_plugins);
139 return @$enabled_plugins;
143 =head2 feature_enabled
145 Returns a boolean denoting whether a plugin based feature is enabled or not.
147 $enabled = Koha::Plugins->feature_enabled('method_name');
151 sub feature_enabled {
152 my ( $class, $method ) = @_;
154 return 0 unless C4::Context->config('enable_plugins');
156 my $key = "ENABLED_PLUGIN_FEATURE_" . $method;
157 my $feature = Koha::Cache::Memory::Lite->get_from_cache($key);
158 unless ( defined($feature) ) {
159 my @plugins = $class->get_enabled_plugins();
160 my $enabled = any { $_->can($method) } @plugins;
161 Koha::Cache::Memory::Lite->set_in_cache( $key, $enabled );
168 This will return a list of all available plugins, optionally limited by
169 method or metadata value.
171 my @plugins = Koha::Plugins::GetPlugins({
172 method => 'some_method',
173 metadata => { some_key => 'some_value' },
176 The method and metadata parameters are optional.
177 If you pass multiple keys in the metadata hash, all keys must match.
182 my ( $self, $params ) = @_;
184 my $method = $params->{method};
185 my $req_metadata = $params->{metadata} // {};
187 my $filter = ( $method ) ? { plugin_method => $method } : undef;
189 my $plugin_classes = Koha::Plugins::Methods->search(
191 { columns => 'plugin_class',
194 )->_resultset->get_column('plugin_class');
198 # Loop through all plugins that implement at least a method
199 while ( my $plugin_class = $plugin_classes->next ) {
201 if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
204 my $failed_instantiation;
207 $plugin = $plugin_class->new({
208 enable_plugins => $self->{'enable_plugins'}
209 # loads even if plugins are disabled
210 # FIXME: is this for testing without bothering to mock config?
215 $failed_instantiation = 1;
218 next if $failed_instantiation;
220 next unless $plugin->is_enabled or
221 defined($params->{all}) && $params->{all};
223 # filter the plugin out by metadata
224 my $plugin_metadata = $plugin->get_metadata;
228 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
230 push @plugins, $plugin;
231 } elsif ( defined($params->{errors}) && $params->{errors} ){
232 push @plugins, { error => 'cannot_load', name => $plugin_class };
240 =head2 InstallPlugins
242 Koha::Plugins::InstallPlugins()
244 This method iterates through all plugins physically present on a system.
245 For each plugin module found, it will test that the plugin can be loaded,
246 and if it can, will store its available methods in the plugin_methods table.
248 NOTE: We reload all plugins here as a protective measure in case someone
249 has removed a plugin directly from the system without using the UI
254 my ( $self, $params ) = @_;
256 my @plugin_classes = $self->plugins();
259 foreach my $plugin_class (@plugin_classes) {
260 if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
261 next unless $plugin_class->isa('Koha::Plugins::Base');
264 my $failed_instantiation;
267 $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
271 $failed_instantiation = 1;
274 next if $failed_instantiation;
276 Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
278 foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) {
279 Koha::Plugins::Method->new(
281 plugin_class => $plugin_class,
282 plugin_method => $method,
287 push @plugins, $plugin;
289 my $error = $Module::Load::Conditional::ERROR;
290 # Do not warn the error if the plugin has been uninstalled
291 warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
295 Koha::Cache::Memory::Lite->clear_from_cache(ENABLED_PLUGINS_CACHE_KEY);
305 Kyle M Hall <kyle.m.hall@gmail.com>