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::Exceptions::Plugin;
34 use Koha::Plugins::Methods;
37 my $pluginsdir = C4::Context->config("pluginsdir");
38 my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
39 push @INC, array_minus(@pluginsdir, @INC) ;
40 pop @INC if $INC[-1] eq '.';
45 Koha::Plugins - Module for loading and managing plugins.
50 my ( $class, $args ) = @_;
52 return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
54 $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
56 return bless( $args, $class );
61 Calls a plugin method for all enabled plugins
63 @responses = Koha::Plugins->call($method, @args)
68 my ($class, $method, @args) = @_;
71 if (C4::Context->config('enable_plugins')) {
72 my @plugins = $class->new({ enable_plugins => 1 })->GetPlugins({ method => $method });
73 @plugins = grep { $_->can($method) } @plugins;
74 foreach my $plugin (@plugins) {
75 my $response = eval { $plugin->$method(@args) };
77 warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
81 push @responses, $response;
90 This will return a list of all available plugins, optionally limited by
91 method or metadata value.
93 my @plugins = Koha::Plugins::GetPlugins({
94 method => 'some_method',
95 metadata => { some_key => 'some_value' },
98 The method and metadata parameters are optional.
99 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
100 If you pass multiple keys in the metadata hash, all keys must match.
105 my ( $self, $params ) = @_;
107 my $method = $params->{method};
108 my $req_metadata = $params->{metadata} // {};
110 my $filter = ( $method ) ? { plugin_method => $method } : undef;
112 my $plugin_classes = Koha::Plugins::Methods->search(
114 { columns => 'plugin_class',
117 )->_resultset->get_column('plugin_class');
121 # Loop through all plugins that implement at least a method
122 while ( my $plugin_class = $plugin_classes->next ) {
124 if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
127 my $failed_instantiation;
130 $plugin = $plugin_class->new({
131 enable_plugins => $self->{'enable_plugins'}
132 # loads even if plugins are disabled
133 # FIXME: is this for testing without bothering to mock config?
138 $failed_instantiation = 1;
141 next if $failed_instantiation;
143 next unless $plugin->is_enabled or
144 defined($params->{all}) && $params->{all};
146 # filter the plugin out by metadata
147 my $plugin_metadata = $plugin->get_metadata;
151 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
153 push @plugins, $plugin;
154 } elsif ( defined($params->{errors}) && $params->{errors} ){
155 push @plugins, { error => 'cannot_load', name => $plugin_class };
163 =head2 InstallPlugins
165 Koha::Plugins::InstallPlugins()
167 This method iterates through all plugins physically present on a system.
168 For each plugin module found, it will test that the plugin can be loaded,
169 and if it can, will store its available methods in the plugin_methods table.
171 NOTE: We re-load all plugins here as a protective measure in case someone
172 has removed a plugin directly from the system without using the UI
177 my ( $self, $params ) = @_;
179 my @plugin_classes = $self->plugins();
182 foreach my $plugin_class (@plugin_classes) {
183 if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
184 next unless $plugin_class->isa('Koha::Plugins::Base');
187 my $failed_instantiation;
190 $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
194 $failed_instantiation = 1;
197 next if $failed_instantiation;
199 Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
201 foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) {
202 Koha::Plugins::Method->new(
204 plugin_class => $plugin_class,
205 plugin_method => $method,
210 push @plugins, $plugin;
212 my $error = $Module::Load::Conditional::ERROR;
213 # Do not warn the error if the plugin has been uninstalled
214 warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
223 =head1 AVAILABLE HOOKS
225 =head2 after_hold_create
231 =item * C<$hold> - A Koha::Hold object that has just been inserted in database
241 sub after_hold_create {
242 my ($self, $hold) = @_;
244 warn "New hold for borrower " . $hold->borrower->borrowernumber;
249 Kyle M Hall <kyle.m.hall@gmail.com>