From ef9bde11c3cd196d15a9f8055fd57bf33d06d419 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Wed, 12 Feb 2020 14:43:01 +0000
Subject: [PATCH] Bug 23975: Add 'Install' support for github results
---
.../prog/en/modules/plugins/plugins-home.tt | 3 +--
plugins/plugins-home.pl | 8 ++++++
plugins/plugins-upload.pl | 25 +++++++++++++------
3 files changed, 27 insertions(+), 9 deletions(-)
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 3ae2cd5e3a..09204f5e3e 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
@@ -71,7 +71,7 @@
<td><a href="[% sr.result.html_url | url %]" target="_new">[% sr.result.name %]</a></td>
<td>[% sr.result.description %]</td>
<td>[% sr.repo.name %]</td>
- <td><button class="btn btn-install-plugin"><i class="fa fa-download"></i> Install</button></td>
+ <td><a class="btn btn-install-plugin" href="/cgi-bin/koha/plugins/plugins-upload.pl?op=Upload&uploadfile=[% sr.result.install_name | uri %]&uploadlocation=[% sr.result.install_url | uri %]"><i class="fa fa-download"></i> Install</a></td>
</tr>
[% END %]
</table>
@@ -196,7 +196,6 @@
[% INCLUDE 'calendar.inc' %]
<script>
$(document).ready(function(){
- $(".btn-install-plugin").on("click", function() { alert("Sorry, this functionality doesn't exist yet!"); });
$(".uninstall_plugin").on("click", function(){
$(".dropdown").removeClass("open");
var plugin_name = $(this).data("plugin-name");
diff --git a/plugins/plugins-home.pl b/plugins/plugins-home.pl
index 0c17d9b213..0e7de024c5 100755
--- a/plugins/plugins-home.pl
+++ b/plugins/plugins-home.pl
@@ -69,6 +69,14 @@ if ($plugins_enabled) {
my $response = from_json( get($url) );
foreach my $result ( @{ $response->{items} } ) {
next unless $result->{name} =~ /^koha-plugin-/;
+ my $releases = $result->{url} . "/releases/latest";
+ my $release = from_json( get($releases) );
+ for my $asset ( @{$release->{assets}} ) {
+ if ($asset->{browser_download_url} =~ m/\.kpz$/) {
+ $result->{install_name} = $asset->{name};
+ $result->{install_url} = $asset->{browser_download_url};
+ }
+ }
push( @results, { repo => $r, result => $result } );
}
}
diff --git a/plugins/plugins-upload.pl b/plugins/plugins-upload.pl
index c849942c47..7f873fa4fd 100755
--- a/plugins/plugins-upload.pl
+++ b/plugins/plugins-upload.pl
@@ -20,6 +20,7 @@ use Modern::Perl;
use Archive::Extract;
use CGI qw ( -utf8 );
+use Mojo::UserAgent;
use File::Copy;
use File::Temp;
@@ -46,6 +47,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
my $uploadfilename = $input->param('uploadfile');
my $uploadfile = $input->upload('uploadfile');
+my $uploadlocation = $input->param('uploadlocation');
my $op = $input->param('op') || q{};
my ( $total, $handled, @counts, $tempfile, $tfh );
@@ -53,7 +55,7 @@ my ( $total, $handled, @counts, $tempfile, $tfh );
my %errors;
if ($plugins_enabled) {
- if ( ( $op eq 'Upload' ) && $uploadfile ) {
+ if ( ( $op eq 'Upload' ) && ( $uploadfile || $uploadlocation ) ) {
my $plugins_dir = C4::Context->config("pluginsdir");
$plugins_dir = ref($plugins_dir) eq 'ARRAY' ? $plugins_dir->[0] : $plugins_dir;
@@ -69,15 +71,24 @@ if ($plugins_enabled) {
$errors{'NOTKPZ'} = 1 if ( $uploadfilename !~ /\.kpz$/i );
$errors{'NOWRITETEMP'} = 1 unless ( -w $dirname );
$errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir );
- $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
+
+ if ( $uploadlocation ) {
+ my $ua = Mojo::UserAgent->new(max_redirects => 5);
+ my $tx = $ua->get($uploadlocation);
+ $tx->result->save_to($tempfile);
+ } else {
+ $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
+ }
if (%errors) {
$template->param( ERRORS => [ \%errors ] );
} else {
- while (<$uploadfile>) {
- print $tfh $_;
+ if ( $uploadfile ) {
+ while (<$uploadfile>) {
+ print $tfh $_;
+ }
+ close $tfh;
}
- close $tfh;
my $ae = Archive::Extract->new( archive => $tempfile, type => 'zip' );
unless ( $ae->extract( to => $plugins_dir ) ) {
@@ -90,11 +101,11 @@ if ($plugins_enabled) {
Koha::Plugins->new()->InstallPlugins();
}
- } elsif ( ( $op eq 'Upload' ) && !$uploadfile ) {
+ } elsif ( ( $op eq 'Upload' ) && !$uploadfile && !$uploadlocation ) {
warn "Problem uploading file or no file uploaded.";
}
- if ( $uploadfile && !%errors && !$template->param('ERRORS') ) {
+ if ( ($uploadfile || $uploadlocation) && !%errors && !$template->param('ERRORS') ) {
print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
} else {
output_html_with_http_headers $input, $cookie, $template->output;
--
2.20.1