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

(-)a/Koha/Plugins.pm (-1 / +29 lines)
Lines 284-295 sub InstallPlugins { Link Here
284
    my $verbose = $params->{verbose} // $self->_verbose;
284
    my $verbose = $params->{verbose} // $self->_verbose;
285
285
286
    my @plugin_classes = $self->plugins();
286
    my @plugin_classes = $self->plugins();
287
    my @plugins;
287
    my (@plugins, @classes_filters);
288
289
    my $has_filters = defined($params->{include}) || defined($params->{exclude});
290
291
    # Warn user if the specified classes doesn't exist and return nothing
292
    if ($has_filters) {
293
        @classes_filters = defined($params->{include}) ? @{$params->{include}} : @{$params->{exclude}};
294
295
        foreach my $classes_filter (@classes_filters) {
296
            my $is_found = 0;
297
298
            foreach my $plugin_class (@plugin_classes) {
299
                $is_found = 1 if $plugin_class =~ ":$classes_filter\$|^$classes_filter\$" || ($classes_filter =~ "^::" && $plugin_class =~ "$classes_filter\$");
300
            }
301
            unless ($is_found) {
302
                warn "$classes_filter have not been found, try a different name";
303
                return;
304
            }
305
        }
306
    }
288
307
289
    foreach my $plugin_class (@plugin_classes) {
308
    foreach my $plugin_class (@plugin_classes) {
290
        if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) {
309
        if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) {
291
            next unless $plugin_class->isa('Koha::Plugins::Base');
310
            next unless $plugin_class->isa('Koha::Plugins::Base');
292
311
312
            # Apply the filters
313
            if ($has_filters) {
314
                my $is_found = 0;
315
                foreach my $classes_filter (@classes_filters) {
316
                    $is_found = 1 if $plugin_class =~ ":$classes_filter\$|^$classes_filter\$" || ($classes_filter =~ "^::" && $plugin_class =~ "$classes_filter\$");
317
                }
318
                next if (defined($params->{include}) && !$is_found) || (defined($params->{exclude}) && $is_found);
319
            }
320
293
            my $plugin;
321
            my $plugin;
294
            my $failed_instantiation;
322
            my $failed_instantiation;
295
323
(-)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 "The plugin system must be enabled for one to be able to install plugins\n";
42
    print "The plugin system must be enabled for one to be able to install plugins\n";
36
    exit 1;
43
    exit 1;
Lines 47-53 for my $existing_plugin (@existing_plugins) { Link Here
47
    $existing_plugins->{ $existing_plugin->{metadata}->{name} } = $existing_plugin->{metadata}->{version};
54
    $existing_plugins->{ $existing_plugin->{metadata}->{name} } = $existing_plugin->{metadata}->{version};
48
}
55
}
49
56
50
my @installed_plugins = Koha::Plugins->new()->InstallPlugins( { verbose => 0 } );
57
my $params = {};
58
$params->{'include'} = \@include if @include;
59
$params->{'exclude'} = \@exclude if @exclude;
60
61
my @installed_plugins = Koha::Plugins->new()->InstallPlugins($params);
62
51
unless (@installed_plugins) {
63
unless (@installed_plugins) {
52
    my $plugins_dir = C4::Context->config("pluginsdir");
64
    my $plugins_dir = C4::Context->config("pluginsdir");
53
    if ( ref($plugins_dir) eq 'ARRAY' ) {
65
    if ( ref($plugins_dir) eq 'ARRAY' ) {
Lines 87-92 install_plugins.pl - install all plugins found in plugins_dir Link Here
87
99
88
Options:
100
Options:
89
  -?|--help        brief help message
101
  -?|--help        brief help message
102
  --include        install only the plugins of the specified classes
103
  --exclude        install all the plugins except the specified ones
90
104
91
=head1 OPTIONS
105
=head1 OPTIONS
92
106
Lines 96-101 Options: Link Here
96
110
97
Print a brief help message and exits
111
Print a brief help message and exits
98
112
113
=item B<--include>
114
115
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>).
116
117
=item B<--exclude>
118
119
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>).
120
99
=back
121
=back
100
122
101
=head1 DESCRIPTION
123
=head1 DESCRIPTION
(-)a/t/db_dependent/Koha/Plugins/Plugins.t (-2 / +55 lines)
Lines 25-31 use FindBin qw($Bin); Link Here
25
use Module::Load::Conditional qw(can_load);
25
use Module::Load::Conditional qw(can_load);
26
use Test::MockModule;
26
use Test::MockModule;
27
use Test::NoWarnings;
27
use Test::NoWarnings;
28
use Test::More tests => 19;
28
use Test::More tests => 31;
29
use Test::Warn;
29
use Test::Warn;
30
30
31
use C4::Context;
31
use C4::Context;
Lines 191-196 subtest 'GetPlugins() tests' => sub { Link Here
191
    $schema->storage->txn_rollback;
191
    $schema->storage->txn_rollback;
192
};
192
};
193
193
194
subtest 'InstallPlugins() tests' => sub {
195
196
    plan tests => 4;
197
198
    $schema->storage->txn_begin;
199
200
    # Temporarily remove any installed plugins data
201
    Koha::Plugins::Methods->delete;
202
    $schema->resultset('PluginData')->delete;
203
204
    # Tests for the exclude paramater
205
    # Test the returned plugins of the InstallPlugins subroutine
206
    my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
207
    my @installed_plugins = $plugins->InstallPlugins({ exclude => ["Test", "Koha::Plugin::MarcFieldValues"] });
208
    my $plugin_classes = join(" ", map{$_->{class}} @installed_plugins);
209
210
    my $result = grep { $plugin_classes !~ $_ } [":Test |:Test\$", ":MarcFieldValues |:MarcFieldValues\$"]
211
        && $plugin_classes =~ ":TestItemBarcodeTransform |:TestItemBarcodeTransform\$";
212
    ok($result, "Excluded plugins are not returned");
213
214
    # Test the plugins in the database
215
    my @plugins = $plugins->GetPlugins({ all => 1, error => 1 });
216
    $plugin_classes = join(" ", map{$_->{class}} @plugins);
217
218
    $result = grep { $plugin_classes !~ $_ } [":Test |:Test\$", ":MarcFieldValues |:MarcFieldValues\$"]
219
        && $plugin_classes =~ ":TestItemBarcodeTransform |:TestItemBarcodeTransform\$";
220
    ok($result, "Excluded plugins are not installed");
221
222
    # Remove installed plugins data
223
    Koha::Plugins::Methods->delete;
224
    $schema->resultset('PluginData')->delete;
225
226
    # Tests for the include parameter
227
    # Test the returned plugins of the InstallPlugins subroutine
228
    @installed_plugins = $plugins->InstallPlugins({ include => ["Test", "Koha::Plugin::MarcFieldValues"]});
229
230
    $result = 1;
231
    foreach my $plugin_class ( map{ $_->{class} } @installed_plugins ) {
232
        $result = 0 unless("$plugin_class" =~ ":Test\$" || "$plugin_class" =~ ":MarcFieldValues\$");
233
    }
234
    ok($result, "Only included plugins are returned");
235
236
    # Test the plugins in the database
237
    @plugins = $plugins->GetPlugins({ all => 1, error => 1});
238
239
    $result = 1;
240
    foreach my $plugin_class ( map{ $_->{class} } @plugins ) {
241
        $result = 0 unless("$plugin_class" =~ ":Test\$" || "$plugin_class" =~ ":MarcFieldValues\$");
242
    }
243
    ok($result, "Only included plugins are installed");
244
245
    $schema->storage->txn_rollback;
246
};
247
194
subtest 'Version upgrade tests' => sub {
248
subtest 'Version upgrade tests' => sub {
195
249
196
    plan tests => 1;
250
    plan tests => 1;
197
- 

Return to bug 34978