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

(-)a/Koha/Plugins/Utils.pm (+69 lines)
Line 0 Link Here
1
package Koha::Plugins::Utils;
2
3
# Copyright 2020 Prosentient Systems
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Archive::Extract;
22
use File::Temp;
23
use File::Copy;
24
25
sub download_plugin {
26
    my ($class, $args) = @_;
27
    my $plugin;
28
    my $url = $args->{url};
29
    if ($url){
30
31
        my ( $tfh, $tempfile ) = File::Temp::tempfile( UNLINK => 1 );
32
33
        my $ua = Mojo::UserAgent->new(max_redirects => 5);
34
        my $tx = $ua->get($url);
35
        $tx->result->content->asset->move_to($tempfile);
36
37
        $plugin = $tempfile;
38
    }
39
    return $plugin;
40
}
41
42
sub unzip_plugin {
43
    my ($class, $args) = @_;
44
    my $unzipped_plugin;
45
    my $zip = $args->{zip};
46
    if ($zip){
47
        my $staging_dir = File::Temp::tempdir( CLEANUP => 1 );
48
        my $ae = Archive::Extract->new( archive => $zip, type => 'zip' );
49
        $ae->extract( to => $staging_dir );
50
        $unzipped_plugin = $staging_dir;
51
    }
52
    return $unzipped_plugin;
53
}
54
55
sub copy_unzipped_plugin {
56
    my ($class, $args) = @_;
57
    my $copied;
58
    my $unzipped_plugin = $args->{unzipped_plugin};
59
    my $plugin_dir = $args->{plugin_dir};
60
    if ($unzipped_plugin && -d $unzipped_plugin && $plugin_dir && -d $plugin_dir ){
61
        #NOTE: Perl doesn't have a built-in recursive copy, so we use system instead
62
        if ( system("cp","-r","$unzipped_plugin/.",$plugin_dir) == 0 ){
63
           $copied = 1;
64
        }
65
    }
66
    return $copied;
67
}
68
69
1;
(-)a/debian/scripts/koha-plugin (+123 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
#
4
# Copyright 2020 Prosentient Systems
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
use Getopt::Long;
20
use Modern::Perl;
21
use Pod::Usage;
22
use URI;
23
use LWP::UserAgent;
24
25
my $help        = 0;
26
my $install     = 0;
27
my $uninstall   = 0;
28
GetOptions(
29
    'help'      => \$help,
30
    'install'   => \$install,
31
    'uninstall' => \$uninstall,
32
);
33
usage() if $help;
34
usage() unless ($install || $uninstall);
35
36
my $plugin_arg = shift @ARGV;
37
my @instances = @ARGV;
38
39
if ($install){
40
    my $plugin_uri = URI->new($plugin_arg);
41
    if ($plugin_uri){
42
        if ( _is_file_uri($plugin_uri) ){
43
            warn "file";
44
        }
45
        elsif ( _is_http_uri($plugin_uri) ){
46
            warn "http";
47
            my $ua = LWP::UserAgent->new();
48
            my $response = $ua->get($plugin_uri);
49
            if ($response->is_success){
50
                warn "Success";
51
            }
52
            else {
53
                warn "Unable to GET $plugin_uri";
54
                die $response->status_line;
55
            }
56
57
        }
58
    }
59
}
60
elsif ($uninstall){
61
    my $plugin_class = $plugin_arg; #Koha::Plugin::Com::Company::Plugin
62
    #Switch to
63
    warn $plugin_class;
64
65
}
66
67
sub _is_http_uri {
68
    my ($uri) = @_;
69
    my $is_http = 0;
70
    #NOTE: This includes both http and https URIs
71
    if ($uri->isa("URI::http")){
72
        $is_http = 1;
73
    }
74
    return $is_http;
75
}
76
77
sub _is_file_uri {
78
    my ($uri) = @_;
79
    my $is_file = 0;
80
    if ( (! defined $uri->scheme) || ($uri->scheme eq 'file') ){
81
        $is_file = 1;
82
    }
83
    return $is_file;
84
}
85
86
sub usage {
87
    pod2usage( -verbose => 1, -message => "koha-plugin\n\nThis script lets you manage Koha plugins for your Koha instances.\n", -width => 90 );
88
    exit;
89
}
90
91
=head1 NAME
92
93
koha-plugin - This script lets you manage Koha plugins for your Koha instances
94
95
=head1 USAGE
96
97
=over
98
99
=item koha-plugin --install file:///koha-plugin.kpz instancename1 [instancename2]
100
101
=item koha-plugin --install https://localhost/koha-plugin.kpz instancename1 [instancename2]
102
103
=item koha-plugin --uninstall plugin_class instancename1 [instancename2]
104
105
=item koha-plugin -h|--help
106
107
=back
108
109
=over
110
111
=item --install
112
113
Install the plugin for the specified instances
114
115
=item --uninstall
116
117
Uninstall the plugin for the specified instances
118
119
=item --help|h
120
121
Display this help message
122
123
=back
(-)a/misc/koha_plugin.pl (-1 / +146 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
#
4
# Copyright 2020 Prosentient Systems
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
use Getopt::Long;
20
use Modern::Perl;
21
use Pod::Usage;
22
use URI;
23
use Mojo::UserAgent;
24
25
use Koha::Plugins;
26
use Koha::Plugins::Handler;
27
use Koha::Plugins::Utils;
28
29
my $plugins_dir = C4::Context->config("pluginsdir");
30
31
my $help        = 0;
32
my $install     = '';
33
my $uninstall   = '';
34
GetOptions(
35
    'help'      => \$help,
36
    'install=s'   => \$install,
37
    'uninstall=s' => \$uninstall,
38
);
39
usage() if $help;
40
usage() unless ($install || $uninstall);
41
42
my $plugin_arg = shift @ARGV;
43
my @instances = @ARGV;
44
45
if ($install){
46
    my $plugin_arg = $install;
47
    my $plugin_uri = URI->new($plugin_arg);
48
    if ($plugin_uri){
49
        my $plugin;
50
        if ( _is_file_uri($plugin_uri) ){
51
52
            warn "file";
53
        }
54
        elsif ( _is_http_uri($plugin_uri) ){
55
            $plugin = Koha::Plugins::Utils->download_plugin({
56
                url => $plugin_uri->as_string,
57
            });
58
        }
59
        if ($plugin){
60
            my $unzipped_plugin;
61
            if ( -f $plugin ){
62
                $unzipped_plugin = Koha::Plugins::Utils->unzip_plugin({
63
                    zip => $plugin,
64
                });
65
            }
66
            elsif ( -d $plugin ){
67
                $unzipped_plugin = $plugin;
68
            }
69
            if ($unzipped_plugin){
70
                my $copied = Koha::Plugins::Utils->copy_unzipped_plugin({
71
                    unzipped_plugin => $unzipped_plugin,
72
                    plugin_dir => $plugins_dir,
73
                });
74
                if ($copied){
75
                    my @installed_plugins = Koha::Plugins->new()->InstallPlugins();
76
                    warn scalar @installed_plugins;
77
                }
78
            }
79
        }
80
    }
81
}
82
elsif ($uninstall){
83
    my $class = $uninstall;
84
    Koha::Plugins::Handler->delete( { class => $class } );
85
    #FIXME: The above method should return more meaningful output
86
}
87
88
sub _is_http_uri {
89
    my ($uri) = @_;
90
    my $is_http = 0;
91
    #NOTE: This includes both http and https URIs
92
    if ($uri->isa("URI::http")){
93
        $is_http = 1;
94
    }
95
    return $is_http;
96
}
97
98
sub _is_file_uri {
99
    my ($uri) = @_;
100
    my $is_file = 0;
101
    if ( (! defined $uri->scheme) || ($uri->scheme eq 'file') ){
102
        $is_file = 1;
103
    }
104
    return $is_file;
105
}
106
107
sub usage {
108
    pod2usage( -verbose => 1, -message => "koha-plugin\n\nThis script lets you manage Koha plugins for your Koha instances.\n", -width => 90 );
109
    exit;
110
}
111
112
=head1 NAME
113
114
koha_plugin.pl - This script lets you manage Koha plugins for your Koha instance
115
116
=head1 USAGE
117
118
=over
119
120
=item koha_plugin.pl --install file:///unzipped_koha-plugin
121
122
=item koha_plugin.pl --install file:///koha-plugin.kpz
123
124
=item koha_plugin.pl --install https://localhost/koha-plugin.kpz
125
126
=item koha_plugin.pl --uninstall plugin_class
127
128
=item koha_plugin.pl -h|--help
129
130
=back
131
132
=over
133
134
=item --install
135
136
Install the plugin for the specified instances
137
138
=item --uninstall
139
140
Uninstall the plugin for the specified instances
141
142
=item --help|h
143
144
Display this help message
145
146
=back

Return to bug 25671