| 1 | package SWISH::Prog::Aggregator::FS; | ||
| 2 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use base qw( SWISH::Prog::Aggregator ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 4 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 4 | ▶ | use File::Find; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 4 | ▶ | use File::Rules; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 4 | ▶ | use Data::Dump qw( dump ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use SWISH::3; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | |||
| 12 | our $VERSION = '0.75'; | ||
| 13 | |||
| 14 | # we rely on file extensions to determine content type | ||
| 15 | # and thus parser type. If a file has no extension, | ||
| 16 | # assume this one. | ||
| 17 | our $DEFAULT_EXTENSION = 'txt'; | ||
| 18 | |||
| 19 | =pod | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | SWISH::Prog::Aggregator::FS - crawl a filesystem | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | use SWISH::Prog::Aggregator::FS; | ||
| 28 | my $fs = SWISH::Prog::Aggregator::FS->new( | ||
| 29 | indexer => SWISH::Prog::Indexer->new | ||
| 30 | ); | ||
| 31 | |||
| 32 | $fs->indexer->start; | ||
| 33 | $fs->crawl( $path ); | ||
| 34 | $fs->indexer->finish; | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | SWISH::Prog::Aggregator::FS is a filesystem aggregator implementation | ||
| 39 | of the SWISH::Prog::Aggregator API. It is similar to the DirTree.pl | ||
| 40 | script in the Swish-e 2.4 distribution. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | See SWISH::Prog::Aggregator. | ||
| 47 | |||
| 48 | =head2 init | ||
| 49 | |||
| 50 | Implements the base init() method called by new(). | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub init { | ||
| 55 | my $self = shift; | ||
| 56 | $self->SUPER::init(@_); | ||
| 57 | |||
| 58 | # create .ext regex to match in file_ok() | ||
| 59 | if ( $self->config->IndexOnly ) { | ||
| 60 | my $re = join( '|', | ||
| 61 | grep {s/^\.//} split( m/\s+/, $self->config->IndexOnly ) ); | ||
| 62 | $self->{_ext_re} = qr{\.($re)}io; | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | $self->{_ext_re} = $SWISH::Prog::Utils::ExtRE; | ||
| 66 | } | ||
| 67 | |||
| 68 | } | ||
| 69 | |||
| 70 | =head2 file_ok( I<full_path> ) | ||
| 71 | |||
| 72 | Check I<full_path> before fetch()ing it. | ||
| 73 | |||
| 74 | Returns 0 if I<full_path> should be skipped. | ||
| 75 | |||
| 76 | Returns file extension of I<full_path> if I<full_path> should be processed. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub file_ok { | ||
| 81 | my $self = shift; | ||
| 82 | my $full_path = shift; | ||
| 83 | my $stat = shift; | ||
| 84 | |||
| 85 | $self->debug and warn "checking file $full_path\n"; | ||
| 86 | |||
| 87 | my ( $path, $file, $ext ) | ||
| 88 | = SWISH::Prog::Utils->path_parts( $full_path, $self->{_ext_re} ); | ||
| 89 | |||
| 90 | $self->debug and warn "path=$path file=$file ext=$ext\n"; | ||
| 91 | |||
| 92 | # treat no extension like plain text | ||
| 93 | $ext = $DEFAULT_EXTENSION unless length $ext; | ||
| 94 | |||
| 95 | return 0 if $file =~ m/^\./; | ||
| 96 | |||
| 97 | #carp "parsed file: $file\npath: $path\next: $ext"; | ||
| 98 | |||
| 99 | $stat ||= [ stat($full_path) ]; | ||
| 100 | return 0 unless -r _; | ||
| 101 | return 0 if -d _; | ||
| 102 | if ( $self->ok_if_newer_than | ||
| 103 | and $self->ok_if_newer_than >= $stat->[9] ) | ||
| 104 | { | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | return 0 | ||
| 108 | if ( $self->_apply_file_rules($full_path) | ||
| 109 | && !$self->_apply_file_match($full_path) ); | ||
| 110 | |||
| 111 | $self->debug and warn " $full_path -> ok\n"; | ||
| 112 | if ( $self->verbose & 4 ) { | ||
| 113 | local $| = 1; # don't buffer | ||
| 114 | print "crawling file $full_path\n"; | ||
| 115 | } | ||
| 116 | |||
| 117 | return $ext; | ||
| 118 | } | ||
| 119 | |||
| 120 | =head2 dir_ok( I<directory> ) | ||
| 121 | |||
| 122 | Called by find() for all directories. You can control | ||
| 123 | the recursion into I<directory> via the config() params | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub dir_ok { | ||
| 128 | my $self = shift; | ||
| 129 | my $dir = shift; | ||
| 130 | my $stat = shift || [ stat($dir) ]; | ||
| 131 | |||
| 132 | $self->debug and warn "checking dir $dir\n"; | ||
| 133 | |||
| 134 | return 0 unless -d _; | ||
| 135 | return 0 if $dir =~ m!/\.!; | ||
| 136 | return 0 if $dir =~ m/^\.[^\.]/; # could be ../foo | ||
| 137 | return 0 if $dir =~ m!/(\.svn|RCS)/!; | ||
| 138 | return 0 | ||
| 139 | if ( $self->_apply_file_rules($dir) | ||
| 140 | && !$self->_apply_file_match($dir) ); | ||
| 141 | |||
| 142 | $self->debug and warn " $dir -> ok\n"; | ||
| 143 | if ( $self->verbose & 2 ) { | ||
| 144 | local $| = 1; # don't buffer | ||
| 145 | print "crawling dir $dir\n"; | ||
| 146 | } | ||
| 147 | |||
| 148 | 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | =head2 get_doc( I<file_path> [, I<stat>, I<ext> ] ) | ||
| 152 | |||
| 153 | Returns a doc_class() instance representing I<file_path>. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub get_doc { | ||
| 158 | my $self = shift; | ||
| 159 | my $url = shift or croak "file path required"; | ||
| 160 | my ( $stat, $ext ) = @_; | ||
| 161 | my $buf; | ||
| 162 | |||
| 163 | # NOTE we always read in binary (raw) mode in case | ||
| 164 | # the file is compressed, binary, etc. | ||
| 165 | eval { | ||
| 166 | |||
| 167 | # the 2nd param runs in raw mode (no NULL substitution) | ||
| 168 | $buf = SWISH::3->slurp( $url, 1 ); | ||
| 169 | $url =~ s/\.gz$//; # post-slurp, in case it failed. | ||
| 170 | }; | ||
| 171 | |||
| 172 | if ($@) { | ||
| 173 | carp "unable to read $url - skipping"; | ||
| 174 | return; | ||
| 175 | } | ||
| 176 | |||
| 177 | $stat ||= [ stat($url) ]; | ||
| 178 | |||
| 179 | # TODO SWISH::3 has this function too. | ||
| 180 | # might be faster since no OO overhead. | ||
| 181 | my $type = SWISH::Prog::Utils->mime_type( $url, $ext ); | ||
| 182 | |||
| 183 | if ( $self->ok_if_newer_than | ||
| 184 | and $self->ok_if_newer_than >= $stat->[9] ) | ||
| 185 | { | ||
| 186 | warn "skipping $url ... too old\n"; | ||
| 187 | return; | ||
| 188 | } | ||
| 189 | |||
| 190 | return $self->doc_class->new( | ||
| 191 | url => $url, | ||
| 192 | modtime => $stat->[9], | ||
| 193 | content => $buf, | ||
| 194 | type => $type, | ||
| 195 | size => $stat->[7], | ||
| 196 | debug => $self->debug | ||
| 197 | ); | ||
| 198 | |||
| 199 | } | ||
| 200 | |||
| 201 | sub _do_file { | ||
| 202 | my $self = shift; | ||
| 203 | my $file = shift; | ||
| 204 | if ( my $ext = $self->file_ok($file) ) { | ||
| 205 | my $doc = $self->get_doc( $file, [ stat(_) ], $ext ); | ||
| 206 | $self->swish_filter($doc); | ||
| 207 | if ( $self->test_mode ) { | ||
| 208 | warn join( ' ', $doc->url, $doc->type ) . "\n"; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | $self->{indexer}->process($doc); | ||
| 212 | } | ||
| 213 | $self->_increment_count; | ||
| 214 | } | ||
| 215 | else { | ||
| 216 | $self->debug and warn "skipping file $file\n"; | ||
| 217 | if ( $self->verbose & 4 ) { | ||
| 218 | local $| = 1; | ||
| 219 | print "skipping $file\n"; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | # | ||
| 225 | # the basic wanted() code here based on Bill Moseley's DirTree.pl, | ||
| 226 | # part of the Swish-e 2.4 distrib. | ||
| 227 | |||
| 228 | =head2 crawl( I<paths_or_files> ) | ||
| 229 | |||
| 230 | Crawl the filesystem recursively within I<paths_or_files>, processing | ||
| 231 | each document specified by the config(). | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | sub crawl { | ||
| 236 | my $self = shift; | ||
| 237 | |||
| 238 | my @paths = @_; | ||
| 239 | |||
| 240 | my @files = grep { !-d } @paths; | ||
| 241 | my @dirs = grep {-d} @paths; | ||
| 242 | |||
| 243 | for my $f (@files) { | ||
| 244 | $self->_do_file($f); | ||
| 245 | } | ||
| 246 | |||
| 247 | # TODO set some flags here for filtering out files/dirs | ||
| 248 | # based on $self->indexer->config. | ||
| 249 | |||
| 250 | if (@dirs) { | ||
| 251 | |||
| 252 | find( | ||
| 253 | { wanted => sub { | ||
| 254 | |||
| 255 | # canonpath cleans up any leading . | ||
| 256 | my $path = File::Spec->canonpath($File::Find::name); | ||
| 257 | |||
| 258 | if (-d) { | ||
| 259 | unless ( $self->dir_ok( $path, [ stat(_) ] ) ) { | ||
| 260 | if ( $self->verbose & 2 ) { | ||
| 261 | local $| = 1; | ||
| 262 | print "skipping $path\n"; | ||
| 263 | } | ||
| 264 | $File::Find::prune = 1; | ||
| 265 | return; | ||
| 266 | } | ||
| 267 | |||
| 268 | #warn "-d $path\n"; | ||
| 269 | return; | ||
| 270 | } | ||
| 271 | else { | ||
| 272 | |||
| 273 | #warn "!-d $path\n"; | ||
| 274 | } | ||
| 275 | |||
| 276 | $self->_do_file($path); | ||
| 277 | |||
| 278 | }, | ||
| 279 | no_chdir => 1, | ||
| 280 | follow => $self->config->FollowSymLinks, | ||
| 281 | |||
| 282 | }, | ||
| 283 | @dirs | ||
| 284 | ); | ||
| 285 | } | ||
| 286 | |||
| 287 | return $self->{count}; | ||
| 288 | } | ||
| 289 | |||
| 290 | 1; | ||
| 291 | |||
| 292 | __END__ | ||