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