8 |
#use File::MMagic; |
#use File::MMagic; |
9 |
use File::MMagic::XS qw/:compat/; |
use File::MMagic::XS qw/:compat/; |
10 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
11 |
|
use HTML::TreeBuilder; |
12 |
|
use Data::Dump qw/dump/; |
13 |
|
|
14 |
my $collection; # name which will be inserted |
my $collection; # name which will be inserted |
15 |
my $path_add; # add additional info in path |
my $path_add; # add additional info in path |
61 |
my $filter; |
my $filter; |
62 |
foreach my $f (qw/pdftotext pstotext/) { |
foreach my $f (qw/pdftotext pstotext/) { |
63 |
my $w = which($f); |
my $w = which($f); |
64 |
if ($f) { |
if ($w) { |
65 |
$filter->{$f} = $w; |
$filter->{$f} = $w; |
66 |
print STDERR "using $f filter at $w\n" if ($verbose); |
print STDERR "using $f filter at $w\n" if ($verbose); |
67 |
} |
} |
80 |
user => 'admin', |
user => 'admin', |
81 |
passwd => 'admin', |
passwd => 'admin', |
82 |
croak_on_error => 1, |
croak_on_error => 1, |
83 |
|
create => 1, |
84 |
); |
); |
85 |
|
|
86 |
|
# |
87 |
|
# check if hhc file exists, and if it does, extract information from it |
88 |
|
# |
89 |
|
|
90 |
|
my $hhc_file; |
91 |
|
# try to find hhc |
92 |
|
find({ wanted => sub { |
93 |
|
return unless( m!\.hhc$!i ); |
94 |
|
$hhc_file = $_; |
95 |
|
warn "using $hhc_file for tree structure\n"; |
96 |
|
}, |
97 |
|
follow => 1, |
98 |
|
follow_skip => 2, |
99 |
|
no_chdir => 1, |
100 |
|
}, $dir); |
101 |
|
|
102 |
|
my $meta; |
103 |
|
|
104 |
|
if ($hhc_file) { |
105 |
|
|
106 |
|
sub param { |
107 |
|
my ($el) = @_; |
108 |
|
|
109 |
|
my $n; |
110 |
|
foreach my $p ( $el->find('param') ) { |
111 |
|
$n->{ $p->attr('name') } = $p->attr('value'); |
112 |
|
} |
113 |
|
|
114 |
|
if ( ! defined($n->{Local}) ) { |
115 |
|
warn "### skipped = ",dump($n),$/; |
116 |
|
return; |
117 |
|
} |
118 |
|
|
119 |
|
my $path = $dir . '/' . $n->{Local}; |
120 |
|
$path =~ s!//!/!g; |
121 |
|
$path = lc($path); |
122 |
|
|
123 |
|
$n->{path} = $path; |
124 |
|
|
125 |
|
my $nr = $n->{ImageNumber} || next; |
126 |
|
|
127 |
|
if ($nr == 27) { |
128 |
|
$meta->{title} = $n->{Name}; |
129 |
|
$meta->{index_path} = $path; |
130 |
|
} elsif ($nr == 21) { |
131 |
|
$meta->{toc_path} = $path; |
132 |
|
} elsif ($nr == 1) { |
133 |
|
$meta->{foreword_path} = $path; |
134 |
|
} elsif ($nr == 11) { |
135 |
|
# nop |
136 |
|
} else { |
137 |
|
warn "unknown ImageNumber: $nr\n"; |
138 |
|
} |
139 |
|
|
140 |
|
return $n; |
141 |
|
} |
142 |
|
|
143 |
|
my $tree = HTML::TreeBuilder->new; |
144 |
|
$tree->parse_file($hhc_file); |
145 |
|
|
146 |
|
my $prefix = $collection ? ( $collection . ' :: ' ) : ''; |
147 |
|
|
148 |
|
my @prefix; |
149 |
|
my $depth = 0; |
150 |
|
|
151 |
|
foreach my $e ( $tree->look_down( sub { $_[0]->tag =~ m/(object)/ } ) ) { |
152 |
|
|
153 |
|
# printf("%05s %s\n", $e->parent->address(), $e->as_HTML() ); |
154 |
|
|
155 |
|
my $l = ($e->depth() / 2) - 1; |
156 |
|
|
157 |
|
$prefix[ 0 ] = $meta->{title} || ''; |
158 |
|
|
159 |
|
my $n = param($e); |
160 |
|
$prefix[ $l ] = $n->{Name}; |
161 |
|
|
162 |
|
next unless ($n->{path}); |
163 |
|
|
164 |
|
my $t = ''; |
165 |
|
my @p; |
166 |
|
foreach my $i ( 0 .. $l ) { |
167 |
|
push @p, $prefix[ $i ] if ($prefix[ $i ]); |
168 |
|
} |
169 |
|
$t = join(' :: ', @p ) if (@p); |
170 |
|
|
171 |
|
$meta->{path2title}->{ $n->{path} } = $t; |
172 |
|
|
173 |
|
} |
174 |
|
|
175 |
|
$tree->delete; |
176 |
|
|
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
find({ wanted => \&file, |
find({ wanted => \&file, |
181 |
follow => 1, |
follow => 1, |
182 |
follow_skip => 2, |
follow_skip => 2, |
186 |
my $dur = (time() - $start_t) || 1; |
my $dur = (time() - $start_t) || 1; |
187 |
printf STDERR "%d documents in %.2fs [%.2f docs/s]\n", $docs, $dur, ($docs / $dur); |
printf STDERR "%d documents in %.2fs [%.2f docs/s]\n", $docs, $dur, ($docs / $dur); |
188 |
|
|
189 |
|
$db->master( |
190 |
|
action => 'sync' |
191 |
|
); |
192 |
|
|
193 |
|
|
194 |
exit; |
exit; |
195 |
|
|
196 |
sub dump_contents { |
sub dump_contents { |
213 |
# create a document object |
# create a document object |
214 |
my $doc = new Search::Estraier::Document; |
my $doc = new Search::Estraier::Document; |
215 |
|
|
216 |
my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is); |
my $title; |
217 |
|
|
218 |
# chop long titles to 100 chars |
if ( defined($meta->{path2title}->{lc($path)}) ) { |
219 |
$title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); |
$title = $meta->{path2title}->{lc($path)}; |
220 |
# use path if no title is found |
warn " $title\n"; |
221 |
$title ||= $path; |
} else { |
222 |
|
|
223 |
|
$title = $1 if ($contents =~ m#<title>(.+?)</title>#is); |
224 |
|
|
225 |
|
# chop long titles to 100 chars |
226 |
|
$title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); |
227 |
|
# use path if no title is found |
228 |
|
$title ||= $path; |
229 |
|
|
230 |
|
} |
231 |
|
|
232 |
# add attributes to the document object |
# add attributes to the document object |
233 |
$doc->add_attr('@uri', "file:///$path"); |
$doc->add_attr('@uri', "file:///$path"); |
247 |
# boost title |
# boost title |
248 |
$doc->add_hidden_text($title); |
$doc->add_hidden_text($title); |
249 |
|
|
250 |
print $doc->dump_draft if ($verbose); |
print $doc->dump_draft if ($debug); |
251 |
|
|
252 |
# register the document object to the database |
# register the document object to the database |
253 |
$db->put_doc($doc); |
$db->put_doc($doc); |
306 |
my $contents; |
my $contents; |
307 |
|
|
308 |
return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak|.gif)$/); |
return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak|.gif)$/); |
309 |
|
return if (! $all && -d $path); |
310 |
|
|
311 |
my $mtime = (stat($path))[9] || -1; |
my $mtime = (stat($path))[9] || -1; |
312 |
my $mtime_db = eval { $db->get_doc_attr_by_uri("file:///$path", '@mtime') }; |
my $mtime_db = eval { $db->get_doc_attr_by_uri("file:///$path", '@mtime') }; |