3 |
use File::Find; |
use File::Find; |
4 |
use Getopt::Long; |
use Getopt::Long; |
5 |
use File::Which; |
use File::Which; |
|
use HyperEstraier; |
|
6 |
use Search::Estraier; |
use Search::Estraier; |
7 |
use Text::Iconv; |
use Text::Iconv; |
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/; |
11 |
# do we use Node API? |
use HTML::TreeBuilder; |
12 |
my $node_url; |
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 |
19 |
#$verbose = 1; |
#$verbose = 1; |
20 |
my $debug = 0; |
my $debug = 0; |
21 |
my $force = 0; |
my $force = 0; |
22 |
my $native = 0; |
my $all = 0; |
23 |
|
|
24 |
my $result = GetOptions( |
my $result = GetOptions( |
25 |
"collection=s" => \$collection, |
"collection=s" => \$collection, |
27 |
"verbose!" => \$verbose, |
"verbose!" => \$verbose, |
28 |
"debug!" => \$debug, |
"debug!" => \$debug, |
29 |
"exclude=s" => \$exclude, |
"exclude=s" => \$exclude, |
|
"node=s" => \$node_url, |
|
30 |
"force!" => \$force, |
"force!" => \$force, |
31 |
"native!" => \$native, |
"all!" => \$all, |
32 |
); |
); |
33 |
|
|
34 |
my $dir = shift @ARGV || die "usage: $0 [dir]"; |
my ($node_url,$dir) = @ARGV; |
35 |
|
|
36 |
|
die <<"_END_OF_USAGE_" if (! $node_url || ! $dir); |
37 |
|
usage: $0 http://localhost:1978/node/my_dir /path/to/directory |
38 |
|
|
39 |
|
options: |
40 |
|
--collection="name of collection" |
41 |
|
--path=/path/to/add/at/end |
42 |
|
--exclude=regex_to_exclude |
43 |
|
--verbose |
44 |
|
--force |
45 |
|
--debug |
46 |
|
--all save placeholders for all files |
47 |
|
_END_OF_USAGE_ |
48 |
|
|
49 |
if (! -e $dir) { |
if (! -e $dir) { |
50 |
warn "directory $dir doesn't exist, skipping\n"; |
warn "directory $dir doesn't exist, skipping\n"; |
55 |
#$basedir =~ s,/[^/]+$,/,; |
#$basedir =~ s,/[^/]+$,/,; |
56 |
#require "$basedir/filter.pm"; |
#require "$basedir/filter.pm"; |
57 |
|
|
58 |
my $pdftotext = which('pdftotext'); |
my $docs = 0; |
59 |
|
my $start_t = time(); |
60 |
|
|
61 |
|
my $filter; |
62 |
|
foreach my $f (qw/pdftotext pstotext/) { |
63 |
|
my $w = which($f); |
64 |
|
if ($w) { |
65 |
|
$filter->{$f} = $w; |
66 |
|
print STDERR "using $f filter at $w\n" if ($verbose); |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
#my $mm = new File::MMagic('/usr/share/misc/file/magic'); |
#my $mm = new File::MMagic('/usr/share/misc/file/magic'); |
71 |
my $mm = new File::MMagic::XS(); |
my $mm = new File::MMagic::XS(); |
75 |
select(STDERR); $|=1; |
select(STDERR); $|=1; |
76 |
select(STDOUT); $|=1; |
select(STDOUT); $|=1; |
77 |
|
|
78 |
print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose); |
my $db = new Search::Estraier::Node( |
79 |
|
url => $node_url, |
80 |
|
user => 'admin', |
81 |
|
passwd => 'admin', |
82 |
|
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 $db; |
my $meta; |
103 |
if ($node_url) { |
|
104 |
if ($native) { |
if ($hhc_file) { |
105 |
$db = HyperEstraier::Node->new($node_url); |
|
106 |
} else { |
sub param { |
107 |
$db = new Search::Estraier::Node; |
my ($el) = @_; |
108 |
$db->set_url($node_url); |
|
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 |
$db->set_auth('admin', 'admin'); |
|
143 |
} else { |
my $tree = HTML::TreeBuilder->new; |
144 |
# open the database |
$tree->parse_file($hhc_file); |
145 |
$db = HyperEstraier::Database->new(); |
|
146 |
$db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
my $prefix = $collection ? ( $collection . ' :: ' ) : ''; |
147 |
|
|
148 |
sub signal { |
my @prefix; |
149 |
my($sig) = @_; |
my $depth = 0; |
150 |
print "\nCaught a SIG$sig--syncing database and shutting down\n"; |
|
151 |
$db->sync(); |
foreach my $e ( $tree->look_down( sub { $_[0]->tag =~ m/(object)/ } ) ) { |
152 |
exit(0); |
|
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 |
$SIG{'INT'} = \&signal; |
$tree->delete; |
176 |
$SIG{'QUIT'} = \&signal; |
|
177 |
} |
} |
178 |
|
|
179 |
|
|
180 |
find({ wanted => \&file, |
find({ wanted => \&file, |
181 |
follow => 1, |
follow => 1, |
182 |
follow_skip => 2, |
follow_skip => 2, |
183 |
no_chdir => 1, |
no_chdir => 1, |
184 |
}, $dir); |
}, $dir); |
185 |
|
|
186 |
unless ($node_url) { |
my $dur = (time() - $start_t) || 1; |
187 |
print "--- sync\n"; |
printf STDERR "%d documents in %.2fs [%.2f docs/s]\n", $docs, $dur, ($docs / $dur); |
188 |
$db->sync(); |
|
189 |
|
$db->master( |
190 |
|
action => 'sync' |
191 |
|
); |
192 |
|
|
193 |
|
|
|
print "--- optimize...\n"; |
|
|
$db->optimize(0); |
|
|
} |
|
194 |
exit; |
exit; |
195 |
|
|
196 |
sub dump_contents($$$$) { |
sub dump_contents { |
197 |
my ($db,$contents,$mtime,$path) = @_; |
my ($db,$contents,$mtime,$path,$size) = @_; |
198 |
|
|
199 |
return unless ($contents); # don't die on empty files |
return unless (defined($contents)); # don't die on empty files |
200 |
|
|
201 |
if ($exclude && $path =~ m/$exclude/i) { |
if ($exclude && $path =~ m/$exclude/i) { |
202 |
print STDERR "skip: $path\n" if ($verbose); |
print STDERR "skip: $path\n" if ($verbose); |
204 |
} |
} |
205 |
|
|
206 |
use bytes; |
use bytes; |
207 |
my $size = length $contents; |
if (! $size) { |
208 |
|
$size = length $contents; |
209 |
|
} |
210 |
|
|
211 |
print STDERR " [$size]" if ($verbose); |
print STDERR " [$size]" if ($verbose); |
212 |
|
|
213 |
# create a document object |
# create a document object |
214 |
my $doc; |
my $doc = new Search::Estraier::Document; |
215 |
if ($native) { |
|
216 |
$doc = HyperEstraier::Document->new; |
my $title; |
217 |
|
|
218 |
|
if ( defined($meta->{path2title}->{lc($path)}) ) { |
219 |
|
$title = $meta->{path2title}->{lc($path)}; |
220 |
|
warn " $title\n"; |
221 |
} else { |
} else { |
|
$doc = new Search::Estraier::Document; |
|
|
} |
|
222 |
|
|
223 |
my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is); |
$title = $1 if ($contents =~ m#<title>(.+?)</title>#is); |
224 |
|
|
225 |
# chop long titles to 100 chars |
# chop long titles to 100 chars |
226 |
$title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); |
$title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); |
227 |
# use path if no title is found |
# use path if no title is found |
228 |
$title ||= $path; |
$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"); |
235 |
$doc->add_attr('@size', $size); |
$doc->add_attr('@size', $size); |
236 |
$doc->add_attr('@mtime', $mtime); |
$doc->add_attr('@mtime', $mtime); |
237 |
|
|
238 |
# html2text |
if ($contents) { |
239 |
$contents =~ s#<[^>]+/*>##gs; |
# html2text |
240 |
$contents =~ s#\s\s+# #gs; |
$contents =~ s#<script.*?</script>##gis; |
241 |
|
$contents =~ s#<[^>]+/*>##gs; |
242 |
$doc->add_text($iconv->convert($contents)); |
$contents =~ s#\s\s+# #gs; |
243 |
|
|
244 |
# print $doc->dump_draft if ($verbose); |
$doc->add_text($iconv->convert($contents)); |
|
|
|
|
# register the document object to the database |
|
|
if ($node_url) { |
|
|
$db->put_doc($doc); |
|
|
} else { |
|
|
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
|
245 |
} |
} |
246 |
|
# store path |
247 |
|
$doc->add_hidden_text($path); |
248 |
|
# boost title |
249 |
|
$doc->add_hidden_text($title); |
250 |
|
|
251 |
} |
print $doc->dump_draft if ($debug); |
|
|
|
|
sub file { |
|
252 |
|
|
253 |
my $path = $_; |
# register the document object to the database |
254 |
my $contents; |
$db->put_doc($doc); |
|
|
|
|
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$#); |
|
255 |
|
|
256 |
my $type = $mm->checktype_filename($path); |
$docs++; |
|
$type =~ s/\s+/ /gs; |
|
257 |
|
|
258 |
print STDERR "# $path $type\n" if ($debug); |
} |
259 |
|
|
260 |
if ($pdftotext && -f $path && $type =~ m/pdf/i) { |
sub filter_to_pages { |
261 |
|
my ($path, $mtime, $command) = @_; |
262 |
|
|
263 |
print STDERR "$path {converting}" if ($verbose); |
print STDERR "$path {converting}" if ($verbose); |
264 |
|
|
265 |
open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'"; |
open(F,"$command |") || die "can't open $command with '$path': $!"; |
266 |
my $html; |
my $html; |
267 |
while(<F>) { |
while(<F>) { |
|
# XXX why pdftotext barks if I try to use this is beyond me. |
|
|
#$contents .= $_; |
|
|
|
|
268 |
$html .= $_; |
$html .= $_; |
269 |
} |
} |
270 |
close(F); |
close(F); |
297 |
$page_nr++; |
$page_nr++; |
298 |
} |
} |
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
} |
303 |
|
|
304 |
|
sub file { |
305 |
|
|
306 |
|
my $path = $_; |
307 |
|
my $contents; |
308 |
|
|
309 |
|
return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak|.gif)$/); |
310 |
|
return if (! $all && -d $path); |
311 |
|
|
312 |
|
my $mtime = (stat($path))[9] || -1; |
313 |
|
my $mtime_db; |
314 |
|
eval { $db->get_doc_attr_by_uri("file:///$path", '@mtime') } unless ($force); |
315 |
|
$mtime_db ||= -2; |
316 |
|
|
317 |
|
if ($mtime == $mtime_db) { |
318 |
|
print STDERR "# same: $path $mtime\n" if ($verbose); |
319 |
|
return unless($force); |
320 |
|
} else { |
321 |
|
print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug); |
322 |
|
} |
323 |
|
|
324 |
|
# skip files on which File::MMagic::XS croaks |
325 |
|
if ($path =~ m#\.au$#) { |
326 |
|
warn "skipped '$path' to prevent File::MMagic::XS bug\n" if ($debug); |
327 |
|
return; |
328 |
|
} |
329 |
|
|
330 |
|
my $type = $mm->checktype_filename($path); |
331 |
|
$type =~ s/\s+/ /gs; |
332 |
|
|
333 |
|
print STDERR "# $path $type\n" if ($debug); |
334 |
|
|
335 |
|
if ($type =~ m/pdf/i) { |
336 |
|
if ($filter->{pdftotext}) { |
337 |
|
filter_to_pages($path, $mtime, qq( $filter->{pdftotext} -htmlmeta "$path" - )); |
338 |
|
} else { |
339 |
|
warn "skipping '$path', no pdftotext filter\n" if ($verbose); |
340 |
|
return; |
341 |
|
} |
342 |
|
} elsif ($type eq 'application/postscript') { |
343 |
|
if ($filter->{pstotext}) { |
344 |
|
filter_to_pages($path, $mtime, qq( $filter->{pstotext} "$path" )); |
345 |
|
} else { |
346 |
|
warn "skipping '$path', no pstotext filter\n" if ($verbose); |
347 |
|
return; |
348 |
|
} |
349 |
} else { |
} else { |
350 |
|
|
351 |
# return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i); |
# return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i); |
352 |
return unless (-f $path && $type =~ m/html/ || |
if (-f $path && |
353 |
($type =~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io) |
$type !~ m/html/ && |
354 |
); |
$path !~ m/\.(php|pl|txt|info|log|text)$/io |
355 |
|
) { |
356 |
|
dump_contents($db, '', $mtime, $path, -s $path) if ($all); |
357 |
|
return; |
358 |
|
} |
359 |
|
|
360 |
# skip index files |
# skip index files |
361 |
return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i); |
return if ($path =~ m/index_(?:[a-z]+|symbol)\.html*/i); |
362 |
|
|
363 |
open(F,"$path") || die "can't open file: $path"; |
open(F,"$path") || die "can't open file: $path"; |
364 |
print STDERR "$path ($type)" if ($verbose); |
print STDERR "$path ($type)" if ($verbose); |