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::Datas;
36 use Koha::Plugins::Methods;
38 use constant ENABLED_PLUGINS_CACHE_KEY => 'enabled_plugins';
41 my $pluginsdir = C4::Context->config("pluginsdir");
42 my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
43 push @INC, array_minus(@pluginsdir, @INC) ;
44 pop @INC if $INC[-1] eq '.';
49 Koha::Plugins - Module for loading and managing plugins.
58 my ( $class, $args ) = @_;
60 return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
62 $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
64 return bless( $args, $class );
69 Calls a plugin method for all enabled plugins
71 @responses = Koha::Plugins->call($method, @args)
73 Note: Pass your arguments as refs, when you want subsequent plugins to use the value
74 updated by preceding plugins, provided that these plugins support that.
79 my ($class, $method, @args) = @_;
81 return unless C4::Context->config('enable_plugins');
84 my @plugins = $class->get_enabled_plugins();
85 @plugins = grep { $_->can($method) } @plugins;
87 # TODO: Remove warn when after_hold_create is removed from the codebase
88 warn "after_hold_create is deprecated and will be removed soon. Contact the following plugin's authors: " . join( ', ', map {$_->{metadata}->{name}} @plugins)
89 if $method eq 'after_hold_create' and @plugins;
91 foreach my $plugin (@plugins) {
92 my $response = eval { $plugin->$method(@args) };
94 warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
98 push @responses, $response;
104 =head2 get_enabled_plugins
106 Returns a list of enabled plugins.
108 @plugins = Koha::Plugins->get_enabled_plugins( [ verbose => 1 ] );
112 sub get_enabled_plugins {
113 my ( $class, $params ) = @_;
115 return unless C4::Context->config('enable_plugins');
117 my $enabled_plugins = Koha::Cache::Memory::Lite->get_from_cache(ENABLED_PLUGINS_CACHE_KEY);
118 unless ($enabled_plugins) {
119 my $verbose = $params->{verbose} // $class->_verbose;
120 $enabled_plugins = [];
124 my $rs = Koha::Database->schema->resultset('PluginData');
125 $rs = $rs->search({ plugin_key => '__ENABLED__', plugin_value => 1 });
126 @plugin_classes = $rs->get_column('plugin_class')->all();
131 foreach my $plugin_class (@plugin_classes) {
132 next unless can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 );
134 my $plugin = eval { $plugin_class->new() };
135 if ($@ || !$plugin) {
136 warn "Failed to instantiate plugin $plugin_class: $@";
140 push @$enabled_plugins, $plugin;
142 Koha::Cache::Memory::Lite->set_in_cache(ENABLED_PLUGINS_CACHE_KEY, $enabled_plugins);
145 return @$enabled_plugins;
150 # Return false when running unit tests
151 return exists $ENV{_} && $ENV{_} =~ /\/prove(\s|$)|\.t$/ ? 0 : 1;
154 =head2 feature_enabled
156 Returns a boolean denoting whether a plugin based feature is enabled or not.
158 $enabled = Koha::Plugins->feature_enabled('method_name');
162 sub feature_enabled {
163 my ( $class, $method ) = @_;
165 return 0 unless C4::Context->config('enable_plugins');
167 my $key = "ENABLED_PLUGIN_FEATURE_" . $method;
168 my $feature = Koha::Cache::Memory::Lite->get_from_cache($key);
169 unless ( defined($feature) ) {
170 my @plugins = $class->get_enabled_plugins();
171 my $enabled = any { $_->can($method) } @plugins;
172 Koha::Cache::Memory::Lite->set_in_cache( $key, $enabled );
179 This will return a list of all available plugins, optionally limited by
180 method or metadata value.
182 my @plugins = Koha::Plugins::GetPlugins({
183 method => 'some_method',
184 metadata => { some_key => 'some_value' },
188 The method and metadata parameters are optional.
189 If you pass multiple keys in the metadata hash, all keys must match.
194 my ( $self, $params ) = @_;
196 my $method = $params->{method};
197 my $req_metadata = $params->{metadata} // {};
198 my $verbose = delete $params->{verbose} // $self->_verbose;
200 my $filter = ( $method ) ? { plugin_method => $method } : undef;
202 my $plugin_classes = Koha::Plugins::Methods->search(
204 { columns => 'plugin_class',
207 )->_resultset->get_column('plugin_class');
211 # Loop through all plugins that implement at least a method
212 while ( my $plugin_class = $plugin_classes->next ) {
214 if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) {
217 my $failed_instantiation;
220 $plugin = $plugin_class->new({
221 enable_plugins => $self->{'enable_plugins'}
222 # loads even if plugins are disabled
223 # FIXME: is this for testing without bothering to mock config?
228 $failed_instantiation = 1;
231 next if $failed_instantiation;
233 next unless $plugin->is_enabled or
234 defined($params->{all}) && $params->{all};
236 # filter the plugin out by metadata
237 my $plugin_metadata = $plugin->get_metadata;
241 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
243 push @plugins, $plugin;
244 } elsif ( defined($params->{errors}) && $params->{errors} ){
245 push @plugins, { error => 'cannot_load', name => $plugin_class };
253 =head2 InstallPlugins
255 Koha::Plugins::InstallPlugins( [ verbose => 1 ] )
257 This method iterates through all plugins physically present on a system.
258 For each plugin module found, it will test that the plugin can be loaded,
259 and if it can, will store its available methods in the plugin_methods table.
261 NOTE: We reload all plugins here as a protective measure in case someone
262 has removed a plugin directly from the system without using the UI
267 my ( $self, $params ) = @_;
268 my $verbose = $params->{verbose} // $self->_verbose;
270 my @plugin_classes = $self->plugins();
273 foreach my $plugin_class (@plugin_classes) {
274 if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) {
275 next unless $plugin_class->isa('Koha::Plugins::Base');
278 my $failed_instantiation;
281 $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
285 $failed_instantiation = 1;
288 next if $failed_instantiation;
290 Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
292 foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) {
293 Koha::Plugins::Method->new(
295 plugin_class => $plugin_class,
296 plugin_method => $method,
301 push @plugins, $plugin;
305 Koha::Cache::Memory::Lite->clear_from_cache(ENABLED_PLUGINS_CACHE_KEY);
312 Koha::Plugins->RemovePlugins( {
313 [ plugin_class => MODULE_NAME, destructive => 1, disable => 1 ],
316 This is primarily for unit testing. Take care when you pass the
317 destructive flag (know what you are doing)!
319 The method removes records from plugin_methods for one or all plugins.
321 If you pass the destructive flag, it will remove records too from
322 plugin_data for one or all plugins. Destructive overrules disable.
324 If you pass disable, it will disable one or all plugins (in plugin_data).
326 If you do not pass destructive or disable, this method does not touch
327 records in plugin_data. The cache key for enabled plugins will be cleared
328 only if you pass disabled or destructive.
333 my ( $class, $params ) = @_;
336 $params->{plugin_class}
337 ? ( plugin_class => $params->{plugin_class} )
340 Koha::Plugins::Methods->search($cond)->delete;
341 if ( $params->{destructive} ) {
342 Koha::Plugins::Datas->search($cond)->delete;
343 Koha::Cache::Memory::Lite->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY );
344 } elsif ( $params->{disable} ) {
345 $cond->{plugin_key} = '__ENABLED__';
346 Koha::Plugins::Datas->search($cond)->update( { plugin_value => 0 } );
347 Koha::Cache::Memory::Lite->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY );
356 Kyle M Hall <kyle.m.hall@gmail.com>