3 # Copyright 2010 Kyle M Hall <kyle.m.hall@gmail.com>
5 # This file is part of Koha.
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.
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.
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>.
24 use JSON qw( from_json );
25 use LWP::Simple qw( get );
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_html_with_http_headers );
32 my $plugins_enabled = C4::Context->config("enable_plugins");
35 my $method = $input->param('method');
36 my $plugin_search = $input->param('plugin-search');
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39 { template_name => ($plugins_enabled) ? "plugins/plugins-home.tt" : "plugins/plugins-disabled.tt",
42 flagsrequired => { plugins => '*' },
46 if ($plugins_enabled) {
49 koha_version => C4::Context->preference("Version"),
53 my @plugins = Koha::Plugins->new()->GetPlugins(
61 $template->param( plugins => \@plugins, );
63 $template->param( can_search => C4::Context->config('plugin_repos') ? 1 : 0 );
66 my $repos = C4::Context->config('plugin_repos');
68 # Fix data structure if only one repo defined
69 if ( ref($repos->{repo}) eq 'HASH' ) {
70 $repos = { repo => [ $repos->{repo} ] };
73 foreach my $r ( @{ $repos->{repo} } ) {
74 if ( $r->{service} eq 'github' ) {
75 my $url = "https://api.github.com/search/repositories?q=$plugin_search+user:$r->{org_name}+in:name,description";
76 my $response = from_json( get($url) );
77 foreach my $result ( @{ $response->{items} } ) {
78 next unless $result->{name} =~ /^koha-plugin-/;
79 my $releases = $result->{url} . "/releases/latest";
80 my $release_info = get($releases);
81 next unless $release_info;
82 my $release = from_json( $release_info );
83 my $tag_name = $release->{tag_name};
84 for my $asset ( @{$release->{assets}} ) {
85 if ($asset->{browser_download_url} =~ m/\.kpz$/) {
86 $result->{install_name} = $asset->{name};
87 $result->{install_url} = $asset->{browser_download_url};
88 $result->{tag_name} = $tag_name;
91 push( @results, { repo => $r, result => $result } );
94 elsif ( $r->{service} eq 'gitlab' ) {
95 my $org_name = $r->{org_name};
96 my $url = "https://gitlab.com/api/v4/groups/$org_name/projects?with_issues_enabled=no\&with_merge_requests_enabled=no\&with_shared=no\&include_subgroups=yes\&search=koha-plugin+$plugin_search";
97 my $response = from_json( get($url) );
98 foreach my $result ( @{ $response } ) {
99 next unless $result->{name} =~ /^koha-plugin-/;
100 my $project_id = $result->{id};
101 my $description = $result->{description} // '';
102 my $web_url = $result->{web_url};
103 my $releases_url = "https://gitlab.com/api/v4/projects/$project_id/releases";
104 my @releases = @{ from_json( get($releases_url) ) };
106 if ( scalar @releases > 0 ) {
108 # Pick the first one, the latest release
109 my $latest = $releases[0];
110 my $name = $latest->{name};
111 my $tag_name = $latest->{tag_name};
112 my @links = @{ $latest->{assets}->{links} };
113 my $url = $links[0]->{direct_asset_url};
114 my @parts = split( '/', $url );
115 my $filename = $parts[-1];
116 next unless $url =~ m/\.kpz$/;
118 description => $description,
119 install_name => $filename,
121 html_url => $web_url,
123 tag_name => $tag_name,
125 push @results, { repo => $r, result => $result };
132 search_results => \@results,
133 search_term => $plugin_search,
138 output_html_with_http_headers( $input, $cookie, $template->output );