Bug 21073: Improve plugin performance
[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 Class::Inspector;
23 use List::MoreUtils qw( any );
24 use Module::Load::Conditional qw(can_load);
25 use Module::Load qw(load);
26 use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/;
27
28 use C4::Context;
29 use C4::Output;
30 use Koha::Plugins::Methods;
31
32 BEGIN {
33     my $pluginsdir = C4::Context->config("pluginsdir");
34     my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
35     push( @INC, @pluginsdir );
36     pop @INC if $INC[-1] eq '.';
37 }
38
39 =head1 NAME
40
41 Koha::Plugins - Module for loading and managing plugins.
42
43 =cut
44
45 sub new {
46     my ( $class, $args ) = @_;
47
48     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
49
50     $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
51
52     return bless( $args, $class );
53 }
54
55 =head2 GetPlugins
56
57 This will return a list of all available plugins, optionally limited by
58 method or metadata value.
59
60     my @plugins = Koha::Plugins::GetPlugins({
61         method => 'some_method',
62         metadata => { some_key => 'some_value' },
63     });
64
65 The method and metadata parameters are optional.
66 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
67 If you pass multiple keys in the metadata hash, all keys must match.
68
69 =cut
70
71 sub GetPlugins {
72     my ( $self, $params ) = @_;
73     my $method = $params->{method};
74     my $req_metadata = $params->{metadata} // {};
75
76     my $dbh = C4::Context->dbh;
77     my $plugin_classes = $dbh->selectcol_arrayref('SELECT DISTINCT(plugin_class) FROM plugin_methods');
78     my @plugins;
79
80     foreach my $plugin_class (@$plugin_classes) {
81         next if $method && !Koha::Plugins::Methods->search({ plugin_class => $plugin_class, plugin_method => $method })->count;
82         load $plugin_class;
83         my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
84
85         my $plugin_enabled = $plugin->retrieve_data('__ENABLED__');
86         $plugin->{enabled} = $plugin_enabled;
87
88         # Want all plugins. Not only enabled ones.
89         if ( defined($params->{all}) && $params->{all} ) {
90             $plugin_enabled = 1;
91         }
92
93         next unless $plugin_enabled;
94
95         push @plugins, $plugin;
96     }
97     return @plugins;
98 }
99
100 =head2
101
102 Koha::Plugins::InstallPlugins()
103
104 This method iterates through all plugins physically present on a system.
105 For each plugin module found, it will test that the plugin can be loaded,
106 and if it can, will store its available methods in the plugin_methods table.
107
108 =cut
109
110 sub InstallPlugins {
111     my ( $self, $params ) = @_;
112
113     my @plugin_classes = $self->plugins();
114     my @plugins;
115
116     foreach my $plugin_class (@plugin_classes) {
117         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
118             next unless $plugin_class->isa('Koha::Plugins::Base');
119
120             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
121
122             Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
123
124             foreach my $method ( @{ Class::Inspector->methods($plugin_class) } ) {
125                 Koha::Plugins::Method->new(
126                     {
127                         plugin_class  => $plugin_class,
128                         plugin_method => $method,
129                     }
130                 )->store();
131             }
132
133             push @plugins, $plugin;
134         } else {
135             my $error = $Module::Load::Conditional::ERROR;
136             # Do not warn the error if the plugin has been uninstalled
137             warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
138         }
139     }
140     return @plugins;
141 }
142
143 1;
144 __END__
145
146 =head1 AUTHOR
147
148 Kyle M Hall <kyle.m.hall@gmail.com>
149
150 =cut