| 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 <ot@w3.org> for W3C | ||
| 6 | # | ||
| 7 | # $Id: LinkReferer.pm,v 1.2 2006/08/08 05:39:34 ot Exp $ | ||
| 8 | |||
| 9 | package W3C::LogValidator::LinkReferer; | ||
| 10 | use strict; | ||
| 11 | no strict "refs"; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | |||
| 15 | require Exporter; | ||
| 16 | our @ISA = qw(Exporter); | ||
| 17 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | ||
| 18 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 19 | our @EXPORT = qw(); | ||
| 20 | our $VERSION = sprintf "%d.%03d",q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/; | ||
| 21 | |||
| 22 | |||
| 23 | ########################### | ||
| 24 | # usual package interface # | ||
| 25 | ########################### | ||
| 26 | our $verbose = 1; | ||
| 27 | our %config; | ||
| 28 | |||
| 29 | sub new | ||
| 30 | { | ||
| 31 | my $self = {}; | ||
| 32 | my $proto = shift; | ||
| 33 | my $class = ref($proto) || $proto; | ||
| 34 | # mandatory vars for the API | ||
| 35 | @{$self->{URIs}} = undef; | ||
| 36 | # don't change this | ||
| 37 | if (@_) {%config = %{(shift)};} | ||
| 38 | if (exists $config{verbose}) {$verbose = $config{verbose}} | ||
| 39 | bless($self, $class); | ||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub uris { | ||
| 44 | my $self = shift; | ||
| 45 | if (@_) { @{$self->{URIs}} = @_ } | ||
| 46 | return @{$self->{URIs}}; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub trim_uris | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | my @trimmed_uris; | ||
| 54 | my $exclude_regexp = ""; | ||
| 55 | my @exclude_areas; | ||
| 56 | $exclude_regexp = $config{ExcludeAreas}; | ||
| 57 | if ($exclude_regexp){ | ||
| 58 | $exclude_regexp =~ s/\//\\\//g ; | ||
| 59 | @exclude_areas = split(" ", $exclude_regexp); | ||
| 60 | } | ||
| 61 | else { print "nothing to exclude\n" if ($verbose >2);} | ||
| 62 | my $uri; | ||
| 63 | while ($uri = shift) | ||
| 64 | { | ||
| 65 | my $acceptable = 1; | ||
| 66 | foreach my $area (@exclude_areas) | ||
| 67 | { | ||
| 68 | if ($uri =~ /$area/) | ||
| 69 | { | ||
| 70 | my $slasharea = $area; | ||
| 71 | $slasharea =~ s/\\\//\//g; | ||
| 72 | $slasharea =~ s/\\././g; | ||
| 73 | print "Ignoring $uri matching $slasharea \n" if ($verbose > 2) ; | ||
| 74 | $acceptable = 0; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | push @trimmed_uris,$uri if ($acceptable); | ||
| 78 | } | ||
| 79 | return @trimmed_uris; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | ######################################### | ||
| 84 | # Actual subroutine to check the list of uris # | ||
| 85 | ######################################### | ||
| 86 | |||
| 87 | |||
| 88 | sub process_list | ||
| 89 | { | ||
| 90 | my $self = shift; | ||
| 91 | my $max_invalid = undef; | ||
| 92 | my $max_documents = undef; | ||
| 93 | if (exists $config{MaxDocuments}) {$max_documents = $config{MaxDocuments}} | ||
| 94 | else {$max_documents = 0} | ||
| 95 | |||
| 96 | # This basic module does not actually "validates" | ||
| 97 | # so MaxInvalid is not relevant... Keeping it anyway | ||
| 98 | if (exists $config{MaxInvalid}) {$max_invalid = $config{MaxInvalid}} | ||
| 99 | else {$max_invalid = 0} | ||
| 100 | my $name = ""; | ||
| 101 | if (exists $config{ServerName}) {$name = $config{ServerName}} | ||
| 102 | |||
| 103 | print "Now Using the Link Referer module... \n" if $verbose; | ||
| 104 | my %hits; | ||
| 105 | my %HTTPcodes; | ||
| 106 | my %referers; | ||
| 107 | my @uris = undef; | ||
| 108 | use DB_File; | ||
| 109 | if (defined ($config{tmpfile})) | ||
| 110 | { | ||
| 111 | my $tmp_file = $config{tmpfile}; | ||
| 112 | tie (%hits, 'DB_File', "$tmp_file", O_RDONLY) || | ||
| 113 | die ("Cannot create or open $tmp_file"); | ||
| 114 | @uris = sort { $hits{$b} <=> $hits{$a} } keys %hits; | ||
| 115 | } | ||
| 116 | |||
| 117 | elsif ($self->uris()) | ||
| 118 | { | ||
| 119 | @uris = $self->uris(); | ||
| 120 | foreach my $uri (@uris) { $hits{$uri} = 0 } | ||
| 121 | } | ||
| 122 | |||
| 123 | @uris = $self->trim_uris(@uris); | ||
| 124 | |||
| 125 | |||
| 126 | if (defined ($config{tmpfile_HTTP_codes})) | ||
| 127 | { | ||
| 128 | my $tmp_file_HTTP_codes = $config{tmpfile_HTTP_codes}; | ||
| 129 | tie (%HTTPcodes, 'DB_File', "$tmp_file_HTTP_codes", O_RDONLY) || | ||
| 130 | die ("Cannot create or open $tmp_file_HTTP_codes"); | ||
| 131 | } | ||
| 132 | |||
| 133 | if (defined ($config{tmpfile_referers})) | ||
| 134 | { | ||
| 135 | my $tmp_file_referers = $config{tmpfile_referers}; | ||
| 136 | tie (%referers, 'DB_File', "$tmp_file_referers", O_RDONLY) || | ||
| 137 | die ("Cannot create or open $tmp_file_referers"); | ||
| 138 | print "size of hash: " . keys( %referers ) . ".\n"; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | my $intro="Here are the <census> most popular problematic documents (404 not found etc),"; | ||
| 143 | $intro .="along with their top referer, that I could find for $name."; | ||
| 144 | if (exists $config{LogProcessor}{RefererMatch}) { | ||
| 145 | if ($config{LogProcessor}{RefererMatch} != ".*") { | ||
| 146 | my $intro .="\n\nOnly referers matching ".$config{LogProcessor}{RefererMatch}." were considered."; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | my @result; | ||
| 150 | my @result_head; | ||
| 151 | push @result_head, "Rank"; | ||
| 152 | push @result_head, "Hits"; | ||
| 153 | push @result_head, "Address"; | ||
| 154 | push @result_head, "Status Code"; | ||
| 155 | push @result_head, "Top Referer"; | ||
| 156 | my $census = 0; | ||
| 157 | while ( (@uris) and (($census < $max_documents) or (!$max_documents)) ) | ||
| 158 | { | ||
| 159 | my $uri = shift (@uris); | ||
| 160 | chomp ($uri); | ||
| 161 | my @result_tmp; | ||
| 162 | if (defined $HTTPcodes{$uri}) | ||
| 163 | { if ( $HTTPcodes{$uri} =~ /(404|5..)/) | ||
| 164 | { # This module should ignore requests that resulted in success codes | ||
| 165 | $census++; | ||
| 166 | push @result_tmp, "$census"; | ||
| 167 | push @result_tmp, "$hits{$uri}"; | ||
| 168 | push @result_tmp, "$uri"; | ||
| 169 | push @result_tmp, "$HTTPcodes{$uri}"; | ||
| 170 | my %this_uri_referers; | ||
| 171 | my $referer_string = ""; | ||
| 172 | foreach my $urireferer (keys %referers) | ||
| 173 | { | ||
| 174 | if ($urireferer =~ /$uri : (.*)/) { | ||
| 175 | $this_uri_referers{$1} = $referers{$urireferer}; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | my @sorted_refs = sort { $this_uri_referers{$a} cmp $this_uri_referers{$b} } keys %this_uri_referers; | ||
| 179 | if (defined $sorted_refs[0]) { | ||
| 180 | my $top_referer = pop @sorted_refs; | ||
| 181 | $referer_string .= $top_referer." (".$this_uri_referers{$top_referer}.")"; | ||
| 182 | } | ||
| 183 | push @result_tmp, $referer_string; | ||
| 184 | if ($referer_string ne "") { | ||
| 185 | push @result, [@result_tmp]; | ||
| 186 | } | ||
| 187 | else { | ||
| 188 | $census--; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | print "Done!\n" if $verbose; | ||
| 194 | if ($census eq 1) # let's repect grammar here | ||
| 195 | { | ||
| 196 | $intro=~ s/are/is/; | ||
| 197 | $intro=~ s/<census> //; | ||
| 198 | $intro=~ s/document\(s\)/document/; | ||
| 199 | } | ||
| 200 | else | ||
| 201 | { | ||
| 202 | $intro=~ s/<census>/$census/; | ||
| 203 | } | ||
| 204 | if (defined ($config{tmpfile})) { | ||
| 205 | untie %hits; | ||
| 206 | } | ||
| 207 | if (defined ($config{tmpfile_HTTP_codes})) { untie %HTTPcodes; } | ||
| 208 | if (defined ($config{tmpfile_referers})) { untie %referers; } | ||
| 209 | my $outro=""; | ||
| 210 | my %returnhash; | ||
| 211 | $returnhash{"name"}="Links referers"; | ||
| 212 | $returnhash{"intro"}=$intro; | ||
| 213 | $returnhash{"outro"}=$outro; | ||
| 214 | @{$returnhash{"thead"}}=@result_head; | ||
| 215 | @{$returnhash{"trows"}}=@result; | ||
| 216 | return %returnhash; | ||
| 217 | } | ||
| 218 | |||
| 219 | package W3C::LogValidator::LinkReferer; | ||
| 220 | |||
| 221 | 1; | ||
| 222 |