Prog/Aggregator.pm

filter: Statementstmt 58.6% 68 / 116 Branchbran 15.7% 6 / 38 Conditioncond 21.6% 8 / 37 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 76.1% 16 / 21 Podpod 100% 7 / 7 totaltotal 43.9% 105 / 239 CC 35 SCAR 47.5
CC
35
Cov
43%
swish_filter
342.0
_apply_file_rules
15.1
set_ok_if_newer_than
12.0
CRAP 116.1
« Prog/Aggregator/Spider.pm ↑ Summary Prog/Headers.pm »
1 package SWISH::Prog::Aggregator;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use base qw( SWISH::Prog::Class );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use SWISH::Prog::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use SWISH::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use SWISH::Prog::Doc;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 our $VERSION = '0.75';
13  
14 9 __PACKAGE__->mk_accessors(
15 qw(
16 set_parser_from_type
17 indexer
18 doc_class
19 swish_filter_obj
20 test_mode filter
21 ok_if_newer_than
22 progress
23 )
24 );
25 9 __PACKAGE__->mk_ro_accessors(qw( count ));
26  
27 =pod
28  
29 =head1 NAME
30  
31 SWISH::Prog::Aggregator - document aggregation base class
32  
33 =head1 SYNOPSIS
34  
35 package MyAggregator;
36 use strict;
37 use base qw( SWISH::Prog::Aggregator );
38
39 sub get_doc {
40 my ($self, $url) = @_;
41
42 # do something to create a SWISH::Prog::Doc object from $url
43
44 return $doc;
45 }
46
47 sub crawl {
48 my ($self, @where) = @_;
49
50 foreach my $place (@where) {
51
52 # do something to search $place for docs to pass to get_doc()
53
54 }
55 }
56
57 1;
58  
59 =head1 DESCRIPTION
60  
61 SWISH::Prog::Aggregator is a base class that defines the basic API for writing
62 an aggregator. Only two methods are required: get_doc() and crawl(). See
63 the SYNOPSIS for the prototypes.
64  
65 See SWISH::Prog::Aggregator::FS and SWISH::Prog::Aggregator::Spider for examples
66 of aggregators that crawl the filesystem and web, respectively.
67  
68 =head1 METHODS
69  
70 =head2 init
71  
72 Set object flags per SWISH::Prog::Class API. These are also accessors,
73 and include:
74  
75 =over
76  
77 =item set_parser_from_type
78  
79 This will set the parser() value in swish_filter() based on the
80 MIME type of the doc_class() object.
81  
82 =item indexer
83  
84 A SWISH::Prog::Indexer object.
85  
86 =item doc_class
87  
88 The name of the SWISH::Prog::Doc-derived class to use in get_doc().
89 Default is SWISH::Prog::Doc.
90  
91 =item swish_filter_obj
92  
93 A SWISH::Filter object. If not passed in new() one is created for you.
94  
95 =item test_mode
96  
97 Dry run mode, just prints info on stderr but does not
98 build index.
99  
100 =item filter
101  
102 Value should be a CODE ref. This is passed through to set_filter();
103 there is no C<filter> mutator method.
104  
105 =item ok_if_newer_than
106  
107 Value should be a Unix timestamp (epoch seconds). Default is undef.
108 If set, aggregators should skip files that have a modification time
109 older than the timestamp.
110  
111 You may get/set the ok_if_newer_than value with the ok_if_newer_than()
112 attribute method, but use set_ok_if_newer_than() to include validation
113 of the supplied I<timestamp> value.
114  
115 =item progress( I<Term::ProgressBar object> )
116  
117 Get/set a progress object. The default used in the examples/swish3
118 script is Term::ProgressBar. If set, it will be incremented
119 just like count() is.
120  
121 =back
122  
123 =cut
124  
125 1 ▶sub init {
Subroutine init: called CC=5 SCAR=16.8
Pod documented
126 1 my $self = shift;
127 1 my %arg = @_;
128 1 my $filter = delete $arg{filter};
129 1 $self->SUPER::init(%arg);
130 1 ▶ $self->{verbose} ||= 0;
Condition50%1/2
$self->{"verbose"} ||= 0
l!l
01
MC/DC0%0/1
$self->{"verbose"} ||= 0
$self->{"verbose"}
Truth table50%1/2
A
A= $self->{"verbose"}
Aresult
11
00
131 1 $self->{__progress_so_far} = 0;
132 1 $self->{__progress_next} = 0;
133  
134 1 ▶ $self->{doc_class} ||= 'SWISH::Prog::Doc';
Condition50%1/2
$self->{"doc_class"} ||= "SWISH::Prog::Doc"
l!l
01
MC/DC0%0/1
$self->{"doc_class"} ||= "SWISH::Prog::Doc"
$self->{"doc_class"}
Truth table50%1/2
A
A= $self->{"doc_class"}
Aresult
11
00
135 1 ▶ $self->{swish_filter_obj} ||= SWISH::Filter->new;
Condition33%1/3
$self->{"swish_filter_obj"} ||= "SWISH::Filter"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"swish_filter_obj"} ||= "SWISH::Filter"->new
$self->{"swish_filter_obj"} "SWISH::Filter"->new
Truth table33%1/3
A ||= B
A= $self->{"swish_filter_obj"}B= "SWISH::Filter"->new
ABresult
1X1
011
000
136  
137 1 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 1 0 1
138 1 $self->set_filter($filter);
139 }
140  
141 }
142  
143 =head2 config
144  
145 Returns the SWISH::Prog::Config object from the Indexer
146 being used. This is a read-only method (accessor not mutator).
147  
148 =cut
149  
150 0 ▶sub config {
Subroutine config: not called CC=1 SCAR=6.9
Pod documented
151 0 return shift->{indexer}->config;
152 }
153  
154 =head2 count
155  
156 Returns the total number of doc_class() objects returned by get_doc().
157  
158 =cut
159  
160 =head2 crawl( I<@where> )
161  
162 Override this method in your subclass. It does the aggregation,
163 and passes each doc_class() object from get_doc() to indexer->process().
164  
165 =cut
166  
167 0 ▶sub crawl {
Subroutine crawl: not called CC=1 SCAR=6.9
Pod documented
168 0 my $self = shift;
169 0 croak ref($self) . " does not implement crawl()";
170 }
171  
172 =head2 get_doc( I<url> )
173  
174 Override this method in your subclass. Should return a doc_class()
175 object.
176  
177 =cut
178  
179 0 ▶sub get_doc {
Subroutine get_doc: not called CC=1 SCAR=6.9
Pod documented
180 0 my $self = shift;
181 0 croak ref($self) . " does not implement get_doc()";
182 }
183  
184 =head2 swish_filter( I<doc_class_object> )
185  
186 Passes the content() of the SPD object through SWISH::Filter
187 and transforms it to something index-able. Returns
188 the I<doc_class_object>, filtered.
189  
190 B<NOTE:> This method should be called by all aggregators after
191 get_doc() and before passing to the indexer().
192  
193 See the SWISH::Filter documentation.
194  
195 =cut
196  
197 0 ▶sub swish_filter {
Subroutine swish_filter: not called CC=18 SCAR=58.3
Pod documented
198 0 my $self = shift;
199 0 my $doc = shift;
200 0 ▶ unless ( $doc && blessed($doc) && $doc->isa('SWISH::Prog::Doc') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc and builtin::blessed($doc) and $doc->isa("SWISH::Prog::Doc")) 0 0 0
Condition0%0/6
$doc and builtin::blessed($doc) and $doc->isa("SWISH::Prog::Doc")
!ll&&!rl&&r
000
$doc and builtin::blessed($doc)
!ll&&!rl&&r
000
MC/DC0%0/3
$doc and builtin::blessed($doc) and $doc->isa("SWISH::Prog::Doc")
$doc builtin::blessed($doc) $doc->isa("SWISH::Prog::Doc")
Truth table0%0/4
A and B and C
A= $docB= builtin::blessed($doc)C= $doc->isa("SWISH::Prog::Doc")
ABCresult
0XX0
10X0
1100
1111
201 0 croak "SWISH::Prog::Doc-derived object required";
202 }
203  
204 0 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
205 0 warn "checking filter for " . $doc->url;
206 }
207  
208 0 ▶ unless ( defined $doc->parser ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $doc->parser) 0 0 0
209 0 ▶ if ( $self->set_parser_from_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->set_parser_from_type) 0 0 0
210 0 ▶ my $type = $doc->type || 'default';
Condition0%0/2
$doc->type || 'default'
l!l
00
MC/DC0%0/1
$doc->type || 'default'
$doc->type
Truth table0%0/2
A
A= $doc->type
Aresult
11
00
211 0 $doc->parser( $SWISH::Prog::Utils::ParserTypes{$type} );
212 }
213 }
214  
215 0 ▶ if ( $self->{swish_filter_obj}->can_filter( $doc->type ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'swish_filter_obj'}->can_filter($doc->type)) { } 0 0 0
216  
217 0 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
218 0 warn sprintf
219 "debug=%d can_filter true for %s with parser %s for type %s",
220 $self->debug, $doc->url,
221 $doc->parser, $doc->type;
222 }
223  
224 0 my $content = $doc->content;
225 0 my $url = $doc->url;
226 0 my $type = $doc->type;
227 my $f = $self->{swish_filter_obj}->convert(
228 0 document => \$content,
229 content_type => $type,
230 name => $url
231 );
232  
233 0 ▶ if ( !$f
Branch: 1 branch
BranchTrueFalseTotal
if (not $f && $f->was_filtered or $f->is_binary) 0 0 0
Condition0%0/6
not $f && $f->was_filtered or $f->is_binary
l!l&&r!l&&!r
000
$f && $f->was_filtered
!ll&&!rl&&r
000
MC/DC0%0/3
not $f && $f->was_filtered or $f->is_binary
$f $f->was_filtered $f->is_binary
Truth table0%0/4
not(A && B) or C
A= $fB= $f->was_filteredC= $f->is_binary
ABCresult
0XX1
10X1
1111
1100
234 || !$f->was_filtered
235 || $f->is_binary ) # is is_binary necessary?
236 {
237 0 warn "skipping $url - filtering error\n";
238 0 return;
239 }
240  
241 0 ▶ if ( $self->debug > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug > 1) 0 0 0
242 0 warn "$url [$type] was filtered\n";
243 0 ▶ if ( $doc->content ne ${ $f->fetch_doc } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->content ne ${$f->fetch_doc;}) 0 0 0
244 0 warn sprintf "content changed:'%s'\n", ${ $f->fetch_doc };
245 }
246 }
247  
248 0 $doc->content( ${ $f->fetch_doc } );
249  
250 # leave type and parser as-is
251 # since we want to store original mime in indexer.
252 # TODO test this.
253 # what about parser?
254 # since type will have changed ( $f->content_type ) from original
255 # the parser type might also have changed?
256  
257 0 ▶ $doc->parser( $f->swish_parser_type ) if $self->set_parser_from_type;
Branch: 1 branch
BranchTrueFalseTotal
if $self->set_parser_from_type 0 0 0
258  
259 }
260 else {
261  
262 0 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
263 0 warn sprintf(
264 "No filter applied to %s - cannot filter %s (parser %s)\n",
265 $doc->url, $doc->type, $doc->parser, );
266 warn sprintf( " available filter: %s\n", $_ )
267 0 for $self->{swish_filter_obj}->filter_list;
268 }
269  
270 }
271  
272 }
273  
274 =head2 set_filter( I<code_ref> )
275  
276 Use I<code_ref> as the C<doc_class> filter. This method called by init() if
277 C<filter> param set in constructor.
278  
279 =cut
280  
281 1 ▶sub set_filter {
Subroutine set_filter: called CC=2 SCAR=7.0
Pod documented
282 1 my $self = shift;
283 1 my $filter = shift;
284 1 ▶ unless ( ref($filter) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $filter eq "CODE") 0 1 1
285 0 croak "filter must be a CODE ref";
286 }
287  
288 # cheat a little by using this code instead of the default
289 # method in doc_class
290 {
291 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
292 9 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
293 1 *{ $self->{doc_class} . '::filter' } = $filter;
294 }
295  
296 }
297  
298 =head2 set_ok_if_newer_than( I<timestamp> )
299  
300 Set the ok_if_newer_than attribute. I<timestamp> should be a Unix
301 epoch value.
302  
303 =cut
304  
305 0 ▶sub set_ok_if_newer_than {
Subroutine set_ok_if_newer_than: not called CC=3 SCAR=24.8
Pod documented
306 0 my $self = shift;
307 0 ▶ my $ts = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
308 0 ▶ if ( $ts =~ m/\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts =~ /\D/) 0 0 0
309 0 croak "timestamp should be an integer";
310 }
311 0 $self->ok_if_newer_than($ts);
312 }
313  
314 #
315 # private methods
316 #
317  
318 5 ▶sub _increment_count {
Subroutine _increment_count: called CC=4 SCAR=17.9
319 5 my $self = shift;
320 5 ▶ my $count = shift || 1;
Condition50%1/2
shift() || 1
l!l
05
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
321 5 $self->{count} += $count;
322 5 ▶ if ( $self->{progress} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"progress"}) 0 5 5
323 0 $self->{__progress_so_far} += $count;
324 0 ▶ if ( $self->{__progress_so_far} >= $self->{__progress_next} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__progress_so_far"} >= $self->{"__progress_next"}) 0 0 0
325 $self->{__progress_next}
326 0 = $self->{progress}->update( $self->{__progress_so_far} );
327 }
328 }
329 5 return $self;
330 }
331  
332 15 ▶sub _apply_file_rules {
Subroutine _apply_file_rules: called CC=8 SCAR=27.2
333 15 my ( $self, $file, $file_rules ) = @_;
334 15 ▶ if ( !$file_rules
Branch: 1 branch
BranchTrueFalseTotal
if (not $file_rules || exists $self->{"_file_rules"} and $self->config->FileRules) 0 15 15
Condition33%2/6
not $file_rules || exists $self->{"_file_rules"} and $self->config->FileRules
!ll&&!rl&&r
1500
$file_rules || exists $self->{'_file_rules'}
l!l&&r!l&&!r
1500
MC/DC0%0/3
not $file_rules || exists $self->{"_file_rules"} and $self->config->FileRules
$file_rules exists $self->{'_file_rules'} $self->config->FileRules
Truth table25%1/4
not(A || B) and C
A= $file_rulesB= exists $self->{'_file_rules'}C= $self->config->FileRules
ABCresult
1XX0
01X0
0000
0011
335 && !exists $self->{_file_rules}
336 && $self->config->FileRules )
337 {
338  
339 # cache obj
340 0 $self->{_file_rules} = File::Rules->new( $self->config->FileRules );
341 }
342 15 ▶ if ( $file_rules or exists $self->{_file_rules} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_rules or exists $self->{"_file_rules"}) 15 0 15
Condition33%1/3
$file_rules or exists $self->{"_file_rules"}
l!l&&r!l&&!r
1500
MC/DC0%0/2
$file_rules or exists $self->{"_file_rules"}
$file_rules exists $self->{"_file_rules"}
Truth table33%1/3
A or B
A= $file_rulesB= exists $self->{"_file_rules"}
ABresult
1X1
011
000
343 15 ▶ $self->debug and warn "$file [applying FileRules]\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "$file [applying FileRules]\n" 0 15 15
344 15 ▶ my $rules = $file_rules || $self->{_file_rules};
Condition33%1/3
$file_rules || $self->{'_file_rules'}
l!l&&r!l&&!r
1500
MC/DC0%0/2
$file_rules || $self->{'_file_rules'}
$file_rules $self->{'_file_rules'}
Truth table33%1/3
A || B
A= $file_rulesB= $self->{'_file_rules'}
ABresult
1X1
011
000
345 15 my $match = $rules->match($file);
346 15 return $match;
347 }
348 0 return 0; # no rules
349 }
350  
351 2 ▶sub _apply_file_match {
Subroutine _apply_file_match: called CC=1 SCAR=0.0
352 2 my ( $self, $file ) = @_;
353  
354 # TODO
355 2 return 0; # no-op for now
356 }
357  
358 9 1;
359  
360 __END__
« Prog/Aggregator/Spider.pm ↑ Summary Prog/Headers.pm »