blib/lib/PGXN/API/Indexer.pm

filter: Statementstmt 96.9% 626 / 646 Branchbran 77.5% 180 / 232 Conditioncond 69.2% 81 / 117 MC/DCmcdc 46.9% 31 / 66 Subroutinesub 96.8% 61 / 63 Podpod 100% 17 / 17 totaltotal 87.2% 996 / 1141 CC 182 SCAR 27.5
CC
182
Cov
89%
_clean_html_body
42.4
_idx_distmeta
30.0
update_extensions
21.7
CRAP 15.6
« blib/lib/PGXN/API/Sync.pm ↑ Summary Build »
1 package PGXN::API::Indexer;
2  
3 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use PGXN::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Spec::Functions qw(catfile catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Copy::Recursive qw(fcopy dircopy);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Text::Markup;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Text::Markup::None qr/te?xt/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Lucy::Plan::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Lucy::Analysis::PolyAnalyzer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Lucy::Analysis::RegexTokenizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Lucy::Index::Indexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Archive::Zip qw(AZ_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 our $VERSION = v0.22.0;
24  
25 3 has verbose => (is => 'rw', isa => 'Int', default => 0);
26 3 has _index_it => (is => 'rw', isa => 'Bool', default => 1);
27 has to_index => (is => 'ro', isa => 'HashRef', default => sub { +{
28 2 ▶ map { $_ => [] } qw(docs dists extensions users tags)
Subroutine __ANON__: called CC=1 SCAR=0.0
29 3 } });
30  
31 2 ▶has _user_names => (is => 'ro', isa => 'HashRef', default => sub { +{ } });
Subroutine __ANON__: called CC=1 SCAR=0.0
32  
33 has libxml => (is => 'ro', isa => 'XML::LibXML', lazy => 1, default => sub {
34 1 ▶ XML::LibXML->new(
Subroutine __ANON__: called CC=1 SCAR=0.0
35 recover => 2,
36 no_network => 1,
37 no_blanks => 1,
38 no_cdata => 1,
39 );
40 3 });
41  
42 has index_dir => (is => 'ro', isa => 'Str', lazy => 1, default => sub {
43 1 ▶ my $dir = catdir +PGXN::API->instance->doc_root, '_index';
Subroutine __ANON__: called CC=2 SCAR=7.0
44 1 ▶ if (!-e $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dir) 1 0 1
45 1 require File::Path;
46 1 File::Path::make_path($dir);
47 }
48 1 $dir;
49 3 });
50  
51 has schemas => ( is => 'ro', isa => 'HashRef', lazy => 1, default => sub {
52 1 ▶ my $polyanalyzer = Lucy::Analysis::PolyAnalyzer->new(
Subroutine __ANON__: called CC=3 SCAR=11.0
53 language => 'en',
54 );
55  
56 1 my $fti = Lucy::Plan::FullTextType->new(
57 analyzer => $polyanalyzer,
58 highlightable => 0,
59 );
60  
61 1 my $ftih = Lucy::Plan::FullTextType->new(
62 analyzer => $polyanalyzer,
63 highlightable => 1,
64 );
65  
66 1 my $string = Lucy::Plan::StringType->new(
67 indexed => 1,
68 stored => 1,
69 );
70  
71 1 my $indexed = Lucy::Plan::StringType->new(
72 indexed => 1,
73 stored => 0,
74 );
75  
76 1 my $stored = Lucy::Plan::StringType->new(
77 indexed => 0,
78 stored => 1,
79 );
80  
81 1 my $list = Lucy::Plan::FullTextType->new(
82 indexed => 1,
83 stored => 1,
84 highlightable => 1,
85 analyzer => Lucy::Analysis::RegexTokenizer->new(
86 pattern => '[^\003]+'
87 ),
88 );
89  
90 1 my %schemas;
91 1 for my $spec (
92 [ docs => [
93 [ key => $indexed ],
94 [ title => $fti ],
95 [ abstract => $fti ],
96 [ body => $ftih ],
97 [ dist => $fti ],
98 [ version => $stored ],
99 [ docpath => $stored ],
100 [ date => $stored ],
101 [ user => $string ],
102 [ user_name => $fti ],
103 ]],
104 [ dists => [
105 [ key => $indexed ],
106 [ dist => $fti ],
107 [ abstract => $fti ],
108 [ description => $fti ],
109 [ readme => $ftih ],
110 [ tags => $list ],
111 [ version => $stored ],
112 [ date => $stored ],
113 [ user => $string ],
114 [ user_name => $fti ],
115 ]],
116 [ extensions => [
117 [ key => $indexed ],
118 [ extension => $fti ],
119 [ abstract => $ftih ],
120 [ docpath => $stored ],
121 [ dist => $stored ],
122 [ version => $stored ],
123 [ date => $stored ],
124 [ user => $string ],
125 [ user_name => $fti ],
126 ]],
127 [ users => [
128 [ key => $indexed ],
129 [ user => $fti ],
130 [ name => $fti ],
131 [ email => $string ],
132 [ uri => $string ],
133 [ details => $ftih ],
134 ]],
135 [ tags => [
136 [ key => $indexed ],
137 [ tag => $fti ],
138 ]],
139 ) {
140 5 my ($name, $fields) = @{ $spec };
141 5 my $schema = Lucy::Plan::Schema->new;
142 $schema->spec_field(name => $_->[0], type => $_->[1] )
143 5 for @{ $fields };
144 5 $schemas{$name} = $schema;
145 }
146 1 return \%schemas;
147 3 });
148  
149 sub indexer_for {
150 25 ▶ my ($self, $iname) = @_;
Subroutine indexer_for: called CC=1 SCAR=0.0
Pod documented
151 Lucy::Index::Indexer->new(
152 index => catdir($self->index_dir, $iname),
153 25 schema => $self->schemas->{$iname},
154 create => 1,
155 );
156 }
157  
158 sub update_root_json {
159 2 ▶ my $self = shift;
Subroutine update_root_json: called CC=2 SCAR=6.9
Pod documented
160 2 my $api = PGXN::API->instance;
161 2 ▶ say "Updating mirror root JSON" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 2 2
162  
163 # Augment and write index.json.
164 2 my $src = catfile $api->mirror_root, 'index.json';
165 2 my $dst = catfile $api->doc_root, 'index.json';
166 2 my $tmpl = $api->read_json_from($src);
167 2 $tmpl->{source} = "/src/{dist}/{dist}-{version}/";
168 2 $tmpl->{search} = '/search/{in}/';
169 2 $tmpl->{userlist} = '/users/{letter}.json';
170 2 ($tmpl->{htmldoc} = $tmpl->{meta}) =~ s{/META[.]json$}{/{+docpath}.html};
171 2 $api->write_json_to($dst, $tmpl);
172  
173 2 return $self;
174 }
175  
176 sub copy_from_mirror {
177 1 ▶ my $self = shift;
Subroutine copy_from_mirror: called CC=3 SCAR=11.2
Pod documented
178 1 my @path = split qr{/} => shift;
179 1 my $api = PGXN::API->instance;
180 1 my $src = catfile $api->mirror_root, @path;
181 1 my $dst = catfile $api->doc_root, @path;
182 1 ▶ say "Copying $src to $dst" if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 1 1
183 1 ▶ fcopy $src, $dst or die "Cannot copy $src to $dst: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
fcopy($src, $dst) or die "Cannot copy $src to ${dst}: $!\n" 0 1 1
184 }
185  
186 sub parse_from_mirror {
187 3 ▶ my ($self, $path, $format) = @_;
Subroutine parse_from_mirror: called CC=5 SCAR=16.2
Pod documented
188 3 my @path = split qr{/} => $path;
189 3 my $api = PGXN::API->instance;
190 3 my $src = catfile $api->mirror_root, @path;
191 3 my $dst = catfile $api->doc_root, @path;
192 3 my $mark = Text::Markup->new(default_encoding => 'UTF-8');
193 3 $dst =~ s/[.][^.]+$/.html/;
194  
195 3 ▶ say "Parsing $src to $dst" if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 3 3
196 3 ▶ my $html = $mark->parse(
Branch: 1 branch
BranchTrueFalseTotal
my $html = $mark->parse("file", $src, "format", $format) or return $self 1 2 3
197 file => $src,
198 format => $format,
199 ) or return $self;
200  
201 2 my $doc = $self->_parse_html_string($html);
202  
203 2 make_path dirname $dst;
204 2 ▶ open my $fh, '>:utf8', $dst or die "Cannot open $dst: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:utf8", $dst or die "Cannot open ${dst}: $!\n" 0 2 2
205 2 $doc = $self->_clean_html_body($doc->findnodes('/html/body'));
206 2 print $fh $doc->toString, "\n";
207 2 ▶ close $fh or die "Cannot close $dst: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${dst}: $!\n" 0 2 2
208  
209 2 return $self;
210 }
211  
212 sub add_distribution {
213 4 ▶ my ($self, $params) = @_;
Subroutine add_distribution: called CC=6 SCAR=19.1
Pod documented
214  
215 # merge_distmeta will set _index_it, but we don't want it changed after this
216 # method finishes.
217 4 local $self->{_index_it} = 1;
218 4 ▶ $self->copy_files($params) or return $self->_rollback;
Branch: 1 branch
BranchTrueFalseTotal
$self->copy_files($params) or return $self->_rollback 0 4 4
219 4 ▶ $self->merge_distmeta($params) or return $self->_rollback;
Branch: 1 branch
BranchTrueFalseTotal
$self->merge_distmeta($params) or return $self->_rollback 0 4 4
220 4 ▶ $self->update_user($params) or return $self->_rollback;
Branch: 1 branch
BranchTrueFalseTotal
$self->update_user($params) or return $self->_rollback 0 4 4
221 4 ▶ $self->update_tags($params) or return $self->_rollback;
Branch: 1 branch
BranchTrueFalseTotal
$self->update_tags($params) or return $self->_rollback 0 4 4
222 4 ▶ $self->update_extensions($params) or return $self->_rollback;
Branch: 1 branch
BranchTrueFalseTotal
$self->update_extensions($params) or return $self->_rollback 0 4 4
223 4 return $self->_commit;
224 }
225  
226 sub copy_files {
227 5 ▶ my ($self, $p) = @_;
Subroutine copy_files: called CC=7 SCAR=19.5
Pod documented
228 5 my $meta = $p->{meta};
229 5 ▶ say " Copying \L$meta->{name}-$meta->{version}\E files" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 5 5
230  
231 # Need to copy the README, zip file, and dist meta file.
232 5 for my $file (qw(download readme)) {
233 9 my $src = $self->mirror_file_for($file => $meta);
234 9 my $dst = $self->doc_root_file_for($file => $meta);
235 9 ▶ next if $file eq 'readme' && !-e $src;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "readme" and not -e $src 2 7 9
Condition100%3/3
$file eq "readme" and not -e $src
!ll&&!rl&&r
522
MC/DC100%2/2
$file eq "readme" and not -e $src
$file eq "readme" not -e $src
Truth table100%3/3
A and not(B)
A= $file eq "readme"B= not -e $src
ABresult
0X0
100
111
236 7 ▶ say " \L$meta->{name}-$meta->{version}\E.$file" if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 7 7
237 7 ▶ fcopy $src, $dst or die "Cannot copy $src to $dst: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
fcopy($src, $dst) or die "Cannot copy $src to ${dst}: $!\n" 1 6 7
238 }
239 4 return $self;
240 }
241  
242 sub merge_distmeta {
243 6 ▶ my ($self, $p) = @_;
Subroutine merge_distmeta: called CC=20 SCAR=30.5
Pod documented
244 6 my $meta = $p->{meta};
245 6 ▶ say " Merging \L$meta->{name}-$meta->{version}\E META.json" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 6 6
246  
247 # Merge the list of versions into the meta file.
248 6 my $api = PGXN::API->instance;
249 6 my $dist_file = $self->mirror_file_for(dist => $meta);
250 6 my $dist_meta = $api->read_json_from($dist_file);
251 6 $meta->{releases} = $dist_meta->{releases};
252  
253 # Determine whether to full-text index this distribution.
254 $self->_index_it(
255 # Always index a stable release.
256 $meta->{release_status} eq 'stable'
257 # Index if testing and no stable.
258 || ($meta->{release_status} eq 'testing' && !$meta->{releases}{stable})
259 # Index if there are only unstable releases.
260 || !$meta->{releases}{testing}
261 6 ▶ );
Condition55%5/9
$meta->{'release_status'} eq 'stable' || $meta->{'release_status'} eq 'testing' && !$meta->{'releases'}{'stable'} || !$meta->{'releases'}{'testing'}
l!l&&r!l&&!r
402
$meta->{"release_status"} eq "stable" or $meta->{"release_status"} eq "testing" and not $meta->{"releases"}{"stable"}
l!l&&r!l&&!r
402
$meta->{"release_status"} eq "testing" and not $meta->{"releases"}{"stable"}
!ll&&!rl&&r
020
MC/DC25%1/4
$meta->{'release_status'} eq 'stable' || $meta->{'release_status'} eq 'testing' && !$meta->{'releases'}{'stable'} || !$meta->{'releases'}{'testing'}
$meta->{"release_status"} eq "stable" $meta->{"release_status"} eq "testing" not $meta->{"releases"}{"stable"} !$meta->{'releases'}{'testing'}
Truth table33%2/6
A or B and not(C) || not(D)
A= $meta->{"release_status"} eq "stable"B= $meta->{"release_status"} eq "testing"C= not $meta->{"releases"}{"stable"}D= !$meta->{'releases'}{'testing'}
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
262  
263 # Add a list of special files and docs.
264 6 $meta->{special_files} = $self->_source_files($p);
265 6 $meta->{docs} = $self->parse_docs($p);
266  
267 # Add doc paths to provided extensions where appropriate.
268 6 ▶ while (my ($noext_path, $data) = each %{ $meta->{docs} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$meta{"docs"};}) 9 6 15
269 9 ▶ if (my $ext = delete $data->{extension}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ext = delete $data->{"extension"}) 1 8 9
270 # The extension referenced a docfile. Use it.
271 1 ▶ my $d = $meta->{provides}{$ext}
Condition50%1/2
$meta->{'provides'}{$ext} || die("Extension $ext missing from meta spec")
l!l
10
MC/DC0%0/1
$meta->{'provides'}{$ext} || die("Extension $ext missing from meta spec")
$meta->{'provides'}{$ext}
Truth table50%1/2
A
A= $meta->{'provides'}{$ext}
Aresult
11
00
272 || die "Extension $ext missing from meta spec";
273 1 $d->{docpath} = $noext_path;
274 1 next;
275 }
276  
277 # No doc specified or it wasn't found. Try to find a likely candidate
278 # where the extension name is the file base name.
279 8 my ($basename) = $noext_path =~ m{([^/]+)$};
280 8 ▶ if (my $d = $meta->{provides}{$basename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $meta->{"provides"}{$basename}) 5 3 8
281 5 ▶ $d->{docpath} ||= $noext_path;
Condition66%2/3
$d->{"docpath"} ||= $noext_path
l!l&&r!l&&!r
140
MC/DC0%0/2
$d->{"docpath"} ||= $noext_path
$d->{"docpath"} $noext_path
Truth table66%2/3
A ||= B
A= $d->{"docpath"}B= $noext_path
ABresult
1X1
011
000
282 }
283 }
284  
285 # Write the merge metadata to the file.
286 6 my $fn = $self->doc_root_file_for(meta => $meta);
287 6 make_path dirname $fn;
288 6 $api->write_json_to($fn, $meta);
289  
290 6 $dist_file = $self->doc_root_file_for(dist => $meta);
291 6 ▶ if ($meta->{release_status} eq 'stable') {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{'release_status'} eq 'stable') { } 4 2 6
292 # Copy it to its dist home.
293 4 ▶ fcopy $fn, $dist_file or die "Cannot copy $fn to $dist_file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
fcopy($fn, $dist_file) or die "Cannot copy $fn to ${dist_file}: $!\n" 0 4 4
294 } else {
295 # Determine latest stable release or fall back on testing, unstable.
296 2 for my $status (qw(stable testing unstable)) {
297 2 ▶ my $rels = $meta->{releases}{$status} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $rels = $meta->{"releases"}{$status} or next 0 2 2
298 2 $meta->{version} = $rels->[0]{version};
299 2 last;
300 }
301  
302 # Now write out the dist version.
303 2 $api->write_json_to($dist_file => $meta);
304 }
305  
306 # Index it.
307 $self->_index(dists => {
308 key => lc $meta->{name},
309 dist => $meta->{name},
310 abstract => $meta->{abstract},
311 description => $meta->{description} || '',
312 readme => $self->_readme($p),
313 6 ▶ tags => join("\003" => @{ $meta->{tags} || [] }),
Branch: 1 branch
BranchTrueFalseTotal
$meta->{"tags"} or [] 0 6 6
314 version => $meta->{version},
315 date => $meta->{date},
316 user_name => $self->_get_user_name($meta),
317 user => $meta->{user},
318 6 ▶ });
Condition50%1/2
$meta->{'description'} || ''
l!l
60
MC/DC0%0/1
$meta->{'description'} || ''
$meta->{'description'}
Truth table50%1/2
A
A= $meta->{'description'}
Aresult
11
00
319  
320 # Now update all older versions with the complete list of releases.
321 6 for my $releases ( values %{ $meta->{releases} }) {
322 11 for my $release (@{ $releases}) {
323 16 ▶ next if $release->{version} eq $meta->{version};
Branch: 1 branch
BranchTrueFalseTotal
if $release->{"version"} eq $meta->{"version"} 6 10 16
324 10 local $meta->{version} = $release->{version};
325  
326 10 my $vmeta_file = $self->doc_root_file_for(meta => $meta);
327 10 ▶ next unless -e $vmeta_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $vmeta_file 1 9 10
328 9 my $vmeta = $api->read_json_from($vmeta_file);
329 9 $vmeta->{releases} = $meta->{releases};
330 9 $api->write_json_to($vmeta_file => $vmeta);
331 }
332 }
333  
334 6 return $self;
335 }
336  
337 sub merge_user {
338 3 ▶ my ($self, $nick) = @_;
Subroutine merge_user: called CC=5 SCAR=16.1
Pod documented
339 3 ▶ return $self if $self->_user_names->{$nick};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_user_names->{$nick} 1 2 3
340  
341 2 ▶ say " Merging user $nick" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 2 2
342 2 my $api = PGXN::API->instance;
343 2 my $mir_file = $self->mirror_file_for('user', undef, user => $nick);
344 2 my $mir_data = $api->read_json_from($mir_file);
345 2 my $doc_file = $self->doc_root_file_for('user', undef, user => $nick);
346 2 ▶ my $doc_data = -e $doc_file ? $api->read_json_from($doc_file) : {};
Branch: 1 branch
BranchTrueFalseTotal
-e $doc_file ? : 1 1 2
347  
348 # Merge in the releases and rewrite.
349 2 ▶ $mir_data->{releases} = $doc_data->{releases} || {};
Condition100%2/2
$doc_data->{'releases'} || {}
l!l
11
MC/DC100%1/1
$doc_data->{'releases'} || {}
$doc_data->{'releases'}
Truth table100%2/2
A
A= $doc_data->{'releases'}
Aresult
11
00
350 2 $api->write_json_to($doc_file, $mir_data);
351  
352 # Update the full name lookup & the search index and return.
353 2 $self->_user_names->{lc $nick} = $mir_data->{name};
354 2 $self->_index_user($mir_data);
355 2 return $self;
356 }
357  
358 sub update_user {
359 6 ▶ my ($self, $p) = @_;
Subroutine update_user: called CC=2 SCAR=7.0
Pod documented
360 6 ▶ say " Updating user $p->{meta}{user}" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 6 6
361 6 my $user = $self->_update_releases(user => $p->{meta});
362 6 $self->_index_user($user);
363 6 return $self;
364 }
365  
366 sub update_tags {
367 6 ▶ my ($self, $p) = @_;
Subroutine update_tags: called CC=5 SCAR=16.4
Pod documented
368 6 my $meta = $p->{meta};
369 6 ▶ say " Updating \L$meta->{name}-$meta->{version}\E tags" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 6 6
370  
371 6 ▶ my $tags = $meta->{tags} or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $tags = $meta->{"tags"} or return $self 0 6 6
372  
373 6 for my $tag (@{ $tags }) {
374 16 ▶ say " $tag" if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 16 16
375 16 my $data = $self->_update_releases(tag => $meta, tag => lc $tag);
376 16 $self->_index(tags => { key => lc $tag, tag => $tag });
377 }
378 6 return $self;
379 }
380  
381 sub update_extensions {
382 8 ▶ my ($self, $p) = @_;
Subroutine update_extensions: called CC=21 SCAR=30.8
Pod documented
383 8 my $meta = $p->{meta};
384 8 my $api = PGXN::API->instance;
385 8 ▶ say " Updating \L$meta->{name}-$meta->{version}\E extensions"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 8 8
386 if $self->verbose;
387  
388 8 ▶ while (my ($ext, $data) = each %{ $meta->{provides} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$meta{"provides"};}) 8 8 16
389 8 ▶ say " $ext" if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 8 8
390 8 $ext = lc $ext;
391 # Read in extension metadata from the mirror.
392 8 my $mir_file = $self->mirror_file_for(
393 extension => $meta,
394 extension => $ext,
395 );
396 8 my $mir_meta = $api->read_json_from($mir_file);
397  
398 # Read in extension metadata from the doc root.
399 8 my $doc_file = $self->doc_root_file_for(
400 extension => $meta,
401 extension => $ext,
402 );
403 8 ▶ my $doc_meta = -e $doc_file ? $api->read_json_from($doc_file) : {};
Branch: 1 branch
BranchTrueFalseTotal
-e $doc_file ? : 7 1 8
404  
405 # Add the abstract and doc path to the mirror data.
406 8 my $status = $meta->{release_status};
407 8 $mir_meta->{$status}{abstract} = $data->{abstract};
408 8 ▶ $mir_meta->{$status}{docpath} = $data->{docpath} if $data->{docpath};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"docpath"} 6 2 8
409  
410 # Copy the other release status data from the doc data.
411 8 $mir_meta->{$_} = $doc_meta->{$_} for grep {
412 24 ▶ $doc_meta->{$_} && $_ ne $status
Branch: 1 branch
BranchTrueFalseTotal
$doc_meta->{$_} and $_ ne $status 13 11 24
413 } qw(stable testing unstable);
414  
415 # Copy the version info from the doc to the mirror and add the date.
416 8 my $version = $data->{version};
417 8 my $mir_vers = $mir_meta->{versions};
418 8 my $mir_dists = $mir_vers->{$version};
419 8 ▶ my $doc_dists = $doc_meta->{versions}{$version} ||= [];
Condition100%2/2
$doc_meta->{"versions"}{$version} ||= []
l!l
44
MC/DC100%1/1
$doc_meta->{"versions"}{$version} ||= []
$doc_meta->{"versions"}{$version}
Truth table100%2/2
A
A= $doc_meta->{"versions"}{$version}
Aresult
11
00
420  
421 # Copy the doc root versions.
422 8 $mir_meta->{versions} = $doc_meta->{versions};
423  
424 # Find the current release distribution in the versions.
425 8 for my $i (0..$#$mir_dists) {
426 11 my $dist = $mir_dists->[$i];
427 # Make sure the doc dists are in sync.
428 11 ▶ if (!$doc_dists->[$i]
Branch: 1 branch
BranchTrueFalseTotal
if (not $doc_dists->[$i] or $dist->{"dist"} ne $doc_dists->[$i]{"dist"} or $dist->{"version"} ne $doc_dists->[$i]{"version"}) 1 6 7
Condition83%5/6
not $doc_dists->[$i] or $dist->{"dist"} ne $doc_dists->[$i]{"dist"} or $dist->{"version"} ne $doc_dists->[$i]{"version"}
l!l&&r!l&&!r
506
not $doc_dists->[$i] or $dist->{"dist"} ne $doc_dists->[$i]{"dist"}
l!l&&r!l&&!r
416
MC/DC66%2/3
not $doc_dists->[$i] or $dist->{"dist"} ne $doc_dists->[$i]{"dist"} or $dist->{"version"} ne $doc_dists->[$i]{"version"}
not $doc_dists->[$i] $dist->{"dist"} ne $doc_dists->[$i]{"dist"} $dist->{"version"} ne $doc_dists->[$i]{"version"}
Truth table75%3/4
not(A) or B or C
A= not $doc_dists->[$i]B= $dist->{"dist"} ne $doc_dists->[$i]{"dist"}C= $dist->{"version"} ne $doc_dists->[$i]{"version"}
ABCresult
1XX1
01X1
0011
0000
429 || $dist->{dist} ne $doc_dists->[$i]{dist}
430 || $dist->{version} ne $doc_dists->[$i]{version}
431 ) {
432 5 splice @{ $doc_dists }, $i, 0, $dist;
433 }
434  
435 # Is this the distribution we're currently updating?
436 11 ▶ if ($dist->{dist} eq $meta->{name}
Branch: 1 branch
BranchTrueFalseTotal
if ($dist->{"dist"} eq $meta->{"name"} and $dist->{"version"} eq $meta->{"version"}) 8 3 11
Condition66%2/3
$dist->{"dist"} eq $meta->{"name"} and $dist->{"version"} eq $meta->{"version"}
!ll&&!rl&&r
308
MC/DC50%1/2
$dist->{"dist"} eq $meta->{"name"} and $dist->{"version"} eq $meta->{"version"}
$dist->{"dist"} eq $meta->{"name"} $dist->{"version"} eq $meta->{"version"}
Truth table66%2/3
A and B
A= $dist->{"dist"} eq $meta->{"name"}B= $dist->{"version"} eq $meta->{"version"}
ABresult
0X0
100
111
437 && $dist->{version} eq $meta->{version}
438 ) {
439 # Got it. Add the release date.
440 8 $dist->{date} = $meta->{date};
441 }
442 }
443  
444 # Remove any deleted versions.
445 8 my $ddvers = $doc_meta->{versions};
446 8 for my $v (keys %{ $ddvers }) {
447 21 ▶ delete $ddvers->{$v} unless $mir_vers->{$v};
Branch: 1 branch
BranchTrueFalseTotal
unless $mir_vers->{$v} 1 20 21
448 }
449  
450 # Only index testing if no stable, and unstable if no testing.
451 my $release = $mir_meta->{stable}
452 || $mir_meta->{testing}
453 8 ▶ || $mir_meta->{unstable};
Condition33%2/6
$mir_meta->{'stable'} || $mir_meta->{'testing'} || $mir_meta->{'unstable'}
l!l&&r!l&&!r
800
$mir_meta->{"stable"} or $mir_meta->{"testing"}
l!l&&r!l&&!r
800
MC/DC0%0/3
$mir_meta->{'stable'} || $mir_meta->{'testing'} || $mir_meta->{'unstable'}
$mir_meta->{"stable"} $mir_meta->{"testing"} $mir_meta->{'unstable'}
Truth table25%1/4
A or B || C
A= $mir_meta->{"stable"}B= $mir_meta->{"testing"}C= $mir_meta->{'unstable'}
ABCresult
1XX1
01X1
0011
0000
454  
455 # Write it back out and index it.
456 8 $api->write_json_to($doc_file => $mir_meta);
457 $self->_index(extensions => {
458 key => lc $mir_meta->{extension},
459 extension => $mir_meta->{extension},
460 abstract => $release->{abstract} || '',
461 docpath => $data->{docpath} || '',
462 dist => $meta->{name},
463 version => $release->{version},
464 date => $meta->{date},
465 user_name => $self->_get_user_name($meta),
466 user => $meta->{user},
467 8 ▶ });
Condition75%3/4
$release->{'abstract'} || ''
l!l
80
$data->{'docpath'} || ''
l!l
62
MC/DC0%0/1
$release->{'abstract'} || ''
$release->{'abstract'}
MC/DC100%1/1
$data->{'docpath'} || ''
$data->{'docpath'}
Truth table50%1/2
A
A= $release->{'abstract'}
Aresult
11
00
Truth table100%2/2
A
A= $data->{'docpath'}
Aresult
11
00
468 }
469  
470 8 return $self;
471 }
472  
473 sub _start_doc {
474 37 ▶ my ($self, $meta, $fn, $ext) = @_;
Subroutine _start_doc: called CC=1 SCAR=0.0
475 37 (my $noext = $fn) =~ s{[.][^.]+$}{};
476 return {
477 37 filename => $fn,
478 extension => $ext,
479 noext => $noext,
480 dest => $self->doc_root_file_for(
481 htmldoc => $meta,
482 docpath => $noext,
483 ),
484 };
485 }
486  
487 sub find_docs {
488 19 ▶ my ($self, $p) = @_;
Subroutine find_docs: called CC=13 SCAR=25.7
Pod documented
489 19 my $meta = $p->{meta};
490 19 my $dir = $self->doc_root_file_for(source => $meta);
491 19 my $prefix = quotemeta lc "$meta->{name}-$meta->{version}";
492 19 ▶ my $skip = { directory => [], file => [], %{ $meta->{no_index} || {} } };
Branch: 1 branch
BranchTrueFalseTotal
$meta->{"no_index"} or {} 13 6 19
493 19 my $markup = Text::Markup->new;
494 19 my (%seen, @docs);
495 19 ▶ while (my ($ext, $info) = each %{ $meta->{provides} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$meta{"provides"};}) 19 19 38
496 19 my $fn = $info->{docfile};
497 19 ▶ next unless $fn && $markup->guess_format($fn) && -e catfile $dir, $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn and $markup->guess_format($fn) and -e catfile($dir, $fn) 2 8 10
Condition100%6/6
$fn and $markup->guess_format($fn) and -e catfile($dir, $fn)
!ll&&!rl&&r
1018
$fn and $markup->guess_format($fn)
!ll&&!rl&&r
919
MC/DC100%3/3
$fn and $markup->guess_format($fn) and -e catfile($dir, $fn)
$fn $markup->guess_format($fn) -e catfile($dir, $fn)
Truth table100%4/4
A and B and C
A= $fnB= $markup->guess_format($fn)C= -e catfile($dir, $fn)
ABCresult
0XX0
10X0
1100
1111
498 8 push @docs => $self->_start_doc($meta, $fn, $ext);
499 8 $seen{$fn}++;
500 }
501  
502 19 for my $member ($p->{zip}->members) {
503 275 ▶ next if $member->isDirectory;
Branch: 1 branch
BranchTrueFalseTotal
if $member->isDirectory 114 161 275
504  
505 # Skip files that should not be indexed.
506 161 (my $fn = $member->fileName) =~ s{^$prefix/}{};
507 161 ▶ next if $seen{$fn}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$fn}++ 3 158 161
508 158 ▶ next if first { $fn eq $_ } @{ $skip->{file} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $fn eq $_; } , @{$$skip{"file"};}) 2 156 158
509 156 ▶ next if first { $fn =~ /^\Q$_/ } @{ $skip->{directory} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $fn =~ /^\Q$_\E/u; } , @{$$skip{"directory"};}) 4 152 156
510 152 ▶ next unless $markup->guess_format($fn) || $fn =~ /^README(?:[.][^.]+)?$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $markup->guess_format($fn) or $fn =~ /^README(?:[.][^.]+)?$/iu 123 29 152
Condition66%2/3
$markup->guess_format($fn) or $fn =~ /^README(?:[.][^.]+)?$/iu
l!l&&r!l&&!r
290123
MC/DC50%1/2
$markup->guess_format($fn) or $fn =~ /^README(?:[.][^.]+)?$/iu
$markup->guess_format($fn) $fn =~ /^README(?:[.][^.]+)?$/iu
Truth table66%2/3
A or B
A= $markup->guess_format($fn)B= $fn =~ /^README(?:[.][^.]+)?$/iu
ABresult
1X1
011
000
511 29 push @docs => $self->_start_doc($meta, $fn);
512 }
513  
514 19 return @docs;
515 }
516  
517 sub parse_docs {
518 11 ▶ my ($self, $p) = @_;
Subroutine parse_docs: called CC=18 SCAR=29.2
Pod documented
519 11 my $meta = $p->{meta};
520 11 ▶ say " Parsing \L$meta->{name}-$meta->{version}\E docs" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 11 11
521  
522 11 my $markup = Text::Markup->new(default_encoding => 'UTF-8');
523 11 my $dir = $self->doc_root_file_for(source => $meta);
524  
525 # Find all doc files.
526 11 my @files = $self->find_docs($p);
527  
528 11 my (%docs, $readme);
529 11 for my $spec (@files) {
530 18 my ($fn, $dst, $noext) = @{$spec}{qw(filename dest noext)};
531 18 my $src = catfile $dir, $fn;
532 18 ▶ next unless -e $src;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $src 2 16 18
533  
534 16 ▶ say " Parsing markup in $src" if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 16 16
535 16 ▶ my $doc = $self->_parse_html_string($markup->parse(file => $src) or next);
Condition100%2/2
$markup->parse('file', $src) || (next)
l!l
151
MC/DC100%1/1
$markup->parse('file', $src) || (next)
$markup->parse('file', $src)
Truth table100%2/2
A
A= $markup->parse('file', $src)
Aresult
11
00
536 15 make_path dirname $dst;
537  
538 # Determine the title before we mangle the HTML.
539 15 my $basename = basename $noext;
540 15 ▶ my $title = $doc->findvalue('/html/head/title')
Condition33%2/6
$doc->findvalue('/html/head/title') || $doc->findvalue('//h1[1]') || $basename
l!l&&r!l&&!r
1500
$doc->findvalue("/html/head/title") or $doc->findvalue("//h1[1]")
l!l&&r!l&&!r
0150
MC/DC0%0/3
$doc->findvalue('/html/head/title') || $doc->findvalue('//h1[1]') || $basename
$doc->findvalue("/html/head/title") $doc->findvalue("//h1[1]") $basename
Truth table25%1/4
A or B || C
A= $doc->findvalue("/html/head/title")B= $doc->findvalue("//h1[1]")C= $basename
ABCresult
1XX1
01X1
0011
0000
541 || $doc->findvalue('//h1[1]')
542 || $basename;
543  
544 # Grab abstract if this looks like extension documentation.
545 my $abstract = $meta->{provides}{$basename}
546 ? $meta->{provides}{$basename}{abstract}
547 15 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
$meta->{'provides'}{$basename} ? : 5 10 15
548  
549 # Clean up the HTML and write it out.
550 15 ▶ open my $fh, '>:utf8', $dst or die "Cannot open $dst: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:utf8", $dst or die "Cannot open ${dst}: $!\n" 0 15 15
551 15 $doc = $self->_clean_html_body($doc->findnodes('/html/body'), $spec, \@files, $p);
552 15 print $fh $doc->toString, "\n";
553 15 ▶ close $fh or die "Cannot close $dst: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${dst}: $!\n" 0 15 15
554  
555 $docs{$noext} = {
556 15 ▶ ($spec->{extension} ? (extension => $spec->{extension}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$spec->{'extension'} ? : 2 13 15
$abstract ? : 5 10 15
557 title => $title,
558 ($abstract) ? (abstract => $abstract) : ()
559 };
560  
561 # Prepare the index entry.
562 my $to_index = {
563 key => lc "$meta->{name}/$noext",
564 docpath => $noext,
565 title => $title,
566 abstract => $abstract || '',
567 body => _strip_html( $doc->findnodes('.//div[@id="pgxnbod"]')->shift),
568 dist => $meta->{name},
569 version => $meta->{version},
570 date => $meta->{date},
571 user_name => $self->_get_user_name($meta),
572 user => $meta->{user},
573 15 ▶ };
Condition100%2/2
$abstract || ''
l!l
510
MC/DC100%1/1
$abstract || ''
$abstract
Truth table100%2/2
A
A= $abstract
Aresult
11
00
574  
575 # If $fn is a README and isn't referenced by a docfile field...
576 15 ▶ if (!$spec->{extension} && $fn =~ qr{^(?i:README(?:[.][^.]+)?)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $spec->{'extension'} and $fn =~ qr/^(?i:README(?:[.][^.]+)?)$/u) { } 8 7 15
Condition100%3/3
not $spec->{"extension"} and $fn =~ qr/^(?i:README(?:[.][^.]+)?)$/u
!ll&&!rl&&r
258
MC/DC100%2/2
not $spec->{"extension"} and $fn =~ qr/^(?i:README(?:[.][^.]+)?)$/u
not $spec->{"extension"} $fn =~ qr/^(?i:README(?:[.][^.]+)?)$/u
Truth table100%3/3
not(A) and B
A= not $spec->{"extension"}B= $fn =~ qr/^(?i:README(?:[.][^.]+)?)$/u
ABresult
0X0
100
111
577 # Hang on to it to use in case there are no other docs.
578 8 $readme = $to_index;
579 } else {
580 # Add it to the search index.
581 7 $self->_index(docs => $to_index);
582 }
583 }
584  
585 # No docs found, just the README. So index it as documentation.
586 11 ▶ $self->_index(docs => $readme) if @files == 1 && $readme;
Branch: 1 branch
BranchTrueFalseTotal
if @files == 1 and $readme 3 8 11
Condition100%3/3
@files == 1 and $readme
!ll&&!rl&&r
713
MC/DC100%2/2
@files == 1 and $readme
@files == 1 $readme
Truth table100%3/3
A and B
A= @files == 1B= $readme
ABresult
0X0
100
111
587  
588 11 return \%docs;
589 }
590  
591 sub _parse_html_string {
592 shift->libxml->parse_html_string(shift, {
593 17 ▶ suppress_warnings => 1,
Subroutine _parse_html_string: called CC=1 SCAR=0.0
594 suppress_errors => 1,
595 recover => 2,
596 });
597 }
598  
599 sub mirror_file_for {
600 49 ▶ my $self = shift;
Subroutine mirror_file_for: called CC=1 SCAR=0.0
Pod documented
601 49 return catfile +PGXN::API->instance->mirror_root,
602 $self->_uri_for(@_)->path_segments;
603 }
604  
605 sub doc_root_file_for {
606 140 ▶ my $self = shift;
Subroutine doc_root_file_for: called CC=1 SCAR=0.0
Pod documented
607 140 return catfile +PGXN::API->instance->doc_root,
608 $self->_uri_for(@_)->path_segments;
609 }
610  
611 sub finalize {
612 1 ▶ my $self = shift;
Subroutine finalize: called CC=1 SCAR=0.0
Pod documented
613 1 $self->update_user_lists;
614 1 $self->_commit;
615 1 return $self;
616 }
617  
618 sub update_user_lists {
619 2 ▶ my $self = shift;
Subroutine update_user_lists: called CC=7 SCAR=19.5
Pod documented
620 2 my $api = PGXN::API->instance;
621 2 my $names = $self->_user_names;
622 2 my %users_for;
623  
624 2 ▶ say "Updating user lists" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 2 2
625 2 ▶ while (my ($nick, $name) = each %{ $names }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$names;}) 7 2 9
626 7 my $letter = lc substr $nick, 0, 1;
627 7 ▶ push @{ $users_for{$letter} ||= [] } => { user => $nick, name => $name };
Condition100%2/2
$users_for{$letter} ||= []
l!l
34
MC/DC100%1/1
$users_for{$letter} ||= []
$users_for{$letter}
Truth table100%2/2
A
A= $users_for{$letter}
Aresult
11
00
628 }
629  
630 2 ▶ while (my ($letter, $users) = each %users_for ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %users_for) 4 2 6
631 4 ▶ say " Updating $letter.json" if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 4 4
632 4 my $fn = $self->doc_root_file_for('userlist', undef, letter => $letter);
633 4 ▶ my $list = -e $fn ? $api->read_json_from($fn) : do {
Branch: 1 branch
BranchTrueFalseTotal
-e $fn ? : 2 2 4
634 2 make_path dirname $fn;
635 2 [];
636 };
637  
638 # Load the users into a hash to eliminate dupes.
639 4 my %updated = map { lc $_->{user} => $_ } @{ $list }, @{ $users };
640  
641 # Write them out in order by nickname.
642 $api->write_json_to($fn, [
643 7 map { $updated{ $_ } }
644 4 sort { $a cmp $b } keys %updated
645 ]);
646 }
647 2 return $self;
648 }
649  
650 sub _idx_distmeta {
651 0 ▶ my $meta = shift;
Subroutine _idx_distmeta: not called CC=5 SCAR=34.0
652 my @lines = (
653 "$meta->{license} license",
654 0 ▶ (ref $meta->{maintainer} ? @{ $meta->{maintainer} } : ($meta->{maintainer})),
Branch: 1 branch
BranchTrueFalseTotal
ref $meta->{'maintainer'} ? : 0 0 0
655 );
656  
657 0 ▶ while (my ($k, $v) = each %{ $meta->{provides}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$meta{"provides"};}) 0 0 0
658 0 ▶ push @lines => $v->{abstract} ? "$k: $v->{abstract}" : $k;
Branch: 1 branch
BranchTrueFalseTotal
$v->{'abstract'} ? : 0 0 0
659 }
660 0 ▶ push @lines, $meta->{description} if $meta->{description};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"description"} 0 0 0
661 0 return join $/ => @lines;
662 }
663  
664 sub _get_user_name {
665 32 ▶ my ($self, $meta) = @_;
Subroutine _get_user_name: called CC=2 SCAR=7.0
666 32 ▶ return $self->_user_names->{ lc $meta->{user} } ||= do {
Condition66%2/3
$self->_user_names->{lc $meta->{"user"}} ||= do {
l!l&&r!l&&!r
3020
MC/DC0%0/2
$self->_user_names->{lc $meta->{"user"}} ||= do { my $user = "PGXN::API"->instance->read_json_from($self->mirror_file_for("user", $meta)); $user->{"name"} }
$self->_user_names->{lc $meta->{"user"}} do { my $user = "PGXN::API"->instance->read_json_from($self->mirror_file_for("user", $meta)); $user->{"name"} }
Truth table66%2/3
A ||= B
A= $self->_user_names->{lc $meta->{"user"}}B= do { my $user = "PGXN::API"->instance->read_json_from($self->mirror_file_for("user", $meta)); $user->{"name"} }
ABresult
1X1
011
000
667 2 my $user = PGXN::API->instance->read_json_from(
668 $self->mirror_file_for(user => $meta)
669 );
670 2 $user->{name};
671 };
672 }
673  
674 sub _strip_html {
675 827 ▶ my $ret = '';
Subroutine _strip_html: called CC=5 SCAR=16.6
676 827 for my $elem (@_) {
677 2079 ▶ $ret .= $elem->nodeType == XML_TEXT_NODE ? $elem->data
Branch: 2 branches
BranchTrueFalseTotal
$elem->nodeType == 3 ? : 1267 812 2079
$elem->nodeType == 1 && $elem->nodeName eq 'br' ? : 0 812 812
Condition33%1/3
$elem->nodeType == 1 and $elem->nodeName eq "br"
!ll&&!rl&&r
08120
MC/DC0%0/2
$elem->nodeType == 1 and $elem->nodeName eq "br"
$elem->nodeType == 1 $elem->nodeName eq "br"
Truth table33%1/3
A and B
A= $elem->nodeType == 1B= $elem->nodeName eq "br"
ABresult
0X0
100
111
678 : $elem->nodeType == XML_ELEMENT_NODE && $elem->nodeName eq 'br' ? ' '
679 : _strip_html($elem->childNodes);
680 }
681  
682 # Normalize whitespace.
683 827 $ret =~ s/^\s+//;
684 827 $ret =~ s/\s+$//;
685 827 $ret =~ s/[\t\n\r]+|\s{2,}/ /gms;
686 827 return $ret;
687 }
688  
689 sub _update_releases {
690 22 ▶ my $self = shift;
Subroutine _update_releases: called CC=2 SCAR=6.9
691 22 my $meta = $_[1];
692 22 my $api = PGXN::API->instance;
693  
694 # Read in metadata from the mirror.
695 22 my $mir_file = $self->mirror_file_for(@_);
696 22 my $mir_meta = $api->read_json_from($mir_file);
697  
698 # Read in metadata from the doc root.
699 22 my $doc_file = $self->doc_root_file_for(@_);
700 22 ▶ my $doc_meta = -e $doc_file ? $api->read_json_from($doc_file) : $mir_meta;
Branch: 1 branch
BranchTrueFalseTotal
-e $doc_file ? : 18 4 22
701  
702 # Update with latest release info and abstract.
703 my $rels = $doc_meta->{releases}{$meta->{name}}
704 22 = $mir_meta->{releases}{$meta->{name}};
705 22 $rels->{abstract} = $meta->{abstract};
706  
707 # Write out the data to the doc root.
708 22 $api->write_json_to($doc_file => $doc_meta);
709 22 return $doc_meta;
710 }
711  
712 sub _index_user {
713 8 ▶ my ($self, $user) = @_;
Subroutine _index_user: called CC=5 SCAR=16.2
714 my $data = {
715 key => lc $user->{nickname},
716 user => $user->{nickname},
717 name => $user->{name} || '',
718 email => $user->{email},
719 8 ▶ uri => $user->{uri} || '',
Condition50%2/4
$user->{'name'} || ''
l!l
80
$user->{'uri'} || ''
l!l
80
MC/DC0%0/1
$user->{'name'} || ''
$user->{'name'}
MC/DC0%0/1
$user->{'uri'} || ''
$user->{'uri'}
Truth table50%1/2
A
A= $user->{'name'}
Aresult
11
00
Truth table50%1/2
A
A= $user->{'uri'}
Aresult
11
00
720 };
721  
722 # Gather up any other details.
723 $data->{details} = join(
724 "\n",
725 1 grep { $_ }
726 1 map { $user->{$_} }
727 41 ▶ sort grep { !$data->{$_} && !ref $user->{$_} && $_ ne 'nickname' }
Branch: 1 branch
BranchTrueFalseTotal
not $data->{$_} || ref $user->{$_} and $_ ne "nickname" 9 32 41
Condition100%3/3
$data->{$_} || ref $user->{$_}
l!l&&r!l&&!r
2489
MC/DC100%2/2
$data->{$_} || ref $user->{$_}
$data->{$_} ref $user->{$_}
Truth table100%3/3
A || B
A= $data->{$_}B= ref $user->{$_}
ABresult
1X1
011
000
728 8 keys %{ $user }
729 );
730 8 $self->_index(users => $data);
731 }
732  
733 sub _index {
734 50 ▶ my ($self, $index, $data) = @_;
Subroutine _index: called CC=2 SCAR=6.9
735 50 ▶ push @{ $self->to_index->{ $index } } => $data if $self->_index_it;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_index_it 38 12 50
736 }
737  
738 sub _rollback {
739 2 ▶ my $self = shift;
Subroutine _rollback: called CC=2 SCAR=6.9
740 2 @{ $self->to_index->{$_} } = () for keys %{ $self->to_index };
741 2 return;
742 }
743  
744 sub _commit {
745 4 ▶ my $self = shift;
Subroutine _commit: called CC=3 SCAR=11.0
746 4 my $to_index = $self->to_index;
747  
748 4 for my $iname (keys %{ $to_index }) {
749 20 my $indexer = $self->indexer_for($iname);
750 20 for my $doc (@{ $to_index->{$iname} }) {
751 14 $indexer->delete_by_term( field => 'key', term => $doc->{key} );
752 14 $indexer->add_doc($doc);
753 }
754 20 $indexer->commit;
755 20 @{ $to_index->{$iname} } = ();
756 }
757  
758 4 return $self;
759 }
760  
761 sub _uri_for {
762 189 ▶ my ($self, $name, $meta, @params) = @_;
Subroutine _uri_for: called CC=4 SCAR=13.9
763 PGXN::API->instance->uri_templates->{$name}->process(
764 dist => lc($meta->{name} || ''),
765 version => lc($meta->{version} || ''),
766 189 ▶ user => lc($meta->{user} || ''),
Condition100%6/6
$meta->{'name'} || ''
l!l
1809
$meta->{'version'} || ''
l!l
1809
$meta->{'user'} || ''
l!l
1818
MC/DC100%1/1
$meta->{'name'} || ''
$meta->{'name'}
MC/DC100%1/1
$meta->{'version'} || ''
$meta->{'version'}
MC/DC100%1/1
$meta->{'user'} || ''
$meta->{'user'}
Truth table100%2/2
A
A= $meta->{'name'}
Aresult
11
00
Truth table100%2/2
A
A= $meta->{'version'}
Aresult
11
00
Truth table100%2/2
A
A= $meta->{'user'}
Aresult
11
00
767 @params,
768 );
769 }
770  
771 sub _source_files {
772 6 ▶ my ($self, $p) = @_;
Subroutine _source_files: called CC=4 SCAR=13.9
773 6 my $zip = $p->{zip};
774 6 my $prefix = quotemeta lc "$p->{meta}{name}-$p->{meta}{version}";
775 6 my @files;
776 6 for my $regex (
777 qr{Change(?:s|Log)(?:[.][^.]+)?}i,
778 qr{(?:README|LICENSE|INSTALL|COPYING|AUTHORS)(?:[.][^.]+)?}i,
779 qr{META[.]json},
780 qr{Makefile},
781 qr{MANIFEST},
782 qr{\Q$p->{meta}{name}\E[.]control},
783 ) {
784 36 my ($member) = $zip->membersMatching(qr{^$prefix/$regex$});
785 36 ▶ unless ($member) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($member) 14 22 36
786 14 ($member) = $zip->membersMatching(qr{^$prefix/$regex[.]in$});
787 14 ▶ next unless $member;
Branch: 1 branch
BranchTrueFalseTotal
unless $member 13 1 14
788 }
789 23 (my $fn = $member->fileName) =~ s{^$prefix/}{};
790 23 push @files => $fn;
791 }
792 6 return \@files;
793 }
794  
795 sub _readme {
796 6 ▶ my ($self, $p) = @_;
Subroutine _readme: called CC=2 SCAR=9.2
797 6 my $zip = $p->{zip};
798 6 my $prefix = quotemeta lc "$p->{meta}{name}-$p->{meta}{version}";
799 6 my ($member) = $zip->membersMatching(
800 qr{^$prefix/(?i:README(?:[.][^.]+)?)$}
801 );
802 6 ▶ return '' unless $member;
Branch: 1 branch
BranchTrueFalseTotal
unless $member 0 6 6
803  
804 6 my $contents;
805 try {
806 6 ▶ local $SIG{__WARN__} = sub { die @_ };
Subroutine __ANON__: called CC=1 SCAR=1.2
807 6 ▶ $contents = $member->contents || '';
Condition50%1/2
$member->contents || ''
l!l
60
MC/DC0%0/1
$member->contents || ''
$member->contents
Truth table50%1/2
A
A= $member->contents
Aresult
11
00
808 } catch {
809 0 ▶ die $_ unless /CRC or size mismatch/;
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
unless /CRC or size mismatch/u 0 0 0
810 # Oy. Work around https://rt.cpan.org/Ticket/Display.html?id=74255.
811 0 my $zip_path = $zip->fileName;
812 0 warn "CRC or size mismatch error reading from $zip_path; reloading.\n";
813 0 $zip = Archive::Zip->new;
814 0 ▶ die "Error re-reading $zip_path\n" if $zip->read($zip_path) != AZ_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $zip->read($zip_path) != 0 0 0 0
815 0 ($member) = $zip->membersMatching(
816 qr{^$prefix/(?i:README(?:[.][^.]+)?)$}
817 );
818 0 ▶ $contents = $member->contents || '';
Condition0%0/2
$member->contents || ''
l!l
00
MC/DC0%0/1
$member->contents || ''
$member->contents
Truth table0%0/2
A
A= $member->contents
Aresult
11
00
819 6 };
820  
821 6 utf8::decode $contents;
822 # Normalize whitespace.
823 6 $contents =~ s/^\s+//;
824 6 $contents =~ s/\s+$//;
825 6 $contents =~ s/[\t\n\r]+|\s{2,}/ /gms;
826 6 return $contents;
827 }
828  
829 # List of allowed elements and attributes.
830 # https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements
831 3 my %allowed = do {
832 3 my $attrs = { title => 1, dir => 1, lang => 1 };
833 3 map { $_ => $attrs } qw(
834 a
835 abbr
836 acronym
837 address
838 area
839 article
840 aside
841 audio
842 b
843 bdi
844 bdo
845 big
846 blockquote
847 br
848 canvas
849 caption
850 cite
851 code
852 col
853 colgroup
854 data
855 dd
856 del
857 details
858 dfn
859 dir
860 div
861 dl
862 dt
863 em
864 figcaption
865 figure
866 footer
867 h1
868 h2
869 h3
870 h4
871 h5
872 h6
873 header
874 hgroup
875 hr
876 i
877 img
878 ins
879 kbd
880 li
881 map
882 mark
883 meter
884 ol
885 output
886 p
887 picture
888 pre
889 q
890 rp
891 rt
892 ruby
893 s
894 samp
895 section
896 small
897 source
898 span
899 strike
900 strong
901 sub
902 summary
903 sup
904 table
905 tbody
906 td
907 tfoot
908 th
909 thead
910 time
911 tr
912 tt
913 u
914 ul
915 var
916 video
917 wbr
918 xmp
919 );
920 };
921  
922 # A few elements may retain other attributes.
923 3 $allowed{a} = { %{ $allowed{a} }, map { $_ => 1 } qw(href hreflang media rel target type) };
924 3 $allowed{area} = { %{ $allowed{area} }, map { $_ => 1 } qw(alt coords href hreflang media rel shape target tytpe) };
925 3 $allowed{article} = { %{ $allowed{article} }, cite => 1, pubdate => 1 };
926 3 $allowed{audio} = { %{ $allowed{audio} }, map { $_ => 1 } qw(src) };
927 3 $allowed{canvas} = { %{ $allowed{canvas} }, map { $_ => 1 } qw(height width) };
928 3 $allowed{col} = { %{ $allowed{col} }, map { $_ => 1 } qw(span align valign width) };
929 3 $allowed{colgroup} = $allowed{col};
930 3 $allowed{del} = { %{ $allowed{del} }, cite => 1, datetime => 1 };
931 3 $allowed{details} = { %{ $allowed{details} }, open => 1 };
932 3 $allowed{img} = { %{ $allowed{img} }, map { $_ => 1 } qw(alt src height ismap usemap width) };
933 3 $allowed{ins} = $allowed{del};
934 3 $allowed{li} = { %{ $allowed{li} }, value => 1 };
935 3 $allowed{map} = { %{ $allowed{map} }, name => 1 };
936 3 $allowed{meter} = { %{ $allowed{meter} }, map { $_ => 1 } qw(high low min max optimum value) };
937 3 $allowed{source} = { %{ $allowed{source} }, map { $_ => 1 } qw(media src srcset type) };
938 3 $allowed{ol} = { %{ $allowed{ol} }, revese => 1, start => 1 };
939 3 $allowed{q} = { %{ $allowed{q} }, cite => 1 };
940 3 $allowed{section} = $allowed{q};
941 3 $allowed{table} = { %{ $allowed{table} }, map { $_ => 1 } qw(summary width) };
942 3 $allowed{tbody} = { %{ $allowed{tbody} }, map { $_ => 1 } qw(align valign) };
943 3 $allowed{td} = { %{ $allowed{td} }, map { $_ => 1 } qw(align colspan headers height nowrap rowspan scope valign width) };
944 3 $allowed{tfoot} = $allowed{tbody};
945 3 $allowed{th} = $allowed{td};
946 3 $allowed{tfoot} = $allowed{tbody};
947 3 $allowed{tr} = $allowed{tbody};
948 3 $allowed{time} = { %{ $allowed{time} }, datetime => 1, pubdate => 1 };
949 3 $allowed{video} = { %{ $allowed{video} }, map { $_ => 1 } qw(audio height poster src width) };
950  
951 # We delete all other elements except for these, for which we keep text.
952 3 my %keep_children = map { $_ => 1 } qw(
953 blink
954 center
955 font
956 );
957  
958 sub _local_dirname {
959 99 ▶ my $dir = [fileparse($_[0])]->[1];
Subroutine _local_dirname: called CC=3 SCAR=11.0
960 99 $dir =~ s/\/\z//;
961 99 ▶ Test::More::diag("DIR: $dir") if $ENV{FOO};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"FOO"} 0 99 99
962 99 ▶ return $dir eq '.' ? '' : $dir;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq '.' ? : 82 17 99
963 }
964  
965 # Resolve the path to a relative URI (/foo/bar, ./foo/bar, foo/../../xyz) by
966 # normalizing away `.` and `..`. If it resolves to a path above itself,
967 # ("escapes containment"), return undef. Otherwise return the normalized path.
968 # Ensures that links don't reference things outside a distribution directory,
969 # which we consider a vulnerability.
970 sub _resolve_file {
971 107 ▶ my ($spec, $uri) = @_;
Subroutine _resolve_file: called CC=8 SCAR=20.8
972  
973 # Unadorned root or current dir always okay.
974 107 ▶ return '' if $uri eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $uri eq "/" 3 104 107
975 104 ▶ return _local_dirname($spec->{filename}) if $uri eq '' || $uri =~ /\A.\/?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $uri eq "" or $uri =~ m[\A./?\z]u 5 99 104
Condition100%3/3
$uri eq "" or $uri =~ m[\A./?\z]u
l!l&&r!l&&!r
3299
MC/DC100%2/2
$uri eq "" or $uri =~ m[\A./?\z]u
$uri eq "" $uri =~ m[\A./?\z]u
Truth table100%3/3
A or B
A= $uri eq ""B= $uri =~ m[\A./?\z]u
ABresult
1X1
011
000
976  
977 # Assemble a prefix that doesn't appear in the URI.
978 99 my $i = 0;
979 99 ▶ $i++ while $uri =~ /root$i/;
Branch: 1 branch
BranchTrueFalseTotal
$uri =~ /root$i/u and do { ++$i } 4 99 103
980 99 my $prefix = "/root$i/";
981  
982 my $path = File::Spec->canonpath(
983 99 ▶ $prefix . ($uri =~ s/\A\/// ? $uri : _local_dirname($spec->{filename}) . "/$uri")
Branch: 1 branch
BranchTrueFalseTotal
$uri =~ s[\A/][]u ? : 5 94 99
984 );
985  
986 # https://gist.github.com/denilsonsa/9b433ee8fe548d0388fec4de19e7d8d6
987 # Remove ".." by using a regex.
988 99 ▶ while ($path =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[ (^|/) # Either the beginning of the string, or a slash, save as $1 ( # Followed by one of these: [^/]| # * Any one character (except slash, obviously) [^./][^/]| # * Two characters where [^/][^./]| # they are not ".." [^/][^/][^/]+ # * Three or more characters ) # Followed by: /\.\./ # "/", followed by "../" ][$1]ux) 37 99 136
989 (^|/) # Either the beginning of the string, or a slash, save as $1
990 ( # Followed by one of these:
991 [^/]| # * Any one character (except slash, obviously)
992 [^./][^/]| # * Two characters where
993 [^/][^./]| # they are not ".."
994 [^/][^/][^/]+ # * Three or more characters
995 ) # Followed by:
996 /\.\./ # "/", followed by "../"
997 }{$1}x
998 ) {
999 # Repeat this substitution until not possible anymore.
1000 }
1001  
1002 # Return path if it doesn't escape containment.
1003 99 ▶ return $path if $path =~ s{\A$prefix}{};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ s/\A$prefix//u 87 12 99
1004  
1005 # Escaped containment, the link should be removed.
1006 12 return undef;
1007 }
1008  
1009 sub _clean_html_body {
1010 25 ▶ my ($self, $elem, $spec, $files, $p) = @_;
Subroutine _clean_html_body: called CC=41 SCAR=37.5
1011 25 my $top = $elem;
1012  
1013 # Create an element for the table of contents.
1014 25 my $toc = XML::LibXML::Element->new('div');
1015 25 $toc->setAttribute(id => 'pgxntoc');
1016 25 $toc->appendText("\n ");
1017 25 my $contents = XML::LibXML::Element->new('h3');
1018 25 $contents->appendText('Contents');
1019 25 $toc->appendChild($contents);
1020 25 $toc->appendText("\n ");
1021  
1022 25 my $topul = my $ul = XML::LibXML::Element->new('ul');
1023 25 $ul->setAttribute(class => 'pgxntocroot');
1024 25 $toc->addChild($ul);
1025  
1026 25 my %gen_ids;
1027 25 my $level = 1;
1028 25 my $pgxnbod;
1029  
1030 25 ▶ while ($elem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 2253 25 2278
1031 2253 ▶ if ($elem->nodeType == XML_ELEMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->nodeType == 1) 884 1369 2253
1032 884 my $name = $elem->nodeName;
1033 884 ▶ if ($name eq 'body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "body") 25 859 884
1034 # Remove all attributes and rewrite it as a div.
1035 25 $elem->removeAttribute($_) for map {
1036 0 $_->nodeName
1037 } $elem->attributes;
1038 25 $elem->setNodeName('div');
1039 25 $elem->setAttribute(id => 'pgxnbod');
1040 25 $pgxnbod = $elem;
1041 25 ▶ $elem = $elem->firstChild || last;
Condition50%1/2
$elem->firstChild || (last)
l!l
250
MC/DC0%0/1
$elem->firstChild || (last)
$elem->firstChild
Truth table50%1/2
A
A= $elem->firstChild
Aresult
11
00
1042 25 next;
1043 }
1044  
1045 859 ▶ if (my $attrs = $allowed{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attrs = $allowed{$name}) { } 853 6 859
1046 # Keep only allowed attributes.
1047 853 ▶ if (!$pgxnbod || !$elem->isSameNode($pgxnbod)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pgxnbod and $elem->isSameNode($pgxnbod)) 852 1 853
Condition66%2/3
$pgxnbod and $elem->isSameNode($pgxnbod)
!ll&&!rl&&r
08521
MC/DC50%1/2
$pgxnbod and $elem->isSameNode($pgxnbod)
$pgxnbod $elem->isSameNode($pgxnbod)
Truth table66%2/3
A and B
A= $pgxnbodB= $elem->isSameNode($pgxnbod)
ABresult
0X0
100
111
1048 852 $elem->removeAttribute($_) for grep { !$attrs->{$_} }
1049 244 map { $_->nodeName } $elem->attributes;
1050 }
1051  
1052 # Map links.
1053 853 ▶ if ($spec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec) 850 3 853
1054 850 my $prefix = lc "$p->{meta}{name}-$p->{meta}{version}";
1055 850 ▶ if ($attrs->{href}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"href"}) 82 768 850
1056 82 my $href = $elem->getAttribute('href');
1057 82 ▶ if ($href && $href !~ /\A[a-z]+:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href and not $href =~ /\A[a-z]+:/u) 30 52 82
Condition66%2/3
$href and not $href =~ /\A[a-z]+:/u
!ll&&!rl&&r
05230
MC/DC50%1/2
$href and not $href =~ /\A[a-z]+:/u
$href not $href =~ /\A[a-z]+:/u
Truth table66%2/3
A and not(B)
A= $hrefB= not $href =~ /\A[a-z]+:/u
ABresult
0X0
100
111
1058 # Relative URL.
1059 30 my $path = _resolve_file($spec, $href);
1060 30 ▶ if (!defined $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $path) { } 1 29 30
elsif (&first(sub { $_->{'filename'} eq $path; } , @{$files;})) { } 4 25 29
1061 # Escaped containment, remove the attribute.
1062 1 $elem->removeAttribute('href');
1063 29 ▶ } elsif (first { $_->{filename} eq $path } @{ $files }) {
Subroutine __ANON__: called CC=1 SCAR=0.0
1064 # Doc in the API; use relative link to it.
1065 4 $href =~ s{[.][^.]+$}{};
1066 4 $elem->setAttribute(href => "$href.html");
1067 } else {
1068 # Link to the mirror.
1069 25 $elem->setAttribute(href => "$p->{src_url}$path");
1070 }
1071 }
1072 }
1073  
1074 850 ▶ if ($attrs->{src}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"src"}) 32 818 850
1075 32 my $src = $elem->getAttribute('src');
1076 32 ▶ if ($src && $src !~ /\A[a-z]+:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src and not $src =~ /\A[a-z]+:/u) 24 8 32
Condition66%2/3
$src and not $src =~ /\A[a-z]+:/u
!ll&&!rl&&r
8024
MC/DC50%1/2
$src and not $src =~ /\A[a-z]+:/u
$src not $src =~ /\A[a-z]+:/u
Truth table66%2/3
A and not(B)
A= $srcB= not $src =~ /\A[a-z]+:/u
ABresult
0X0
100
111
1077 24 ▶ if (defined ($src = _resolve_file($spec, $src))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($src = _resolve_file($spec, $src))) { } 24 0 24
1078 # Link to the mirror.
1079 24 $elem->setAttribute(src => "$p->{src_url}$src");
1080 } else {
1081 # Escaped containment, remove the attribute.
1082 0 $elem->removeAttribute('src');
1083 }
1084 }
1085 }
1086  
1087 850 ▶ if ($attrs->{srcset}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"srcset"}) 8 842 850
1088 8 ▶ if (my $set = $elem->getAttribute('srcset')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $set = $elem->getAttribute("srcset")) 8 0 8
1089 8 my @srcset;
1090 8 for my $src (split /\s*,\s*/, $set) {
1091 24 ▶ if ($src && $src !~ /\A[a-z]+:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src and not $src =~ /\A[a-z]+:/u) { } 24 0 24
Condition33%1/3
$src and not $src =~ /\A[a-z]+:/u
!ll&&!rl&&r
0024
MC/DC0%0/2
$src and not $src =~ /\A[a-z]+:/u
$src not $src =~ /\A[a-z]+:/u
Truth table33%1/3
A and not(B)
A= $srcB= not $src =~ /\A[a-z]+:/u
ABresult
0X0
100
111
1092 24 ▶ if (defined ($src = _resolve_file($spec, $src))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($src = _resolve_file($spec, $src))) 24 0 24
1093 # Link to the mirror.
1094 24 push @srcset => "$p->{src_url}$src";
1095 }
1096 } else {
1097 0 push @srcset => $src;
1098 }
1099 }
1100 8 ▶ if (@srcset) {
Branch: 1 branch
BranchTrueFalseTotal
if (@srcset) { } 8 0 8
1101 # Valid srset, rewrite it.
1102 8 $elem->setAttribute(srcset => join ', ' => @srcset);
1103 } else {
1104 # No valid src, remove it.
1105 0 $elem->removeAttribute('srcset');
1106 }
1107 }
1108 }
1109 }
1110  
1111 853 ▶ if ($name =~ /^h([123])$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^h([123])$/u) 79 774 853
1112 79 my $header = $1;
1113 # Create an ID.
1114 # http://www.w3schools.com/tags/att_standard_id.aps
1115 79 (my $id = $elem->textContent) =~ s{^([^a-zA-Z])}{L$1};
1116 79 $id =~ s{[^a-zA-Z0-9_:.-]+}{.}g;
1117 79 ▶ $id .= $gen_ids{$id}++ || '';
Condition100%2/2
$gen_ids{$id}++ || ''
l!l
376
MC/DC100%1/1
$gen_ids{$id}++ || ''
$gen_ids{$id}++
Truth table100%2/2
A
A= $gen_ids{$id}++
Aresult
11
00
1118 79 $elem->setAttribute(id => $id);
1119 79 ▶ if ($header != $level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header != $level) 23 56 79
1120 # Add and remove unordered lists as needed.
1121 23 ▶ while ($header < $level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header < $level) 5 23 28
1122 5 $ul->appendText("\n " . ' ' x (2 * $level - 2));
1123 5 my $li = $ul->parentNode;
1124 5 $li->appendText("\n " . ' ' x (2 * $level - 3));
1125 5 $ul = $li->parentNode;
1126 5 $level--;
1127 }
1128 23 ▶ while ($header > $level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header > $level) 21 23 44
1129 21 my $newul = XML::LibXML::Element->new('ul');
1130 21 ▶ my $li = $ul->find('./li[last()]')->shift || do {
Condition66%2/3
$ul->find('./li[last()]')->shift || do {
l!l&&r!l&&!r
1920
MC/DC0%0/2
$ul->find('./li[last()]')->shift || do { $ul->appendChild('XML::LibXML::Element'->new('li')) }
$ul->find('./li[last()]')->shift do { $ul->appendChild('XML::LibXML::Element'->new('li')) }
Truth table66%2/3
A || B
A= $ul->find('./li[last()]')->shiftB= do { $ul->appendChild('XML::LibXML::Element'->new('li')) }
ABresult
1X1
011
000
1131 2 $ul->appendChild( XML::LibXML::Element->new('li') );
1132 };
1133 21 $li->appendText("\n " . ' ' x (2 * $level));
1134 21 $li->addChild($newul);
1135 21 $ul = $newul;
1136 21 $level++;
1137 }
1138 }
1139  
1140 # Add the item to the TOC.
1141 79 my $li = XML::LibXML::Element->new('li');
1142 79 my $a = XML::LibXML::Element->new('a');
1143 79 $a->setAttribute(href => "#$id");
1144 79 $a->appendText($elem->textContent);
1145 79 $li->addChild($a);
1146 79 $ul->appendText("\n " . ' ' x (2 * $level - 1));
1147 79 $ul->addChild($li);
1148 }
1149  
1150 # Descend into children.
1151 853 ▶ if (my $next = $elem->firstChild) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $elem->firstChild) 821 32 853
1152 821 $elem = $next;
1153 821 next;
1154 }
1155 } else {
1156 # You are not wanted.
1157 6 my $parent = $elem->parentNode;
1158 6 ▶ if ($keep_children{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_children{$name}) 3 3 6
1159 # Keep the children.
1160 3 $parent->insertAfter($_, $elem) for reverse $elem->childNodes;
1161 }
1162  
1163 # Take it out and jump to the next sibling.
1164 6 my $next = $elem;
1165 NEXT: {
1166 6 ▶ if (my $sib = $next->nextSibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sib = $next->nextSibling) 5 1 6
1167 5 $next = $sib;
1168 5 last;
1169 }
1170  
1171 # No sibling, try parent's sibling
1172 1 $next = $next->parentNode;
1173 1 ▶ redo if $next && !$next->isSameNode($top);
Branch: 1 branch
BranchTrueFalseTotal
if $next and not $next->isSameNode($top) 0 1 1
Condition33%1/3
$next and not $next->isSameNode($top)
!ll&&!rl&&r
010
MC/DC0%0/2
$next and not $next->isSameNode($top)
$next not $next->isSameNode($top)
Truth table33%1/3
A and not(B)
A= $nextB= not $next->isSameNode($top)
ABresult
0X0
100
111
1174 }
1175 6 $parent->removeChild($elem);
1176 6 $elem = $next;
1177 6 next;
1178 }
1179 }
1180  
1181 # Find the next node.
1182 NEXT: {
1183 1401 ▶ if (my $sib = $elem->nextSibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sib = $elem->nextSibling) 1376 920 2296
1184 1376 $elem = $sib;
1185 1376 last;
1186 }
1187  
1188 # No sibling, try parent's sibling
1189 920 $elem = $elem->parentNode;
1190 920 ▶ redo if $elem;
Branch: 1 branch
BranchTrueFalseTotal
if $elem 895 25 920
1191 }
1192 }
1193  
1194 # Add the documentation to the overall document and stringify.
1195 25 my $doc = XML::LibXML::Element->new('div');
1196 25 $doc->setAttribute(id => 'pgxndoc');
1197 25 $doc->appendText("\n ");
1198 25 $doc->addChild($toc);
1199 25 $topul->appendText("\n ");
1200 25 $toc->appendText("\n ");
1201 25 $doc->appendText("\n ");
1202 25 $doc->addChild($top);
1203 25 $top->appendText("");
1204 25 $doc->appendText("\n");
1205 25 return $doc;
1206 }
1207  
1208 3 __PACKAGE__->meta->make_immutable;
1209  
1210 3 1;
1211  
1212 __END__
1213  
1214 =head1 Name
1215  
1216 PGXN::API::Index - PGXN API distribution indexer
1217  
1218 =head1 Synopsis
1219  
1220 use PGXN::API::Indexer;
1221 my $indexer = PGXN::API::Indexer->new(verbose => $verbosity);
1222 $indexer->add_distribution({ meta => $dist_meta, zip => $zip });
1223  
1224 =head1 Description
1225  
1226 This module does the heavy lifting of indexing a PGXN distribution for the API
1227 server. Simply hand off the metadata loaded from a distribution F<META.json>
1228 file and an L<Archive::Zip> object loaded with the distribution download file
1229 and it will:
1230  
1231 =over
1232  
1233 =item 1
1234  
1235 Copy the distribution files from the local mirror to the API document root.
1236  
1237 =item 2
1238  
1239 Merge the distribution metadata between the metadata files matching the
1240 C<meta> and C<dist> URI templates. The templates are themselves loaded from
1241 the F</index.json> file from the mirror root. The two metadata documents each
1242 get additional data useful for API calls and become identical, as well.
1243  
1244 =item 3
1245  
1246 Searches for any and all C<README> files and documentation files and parses
1247 them into HTML. The format of all documentation files may be any recognized by
1248 L<Text::Markup>. The parsed HTML is then cleaned up and an table of contents
1249 added before being saved to its new home as a partial HTML document. See the
1250 pgTAP documentation L<on the PGXN API
1251 server|http://api.pgxn.org/dist/pgTAP/doc/pgtap.html> for a nice example of
1252 the resulting format (generated from a L<Markdown document in the pgTAP
1253 distribution|http://github.org/theory/pgtap/doc/pgtap.md>) and the same
1254 document used L<on the PGXN
1255 site|http://www.pgxn.org/dist/pgTAP/doc/pgtap.html> for how it can be used.
1256  
1257 =item 4
1258  
1259 Merges the user, tag, and extension metadata for the distribution, adding
1260 extra data points useful for the API.
1261  
1262 =item 5
1263  
1264 Adds all documentation as well as, distribution, extension, user, and tag
1265 metadata, to full text indexes. These may be queried via the API server
1266 (provided by L<pgxn_api_server> or locally with L<PGXN::API::Searcher>.
1267  
1268 =back
1269  
1270 The result is a robust API with much more information than is provided by the
1271 spare metadata JSON files on a normal PGXN mirror. The interface offered via
1272 L<pgxn_api_server> is then a superset of that offered by a normal mirror. It's
1273 a PGXN mirror + more!
1274  
1275 =head1 Class Interface
1276  
1277 =head2 Constructor
1278  
1279 =head3 C<new>
1280  
1281 my $indexer = PGXN::API::Indexer->new(verbose => $verbosity);
1282  
1283 Constructs and returns a new PGXN::API::Indexer object. There is only one
1284 parameter, C<verbose>, an incremental integer specifying the level of
1285 verbosity to use while indexing. Defaults to 0, which is as quiet as possible.
1286  
1287 =head1 Instance Interface
1288  
1289 =head2 Instance Methods
1290  
1291 =head3 C<update_root_json>
1292  
1293 Updates the F<index.json> file at the root of the document root, copying the
1294 mirror's F</index.json> to the API's F</index.json> and adding three
1295 additional templates:
1296  
1297 =over
1298  
1299 =item C<source>
1300  
1301 URI for browsing the source of a distribution. Its value is
1302  
1303 /src/{dist}/{dist}-{version}/
1304  
1305 =item C<search>
1306  
1307 The URI for search/ Its value is
1308  
1309 /search/{in}/
1310  
1311 =item C<doc>
1312  
1313 The URI for a documentation file. It's format is copied form the "meta"
1314 template, with the trailing C<META.json> replaced with `{+doc}.html}`.
1315 `{+doc}` is the path to a documentation file (without a file extension) and
1316 may include slashes.
1317  
1318 =back
1319  
1320 =head3 C<copy_from_mirror>
1321  
1322 $indexer->copy_from_mirror($path);
1323  
1324 Copies a file from the mirror to the document root. The path argument must be
1325 specified using Unix semantics (that is, using slashes for directory
1326 separators). Used by L<PGXN::API::Sync> to sync metadata files and stats.
1327  
1328 =head3 C<parse_from_mirror>
1329  
1330 $indexer->parse_from_mirror($path, $format);
1331  
1332 Uses Text::Markup to parse a file at C<$path> on the mirror, sanitizes it and
1333 generates a table of contents, and saves it to the document root with its
1334 suffix changed to F<.html>. Pass an optional C<format> argument to force
1335 Text::Markup to parse the document in that format.
1336  
1337 =head3 C<add_distribution>
1338  
1339 $indexer->add_distribution({ meta => $meta, zip => $zip });
1340  
1341 Adds a distribution to the index. This is the main method called to do all the
1342 work of indexing a distribution. The two required parameters are:
1343  
1344 =over
1345  
1346 =item C<meta>
1347  
1348 The metadata file loaded from a distribution F<META.json> file.
1349  
1350 =item c<zip>
1351  
1352 An L<Archive::Zip> object loaded up with the distribution download file.
1353  
1354 =item C<src_url>
1355  
1356 Source URL for the distribution. Should be the concatenation of the API base
1357 URL and the output of the C<source> template. Used to update relative links
1358 to files in the docs.
1359  
1360 =back
1361  
1362 =head3 C<copy_files>
1363  
1364 $indexer->copy_files($params);
1365  
1366 Copies a distribution download and C<README> files from the mirror to the API
1367 document root. The supported parameters are the same as those for
1368 C<add_distribution()>, by which this method is called internally.
1369  
1370 =head3 C<merge_distmeta>
1371  
1372 $indexer->copy_files($params);
1373  
1374 Merges the distribution metadata between the C<meta> file and the C<dist>
1375 file. These are the names of URI templates in the F</index.json> file. The
1376 supported parameters are the same as those for C<add_distribution()>, by which
1377 this method is called internally.
1378  
1379 Once the merge is complete, the two files will be identical, although the
1380 C<dist> file will only be updated if the new distribution's release status is
1381 "stable" (or if there are no stable distributions). In addition to the data
1382 they provided via the mirror server, they will also have the following new
1383 keys:
1384  
1385 =over
1386  
1387 =item C<special_files>
1388  
1389 An array of the names of special files in the distribution. These include any
1390 files which match the following regular expressions:
1391  
1392 =over
1393  
1394 =item C<qr{Change(?:s|Log)(?:[.][^.]+)?}i>
1395  
1396 =item C<qr{README(?:[.][^.]+)?}i>
1397  
1398 =item C<qr{LICENSE(?:[.][^.]+)?}i>
1399  
1400 =item C<qr{META[.]json}>
1401  
1402 =item C<qr{Makefile}>
1403  
1404 =item C<qr{MANIFEST}>
1405  
1406 =back
1407  
1408 =item C<docs>
1409  
1410 A hash (dictionary) listing the documentation files found in the distribution.
1411 These include a C<README> file and any files found under the F<doc> or F<docs>
1412 directory. The keys are paths to each document (without the file name
1413 extension) and the values are document titles.
1414  
1415 =item C<provides/$extension/doc>
1416  
1417 Each extension listed under C<provides> will get a new key, C<doc>, if there
1418 is a document in the C<docs> hash with the same base name as the extension.
1419 This is on the assumption that an included extension will have for its
1420 documentation a file with the same name (minus the file name extension) as the
1421 extension itself. The value will be the path to the document, the same as the
1422 key for the same document in the C<docs> hash.
1423  
1424 =back
1425  
1426 Getting all of this documentation information is handled via a call to
1427 C<parse_docs()>, which of course also parses any docs it finds.
1428  
1429 And finally, this method updates all other "dist" files for previous versions
1430 of the distribution with the latest C<releases> information, so that they all
1431 have a complete list of all releases of the distribution.
1432  
1433 =head3 C<find_docs>
1434  
1435 my @docs = $indexer->find_docs($params);
1436  
1437 Finds all the likely documentation files in the zip archive. A file is
1438 considered to contain documentation if one of the following is true:
1439  
1440 =over
1441  
1442 =item *
1443  
1444 It is identified under the C<doc> key in the C<provides> hash of the metadata
1445 and exists in the zip archive.
1446  
1447 =item *
1448  
1449 It has an extension recognized by L<Text::Markup> and is not excluded by the
1450 C<no_index> key in the metadata.
1451  
1452 =back
1453  
1454 The list of files returned are relative to an unzipped archive root -- that
1455 is, they do not include the top-level directory prefix.
1456  
1457 Used internally by C<parse_docs()> to determine what files to parse.
1458  
1459 =head3 C<parse_docs>
1460  
1461 $indexer->parse_docs($params);
1462  
1463 Searches the distribution download file for a C<README> and for documentation
1464 files in a F<doc> or F<docs> directory, parses them into HTML (using
1465 L<Text::Markup>), and the runs them through L<XML::LibXML> to remove all
1466 unsafe HTML, to generate a table of contents, and to save them as partial HTML
1467 files. Their contents are also added to the "doc" full text index. Files
1468 matching the rules under the C<no_index> key in the metadata (if any) will be
1469 ignored.
1470  
1471 Returns a hash reference with information about the documentation, with the
1472 keys being paths to the documentation (without file name extensions) and the
1473 values being the titles of the documents. The supported parameters are the
1474 same as those for C<add_distribution()>; this method is called internally by
1475 C<merge_distmeta()>.
1476  
1477 =head3 C<update_extensions>
1478  
1479 $indexer->update_extensions($params);
1480  
1481 Iterates over the list of extensions under the C<provides> key in the metadata
1482 and updates their respective metadata files (as specified by the "extension"
1483 URI template) and updates them with additional information. The supported
1484 parameters are the same as those for C<add_distribution()>, by which this
1485 method is called internally.
1486  
1487 The additional metadata added to the extension files is:
1488  
1489 =over
1490  
1491 =item C<$release_status/doc>
1492  
1493 The path to the documentation (without the file name extension) for the
1494 extension for the given release status.
1495  
1496  
1497 =item C<$release_status/abstract>
1498  
1499 The abstract for the latest release of the given release status.
1500  
1501 =item C<versions/$version/date>
1502  
1503 The date of a given release.
1504  
1505 =back
1506  
1507 The contents of the extension, including is name, abstract, distribution,
1508 distribution version, and doc path are added to the "extension" full text
1509 index.
1510  
1511 =head3 C<update_tags>
1512  
1513 $indexer->update_tags($params);
1514  
1515 Iterates over the list of tags under the C<tags> key in the metadata and
1516 updates their respective metadata files (as specified by the "tag" URI
1517 template). The supported parameters are the same as those for
1518 C<add_distribution()>, by which this method is called internally.
1519  
1520 The data added to each tag metadata file is the list of releases copied from
1521 the distribution metadata. A tag metadata file thus ends up with a complete
1522 list of all distribution releases associated with the tag. The tag is then
1523 added to the "tag" full text index.
1524  
1525 =head3 C<update_user>
1526  
1527 $indexer->update_user($params);
1528  
1529 Updates the metadata for the user specified under the C<user> key in the
1530 distribution metadata. The updated file is specified by the "user" URI
1531 template. The supported parameters are the same as those for
1532 C<add_distribution()>, by which this method is called internally.
1533  
1534 The data added to each user metadata file is the list of releases copied from
1535 the distribution metadata. A user metadata file thus ends up with a complete
1536 list of all distribution releases made by the user. The user is then added to
1537 the "user" full text index, where the name, nickname, email address, URI, and
1538 other metadata are indexed.
1539  
1540 =head3 C<merge_user>
1541  
1542 $indexer->merge_user($nickname);
1543  
1544 Pass in the nickname of a user file and JSON file for that user on the mirror
1545 will be merged with the document index copy. If no document index copy exists,
1546 one will be created with an empty hash under the C<releases> key. Called by
1547 L<PGXN::API::Sync> for each user file seen during the sync.
1548  
1549 =head3 C<finalize>
1550  
1551 $indexer->finalize;
1552  
1553 Method to call when a sync completes. At the moment, all it does is call
1554 C<update_user_lists()> and commit any remaining index data to the full text
1555 index.
1556  
1557 =head3 C<update_user_lists>
1558  
1559 $indexer->update_user_lists;
1560  
1561 Updates the user list files for any users seen in the distribution metadata
1562 processed by C<merge_distmeta()>.
1563  
1564 =head3 C<doc_root_file_for>
1565  
1566 my $doc_root_file = $indexer->doc_root_file_for($tmpl_name, $meta);
1567  
1568 Returns the full path to a file in the API document root for the specified URI
1569 template, and using the specified distribution metadata to populate the
1570 variable values in the template. Used internally to figure out what files
1571 to write to.
1572  
1573 =head3 C<mirror_file_for>
1574  
1575 my $mirror_file = $indexer->mirror_file_for($tmpl_name, $meta);
1576  
1577 Returns the full path to a file in local PGXN mirror directory for the
1578 specified URI template, and using the specified distribution metadata to
1579 populate the variable values in the template. Used internally to figure out
1580 what files to read from.
1581  
1582 =head3 C<indexer_for>
1583  
1584 my $ksi = $indexer->indexer_for($index_name);
1585  
1586 Returns a L<Lucy::Index::Indexer> object for updating named full text
1587 index. Used internally for updating the appropriate full text index when a
1588 distribution has been fully updated.
1589  
1590 =head2 Instance Accessors
1591  
1592 =head3 C<verbose>
1593  
1594 my $verbose = $indexer->verbose;
1595 $indexer->verbose($verbose);
1596  
1597 Get or set an incremental verbosity. The higher the integer specified, the
1598 more verbose the indexing.
1599  
1600 =head3 C<to_index>
1601  
1602 push @{ $indexer->to_index->{ $index } } => $data;
1603  
1604 Stores a hash reference of array references of data to be added to full text
1605 indexes. As a distribution is merged and updated, data for adding to the full
1606 text index is added to this hash. Once the updating and merging has completed
1607 successfully, the data is read from this attribute and written to the
1608 appropriate full text indexes.
1609  
1610 =head3 C<libxml>
1611  
1612 my $libxml = $indexer->libxml;
1613  
1614 Returns the L<XML::LibXML> object used for parsing and cleaning HTML documents.
1615  
1616 =head3 C<index_dir>
1617  
1618 my $index_dir = $indexer->index_dir;
1619  
1620 Returns the path to the parent directory of all of the full-text indexes.
1621  
1622 =head3 C<schemas>
1623  
1624 my $schema = $indexer->schemas->{$index_name};
1625  
1626 Returns a hash reference of L<Lucy::Plan::Schema> objects used to define
1627 the structure of the full text indexes. The keys identify the indexes and the
1628 values are the corresponding L<Lucy::Plan::Schema> objects. The supported
1629 indexes are:
1630  
1631 =over
1632  
1633 =item doc
1634  
1635 =item dist
1636  
1637 =item extension
1638  
1639 =item tag
1640  
1641 =item user
1642  
1643 =back
1644  
1645 =head1 Author
1646  
1647 David E. Wheeler <david.wheeler@pgexperts.com>
1648  
1649 =head1 Copyright and License
1650  
1651 Copyright (c) 2011-2026 David E. Wheeler.
1652  
1653 This module is free software; you can redistribute it and/or modify it under
1654 the L<PostgreSQL License|http://www.opensource.org/licenses/postgresql>.
1655  
1656 Permission to use, copy, modify, and distribute this software and its
1657 documentation for any purpose, without fee, and without a written agreement is
1658 hereby granted, provided that the above copyright notice and this paragraph
1659 and the following two paragraphs appear in all copies.
1660  
1661 In no event shall David E. Wheeler be liable to any party for direct,
1662 indirect, special, incidental, or consequential damages, including lost
1663 profits, arising out of the use of this software and its documentation, even
1664 if David E. Wheeler has been advised of the possibility of such damage.
1665  
1666 David E. Wheeler specifically disclaims any warranties, including, but not
1667 limited to, the implied warranties of merchantability and fitness for a
1668 particular purpose. The software provided hereunder is on an "as is" basis,
1669 and David E. Wheeler has no obligations to provide maintenance, support,
1670 updates, enhancements, or modifications.
1671  
1672 =cut
« blib/lib/PGXN/API/Sync.pm ↑ Summary Build »