Bug 30477: Add new UNIMARC installer translation files
[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         foreach my $plugin (@plugins) {
82             my $response = eval { $plugin->$method(@args) };
83             if ($@) {
84                 warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
85                 next;
86             }
87
88             push @responses, $response;
89         }
90
91     }
92     return @responses;
93 }
94
95 =head2 GetPlugins
96
97 This will return a list of all available plugins, optionally limited by
98 method or metadata value.
99
100     my @plugins = Koha::Plugins::GetPlugins({
101         method => 'some_method',
102         metadata => { some_key => 'some_value' },
103     });
104
105 The method and metadata parameters are optional.
106 If you pass multiple keys in the metadata hash, all keys must match.
107
108 =cut
109
110 sub GetPlugins {
111     my ( $self, $params ) = @_;
112
113     my $method       = $params->{method};
114     my $req_metadata = $params->{metadata} // {};
115
116     my $filter = ( $method ) ? { plugin_method => $method } : undef;
117
118     my $plugin_classes = Koha::Plugins::Methods->search(
119         $filter,
120         {   columns  => 'plugin_class',
121             distinct => 1
122         }
123     )->_resultset->get_column('plugin_class');
124
125     my @plugins;
126
127     # Loop through all plugins that implement at least a method
128     while ( my $plugin_class = $plugin_classes->next ) {
129
130         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
131
132             my $plugin;
133             my $failed_instantiation;
134
135             try {
136                 $plugin = $plugin_class->new({
137                     enable_plugins => $self->{'enable_plugins'}
138                         # loads even if plugins are disabled
139                         # FIXME: is this for testing without bothering to mock config?
140                 });
141             }
142             catch {
143                 warn "$_";
144                 $failed_instantiation = 1;
145             };
146
147             next if $failed_instantiation;
148
149             next unless $plugin->is_enabled or
150                         defined($params->{all}) && $params->{all};
151
152             # filter the plugin out by metadata
153             my $plugin_metadata = $plugin->get_metadata;
154             next
155                 if $plugin_metadata
156                 and %$req_metadata
157                 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
158
159             push @plugins, $plugin;
160         } elsif ( defined($params->{errors}) && $params->{errors} ){
161             push @plugins, { error => 'cannot_load', name => $plugin_class };
162         }
163
164     }
165
166     return @plugins;
167 }
168
169 =head2 InstallPlugins
170
171 Koha::Plugins::InstallPlugins()
172
173 This method iterates through all plugins physically present on a system.
174 For each plugin module found, it will test that the plugin can be loaded,
175 and if it can, will store its available methods in the plugin_methods table.
176
177 NOTE: We reload all plugins here as a protective measure in case someone
178 has removed a plugin directly from the system without using the UI
179
180 =cut
181
182 sub InstallPlugins {
183     my ( $self, $params ) = @_;
184
185     my @plugin_classes = $self->plugins();
186     my @plugins;
187
188     foreach my $plugin_class (@plugin_classes) {
189         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
190             next unless $plugin_class->isa('Koha::Plugins::Base');
191
192             my $plugin;
193             my $failed_instantiation;
194
195             try {
196                 $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
197             }
198             catch {
199                 warn "$_";
200                 $failed_instantiation = 1;
201             };
202
203             next if $failed_instantiation;
204
205             Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
206
207             foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) {
208                 Koha::Plugins::Method->new(
209                     {
210                         plugin_class  => $plugin_class,
211                         plugin_method => $method,
212                     }
213                 )->store();
214             }
215
216             push @plugins, $plugin;
217         } else {
218             my $error = $Module::Load::Conditional::ERROR;
219             # Do not warn the error if the plugin has been uninstalled
220             warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
221         }
222     }
223     return @plugins;
224 }
225
226 1;
227 __END__
228
229 =head1 AUTHOR
230
231 Kyle M Hall <kyle.m.hall@gmail.com>
232
233 =cut