View | Details | Raw Unified | Return to bug 34978
Collapse All | Expand All

(-)a/Koha/Plugins.pm (-1 / +29 lines)
Lines 230-241 sub InstallPlugins { Link Here
230
    my ( $self, $params ) = @_;
230
    my ( $self, $params ) = @_;
231
231
232
    my @plugin_classes = $self->plugins();
232
    my @plugin_classes = $self->plugins();
233
    my @plugins;
233
    my (@plugins, @classes_filters);
234
235
    my $has_filters = defined($params->{include}) || defined($params->{exclude});
236
237
    # Warn user if the specified classes doesn't exist and return nothing
238
    if ($has_filters) {
239
        @classes_filters = defined($params->{include}) ? @{$params->{include}} : @{$params->{exclude}};
240
241
        foreach my $classes_filter (@classes_filters) {
242
            my $is_found = 0;
243
244
            foreach my $plugin_class (@plugin_classes) {
245
                $is_found = 1 if $plugin_class =~ ":$classes_filter\$|^$classes_filter\$" || ($classes_filter =~ "^::" && $plugin_class =~ "$classes_filter\$");
246
            }
247
            unless ($is_found) {
248
                warn "$classes_filter have not been found, try a different name";
249
                return;
250
            }
251
        }
252
    }
234
253
235
    foreach my $plugin_class (@plugin_classes) {
254
    foreach my $plugin_class (@plugin_classes) {
236
        if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
255
        if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
237
            next unless $plugin_class->isa('Koha::Plugins::Base');
256
            next unless $plugin_class->isa('Koha::Plugins::Base');
238
257
258
            # Apply the filters
259
            if ($has_filters) {
260
                my $is_found = 0;
261
                foreach my $classes_filter (@classes_filters) {
262
                    $is_found = 1 if $plugin_class =~ ":$classes_filter\$|^$classes_filter\$" || ($classes_filter =~ "^::" && $plugin_class =~ "$classes_filter\$");
263
                }
264
                next if (defined($params->{include}) && !$is_found) || (defined($params->{exclude}) && $is_found);
265
            }
266
239
            my $plugin;
267
            my $plugin;
240
            my $failed_instantiation;
268
            my $failed_instantiation;
241
269
(-)a/misc/devel/install_plugins.pl (-3 / +25 lines)
Lines 26-36 use Koha::Script; Link Here
26
use C4::Context;
26
use C4::Context;
27
use Koha::Plugins;
27
use Koha::Plugins;
28
28
29
my ($help);
29
my ($help, @include, @exclude);
30
GetOptions( 'help|?' => \$help );
30
31
GetOptions(
32
    'help|?' => \$help,
33
    'include=s' => \@include,
34
    'exclude=s' => \@exclude,
35
) or die "Installation aborted\n";
31
36
32
pod2usage(1) if $help;
37
pod2usage(1) if $help;
33
38
39
die "Installation aborted : --include and --exclude can't be used simultaneously\n" if @include && @exclude;
40
34
unless ( C4::Context->config("enable_plugins") ) {
41
unless ( C4::Context->config("enable_plugins") ) {
35
    print
42
    print
36
"The plugin system must be enabled for one to be able to install plugins\n";
43
"The plugin system must be enabled for one to be able to install plugins\n";
Lines 51-57 for my $existing_plugin (@existing_plugins) { Link Here
51
    }
58
    }
52
}
59
}
53
60
54
my @installed_plugins = Koha::Plugins->new()->InstallPlugins();
61
my $params = {};
62
$params->{'include'} = \@include if @include;
63
$params->{'exclude'} = \@exclude if @exclude;
64
65
my @installed_plugins = Koha::Plugins->new()->InstallPlugins($params);
66
55
unless (@installed_plugins) {
67
unless (@installed_plugins) {
56
    my $plugins_dir = C4::Context->config("pluginsdir");
68
    my $plugins_dir = C4::Context->config("pluginsdir");
57
    if ( ref($plugins_dir) eq 'ARRAY' ) {
69
    if ( ref($plugins_dir) eq 'ARRAY' ) {
Lines 96-101 install_plugins.pl - install all plugins found in plugins_dir Link Here
96
108
97
Options:
109
Options:
98
  -?|--help        brief help message
110
  -?|--help        brief help message
111
  --include        install only the plugins of the specified classes
112
  --exclude        install all the plugins except the specified ones
99
113
100
=head1 OPTIONS
114
=head1 OPTIONS
101
115
Lines 105-110 Options: Link Here
105
119
106
Print a brief help message and exits
120
Print a brief help message and exits
107
121
122
=item B<--include>
123
124
Retrieve the list of plugin classes and install only those. It's possible to specify the package of a class to install a specific plugin (e.g. --include <package::pluginClass> --include <anotherPluginClass>).
125
126
=item B<--exclude>
127
128
Retrieve the list of plugin classes and install all plugins except those. It's possible to specify the package of a class to install a specific plugin (e.g. --exclude <package::pluginClass> --exclude <anotherPluginClass>).
129
108
=back
130
=back
109
131
110
=head1 DESCRIPTION
132
=head1 DESCRIPTION
(-)a/t/db_dependent/Koha/Plugins/Plugins.t (-2 / +55 lines)
Lines 25-31 use File::Temp qw( tempdir tempfile ); Link Here
25
use FindBin qw($Bin);
25
use FindBin qw($Bin);
26
use Module::Load::Conditional qw(can_load);
26
use Module::Load::Conditional qw(can_load);
27
use Test::MockModule;
27
use Test::MockModule;
28
use Test::More tests => 61;
28
use Test::More tests => 62;
29
use Test::Warn;
29
use Test::Warn;
30
30
31
use C4::Context;
31
use C4::Context;
Lines 153-158 subtest 'GetPlugins() tests' => sub { Link Here
153
    $schema->storage->txn_rollback;
153
    $schema->storage->txn_rollback;
154
};
154
};
155
155
156
subtest 'InstallPlugins() tests' => sub {
157
158
    plan tests => 4;
159
160
    $schema->storage->txn_begin;
161
162
    # Temporarily remove any installed plugins data
163
    Koha::Plugins::Methods->delete;
164
    $schema->resultset('PluginData')->delete;
165
166
    # Tests for the exclude paramater
167
    # Test the returned plugins of the InstallPlugins subroutine
168
    my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
169
    my @installed_plugins = $plugins->InstallPlugins({ exclude => ["Test", "Koha::Plugin::MarcFieldValues"] });
170
    my $plugin_classes = join(" ", map{$_->{class}} @installed_plugins);
171
172
    my $result = grep { $plugin_classes !~ $_ } [":Test |:Test\$", ":MarcFieldValues |:MarcFieldValues\$"]
173
        && $plugin_classes =~ ":TestItemBarcodeTransform |:TestItemBarcodeTransform\$";
174
    ok($result, "Excluded plugins are not returned");
175
176
    # Test the plugins in the database
177
    my @plugins = $plugins->GetPlugins({ all => 1, error => 1 });
178
    $plugin_classes = join(" ", map{$_->{class}} @plugins);
179
180
    $result = grep { $plugin_classes !~ $_ } [":Test |:Test\$", ":MarcFieldValues |:MarcFieldValues\$"]
181
        && $plugin_classes =~ ":TestItemBarcodeTransform |:TestItemBarcodeTransform\$";
182
    ok($result, "Excluded plugins are not installed");
183
184
    # Remove installed plugins data
185
    Koha::Plugins::Methods->delete;
186
    $schema->resultset('PluginData')->delete;
187
188
    # Tests for the include parameter
189
    # Test the returned plugins of the InstallPlugins subroutine
190
    @installed_plugins = $plugins->InstallPlugins({ include => ["Test", "Koha::Plugin::MarcFieldValues"]});
191
192
    $result = 1;
193
    foreach my $plugin_class ( map{ $_->{class} } @installed_plugins ) {
194
        $result = 0 unless("$plugin_class" =~ ":Test\$" || "$plugin_class" =~ ":MarcFieldValues\$");
195
    }
196
    ok($result, "Only included plugins are returned");
197
198
    # Test the plugins in the database
199
    @plugins = $plugins->GetPlugins({ all => 1, error => 1});
200
201
    $result = 1;
202
    foreach my $plugin_class ( map{ $_->{class} } @plugins ) {
203
        $result = 0 unless("$plugin_class" =~ ":Test\$" || "$plugin_class" =~ ":MarcFieldValues\$");
204
    }
205
    ok($result, "Only included plugins are installed");
206
207
    $schema->storage->txn_rollback;
208
};
209
156
subtest 'Version upgrade tests' => sub {
210
subtest 'Version upgrade tests' => sub {
157
211
158
    plan tests => 1;
212
    plan tests => 1;
159
- 

Return to bug 34978