From 4831f7efe1bd4abf81f8cd74a9183fbbc9e934f0 Mon Sep 17 00:00:00 2001 From: Fridolin Somers Date: Mon, 13 Sep 2021 20:31:24 -1000 Subject: [PATCH] Bug 29008: Avoid warning when checking Koha version in plugins In Koha plugins home page, each plugin min/max version is compared with actual Koha version. This uses metadatas assuming there are numbers, like 18.11 or 20.1100000. But source code version is actually YY.MM.XX.YYY.ZZZ. We sometimes find it in plugins, for examle : https://github.com/thekesolutions/koha-plugin-bibliocommons/blob/c2da98f1bda450452ad687e5623f45939a967bdf/Koha/Plugin/Com/Theke/BiblioCommons.pm#L31 https://github.com/KohaSuomi/koha-plugin-sms-send-link-mobility-driver/blob/master/Koha/Plugin/Fi/KohaSuomi/SMSSendLinkMobilityDriver.pm#L22 This generates warning : Argument "18.11.01" isn't numeric in numeric lt (<) at /home/koha/src/Koha/Plugins/Base.pm line 377.: src/plugins/plugins-home.pl We should allow this syntax in plugins metadatas. Test plan : 1) Install a Koha plugin with minimum_version containing 3 dots : for example 18.11.01.001 2) Go to plugins home page 3) Check there is no warning 4) Install a plugin with minimum_version higher than current Koha version 5) Check in plugins table the warning appears 6) Install a plugin with maximum_version lower than current Koha version 7) Check in plugins table the warning appears --- Koha/Plugins/Base.pm | 43 +++++++++++++++++++ .../prog/en/modules/plugins/plugins-home.tt | 4 +- plugins/plugins-home.pl | 1 - 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Koha/Plugins/Base.pm b/Koha/Plugins/Base.pm index 92ce7714bd..af000cc56c 100644 --- a/Koha/Plugins/Base.pm +++ b/Koha/Plugins/Base.pm @@ -382,6 +382,49 @@ sub disable { return $self; } +=head2 is_for_newer_koha + +Returns 1 if plugin is for newer Koha version, otherwise returns 0 + +=cut + +sub is_for_newer_koha { + my ($self) = @_; + my $actual_version = Koha::version; + my $min_version = $self->get_metadata->{minimum_version}; + return 0 unless $min_version; + return ( _version_to_number($actual_version) < _version_to_number($min_version) ) ? 1 : 0; +} + +=head2 is_for_older_koha + +Returns 1 if plugin is for older Koha version, otherwise returns 0 + +=cut + +sub is_for_older_koha { + my ($self) = @_; + my $actual_version = Koha::version; + my $max_version = $self->get_metadata->{maximum_version}; + return 0 unless $max_version; + return ( _version_to_number($actual_version) > _version_to_number($max_version) ) ? 1 : 0; +} + +=head2 _version_to_number + +Utility method to convert a Koha version into a float number. +For example 21.05.00.006 into 21.0500006 + +=cut + +sub _version_to_number { + my $koha_version = shift; + return 0 unless defined($koha_version); + # Version in metadata may be 18.11, 20.05.02, 21.05.00.006 ... + $koha_version =~ s/^([^\.]*\.[^\.]*)\.?([^\.]*)\.?([^\.]*)$/$1$2$3/; + return $koha_version; +} + 1; __END__ diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt index c43c5fb2ee..5e7dd41aed 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt @@ -153,13 +153,13 @@ [% plugin.metadata.description | html %] - [% IF ( plugin.metadata.minimum_version && koha_version < plugin.metadata.minimum_version ) %] + [% IF ( plugin.is_for_newer_koha ) %]
Warning: This plugin was written for a newer version of Koha. Use at your own risk.
[% END %] - [% IF ( plugin.metadata.maximum_version && koha_version > plugin.metadata.maximum_version ) %] + [% IF ( plugin.is_for_older_koha ) %]
Warning: This plugin was written for an older version of Koha. Use at your own risk.
diff --git a/plugins/plugins-home.pl b/plugins/plugins-home.pl index c67156222d..c5c2e45966 100755 --- a/plugins/plugins-home.pl +++ b/plugins/plugins-home.pl @@ -46,7 +46,6 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( if ($plugins_enabled) { $template->param( - koha_version => C4::Context->preference("Version"), method => $method, ); -- 2.35.4