| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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[.+?/\*$]
CORE::length $line > 2 and $line =~ m[.+/.+]
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[.+?/\*$]
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 136 | 69 | ▶ | $url .= '/' if $url =~ /\:\/\/[\w\-\.]+$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 137 | 69 | $url = new URI $url; | |||||||||||||||||||||||||||||||||
| 138 | 69 | ▶ | my $pq = ( defined $url->query ) ? $url->path.'?'.$url->query : $url->path; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | 325 | ▶ | if ( !${ $self->patterns }{ $pattern }{ host_regex } ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 146 | 322 | ▶ | return 1 if $url->host =~ ${ $self->patterns }{ $pattern }{ host_r }; } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | else { | ||||||||||||||||||||||||||||||||||
| 148 | 3 | ▶ | return 1 if $self->use_regex && $url->host =~ ${ $self->patterns }{ $pattern }{ host_r }; } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->use_regex and $url->host =~ /do {
$self->patterns
}->{$pattern}{"host_r"}/
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"}/
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 185 | 0 | my @sorted_list; | |||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | @sorted_list = sort keys %{ $self->patterns } if $self->sorting eq 'alphabetical'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | @sorted_list = sort { CORE::length $a <=> CORE::length $b } keys %{ $self->patterns } if $self->sorting eq 'length'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | @sorted_list = sort { ${ $self->patterns }{ $a }{ origorder } <=> ${ $self->patterns }{ $b }{ origorder } } keys %{ $self->patterns } if $self->sorting eq 'origorder'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | return @sorted_list if $self->sortorder; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||