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

(-)a/Koha/Plugins.pm (-10 / +19 lines)
Lines 81-87 sub call { Link Here
81
    return unless C4::Context->config('enable_plugins');
81
    return unless C4::Context->config('enable_plugins');
82
82
83
    my @responses;
83
    my @responses;
84
    my @plugins = $class->get_enabled_plugins();
84
    my @plugins = $class->get_enabled_plugins( { verbose => 0 } );
85
    @plugins = grep { $_->can($method) } @plugins;
85
    @plugins = grep { $_->can($method) } @plugins;
86
86
87
    # TODO: Remove warn when after_hold_create is removed from the codebase
87
    # TODO: Remove warn when after_hold_create is removed from the codebase
Lines 160-166 sub feature_enabled { Link Here
160
    my $key     = "ENABLED_PLUGIN_FEATURE_" . $method;
160
    my $key     = "ENABLED_PLUGIN_FEATURE_" . $method;
161
    my $feature = Koha::Cache::Memory::Lite->get_from_cache($key);
161
    my $feature = Koha::Cache::Memory::Lite->get_from_cache($key);
162
    unless ( defined($feature) ) {
162
    unless ( defined($feature) ) {
163
        my @plugins = $class->get_enabled_plugins();
163
        my @plugins = $class->get_enabled_plugins( { verbose => 0 } );
164
        my $enabled = any { $_->can($method) } @plugins;
164
        my $enabled = any { $_->can($method) } @plugins;
165
        Koha::Cache::Memory::Lite->set_in_cache( $key, $enabled );
165
        Koha::Cache::Memory::Lite->set_in_cache( $key, $enabled );
166
    }
166
    }
Lines 175-186 method or metadata value. Link Here
175
    my @plugins = Koha::Plugins::GetPlugins({
175
    my @plugins = Koha::Plugins::GetPlugins({
176
        method => 'some_method',
176
        method => 'some_method',
177
        metadata => { some_key => 'some_value' },
177
        metadata => { some_key => 'some_value' },
178
        [ verbose => 1 ],
178
        [ all => 1, errors => 1, verbose => 1 ],
179
    });
179
    });
180
180
181
The method and metadata parameters are optional.
181
The method and metadata parameters are optional.
182
If you pass multiple keys in the metadata hash, all keys must match.
182
If you pass multiple keys in the metadata hash, all keys must match.
183
183
184
If you pass errors (only used in plugins-home), we return two arrayrefs:
185
186
    ( $good, $bad ) = Koha::Plugins::GetPlugins( { errors => 1 } );
187
188
If you pass verbose, you can enable or disable explicitly warnings
189
from Module::Load::Conditional. Disabled by default to not flood
190
the logs.
191
184
=cut
192
=cut
185
193
186
sub GetPlugins {
194
sub GetPlugins {
Lines 188-194 sub GetPlugins { Link Here
188
196
189
    my $method       = $params->{method};
197
    my $method       = $params->{method};
190
    my $req_metadata = $params->{metadata} // {};
198
    my $req_metadata = $params->{metadata} // {};
191
    my $verbose      = delete $params->{verbose} // $self->_verbose;
199
    my $errors       = $params->{errors};
200
201
    # By default dont warn here unless asked to do so.
202
    my $verbose      = $params->{verbose} // 0;
192
203
193
    my $filter = ( $method ) ? { plugin_method => $method } : undef;
204
    my $filter = ( $method ) ? { plugin_method => $method } : undef;
194
205
Lines 199-209 sub GetPlugins { Link Here
199
        }
210
        }
200
    )->_resultset->get_column('plugin_class');
211
    )->_resultset->get_column('plugin_class');
201
212
202
    my @plugins;
203
213
204
    # Loop through all plugins that implement at least a method
214
    # Loop through all plugins that implement at least a method
215
    my ( @plugins, @failing );
205
    while ( my $plugin_class = $plugin_classes->next ) {
216
    while ( my $plugin_class = $plugin_classes->next ) {
206
207
        if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) {
217
        if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) {
208
218
209
            my $plugin;
219
            my $plugin;
Lines 234-246 sub GetPlugins { Link Here
234
                and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
244
                and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
235
245
236
            push @plugins, $plugin;
246
            push @plugins, $plugin;
237
        } elsif ( defined($params->{errors}) && $params->{errors} ){
247
        } elsif( $errors ) {
238
            push @plugins, { error => 'cannot_load', name => $plugin_class };
248
            push @failing, { error => 1, name => $plugin_class };
239
        }
249
        }
240
241
    }
250
    }
242
251
243
    return @plugins;
252
    return $errors ? ( \@plugins, \@failing ) : @plugins;
244
}
253
}
245
254
246
=head2 InstallPlugins
255
=head2 InstallPlugins
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt (-2 / +2 lines)
Lines 154-162 Link Here
154
                                            <span class="label label-warning">ERRORS</span>
154
                                            <span class="label label-warning">ERRORS</span>
155
                                        </td>
155
                                        </td>
156
                                        [% IF ( CAN_user_plugins_configure || CAN_user_plugins_manage || CAN_user_plugins_report || CAN_user_plugins_tool ) %]
156
                                        [% IF ( CAN_user_plugins_configure || CAN_user_plugins_manage || CAN_user_plugins_report || CAN_user_plugins_tool ) %]
157
                                        <td colspan="7">Error found whilst attempting to load plugin</td>
157
                                            <td colspan="8">Error found whilst attempting to load plugin</td>
158
                                        [% ELSE %]
158
                                        [% ELSE %]
159
                                        <td colspan="6">Error found whilst attempting to load plugin</td>
159
                                            <td colspan="8">Error found whilst attempting to load plugin</td>
160
                                        [% END %]
160
                                        [% END %]
161
                                    </tr>
161
                                    </tr>
162
                                    [% ELSE %]
162
                                    [% ELSE %]
(-)a/misc/devel/install_plugins.pl (-9 / +4 lines)
Lines 38-57 unless ( C4::Context->config("enable_plugins") ) { Link Here
38
}
38
}
39
39
40
my @existing_plugins = Koha::Plugins->new()->GetPlugins({
40
my @existing_plugins = Koha::Plugins->new()->GetPlugins({
41
    all    => 1,
41
    all     => 1,
42
    errors => 1,
42
    verbose => 1,    # warns about plugins failing to load
43
});
43
});
44
my $existing_plugins;
44
my $existing_plugins;
45
for my $existing_plugin (@existing_plugins) {
45
for my $existing_plugin (@existing_plugins) {
46
    if( defined $existing_plugin->{error} ){
46
    $existing_plugins->{ $existing_plugin->{metadata}->{name} } = $existing_plugin->{metadata}->{version};
47
        print "Could not load: ".$existing_plugin->{name}." any associated method will be removed\n";
48
    } else {
49
        $existing_plugins->{ $existing_plugin->{metadata}->{name} } =
50
          $existing_plugin->{metadata}->{version};
51
    }
52
}
47
}
53
48
54
my @installed_plugins = Koha::Plugins->new()->InstallPlugins();
49
my @installed_plugins = Koha::Plugins->new()->InstallPlugins( { verbose => 0 } );
55
unless (@installed_plugins) {
50
unless (@installed_plugins) {
56
    my $plugins_dir = C4::Context->config("pluginsdir");
51
    my $plugins_dir = C4::Context->config("pluginsdir");
57
    if ( ref($plugins_dir) eq 'ARRAY' ) {
52
    if ( ref($plugins_dir) eq 'ARRAY' ) {
(-)a/plugins/plugins-home.pl (-2 / +2 lines)
Lines 50-56 if ($plugins_enabled) { Link Here
50
        method       => $method,
50
        method       => $method,
51
    );
51
    );
52
52
53
    my @plugins = Koha::Plugins->new()->GetPlugins(
53
    my ( $plugins, $failures ) = Koha::Plugins->new()->GetPlugins(
54
        {
54
        {
55
            method => $method,
55
            method => $method,
56
            all    => 1,
56
            all    => 1,
Lines 58-64 if ($plugins_enabled) { Link Here
58
        }
58
        }
59
    );
59
    );
60
60
61
    $template->param( plugins            => \@plugins, );
61
    $template->param( plugins            => [ @$plugins, @$failures ] );
62
    $template->param( plugins_restricted => C4::Context->config('plugins_restricted') );
62
    $template->param( plugins_restricted => C4::Context->config('plugins_restricted') );
63
63
64
    $template->param( can_search => C4::Context->config('plugin_repos') ? 1 : 0 );
64
    $template->param( can_search => C4::Context->config('plugin_repos') ? 1 : 0 );
(-)a/plugins/plugins-upload.pl (-1 / +4 lines)
Lines 124-130 if ( ( $op eq 'Upload' ) && ( $uploadfile || $uploadlocation ) ) { Link Here
124
            exit;
124
            exit;
125
        }
125
        }
126
126
127
        Koha::Plugins->new()->InstallPlugins();
127
        # Install plugins; verbose not needed, we redirect to plugins-home.
128
        # FIXME There is no good way to verify the install below; we need an
129
        # individual plugin install.
130
        Koha::Plugins->new->InstallPlugins( { verbose => 0 } );
128
    }
131
    }
129
} elsif ( ( $op eq 'Upload' ) && !$uploadfile && !$uploadlocation ) {
132
} elsif ( ( $op eq 'Upload' ) && !$uploadfile && !$uploadlocation ) {
130
    warn "Problem uploading file or no file uploaded.";
133
    warn "Problem uploading file or no file uploaded.";
(-)a/t/db_dependent/Koha/Plugins/Plugins.t (-2 / +25 lines)
Lines 24-30 use File::Temp qw( tempdir tempfile ); Link Here
24
use FindBin qw($Bin);
24
use FindBin qw($Bin);
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::More tests => 17;
27
use Test::More tests => 18;
28
use Test::Warn;
28
use Test::Warn;
29
29
30
use C4::Context;
30
use C4::Context;
Lines 404-409 subtest 'RemovePlugins' => sub { Link Here
404
    $schema->storage->txn_rollback;
404
    $schema->storage->txn_rollback;
405
};
405
};
406
406
407
subtest 'verbose and errors flag' => sub {
408
    plan tests => 6;
409
410
    $schema->storage->txn_begin;
411
    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
412
    Koha::Plugins->RemovePlugins( { destructive => 1 } );    # clean start
413
414
    our $class_basename = 'Koha::Plugin::TestMR::' . time;
415
    my $plugin_mocks = [];
416
417
    # Recreate TestMR plugins without module and mock on Module::Load::Conditional
418
    reload_plugin( $_, $plugin_mocks ) for 1 .. 2;
419
    warnings_like { Koha::Plugins->new->GetPlugins } [], 'Verbose was off';
420
    warnings_like { Koha::Plugins->new->GetPlugins( { verbose => 1 } ) }
421
    [ qr/TestMR/, qr/TestMR/ ], 'Verbose was on, two warns';
422
423
    my ( $plugins, $failures ) = Koha::Plugins->new->GetPlugins( { errors => 1 } );
424
    is( @$plugins,               0,                    'No good plugins left' );
425
    is( @$failures,              2,                    'Two failing plugins' );
426
    is( $failures->[0]->{error}, 1,                    'Failure hash contains error key' );
427
    is( $failures->[0]->{name},  "${class_basename}1", 'Failure hash contains name key' );
428
429
    $schema->storage->txn_rollback;
430
};
407
431
408
$schema->storage->txn_begin;    # Matching rollback at very end
432
$schema->storage->txn_begin;    # Matching rollback at very end
409
433
410
- 

Return to bug 35536