Update release notes for 19.11.18 release
[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 GetPlugins
57
58 This will return a list of all available plugins, optionally limited by
59 method or metadata value.
60
61     my @plugins = Koha::Plugins::GetPlugins({
62         method => 'some_method',
63         metadata => { some_key => 'some_value' },
64     });
65
66 The method and metadata parameters are optional.
67 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
68 If you pass multiple keys in the metadata hash, all keys must match.
69
70 =cut
71
72 sub GetPlugins {
73     my ( $self, $params ) = @_;
74
75     my $method       = $params->{method};
76     my $req_metadata = $params->{metadata} // {};
77
78     my $filter = ( $method ) ? { plugin_method => $method } : undef;
79
80     my $plugin_classes = Koha::Plugins::Methods->search(
81         $filter,
82         {   columns  => 'plugin_class',
83             distinct => 1
84         }
85     )->_resultset->get_column('plugin_class');
86
87     my @plugins;
88
89     # Loop through all plugins that implement at least a method
90     while ( my $plugin_class = $plugin_classes->next ) {
91
92         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
93             my $plugin = $plugin_class->new({
94                 enable_plugins => $self->{'enable_plugins'}
95                     # loads even if plugins are disabled
96                     # FIXME: is this for testing without bothering to mock config?
97             });
98
99             next unless $plugin->is_enabled or
100                         defined($params->{all}) && $params->{all};
101
102             # filter the plugin out by metadata
103             my $plugin_metadata = $plugin->get_metadata;
104             next
105                 if $plugin_metadata
106                 and %$req_metadata
107                 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
108
109             push @plugins, $plugin;
110         } elsif ( defined($params->{errors}) && $params->{errors} ){
111             push @plugins, { error => 'cannot_load', name => $plugin_class };
112         }
113
114     }
115
116     return @plugins;
117 }
118
119 =head2 InstallPlugins
120
121 Koha::Plugins::InstallPlugins()
122
123 This method iterates through all plugins physically present on a system.
124 For each plugin module found, it will test that the plugin can be loaded,
125 and if it can, will store its available methods in the plugin_methods table.
126
127 NOTE: We re-load all plugins here as a protective measure in case someone
128 has removed a plugin directly from the system without using the UI
129
130 =cut
131
132 sub InstallPlugins {
133     my ( $self, $params ) = @_;
134
135     my @plugin_classes = $self->plugins();
136     my @plugins;
137
138     foreach my $plugin_class (@plugin_classes) {
139         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
140             next unless $plugin_class->isa('Koha::Plugins::Base');
141
142             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
143
144             Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
145
146             foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) {
147                 Koha::Plugins::Method->new(
148                     {
149                         plugin_class  => $plugin_class,
150                         plugin_method => $method,
151                     }
152                 )->store();
153             }
154
155             push @plugins, $plugin;
156         } else {
157             my $error = $Module::Load::Conditional::ERROR;
158             # Do not warn the error if the plugin has been uninstalled
159             warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
160         }
161     }
162     return @plugins;
163 }
164
165 1;
166 __END__
167
168 =head1 AUTHOR
169
170 Kyle M Hall <kyle.m.hall@gmail.com>
171
172 =cut