--- trunk/scripts/est-spider 2006/01/25 23:38:57 89 +++ trunk/scripts/est-spider 2006/04/17 10:31:11 119 @@ -7,6 +7,7 @@ use Text::Iconv; #use File::MMagic; use File::MMagic::XS qw/:compat/; +use Time::HiRes qw/time/; my $collection; # name which will be inserted my $path_add; # add additional info in path @@ -16,6 +17,7 @@ #$verbose = 1; my $debug = 0; my $force = 0; +my $all = 0; my $result = GetOptions( "collection=s" => \$collection, @@ -24,6 +26,7 @@ "debug!" => \$debug, "exclude=s" => \$exclude, "force!" => \$force, + "all!" => \$all, ); my ($node_url,$dir) = @ARGV; @@ -38,6 +41,7 @@ --verbose --force --debug + --all save placeholders for all files _END_OF_USAGE_ if (! -e $dir) { @@ -49,7 +53,17 @@ #$basedir =~ s,/[^/]+$,/,; #require "$basedir/filter.pm"; -my $pdftotext = which('pdftotext'); +my $docs = 0; +my $start_t = time(); + +my $filter; +foreach my $f (qw/pdftotext pstotext/) { + my $w = which($f); + if ($f) { + $filter->{$f} = $w; + print STDERR "using $f filter at $w\n" if ($verbose); + } +} #my $mm = new File::MMagic('/usr/share/misc/file/magic'); my $mm = new File::MMagic::XS(); @@ -59,11 +73,12 @@ select(STDERR); $|=1; select(STDOUT); $|=1; -print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose); - -my $db = new Search::Estraier::Node; -$db->set_url($node_url); -$db->set_auth('admin', 'admin'); +my $db = new Search::Estraier::Node( + url => $node_url, + user => 'admin', + passwd => 'admin', + croak_on_error => 1, +); find({ wanted => \&file, follow => 1, @@ -71,13 +86,15 @@ no_chdir => 1, }, $dir); +my $dur = (time() - $start_t) || 1; +printf STDERR "%d documents in %.2fs [%.2f docs/s]\n", $docs, $dur, ($docs / $dur); exit; -sub dump_contents($$$$) { - my ($db,$contents,$mtime,$path) = @_; +sub dump_contents { + my ($db,$contents,$mtime,$path,$size) = @_; - return unless ($contents); # don't die on empty files + return unless (defined($contents)); # don't die on empty files if ($exclude && $path =~ m/$exclude/i) { print STDERR "skip: $path\n" if ($verbose); @@ -85,7 +102,9 @@ } use bytes; - my $size = length $contents; + if (! $size) { + $size = length $contents; + } print STDERR " [$size]" if ($verbose); @@ -105,54 +124,35 @@ $doc->add_attr('@size', $size); $doc->add_attr('@mtime', $mtime); - # html2text - $contents =~ s#<[^>]+/*>##gs; - $contents =~ s#\s\s+# #gs; + if ($contents) { + # html2text + $contents =~ s#<[^>]+/*>##gs; + $contents =~ s#\s\s+# #gs; - $doc->add_text($iconv->convert($contents)); + $doc->add_text($iconv->convert($contents)); + } + # store path + $doc->add_hidden_text($path); + # boost title + $doc->add_hidden_text($title); -# print $doc->dump_draft if ($verbose); + print $doc->dump_draft if ($verbose); # register the document object to the database $db->put_doc($doc); -} - -sub file { - - my $path = $_; - my $contents; - - return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/); - - my $mtime = (stat($path))[9] || -1; - my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2; - - if ($mtime == $mtime_db) { - print STDERR "# same: $path $mtime\n" if ($verbose); - return unless($force); - } else { - print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug); - } - - # skip files on which File::MMagic::XS croaks - return if ($path =~ m#\.au$#); - - my $type = $mm->checktype_filename($path); - $type =~ s/\s+/ /gs; + $docs++; - print STDERR "# $path $type\n" if ($debug); +} - if ($pdftotext && -f $path && $type =~ m/pdf/i) { +sub filter_to_pages { + my ($path, $mtime, $command) = @_; print STDERR "$path {converting}" if ($verbose); - open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'"; + open(F,"$command |") || die "can't open $command with '$path': $!"; my $html; while() { - # XXX why pdftotext barks if I try to use this is beyond me. - #$contents .= $_; - $html .= $_; } close(F); @@ -185,17 +185,66 @@ $page_nr++; } + + +} + +sub file { + + my $path = $_; + my $contents; + + return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak|.gif)$/); + + my $mtime = (stat($path))[9] || -1; + my $mtime_db = eval { $db->get_doc_attr_by_uri("file:///$path", '@mtime') }; + $mtime_db ||= -2; + + if ($mtime == $mtime_db) { + print STDERR "# same: $path $mtime\n" if ($verbose); + return unless($force); + } else { + print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug); + } + + # skip files on which File::MMagic::XS croaks + if ($path =~ m#\.au$#) { + warn "skipped '$path' to prevent File::MMagic::XS bug\n" if ($debug); + return; + } + + my $type = $mm->checktype_filename($path); + $type =~ s/\s+/ /gs; + + print STDERR "# $path $type\n" if ($debug); + + if ($type =~ m/pdf/i) { + if ($filter->{pdftotext}) { + filter_to_pages($path, $mtime, qq( $filter->{pdftotext} -htmlmeta "$path" - )); + } else { + warn "skipping '$path', no pdftotext filter\n" if ($verbose); + return; + } + } elsif ($type eq 'application/postscript') { + if ($filter->{pstotext}) { + filter_to_pages($path, $mtime, qq( $filter->{pstotext} "$path" )); + } else { + warn "skipping '$path', no pstotext filter\n" if ($verbose); + return; + } } else { # return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i); - if (-f $path && $type =~ m/html/ || - ($type !~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io) + if (-f $path && + $type !~ m/html/ && + $path !~ m/\.(php|pl|txt|info|log|text)$/io ) { - dump_contents($db, "$path $type", $mtime, $path); + dump_contents($db, '', $mtime, $path, -s $path) if ($all); + return; } # skip index files - return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i); + return if ($path =~ m/index_(?:[a-z]+|symbol)\.html*/i); open(F,"$path") || die "can't open file: $path"; print STDERR "$path ($type)" if ($verbose);