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 167-173 sub feature_enabled { Link Here
167
    my $key     = "ENABLED_PLUGIN_FEATURE_" . $method;
167
    my $key     = "ENABLED_PLUGIN_FEATURE_" . $method;
168
    my $feature = Koha::Cache::Memory::Lite->get_from_cache($key);
168
    my $feature = Koha::Cache::Memory::Lite->get_from_cache($key);
169
    unless ( defined($feature) ) {
169
    unless ( defined($feature) ) {
170
        my @plugins = $class->get_enabled_plugins();
170
        my @plugins = $class->get_enabled_plugins( { verbose => 0 } );
171
        my $enabled = any { $_->can($method) } @plugins;
171
        my $enabled = any { $_->can($method) } @plugins;
172
        Koha::Cache::Memory::Lite->set_in_cache( $key, $enabled );
172
        Koha::Cache::Memory::Lite->set_in_cache( $key, $enabled );
173
    }
173
    }
Lines 182-193 method or metadata value. Link Here
182
    my @plugins = Koha::Plugins::GetPlugins({
182
    my @plugins = Koha::Plugins::GetPlugins({
183
        method => 'some_method',
183
        method => 'some_method',
184
        metadata => { some_key => 'some_value' },
184
        metadata => { some_key => 'some_value' },
185
        [ verbose => 1 ],
185
        [ all => 1, errors => 1, verbose => 1 ],
186
    });
186
    });
187
187
188
The method and metadata parameters are optional.
188
The method and metadata parameters are optional.
189
If you pass multiple keys in the metadata hash, all keys must match.
189
If you pass multiple keys in the metadata hash, all keys must match.
190
190
191
If you pass errors (only used in plugins-home), we return two arrayrefs:
192
193
    ( $good, $bad ) = Koha::Plugins::GetPlugins( { errors => 1 } );
194
195
If you pass verbose, you can enable or disable explicitly warnings
196
from Module::Load::Conditional. Disabled by default to not flood
197
the logs.
198
191
=cut
199
=cut
192
200
193
sub GetPlugins {
201
sub GetPlugins {
Lines 195-201 sub GetPlugins { Link Here
195
203
196
    my $method       = $params->{method};
204
    my $method       = $params->{method};
197
    my $req_metadata = $params->{metadata} // {};
205
    my $req_metadata = $params->{metadata} // {};
198
    my $verbose      = delete $params->{verbose} // $self->_verbose;
206
    my $errors       = $params->{errors};
207
208
    # By default dont warn here unless asked to do so.
209
    my $verbose      = $params->{verbose} // 0;
199
210
200
    my $filter = ( $method ) ? { plugin_method => $method } : undef;
211
    my $filter = ( $method ) ? { plugin_method => $method } : undef;
201
212
Lines 206-216 sub GetPlugins { Link Here
206
        }
217
        }
207
    )->_resultset->get_column('plugin_class');
218
    )->_resultset->get_column('plugin_class');
208
219
209
    my @plugins;
210
220
211
    # Loop through all plugins that implement at least a method
221
    # Loop through all plugins that implement at least a method
222
    my ( @plugins, @failing );
212
    while ( my $plugin_class = $plugin_classes->next ) {
223
    while ( my $plugin_class = $plugin_classes->next ) {
213
214
        if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) {
224
        if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) {
215
225
216
            my $plugin;
226
            my $plugin;
Lines 241-253 sub GetPlugins { Link Here
241
                and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
251
                and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
242
252
243
            push @plugins, $plugin;
253
            push @plugins, $plugin;
244
        } elsif ( defined($params->{errors}) && $params->{errors} ){
254
        } elsif( $errors ) {
245
            push @plugins, { error => 'cannot_load', name => $plugin_class };
255
            push @failing, { error => 1, name => $plugin_class };
246
        }
256
        }
247
248
    }
257
    }
249
258
250
    return @plugins;
259
    return $errors ? ( \@plugins, \@failing ) : @plugins;
251
}
260
}
252
261
253
=head2 InstallPlugins
262
=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