| 1 | package WWW::RobotRules::AnyDBM_File; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | require WWW::RobotRules; | |||||||||||||||||||||||||||
| 4 | 1 | @ISA = qw(WWW::RobotRules); | |||||||||||||||||||||||||||
| 5 | 1 | $VERSION = "6.00"; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use AnyDBM_File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | WWW::RobotRules::AnyDBM_File - Persistent RobotRules | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | require WWW::RobotRules::AnyDBM_File; | ||||||||||||||||||||||||||||
| 19 | require LWP::RobotUA; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Create a robot useragent that uses a diskcaching RobotRules | ||||||||||||||||||||||||||||
| 22 | my $rules = WWW::RobotRules::AnyDBM_File->new( 'my-robot/1.0', 'cachefile' ); | ||||||||||||||||||||||||||||
| 23 | my $ua = WWW::RobotUA->new( 'my-robot/1.0', 'me@foo.com', $rules ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Then just use $ua as usual | ||||||||||||||||||||||||||||
| 26 | $res = $ua->request($req); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | This is a subclass of I<WWW::RobotRules> that uses the AnyDBM_File | ||||||||||||||||||||||||||||
| 31 | package to implement persistent diskcaching of F<robots.txt> and host | ||||||||||||||||||||||||||||
| 32 | visit information. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | The constructor (the new() method) takes an extra argument specifying | ||||||||||||||||||||||||||||
| 35 | the name of the DBM file to use. If the DBM file already exists, then | ||||||||||||||||||||||||||||
| 36 | you can specify undef as agent name as the name can be obtained from | ||||||||||||||||||||||||||||
| 37 | the DBM database. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub new | ||||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||
| 43 | 4 | ▶ | my ($class, $ua, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | Carp::croak('WWW::RobotRules::AnyDBM_File filename required') unless $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 4 | my $self = bless { }, $class; | |||||||||||||||||||||||||||
| 47 | 4 | $self->{'filename'} = $file; | |||||||||||||||||||||||||||
| 48 | 4 | ▶ | tie %{$self->{'dbm'}}, 'AnyDBM_File', $file, O_CREAT|O_RDWR, 0640 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | or Carp::croak("Can't open $file: $!"); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | ▶ | if ($ua) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | $self->agent($ua); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||
| 55 | # Try to obtain name from DBM file | ||||||||||||||||||||||||||||
| 56 | 2 | $ua = $self->{'dbm'}{"|ua-name|"}; | |||||||||||||||||||||||||||
| 57 | 2 | ▶ | Carp::croak("No agent name specified") unless $ua; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | $self; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub agent { | ||||||||||||||||||||||||||||
| 64 | 5 | ▶ | my($self, $newname) = @_; | ||||||||||||||||||||||||||
|
Subroutine
agent: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 5 | my $old = $self->{'dbm'}{"|ua-name|"}; | |||||||||||||||||||||||||||
| 66 | 5 | ▶ | if (defined $newname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 2 | $newname =~ s!/?\s*\d+.\d+\s*$!!; # loose version | |||||||||||||||||||||||||||
| 68 | 2 | ▶ | unless ($old && $old eq $newname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$old and $old eq $newname
MC/DC0%0/2
$old and $old eq $newname
$old
$old eq $newname
Truth table66%2/3
A and B
A= $oldB= $old eq $newname
| |||||||||||||||||||||||||||||
| 69 | # Old info is now stale. | ||||||||||||||||||||||||||||
| 70 | 2 | my $file = $self->{'filename'}; | |||||||||||||||||||||||||||
| 71 | 2 | untie %{$self->{'dbm'}}; | |||||||||||||||||||||||||||
| 72 | 2 | tie %{$self->{'dbm'}}, 'AnyDBM_File', $file, O_TRUNC|O_RDWR, 0640; | |||||||||||||||||||||||||||
| 73 | 2 | %{$self->{'dbm'}} = (); | |||||||||||||||||||||||||||
| 74 | 2 | $self->{'dbm'}{"|ua-name|"} = $newname; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 5 | $old; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub no_visits { | ||||||||||||||||||||||||||||
| 81 | 5 | ▶ | my ($self, $netloc) = @_; | ||||||||||||||||||||||||||
|
Subroutine
no_visits: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 5 | my $t = $self->{'dbm'}{"$netloc|vis"}; | |||||||||||||||||||||||||||
| 83 | 5 | ▶ | return 0 unless $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 4 | (split(/;\s*/, $t))[0]; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub last_visit { | ||||||||||||||||||||||||||||
| 88 | 2 | ▶ | my ($self, $netloc) = @_; | ||||||||||||||||||||||||||
|
Subroutine
last_visit: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 2 | my $t = $self->{'dbm'}{"$netloc|vis"}; | |||||||||||||||||||||||||||
| 90 | 2 | ▶ | return undef unless $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | (split(/;\s*/, $t))[1]; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub fresh_until { | ||||||||||||||||||||||||||||
| 95 | 6 | ▶ | my ($self, $netloc, $fresh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fresh_until: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 6 | my $old = $self->{'dbm'}{"$netloc|exp"}; | |||||||||||||||||||||||||||
| 97 | 6 | ▶ | if ($old) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 3 | $old =~ s/;.*//; # remove cleartext | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 6 | ▶ | if (defined $fresh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 2 | $fresh .= "; " . localtime($fresh); | |||||||||||||||||||||||||||
| 102 | 2 | $self->{'dbm'}{"$netloc|exp"} = $fresh; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 6 | $old; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub visit { | ||||||||||||||||||||||||||||
| 108 | 4 | ▶ | my($self, $netloc, $time) = @_; | ||||||||||||||||||||||||||
|
Subroutine
visit: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 4 | ▶ | $time ||= time; | ||||||||||||||||||||||||||
|
Condition66%2/3
$time ||= time
MC/DC0%0/2
$time ||= time
$time
time
Truth table66%2/3
A ||= B
A= $timeB= time
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 4 | my $count = 0; | |||||||||||||||||||||||||||
| 112 | 4 | my $old = $self->{'dbm'}{"$netloc|vis"}; | |||||||||||||||||||||||||||
| 113 | 4 | ▶ | if ($old) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 2 | my $last; | |||||||||||||||||||||||||||
| 115 | 2 | ($count,$last) = split(/;\s*/, $old); | |||||||||||||||||||||||||||
| 116 | 2 | ▶ | $time = $last if $last > $time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 4 | $count++; | |||||||||||||||||||||||||||
| 119 | 4 | $self->{'dbm'}{"$netloc|vis"} = "$count; $time; " . localtime($time); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub push_rules { | ||||||||||||||||||||||||||||
| 123 | 4 | ▶ | my($self, $netloc, @rules) = @_; | ||||||||||||||||||||||||||
|
Subroutine
push_rules: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 4 | my $cnt = 1; | |||||||||||||||||||||||||||
| 125 | 4 | ▶ | $cnt++ while $self->{'dbm'}{"$netloc|r$cnt"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 4 | foreach (@rules) { | |||||||||||||||||||||||||||
| 128 | 6 | $self->{'dbm'}{"$netloc|r$cnt"} = $_; | |||||||||||||||||||||||||||
| 129 | 6 | $cnt++; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub clear_rules { | ||||||||||||||||||||||||||||
| 134 | 4 | ▶ | my($self, $netloc) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear_rules: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 4 | my $cnt = 1; | |||||||||||||||||||||||||||
| 136 | 4 | ▶ | while ($self->{'dbm'}{"$netloc|r$cnt"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 4 | delete $self->{'dbm'}{"$netloc|r$cnt"}; | |||||||||||||||||||||||||||
| 138 | 4 | $cnt++; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | sub rules { | ||||||||||||||||||||||||||||
| 143 | 4 | ▶ | my($self, $netloc) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rules: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 144 | 4 | my @rules = (); | |||||||||||||||||||||||||||
| 145 | 4 | my $cnt = 1; | |||||||||||||||||||||||||||
| 146 | 4 | while (1) { | |||||||||||||||||||||||||||
| 147 | 12 | my $rule = $self->{'dbm'}{"$netloc|r$cnt"}; | |||||||||||||||||||||||||||
| 148 | 12 | ▶ | last unless $rule; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 8 | push(@rules, $rule); | |||||||||||||||||||||||||||
| 150 | 8 | $cnt++; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 4 | @rules; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub dump | ||||||||||||||||||||||||||||
| 156 | ▶ | { | |||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | 1; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | L<WWW::RobotRules>, L<LWP::RobotUA> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Hakan Ardo E<lt>hakan@munin.ub2.lu.se>, Gisle Aas E<lt>aas@sn.no> | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||