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