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