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

(-)a/Koha/Plugins/Base.pm (+42 lines)
Lines 47-55 sub new { Link Here
47
    if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
47
    if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
48
        if ( $self->install() ) {
48
        if ( $self->install() ) {
49
            $self->store_data( { '__INSTALLED__' => 1 } );
49
            $self->store_data( { '__INSTALLED__' => 1 } );
50
            if ( my $version = $self->get_metadata->{version} ) {
51
                $self->store_data({ '__INSTALLED_VERSION__' => $version });
52
            }
50
        } else {
53
        } else {
51
            warn "Plugin $class failed during installation!";
54
            warn "Plugin $class failed during installation!";
52
        }
55
        }
56
    } elsif ( $self->can('upgrade') && $self->get_metadata->{version} && $self->retrieve_data('__INSTALLED_VERSION__') ) {
57
        if ( $self->_version_compare( $self->get_metadata->{version}, $self->retrieve_data('__INSTALLED_VERSION__') ) > 0 ) {
58
            unless ( $self->upgrade() ) {
59
                warn "Plugin $class failed during upgrade!";
60
            }
61
        }
53
    }
62
    }
54
63
55
    return $self;
64
    return $self;
Lines 218-223 sub output { Link Here
218
    output_with_http_headers( $self->{cgi}, undef, $data, $content_type, $status, $extra_options );
227
    output_with_http_headers( $self->{cgi}, undef, $data, $content_type, $status, $extra_options );
219
}
228
}
220
229
230
=head2 _version_compare
231
232
Utility method to compare two version numbers
233
234
if ( $self->version_compare( '2.6.26', '2.6.0' ) == 1 ) {
235
    print "2.6.26 is greater than 2.6.0\n";
236
}
237
238
=cut
239
240
sub _version_compare {
241
    my $ver1 = shift || 0;
242
    my $ver2 = shift || 0;
243
    my @v1 = split /[.+:~-]/, $ver1;
244
    my @v2 = split /[.+:~-]/, $ver2;
245
246
    for ( my $i = 0 ; $i < max( scalar(@v1), scalar(@v2) ) ; $i++ ) {
247
248
        # Add missing version parts if one string is shorter than the other
249
        # i.e. 0 should be lt 0.2.1 and not equal, so we append .0
250
        # -> 0.0.0 <=> 0.2.1 -> -1
251
        push( @v1, 0 ) unless defined( $v1[$i] );
252
        push( @v2, 0 ) unless defined( $v2[$i] );
253
        if ( int( $v1[$i] ) > int( $v2[$i] ) ) {
254
            return 1;
255
        }
256
        elsif ( int( $v1[$i] ) < int( $v2[$i] ) ) {
257
            return -1;
258
        }
259
    }
260
    return 0;
261
}
262
221
1;
263
1;
222
__END__
264
__END__
223
265
(-)a/t/Koha/Plugin/Test.pm (+5 lines)
Lines 78-83 sub install { Link Here
78
    return "Koha::Plugin::Test::install";
78
    return "Koha::Plugin::Test::install";
79
}
79
}
80
80
81
sub upgrade {
82
    my ( $self, $args ) = @_;
83
    return "Koha::Plugin::Test::install";
84
}
85
81
sub uninstall {
86
sub uninstall {
82
    my ( $self, $args ) = @_;
87
    my ( $self, $args ) = @_;
83
    return "Koha::Plugin::Test::uninstall";
88
    return "Koha::Plugin::Test::uninstall";
(-)a/t/db_dependent/Plugins.t (-1 / +1 lines)
Lines 49-54 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' ); Link Here
49
ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
49
ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
50
ok( $plugin->can('configure'), 'Test plugin can configure' );
50
ok( $plugin->can('configure'), 'Test plugin can configure' );
51
ok( $plugin->can('install'), 'Test plugin can install' );
51
ok( $plugin->can('install'), 'Test plugin can install' );
52
ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
52
ok( $plugin->can('uninstall'), 'Test plugin can install' );
53
ok( $plugin->can('uninstall'), 'Test plugin can install' );
53
54
54
is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
55
is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
55
- 

Return to bug 20669