File Coverage

blib/lib/W3C/LogValidator/Basic.pm
Criterion Covered Total %
statement 7 48 14.5
branch 0 16 0.0
condition 0 3 0.0
subroutine 3 6 50.0
pod n/a
total 10 73 13.7


line stmt bran cond sub pod time code
1             # Copyright (c) 2002-2005 the World Wide Web Consortium :
2             # Keio University,
3             # European Research Consortium for Informatics and Mathematics
4             # Massachusetts Institute of Technology.
5             # written by Olivier Thereaux for W3C
6             #
7             # $Id: Basic.pm,v 1.18 2008/11/18 16:48:56 ot Exp $
8              
9             package W3C::LogValidator::Basic;
10 1     1   648 use strict;
  1         3  
  1         36  
11 1     1   7 use warnings;
  1         3  
  1         844  
12              
13              
14             require Exporter;
15             our @ISA = qw(Exporter);
16             our %EXPORT_TAGS = ( 'all' => [ qw() ] );
17             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18             our @EXPORT = qw();
19             our $VERSION = sprintf "%d.%03d",q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/;
20              
21              
22             ###########################
23             # usual package interface #
24             ###########################
25             our $verbose = 1;
26             our %config;
27              
28             sub new
29             {
30 0     0     my $self = {};
31 0           my $proto = shift;
32 0   0       my $class = ref($proto) || $proto;
33             # mandatory vars for the API
34 0           @{$self->{URIs}} = undef;
  0            
35             # don't change this
36 0 0         if (@_) {%config = %{(shift)};}
  0            
  0            
37 0 0         if (exists $config{verbose}) {$verbose = $config{verbose}}
  0            
38 0           bless($self, $class);
39 0           return $self;
40             }
41              
42             sub uris {
43 0     0     my $self = shift;
44 0 0         if (@_) { @{$self->{URIs}} = @_ }
  0            
  0            
45 0           return @{$self->{URIs}};
  0            
46             }
47              
48              
49             sub trim_uris
50             {
51 0     0     my $self = shift;
52 0           my @trimmed_uris;
53 0           my $exclude_regexp = "";
54 0           my @exclude_areas;
55 0           $exclude_regexp = $config{ExcludeAreas};
56 0 0         if ($exclude_regexp){
57 0           $exclude_regexp =~ s/\//\\\//g ;
58 0           @exclude_areas = split(" ", $exclude_regexp);
59             }
60 0 0         else { print "nothing to exclude\n" if ($verbose >2);}
61 0           my $uri;
62 0           while ($uri = shift)
63             {
64 0           my $acceptable = 1;
65 0           foreach my $area (@exclude_areas)
66             {
67 0 0         if ($uri =~ /$area/)
68             {
69 0           my $slasharea = $area;
70 0           $slasharea =~ s/\\\//\//g;
71 0           $slasharea =~ s/\\././g;
72 0 0         print "Ignoring $uri matching $slasharea \n" if ($verbose > 2) ;
73 0           $acceptable = 0;
74             }
75             }
76 0 0         push @trimmed_uris,$uri if ($acceptable);
77             }
78 0           return @trimmed_uris;
79             }
80              
81              
82             #########################################
83             # Actual subroutine to check the list of uris #
84             #########################################
85              
86              
87             sub process_list
88             {
89             my $self = shift;
90             my $max_invalid = undef;
91             my $max_documents = undef;
92             if (exists $config{MaxDocuments}) {$max_documents = $config{MaxDocuments}}
93             else {$max_documents = 0}
94              
95             # This basic module does not actually "validates"
96             # so MaxInvalid is not relevant... Keeping it anyway
97             if (exists $config{MaxInvalid}) {$max_invalid = $config{MaxInvalid}}
98             else {$max_invalid = 0}
99             my $name = "";
100             if (exists $config{ServerName}) {$name = $config{ServerName}}
101              
102             print "Now Using the Basic module... \n" if $verbose;
103             my %hits;
104             my %HTTPcodes;
105             my @uris = undef;
106 1     1   891 use DB_File;
  0            
  0            
107             if (defined ($config{tmpfile}))
108             {
109             my $tmp_file = $config{tmpfile};
110             tie (%hits, 'DB_File', "$tmp_file", O_RDONLY) ||
111             die ("Cannot create or open $tmp_file");
112             @uris = sort { $hits{$b} <=> $hits{$a} } keys %hits;
113             }
114              
115             elsif ($self->uris())
116             {
117             @uris = $self->uris();
118             foreach my $uri (@uris) { $hits{$uri} = 0 }
119             }
120              
121             @uris = $self->trim_uris(@uris);
122              
123              
124             if (defined ($config{tmpfile_HTTP_codes}))
125             {
126             my $tmp_file_HTTP_codes = $config{tmpfile_HTTP_codes};
127             tie (%HTTPcodes, 'DB_File', "$tmp_file_HTTP_codes", O_RDONLY) ||
128             die ("Cannot create or open $tmp_file_HTTP_codes");
129             }
130              
131              
132             my $intro="Here are the most popular documents overall for $name.";
133             my @result;
134             my @result_head;
135             push @result_head, "Rank";
136             push @result_head, "Hits";
137             push @result_head, "Address";
138             my $census = 0;
139             while ( (@uris) and (($census < $max_documents) or (!$max_documents)) )
140             {
141             my $uri = shift (@uris);
142             chomp ($uri);
143             my @result_tmp;
144             if (!defined $HTTPcodes{$uri})
145             { # if no HTTP code present, assume it's a 200
146             $census++;
147             push @result_tmp, "$census";
148             push @result_tmp, "$hits{$uri}";
149             push @result_tmp, "$uri";
150             push @result, [@result_tmp];
151             }
152             elsif (($HTTPcodes{$uri} eq "200") or (!$HTTPcodes{$uri} =~ /\d+/))
153             # should perhaps make a subroutine for that instead of DUPing code
154             {
155             $census++;
156             push @result_tmp, "$census";
157             push @result_tmp, "$hits{$uri}";
158             push @result_tmp, "$uri";
159             push @result, [@result_tmp];
160             }
161             elsif ((defined $HTTPcodes{$uri}) and ($verbose > 2)) {
162             print "$uri returned code $HTTPcodes{$uri}, ignoring \n";
163             }
164             }
165             print "Done!\n" if $verbose;
166             if ($census eq 1) # let's repect grammar here
167             {
168             $intro=~ s/are/is/;
169             $intro=~ s/ //;
170             $intro=~ s/document\(s\)/document/;
171             }
172             else
173             {
174             $intro=~ s//$census/;
175             }
176             if (defined ($config{tmpfile})) {
177             untie %hits;
178             }
179             my $outro="";
180             my %returnhash;
181             $returnhash{"name"}="basic";
182             $returnhash{"intro"}=$intro;
183             $returnhash{"outro"}=$outro;
184             @{$returnhash{"thead"}}=@result_head;
185             @{$returnhash{"trows"}}=@result;
186             return %returnhash;
187             }
188              
189             package W3C::LogValidator::Basic;
190              
191             1;
192              
193             __END__