3 # Copyright 2012 Kyle Hall
5 # This file is part of Koha.
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.
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.
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>.
22 use Array
::Utils
qw(array_minus);
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)$/;
31 use Koha
::Plugins
::Methods
;
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 '.';
42 Koha::Plugins - Module for loading and managing plugins.
47 my ( $class, $args ) = @_;
49 return unless ( C4
::Context
->config("enable_plugins") || $args->{'enable_plugins'} );
51 $args->{'pluginsdir'} = C4
::Context
->config("pluginsdir");
53 return bless( $args, $class );
58 Calls a plugin method for all enabled plugins
60 @responses = Koha::Plugins->call($method, @args)
65 my ($class, $method, @args) = @_;
68 if (C4
::Context
->config('enable_plugins')) {
69 my @plugins = $class->new({ enable_plugins
=> 1 })->GetPlugins({ method
=> $method });
70 @plugins = grep { $_->can($method) } @plugins;
71 foreach my $plugin (@plugins) {
72 my $response = eval { $plugin->$method(@args) };
74 warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name
}, $@
);
78 push @responses, $response;
87 This will return a list of all available plugins, optionally limited by
88 method or metadata value.
90 my @plugins = Koha::Plugins::GetPlugins({
91 method => 'some_method',
92 metadata => { some_key => 'some_value' },
95 The method and metadata parameters are optional.
96 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
97 If you pass multiple keys in the metadata hash, all keys must match.
102 my ( $self, $params ) = @_;
104 my $method = $params->{method
};
105 my $req_metadata = $params->{metadata
} // {};
107 my $filter = ( $method ) ?
{ plugin_method
=> $method } : undef;
109 my $plugin_classes = Koha
::Plugins
::Methods
->search(
111 { columns
=> 'plugin_class',
114 )->_resultset->get_column('plugin_class');
118 # Loop through all plugins that implement at least a method
119 while ( my $plugin_class = $plugin_classes->next ) {
122 my $plugin = $plugin_class->new({
123 enable_plugins
=> $self->{'enable_plugins'}
124 # loads even if plugins are disabled
125 # FIXME: is this for testing without bothering to mock config?
128 next unless $plugin->is_enabled or
129 defined($params->{all
}) && $params->{all
};
131 # filter the plugin out by metadata
132 my $plugin_metadata = $plugin->get_metadata;
136 and any
{ !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
138 push @plugins, $plugin;
145 =head2 InstallPlugins
147 Koha::Plugins::InstallPlugins()
149 This method iterates through all plugins physically present on a system.
150 For each plugin module found, it will test that the plugin can be loaded,
151 and if it can, will store its available methods in the plugin_methods table.
153 NOTE: We re-load all plugins here as a protective measure in case someone
154 has removed a plugin directly from the system without using the UI
159 my ( $self, $params ) = @_;
161 my @plugin_classes = $self->plugins();
164 foreach my $plugin_class (@plugin_classes) {
165 if ( can_load
( modules
=> { $plugin_class => undef }, nocache
=> 1 ) ) {
166 next unless $plugin_class->isa('Koha::Plugins::Base');
168 my $plugin = $plugin_class->new({ enable_plugins
=> $self->{'enable_plugins'} });
170 Koha
::Plugins
::Methods
->search({ plugin_class
=> $plugin_class })->delete();
172 foreach my $method ( @
{ Class
::Inspector
->methods( $plugin_class, 'public' ) } ) {
173 Koha
::Plugins
::Method
->new(
175 plugin_class
=> $plugin_class,
176 plugin_method
=> $method,
181 push @plugins, $plugin;
183 my $error = $Module::Load
::Conditional
::ERROR
;
184 # Do not warn the error if the plugin has been uninstalled
185 warn $error unless $error =~ m
|^Could
not find
or check module
'$plugin_class'|;
194 =head1 AVAILABLE HOOKS
196 =head2 after_hold_create
202 =item * C<$hold> - A Koha::Hold object that has just been inserted in database
212 sub after_hold_create {
213 my ($self, $hold) = @_;
215 warn "New hold for borrower " . $hold->borrower->borrowernumber;
220 Kyle M Hall <kyle.m.hall@gmail.com>