blib/lib/WWW/YaCyBlacklist.pm

filter: Statementstmt 76.2% 106 / 139 Branchbran 61.9% 26 / 42 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 83.3% 15 / 18 Podpod 100% 9 / 9 totaltotal 72.5% 161 / 222 CC 27 SCAR 25.9
CC
27
Cov
72%
sort_list
56.0
check_url
10.1
read_from_array
8.1
CRAP 13.3
↑ Summary
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package WWW::YaCyBlacklist;
5 # ABSTRACT: a Perl module to parse and execute YaCy blacklists
6  
7 5 our $AUTHORITY = 'cpan:IBRAUN';
8 5 $WWW::YaCyBlacklist::VERSION = '1.00';
9  
10 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 require 5.033006;
15  
16  
17 # Needed if RegExps do not compile
18 5 has 'use_regex' => (
19 is => 'ro',
20 isa => 'Bool',
21 default => 1,
22 );
23  
24  
25 5 has 'filename' => (
26 is => 'rw',
27 isa => 'Str',
28 default => 'ycb.black',
29 );
30  
31 5 has 'file_charset' => (
32 is => 'ro',
33 isa => 'Str',
34 default => 'UTF-8',
35 init_arg => undef,
36 );
37  
38 5 has 'origorder' => (
39 is => 'rw',
40 isa => 'Int',
41 default => 0,
42 init_arg => undef,
43 );
44  
45  
46 5 has 'sortorder' => (
47 is => 'rw',
48 isa => 'Bool',
49 default => 0,
50 );
51  
52  
53 5 has 'sorting' => (
54 is => 'rw',
55 isa => enum([qw[ alphabetical length origorder random reverse_host ]]),
56 default => 'origorder',
57 );
58  
59 has 'patterns' => (
60 is=>'rw',
61 isa => 'HashRef',
62 traits => [ 'Hash' ],
63 6 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 5 init_arg => undef,
65 );
66  
67 30 ▶sub _check_host_regex {
Subroutine _check_host_regex: called CC=2 SCAR=6.9
68  
69 30 my ($self, $pattern) = @_;
70  
71 30 ▶ return 0 if $pattern =~ /^[\w\-\.\*]+$/; # underscores are not allowed in domain names but sometimes happen in subdomains
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /^[\w\-\.\*]+$/ 28 2 30
72 2 return 1;
73 }
74  
75  
76 4 ▶sub read_from_array {
Subroutine read_from_array: called CC=8 SCAR=20.9
Pod documented
77  
78 4 my ($self, @lines) = @_;
79  
80 4 foreach my $line ( @lines ) {
81  
82 29 ▶ if ( exists( ${ $self->patterns }{ $line } ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists do { $self->patterns }->{$line}) { } 2 27 29
elsif (CORE::length $line > 2 and $line =~ m[.+/.+] and not $line =~ m[.+?/\*$]) { } 27 0 27
83 2 print STDERR "\nNOTICE: ignoring double pattern '", $line, "'\n";
84 2 next;
85 }
86 ▶ elsif ( CORE::length $line > 2 && $line =~ /.+\/.+/ && $line !~ /.+?\/\*$/ ) {
Condition33%2/6
CORE::length $line > 2 and $line =~ m[.+/.+] and not $line =~ m[.+?/\*$]
!ll&&!rl&&r
0027
CORE::length $line > 2 and $line =~ m[.+/.+]
!ll&&!rl&&r
0027
MC/DC0%0/3
CORE::length $line > 2 and $line =~ m[.+/.+] and not $line =~ m[.+?/\*$]
CORE::length $line > 2 $line =~ m[.+/.+] not $line =~ m[.+?/\*$]
Truth table25%1/4
A and B and not(C)
A= CORE::length $line > 2B= $line =~ m[.+/.+]C= not $line =~ m[.+?/\*$]
ABCresult
0XX0
10X0
1100
1111
87 27 $self->origorder( $self->origorder + 1 );
88 27 ${ $self->patterns }{ $line }{ 'origorder' } = $self->origorder;
89 27 ( ${ $self->patterns }{ $line }{ 'host' }, ${ $self->patterns }{ $line }{ 'path' } ) = split /(?!\\)\/+?/, $line, 2;
90 27 ${ $self->patterns }{ $line }{ 'path_r' } = qr/^${ $self->patterns }{ $line }{ 'path' }$/;
91 27 ${ $self->patterns }{ $line }{ 'host_regex' } = $self->_check_host_regex( ${ $self->patterns }{ $line }{ 'host' } );
92  
93 27 ▶ if ( ${ $self->patterns }{ $line }{ 'host_regex' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $self->patterns }->{$line}{'host_regex'}) { } 1 26 27
94 1 ${ $self->patterns }{ $line }{ 'host_r' } = qr/${ $self->patterns }{ $line }{ host }/; }
95 else {
96 26 ▶ if ( index( ${ $self->patterns }{ $line }{ host }, '*') > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(do { $self->patterns }->{$line}{'host'}, '*') != -1) { } 4 22 26
97 4 my $host = ${ $self->patterns }{ $line }{ host };
98 4 $host =~ s/\*/.*/g;
99 4 ${ $self->patterns }{ $line }{ 'host_r' } = qr/^$host$/;
100 }
101 else {
102 22 ${ $self->patterns }{ $line }{ 'host_r' } = qr/^([\w\-]+\.)*${ $self->patterns }{ $line }{ host }$/; }
103 }
104 }
105 0 else { print STDERR "\n\tWARNING: ignoring broken pattern '", $line, "'\n"; }
106 }
107 }
108  
109  
110 0 ▶sub read_from_files {
Subroutine read_from_files: not called CC=1 SCAR=6.9
Pod documented
111  
112 0 my ($self, @files) = @_;
113 0 my @lines;
114  
115 0 grep { push( @lines, io( $_ )->encoding( $self->file_charset )->chomp->slurp ) } @files;
116  
117 # chomp is not fully reliable with Windows files in Linux
118 0 grep { my $s = $_; $s =~ s/\r$//; $s } @lines;
119  
120 0 $self->read_from_array( @lines );
121 }
122  
123  
124 5 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
125  
126 5 my $self = shift;
127 5 return scalar %{ $self->patterns };
128 }
129  
130  
131 69 ▶sub check_url {
Subroutine check_url: called CC=10 SCAR=23.1
Pod documented
132  
133 69 my $self = shift;
134 69 my $url = shift;
135 69 ▶ return 1 if $url !~ /^(ht|f)tps?\:\/\//i;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^(ht|f)tps?\://]i 0 69 69
136 69 ▶ $url .= '/' if $url =~ /\:\/\/[\w\-\.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[\://[\w\-\.]+$] 0 69 69
137 69 $url = new URI $url;
138 69 ▶ my $pq = ( defined $url->query ) ? $url->path.'?'.$url->query : $url->path;
Branch: 1 branch
BranchTrueFalseTotal
defined $url->query ? : 11 58 69
139 69 $pq =~ s/^\///;
140  
141 69 foreach my $pattern ( keys %{ $self->patterns } ) {
142  
143 776 ▶ next if $pq !~ ${ $self->patterns }{ $pattern }{ 'path_r' };
Branch: 1 branch
BranchTrueFalseTotal
unless $pq =~ /do { $self->patterns }->{$pattern}{"path_r"}/ 451 325 776
144  
145 325 ▶ if ( !${ $self->patterns }{ $pattern }{ host_regex } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not do { $self->patterns }->{$pattern}{'host_regex'}) { } 322 3 325
146 322 ▶ return 1 if $url->host =~ ${ $self->patterns }{ $pattern }{ host_r }; }
Branch: 1 branch
BranchTrueFalseTotal
if $url->host =~ /do { $self->patterns }->{$pattern}{"host_r"}/ 50 272 322
147 else {
148 3 ▶ return 1 if $self->use_regex && $url->host =~ ${ $self->patterns }{ $pattern }{ host_r }; }
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_regex and $url->host =~ /do { $self->patterns }->{$pattern}{"host_r"}/ 1 2 3
Condition66%2/3
$self->use_regex and $url->host =~ /do { $self->patterns }->{$pattern}{"host_r"}/
!ll&&!rl&&r
021
MC/DC50%1/2
$self->use_regex and $url->host =~ /do { $self->patterns }->{$pattern}{"host_r"}/
$self->use_regex $url->host =~ /do { $self->patterns }->{$pattern}{"host_r"}/
Truth table66%2/3
A and B
A= $self->use_regexB= $url->host =~ /do { $self->patterns }->{$pattern}{"host_r"}/
ABresult
0X0
100
111
149  
150 }
151 18 return 0;
152 }
153  
154  
155 2 ▶sub find_matches {
Subroutine find_matches: called CC=2 SCAR=6.9
Pod documented
156  
157 2 my $self = shift;
158 2 my @urls;
159 2 ▶ grep { push( @urls, $_ ) if $self->check_url( $_ ) } @_;
Branch: 1 branch
BranchTrueFalseTotal
if $self->check_url($_) 11 5 16
160 2 return @urls;
161 }
162  
163  
164 1 ▶sub find_non_matches {
Subroutine find_non_matches: called CC=2 SCAR=6.9
Pod documented
165  
166 1 my $self = shift;
167 1 my @urls;
168 1 ▶ grep { push( @urls, $_ ) if !$self->check_url( $_ ) } @_;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->check_url($_) 2 6 8
169 1 return @urls;
170 }
171  
172  
173 1 ▶sub delete_pattern {
Subroutine delete_pattern: called CC=2 SCAR=7.0
Pod documented
174  
175 1 my $self = shift;
176 1 my $pattern = shift;
177 1 ▶ delete( ${ $self->patterns }{ $pattern } ) if exists( ${ $self->patterns }{ $pattern } ) ;
Branch: 1 branch
BranchTrueFalseTotal
if exists do { $self->patterns }->{$pattern} 1 0 1
178 }
179  
180  
181 0 ▶sub sort_list {
Subroutine sort_list: not called CC=7 SCAR=40.3
Pod documented
182  
183 0 my $self = shift;
184 0 ▶ return keys %{ $self->patterns } if $self->sorting eq 'random';
Branch: 1 branch
BranchTrueFalseTotal
if $self->sorting eq "random" 0 0 0
185 0 my @sorted_list;
186  
187 0 ▶ @sorted_list = sort keys %{ $self->patterns } if $self->sorting eq 'alphabetical';
Branch: 1 branch
BranchTrueFalseTotal
if $self->sorting eq "alphabetical" 0 0 0
188 0 ▶ @sorted_list = sort { CORE::length $a <=> CORE::length $b } keys %{ $self->patterns } if $self->sorting eq 'length';
Branch: 1 branch
BranchTrueFalseTotal
if $self->sorting eq "length" 0 0 0
189 0 ▶ @sorted_list = sort { ${ $self->patterns }{ $a }{ origorder } <=> ${ $self->patterns }{ $b }{ origorder } } keys %{ $self->patterns } if $self->sorting eq 'origorder';
Branch: 1 branch
BranchTrueFalseTotal
if $self->sorting eq "origorder" 0 0 0
190 0 ▶ @sorted_list = sort { reverse( ${ $self->patterns }{ $a }{ host } ) cmp reverse( ${ $self->patterns }{ $b }{ host } ) } keys %{ $self->patterns } if $self->sorting eq 'reverse_host';
Branch: 1 branch
BranchTrueFalseTotal
if $self->sorting eq "reverse_host" 0 0 0
191  
192 0 ▶ return @sorted_list if $self->sortorder;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sortorder 0 0 0
193 0 return reverse( @sorted_list );
194 }
195  
196  
197 0 ▶sub store_list {
Subroutine store_list: not called CC=1 SCAR=6.9
Pod documented
198  
199 0 my $self = shift;
200 0 join( "\n", $self->sort_list( ) ) . "\n" > io( $self->filename );
201 }
202  
203 5 1;
204 5 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
205 5 __PACKAGE__->meta->make_immutable;
206  
207 __END__
↑ Summary