Bug 24000: Some modules do not return 1
[koha.git] / Koha / Plugins.pm
1 package Koha::Plugins;
2
3 # Copyright 2012 Kyle Hall
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Array::Utils qw(array_minus);
23 use Class::Inspector;
24 use List::MoreUtils qw(any);
25 use Module::Load::Conditional qw(can_load);
26 use Module::Load qw(load);
27 use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/;
28
29 use C4::Context;
30 use C4::Output;
31 use Koha::Plugins::Methods;
32
33 BEGIN {
34     my $pluginsdir = C4::Context->config("pluginsdir");
35     my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
36     push @INC, array_minus(@pluginsdir, @INC) ;
37     pop @INC if $INC[-1] eq '.';
38 }
39
40 =head1 NAME
41
42 Koha::Plugins - Module for loading and managing plugins.
43
44 =cut
45
46 sub new {
47     my ( $class, $args ) = @_;
48
49     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
50
51     $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
52
53     return bless( $args, $class );
54 }
55
56 =head2 call
57
58 Calls a plugin method for all enabled plugins
59
60     @responses = Koha::Plugins->call($method, @args)
61
62 =cut
63
64 sub call {
65     my ($class, $method, @args) = @_;
66
67     my @responses;
68     if (C4::Context->config('enable_plugins')) {
69         my @plugins = $class->new({ enable_plugins => 1 })->GetPlugins({ method => $method });
70         @plugins = grep { $_->can($method) } @plugins;
71         foreach my $plugin (@plugins) {
72             my $response = eval { $plugin->$method(@args) };
73             if ($@) {
74                 warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
75                 next;
76             }
77
78             push @responses, $response;
79         }
80
81     }
82     return @responses;
83 }
84
85 =head2 GetPlugins
86
87 This will return a list of all available plugins, optionally limited by
88 method or metadata value.
89
90     my @plugins = Koha::Plugins::GetPlugins({
91         method => 'some_method',
92         metadata => { some_key => 'some_value' },
93     });
94
95 The method and metadata parameters are optional.
96 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
97 If you pass multiple keys in the metadata hash, all keys must match.
98
99 =cut
100
101 sub GetPlugins {
102     my ( $self, $params ) = @_;
103
104     my $method       = $params->{method};
105     my $req_metadata = $params->{metadata} // {};
106
107     my $filter = ( $method ) ? { plugin_method => $method } : undef;
108
109     my $plugin_classes = Koha::Plugins::Methods->search(
110         $filter,
111         {   columns  => 'plugin_class',
112             distinct => 1
113         }
114     )->_resultset->get_column('plugin_class');
115
116     my @plugins;
117
118     # Loop through all plugins that implement at least a method
119     while ( my $plugin_class = $plugin_classes->next ) {
120
121         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
122             my $plugin = $plugin_class->new({
123                 enable_plugins => $self->{'enable_plugins'}
124                     # loads even if plugins are disabled
125                     # FIXME: is this for testing without bothering to mock config?
126             });
127
128             next unless $plugin->is_enabled or
129                         defined($params->{all}) && $params->{all};
130
131             # filter the plugin out by metadata
132             my $plugin_metadata = $plugin->get_metadata;
133             next
134                 if $plugin_metadata
135                 and %$req_metadata
136                 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
137
138             push @plugins, $plugin;
139         } elsif ( defined($params->{errors}) && $params->{errors} ){
140             push @plugins, { error => 'cannot_load', name => $plugin_class };
141         }
142
143     }
144
145     return @plugins;
146 }
147
148 =head2 InstallPlugins
149
150 Koha::Plugins::InstallPlugins()
151
152 This method iterates through all plugins physically present on a system.
153 For each plugin module found, it will test that the plugin can be loaded,
154 and if it can, will store its available methods in the plugin_methods table.
155
156 NOTE: We re-load all plugins here as a protective measure in case someone
157 has removed a plugin directly from the system without using the UI
158
159 =cut
160
161 sub InstallPlugins {
162     my ( $self, $params ) = @_;
163
164     my @plugin_classes = $self->plugins();
165     my @plugins;
166
167     foreach my $plugin_class (@plugin_classes) {
168         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
169             next unless $plugin_class->isa('Koha::Plugins::Base');
170
171             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
172
173             Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
174
175             foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) {
176                 Koha::Plugins::Method->new(
177                     {
178                         plugin_class  => $plugin_class,
179                         plugin_method => $method,
180                     }
181                 )->store();
182             }
183
184             push @plugins, $plugin;
185         } else {
186             my $error = $Module::Load::Conditional::ERROR;
187             # Do not warn the error if the plugin has been uninstalled
188             warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
189         }
190     }
191     return @plugins;
192 }
193
194 1;
195 __END__
196
197 =head1 AVAILABLE HOOKS
198
199 =head2 after_hold_create
200
201 =head3 Parameters
202
203 =over
204
205 =item * C<$hold> - A Koha::Hold object that has just been inserted in database
206
207 =back
208
209 =head3 Return value
210
211 None
212
213 =head3 Example
214
215     sub after_hold_create {
216         my ($self, $hold) = @_;
217
218         warn "New hold for borrower " . $hold->borrower->borrowernumber;
219     }
220
221 =head1 AUTHOR
222
223 Kyle M Hall <kyle.m.hall@gmail.com>
224
225 =cut