Link/Selector.pm

filter: Statementstmt 34.6% 34 / 98 Branchbran 8.0% 5 / 62 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 27.2% 3 / 11 Podpod 75.0% 3 / 4 totaltotal 25.0% 45 / 180 CC 39 SCAR 38.6
CC
39
Cov
24%
__ANON__
110.0
__ANON__
72.0
__ANON__
56.0
CRAP 47.3
« Link/Tester/Complex.pm ↑ Summary Link/Reporter.pm »
1 =head1 NAME
2  
3 WWW::Link::Selector - link selection functions.
4  
5 =head1 SYNOPSIS
6  
7 use MLDBM qw(DB_File);
8 use CDB_File::BiIndex;
9 use WWW::Link::Selector;
10 use WWW::Link::Reporter;
11  
12 #generate a function which uses lists of regexs to include or
13 #exclude links
14 $::include=WWW::Link::Selector::gen_include_exclude @::exclude, @::include;
15  
16 $::index = new CDB_File::BiIndex $::page_index, $::link_index
17 $::linkdbm = tie %::links, "MLDBM", $::links, O_RDONLY, 0666, $::DB_HASH
18 or die $!;
19 $::reporter=new WWW::Link::Reporter::HTML \*STDOUT, $::index;
20  
21 #generate a function which will use all
22 $::selectfunc = WWW::Link::Selector::generate_select_func
23 ( \%::links, $::reporter, $::include, $::index, );
24  
25 #report on all selectedlinks
26 &$::selectfunc;
27  
28 =head1 DESCRIPTION
29  
30 This is a package (not a class though) which builds functions for
31 selecting links to give information about to a user. So far there are
32 two ways of doing this, either scanning the entire database or using
33 an index to get the information.
34  
35 =cut
36  
37 package WWW::Link::Selector;
38 1 $REVISION=q$Revision: 1.10 $ ; $VERSION = sprintf ( "%d.%02d", $REVISION =~ /(\d+).(\d+)/ );
39 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Carp qw(croak carp cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 $WWW::Link::Selector::ignore_missing = 0;
43  
44 #$WWW::Link::Selector::verbose = 0xFF;
45 1 ▶$WWW::Link::Selector::verbose = 0x00
Branch: 1 branch
BranchTrueFalseTotal
unless defined $WWW::Link::Selector::verbose 1 0 1
46 unless defined $WWW::Link::Selector::verbose;
47  
48  
49 0 ▶sub _check_link ($$$) {
Subroutine _check_link: not called CC=4 SCAR=30.0
50 0 my ($link,$url,$reporter)=@_;
51 0 ▶ (defined $link) or do {
Branch: 1 branch
BranchTrueFalseTotal
defined $link or do { $reporter->not_found($url) unless $WWW::Link::Selector::ignore_missing; return 0 } 0 0 0
52 0 ▶ $reporter->not_found( $url ) unless $WWW::Link::Selector::ignore_missing;
Branch: 1 branch
BranchTrueFalseTotal
unless $WWW::Link::Selector::ignore_missing 0 0 0
53 0 return 0;
54 };
55 0 ▶ ref $link or do {
Branch: 1 branch
BranchTrueFalseTotal
ref $link or do { cluck("Non reference $link found in database for url $url.\n"); return 0 } 0 0 0
56 0 cluck "Non reference $link found in database for url $url.\n";
57 0 return 0;
58 };
59 0 return 1;
60 }
61  
62 =head1 generate_url_func
63  
64 This function creates a url function which will act on each of the
65 links for the urls given in its arguments. If any of the arguments
66 have spaces it split them into different urls around that space.
67  
68 =cut
69  
70 0 ▶sub generate_url_func ($$\@) {
Subroutine generate_url_func: not called CC=3 SCAR=24.8
Pod undocumented
71 0 my $link_database=shift;
72 0 my $reporter=shift;
73 0 my $urls=shift;
74  
75 0 my @urllist=();
76 0 foreach (@$urls) {
77 0 push @urllist, split /\s+/, $_;
78 }
79  
80 0 ▶ croak "empty urllist" unless @urllist;
Branch: 1 branch
BranchTrueFalseTotal
unless @urllist 0 0 0
81  
82 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
83 # a closure with the link database and urllist enclosed
84 0 foreach ( @urllist ) {
85 0 s/\s//g;
86 0 my $url = $_;
87 0 my $link=$link_database->{$url};
88 0 ▶ next unless _check_link($link,$url,$reporter);
Branch: 1 branch
BranchTrueFalseTotal
unless &_check_link($link, $url, $reporter) 0 0 0
89 0 $reporter->examine( $link );
90 }
91 }
92 0 }
93  
94 =head2 generate_select_func(link_database, reporter, include_func, [index])
95  
96 This function generates a selector function which works in one of two modes.
97  
98 In the first, no index is given and it recurses through all of the
99 links in the database.
100  
101 In the second it generates a selection function which recurses through
102 the B<index> working on each url.
103  
104 For each url it finds, it calls the given link B<reporter> if the
105 B<include_func> returns true for that url.
106  
107 =cut
108  
109 0 ▶sub generate_select_func ($$$;$) {
Subroutine generate_select_func: not called CC=2 SCAR=17.9
Pod documented
110 0 my ($link_database, $reporter, $include_func, $index)=@_;
111 0 ▶ if ($index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index) { } 0 0 0
112 0 ▶ return sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
113  
114 0 ▶ print STDERR "Using index to generate list of urls to examine.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Selector::verbose & 16 0 0 0
115 if $WWW::Link::Selector::verbose & 16;
116  
117 #check within an infostructure
118  
119 0 my $url;
120 0 $index->second_reset();
121 0 ▶ URL: while($url=$index->second_next()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url = $index->second_next) 0 0 0
122 0 my $link=$link_database->{$url};
123 0 ▶ print STDERR "WWW::Link::Selector::[generated selector] Looking"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Selector::verbose & 64 0 0 0
124 . " at link $url.\n"
125 if $WWW::Link::Selector::verbose & 64;
126 0 ▶ next unless _check_link($link,$url,$reporter);
Branch: 1 branch
BranchTrueFalseTotal
unless &_check_link($link, $url, $reporter) 0 0 0
127 0 ▶ $reporter->examine ( $link ) if &$include_func($url);
Branch: 1 branch
BranchTrueFalseTotal
if &$include_func($url) 0 0 0
128 }
129  
130 0 ▶ print STDERR "Finished reporting index of urls.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Selector::verbose & 16 0 0 0
131 if $WWW::Link::Selector::verbose & 16;
132  
133 }
134 0 } else {
135 0 ▶ return sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
136  
137 0 ▶ print STDERR "Going through all urls in database\n"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Selector::verbose & 16 0 0 0
138 if $WWW::Link::Selector::verbose & 16;
139  
140 # check across the whole database of links
141  
142 0 my ($url,$link);
143 0 ▶ LINK: while(($url,$link)=each %$link_database) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$link_database) 0 0 0
144 0 ▶ next if (!ref $url) and $url =~ m/^\%\+\+/;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $url and $url =~ /^\%\+\+/ 0 0 0
Condition0%0/3
not ref $url and $url =~ /^\%\+\+/
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $url and $url =~ /^\%\+\+/
not ref $url $url =~ /^\%\+\+/
Truth table0%0/3
not(A) and B
A= not ref $urlB= $url =~ /^\%\+\+/
ABresult
0X0
100
111
145 0 ▶ next unless _check_link($link,$url,$reporter);
Branch: 1 branch
BranchTrueFalseTotal
unless &_check_link($link, $url, $reporter) 0 0 0
146 0 ▶ print STDERR "Looking at link $url.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Selector::verbose & 64 0 0 0
147 if $WWW::Link::Selector::verbose & 64;
148 0 ▶ $reporter->examine ( $link ) if &$include_func($url);
Branch: 1 branch
BranchTrueFalseTotal
if &$include_func($url) 0 0 0
149 }
150 }
151 0 }
152 }
153  
154  
155 =head2 generate_index_select_func(link_database, reporter, include_func, index)
156  
157 This function returns a function which iterates through all of the
158 links found in the index, calling $reporter->examine() for each link.
159  
160 In this select function, the include_func is a function which is
161 called on each page url in our own pages to decide whether or not to
162 report the link.
163  
164 =cut
165  
166 0 ▶sub generate_index_select_func ($$$$) {
Subroutine generate_index_select_func: not called CC=1 SCAR=6.9
Pod documented
167 0 my ($link_database, $reporter, $include_func, $index)=@_;
168 0 ▶ return sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
169 0 my $url;
170 0 $index->second_reset();
171 0 ▶ URL: while($url=$index->second_next()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url = $index->second_next) 0 0 0
172 0 my $pagelist=$index->lookup_second($url);
173  
174 0 my $include=0;
175 0 foreach my $page (@$pagelist) {
176 0 ▶ &$include_func($page) || next;
Branch: 1 branch
BranchTrueFalseTotal
&$include_func($page) or next 0 0 0
177 0 $include=1;
178 }
179 0 ▶ next URL unless $include;
Branch: 1 branch
BranchTrueFalseTotal
unless $include 0 0 0
180 0 my $link=$link_database->{$url};
181 0 ▶ ref $link or do {
Branch: 1 branch
BranchTrueFalseTotal
ref $link or do { warn "Non reference $link found in database for url $url.\n"; next } 0 0 0
182 0 warn "Non reference $link found in database for url $url.\n";
183 0 next;
184 };
185 0 ▶ print STDERR "WWW::Link::Selector::[generated index selector] "
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Selector::verbose & 64 0 0 0
186 . "Looking at link $url.\n" if $WWW::Link::Selector::verbose & 64;
187 0 ▶ unless ($link) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link) 0 0 0
188 0 ▶ $reporter->not_found( $url ) unless $WWW::Link::Selector::ignore_missing;
Branch: 1 branch
BranchTrueFalseTotal
unless $WWW::Link::Selector::ignore_missing 0 0 0
189 0 next URL;
190 }
191 0 $reporter->examine ( $link ) ;
192 }
193 0 ▶ print STDERR "Finished reporting index of urls.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Selector::verbose & 16 0 0 0
194 if $WWW::Link::Selector::verbose & 16;
195 }
196 0 }
197  
198 =head2 gen_include_exclude (@exclude, @include)
199  
200 This function generates a function which will return false if any of
201 the regexps in the exclude_listre match and even then will return
202 false unless one of the regexps in the include listref matches.
203  
204 If the first list is empty then all links matching the include list
205 will be accepted.
206  
207 If the second list is empty, then all links not matching the exlcude
208 list will be accepted.
209  
210 The fuction generated can be used by generate_select_func (see above).
211  
212 =cut
213  
214  
215 # this following fuction could be much more efficient with a compile
216 # once single regex. See Manifest.pm for what seems to be an example.
217  
218 # after Tom Christiansen in his FMTYEWTK on regexps.
219  
220 1 ▶sub gen_include_exclude (\@\@){
Subroutine gen_include_exclude: called CC=9 SCAR=22.3
Pod documented
221 1 my ($excludes, $includes) = @_;
222  
223 1 my @excludearray=();
224 1 foreach my $exclude (@$excludes) {
225 2 $exclude =~ s,(?<=[^\\])((:\\\\)*)/,$1\\/,g;
226 2 push @excludearray, $exclude;
227 }
228 1 my @includearray=();
229 1 foreach my $include (@$includes) {
230 2 $include =~ s,(?<=[^\\])((:\\\\)*)/,$1\\/,g;
231 2 push @includearray, $include;
232 }
233  
234 1 my $code = <<"EOCODE";
235 sub {
236 EOCODE
237 1 ▶ $code .= <<"EOCODE" if @excludearray + @includearray > 5;
Branch: 1 branch
BranchTrueFalseTotal
if @excludearray + @includearray > 5 0 1 1
238 study;
239 EOCODE
240 1 my $pat;
241 1 for $pat (@excludearray) {
242 2 $code .= <<"EOCODE";
243 return 0 if \$_[0] =~ /$pat/;
244 EOCODE
245 }
246 1 ▶ unless (@includearray) {
Branch: 1 branch
BranchTrueFalseTotal
if (@includearray) { } 1 0 1
247 0 $code .= <<"EOCODE";
248 return 1;
249 EOCODE
250 } else {
251 1 for $pat (@includearray) {
252 2 $code .= <<"EOCODE";
253 return 1 if \$_[0] =~ /$pat/;
254 EOCODE
255 }
256 1 $code .= <<"EOCODE";
257 return 0;
258 EOCODE
259 }
260 1 $code .= "}\n";
261 1 ▶ print "CODE: $code\n"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Selector::verbose & 128 0 1 1
262 if $WWW::Link::Selector::verbose & 128;
263 1 my $func = eval $code;
264 1 ▶ die "bad pattern: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
265 1 return $func;
266 }
267  
268 1 1;
« Link/Tester/Complex.pm ↑ Summary Link/Reporter.pm »