--- trunk/scripts/est-spider 2006/04/16 23:22:54 118 +++ trunk/scripts/est-spider 2006/08/26 22:33:34 181 @@ -8,6 +8,8 @@ #use File::MMagic; use File::MMagic::XS qw/:compat/; use Time::HiRes qw/time/; +use HTML::TreeBuilder; +use Data::Dump qw/dump/; my $collection; # name which will be inserted my $path_add; # add additional info in path @@ -17,6 +19,7 @@ #$verbose = 1; my $debug = 0; my $force = 0; +my $all = 0; my $result = GetOptions( "collection=s" => \$collection, @@ -25,6 +28,7 @@ "debug!" => \$debug, "exclude=s" => \$exclude, "force!" => \$force, + "all!" => \$all, ); my ($node_url,$dir) = @ARGV; @@ -39,6 +43,7 @@ --verbose --force --debug + --all save placeholders for all files _END_OF_USAGE_ if (! -e $dir) { @@ -56,7 +61,7 @@ my $filter; foreach my $f (qw/pdftotext pstotext/) { my $w = which($f); - if ($f) { + if ($w) { $filter->{$f} = $w; print STDERR "using $f filter at $w\n" if ($verbose); } @@ -70,9 +75,107 @@ select(STDERR); $|=1; select(STDOUT); $|=1; -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, + create => 1, +); + +# +# check if hhc file exists, and if it does, extract information from it +# + +my $hhc_file; +# try to find hhc +find({ wanted => sub { + return unless( m!\.hhc$!i ); + $hhc_file = $_; + warn "using $hhc_file for tree structure\n"; + }, + follow => 1, + follow_skip => 2, + no_chdir => 1, +}, $dir); + +my $meta; + +if ($hhc_file) { + + sub param { + my ($el) = @_; + + my $n; + foreach my $p ( $el->find('param') ) { + $n->{ $p->attr('name') } = $p->attr('value'); + } + + if ( ! defined($n->{Local}) ) { + warn "### skipped = ",dump($n),$/; + return; + } + + my $path = $dir . '/' . $n->{Local}; + $path =~ s!//!/!g; + $path = lc($path); + + $n->{path} = $path; + + my $nr = $n->{ImageNumber} || next; + + if ($nr == 27) { + $meta->{title} = $n->{Name}; + $meta->{index_path} = $path; + } elsif ($nr == 21) { + $meta->{toc_path} = $path; + } elsif ($nr == 1) { + $meta->{foreword_path} = $path; + } elsif ($nr == 11) { + # nop + } else { + warn "unknown ImageNumber: $nr\n"; + } + + return $n; + } + + my $tree = HTML::TreeBuilder->new; + $tree->parse_file($hhc_file); + + my $prefix = $collection ? ( $collection . ' :: ' ) : ''; + + my @prefix; + my $depth = 0; + + foreach my $e ( $tree->look_down( sub { $_[0]->tag =~ m/(object)/ } ) ) { + +# printf("%05s %s\n", $e->parent->address(), $e->as_HTML() ); + + my $l = ($e->depth() / 2) - 1; + + $prefix[ 0 ] = $meta->{title} || ''; + + my $n = param($e); + $prefix[ $l ] = $n->{Name}; + + next unless ($n->{path}); + + my $t = ''; + my @p; + foreach my $i ( 0 .. $l ) { + push @p, $prefix[ $i ] if ($prefix[ $i ]); + } + $t = join(' :: ', @p ) if (@p); + + $meta->{path2title}->{ $n->{path} } = $t; + + } + + $tree->delete; + +} + find({ wanted => \&file, follow => 1, @@ -83,6 +186,11 @@ my $dur = (time() - $start_t) || 1; printf STDERR "%d documents in %.2fs [%.2f docs/s]\n", $docs, $dur, ($docs / $dur); +$db->master( + action => 'sync' +); + + exit; sub dump_contents { @@ -105,12 +213,21 @@ # create a document object my $doc = new Search::Estraier::Document; - my $title = $1 if ($contents =~ m#(.+?)#is); + my $title; + + if ( defined($meta->{path2title}->{lc($path)}) ) { + $title = $meta->{path2title}->{lc($path)}; + warn " $title\n"; + } else { - # chop long titles to 100 chars - $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); - # use path if no title is found - $title ||= $path; + $title = $1 if ($contents =~ m#(.+?)#is); + + # chop long titles to 100 chars + $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); + # use path if no title is found + $title ||= $path; + + } # add attributes to the document object $doc->add_attr('@uri', "file:///$path"); @@ -120,6 +237,7 @@ if ($contents) { # html2text + $contents =~ s###gis; $contents =~ s#<[^>]+/*>##gs; $contents =~ s#\s\s+# #gs; @@ -130,7 +248,7 @@ # boost title $doc->add_hidden_text($title); -# print $doc->dump_draft if ($verbose); + print $doc->dump_draft if ($debug); # register the document object to the database $db->put_doc($doc); @@ -188,10 +306,12 @@ my $path = $_; my $contents; - return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/); + return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak|.gif)$/); + return if (! $all && -d $path); my $mtime = (stat($path))[9] || -1; - my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2; + 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); @@ -228,15 +348,16 @@ } else { # return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i); - if (-f $path && $type =~ m/html/ || - ($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, '', $mtime, $path, -s $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);