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/; |
|
# do we use Node API? |
|
|
my $node_url; |
|
11 |
|
|
12 |
my $collection; # name which will be inserted |
my $collection; # name which will be inserted |
13 |
my $path_add; # add additional info in path |
my $path_add; # add additional info in path |
17 |
#$verbose = 1; |
#$verbose = 1; |
18 |
my $debug = 0; |
my $debug = 0; |
19 |
my $force = 0; |
my $force = 0; |
20 |
my $native = 0; |
my $all = 0; |
21 |
|
|
22 |
my $result = GetOptions( |
my $result = GetOptions( |
23 |
"collection=s" => \$collection, |
"collection=s" => \$collection, |
25 |
"verbose!" => \$verbose, |
"verbose!" => \$verbose, |
26 |
"debug!" => \$debug, |
"debug!" => \$debug, |
27 |
"exclude=s" => \$exclude, |
"exclude=s" => \$exclude, |
|
"node=s" => \$node_url, |
|
28 |
"force!" => \$force, |
"force!" => \$force, |
29 |
"native!" => \$native, |
"all!" => \$all, |
30 |
); |
); |
31 |
|
|
32 |
my $dir = shift @ARGV || die "usage: $0 [dir]"; |
my ($node_url,$dir) = @ARGV; |
33 |
|
|
34 |
|
die <<"_END_OF_USAGE_" if (! $node_url || ! $dir); |
35 |
|
usage: $0 http://localhost:1978/node/my_dir /path/to/directory |
36 |
|
|
37 |
|
options: |
38 |
|
--collection="name of collection" |
39 |
|
--path=/path/to/add/at/end |
40 |
|
--exclude=regex_to_exclude |
41 |
|
--verbose |
42 |
|
--force |
43 |
|
--debug |
44 |
|
--all save placeholders for all files |
45 |
|
_END_OF_USAGE_ |
46 |
|
|
47 |
if (! -e $dir) { |
if (! -e $dir) { |
48 |
warn "directory $dir doesn't exist, skipping\n"; |
warn "directory $dir doesn't exist, skipping\n"; |
53 |
#$basedir =~ s,/[^/]+$,/,; |
#$basedir =~ s,/[^/]+$,/,; |
54 |
#require "$basedir/filter.pm"; |
#require "$basedir/filter.pm"; |
55 |
|
|
56 |
my $pdftotext = which('pdftotext'); |
my $docs = 0; |
57 |
|
my $start_t = time(); |
58 |
|
|
59 |
|
my $filter; |
60 |
|
foreach my $f (qw/pdftotext pstotext/) { |
61 |
|
my $w = which($f); |
62 |
|
if ($f) { |
63 |
|
$filter->{$f} = $w; |
64 |
|
print STDERR "using $f filter at $w\n" if ($verbose); |
65 |
|
} |
66 |
|
} |
67 |
|
|
68 |
#my $mm = new File::MMagic('/usr/share/misc/file/magic'); |
#my $mm = new File::MMagic('/usr/share/misc/file/magic'); |
69 |
my $mm = new File::MMagic::XS(); |
my $mm = new File::MMagic::XS(); |
73 |
select(STDERR); $|=1; |
select(STDERR); $|=1; |
74 |
select(STDOUT); $|=1; |
select(STDOUT); $|=1; |
75 |
|
|
76 |
print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose); |
my $db = new Search::Estraier::Node( |
77 |
|
url => $node_url, |
78 |
my $db; |
user => 'admin', |
79 |
if ($node_url) { |
passwd => 'admin', |
80 |
if ($native) { |
croak_on_error => 1, |
81 |
$db = HyperEstraier::Node->new($node_url); |
); |
|
} else { |
|
|
$db = new Search::Estraier::Node; |
|
|
$db->set_url($node_url); |
|
|
} |
|
|
$db->set_auth('admin', 'admin'); |
|
|
} else { |
|
|
# open the database |
|
|
$db = HyperEstraier::Database->new(); |
|
|
$db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
|
|
|
|
|
sub signal { |
|
|
my($sig) = @_; |
|
|
print "\nCaught a SIG$sig--syncing database and shutting down\n"; |
|
|
$db->sync(); |
|
|
exit(0); |
|
|
} |
|
|
|
|
|
$SIG{'INT'} = \&signal; |
|
|
$SIG{'QUIT'} = \&signal; |
|
|
} |
|
82 |
|
|
83 |
find({ wanted => \&file, |
find({ wanted => \&file, |
84 |
follow => 1, |
follow => 1, |
86 |
no_chdir => 1, |
no_chdir => 1, |
87 |
}, $dir); |
}, $dir); |
88 |
|
|
89 |
unless ($node_url) { |
my $dur = (time() - $start_t) || 1; |
90 |
print "--- sync\n"; |
printf STDERR "%d documents in %.2fs [%.2f docs/s]\n", $docs, $dur, ($docs / $dur); |
91 |
$db->sync(); |
|
92 |
|
$db->master( |
93 |
|
action => 'sync' |
94 |
|
); |
95 |
|
|
96 |
|
|
|
print "--- optimize...\n"; |
|
|
$db->optimize(0); |
|
|
} |
|
97 |
exit; |
exit; |
98 |
|
|
99 |
sub dump_contents($$$$) { |
sub dump_contents { |
100 |
my ($db,$contents,$mtime,$path) = @_; |
my ($db,$contents,$mtime,$path,$size) = @_; |
101 |
|
|
102 |
return unless ($contents); # don't die on empty files |
return unless (defined($contents)); # don't die on empty files |
103 |
|
|
104 |
if ($exclude && $path =~ m/$exclude/i) { |
if ($exclude && $path =~ m/$exclude/i) { |
105 |
print STDERR "skip: $path\n" if ($verbose); |
print STDERR "skip: $path\n" if ($verbose); |
107 |
} |
} |
108 |
|
|
109 |
use bytes; |
use bytes; |
110 |
my $size = length $contents; |
if (! $size) { |
111 |
|
$size = length $contents; |
112 |
|
} |
113 |
|
|
114 |
print STDERR " [$size]" if ($verbose); |
print STDERR " [$size]" if ($verbose); |
115 |
|
|
116 |
# create a document object |
# create a document object |
117 |
my $doc; |
my $doc = new Search::Estraier::Document; |
|
if ($native) { |
|
|
$doc = HyperEstraier::Document->new; |
|
|
} else { |
|
|
$doc = new Search::Estraier::Document; |
|
|
} |
|
118 |
|
|
119 |
my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is); |
my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is); |
120 |
|
|
129 |
$doc->add_attr('@size', $size); |
$doc->add_attr('@size', $size); |
130 |
$doc->add_attr('@mtime', $mtime); |
$doc->add_attr('@mtime', $mtime); |
131 |
|
|
132 |
# html2text |
if ($contents) { |
133 |
$contents =~ s#<[^>]+/*>##gs; |
# html2text |
134 |
$contents =~ s#\s\s+# #gs; |
$contents =~ s#<[^>]+/*>##gs; |
135 |
|
$contents =~ s#\s\s+# #gs; |
136 |
|
|
137 |
|
$doc->add_text($iconv->convert($contents)); |
138 |
|
} |
139 |
|
# store path |
140 |
|
$doc->add_hidden_text($path); |
141 |
|
# boost title |
142 |
|
$doc->add_hidden_text($title); |
143 |
|
|
144 |
$doc->add_text($iconv->convert($contents)); |
print $doc->dump_draft if ($debug); |
|
|
|
|
# print $doc->dump_draft if ($verbose); |
|
145 |
|
|
146 |
# register the document object to the database |
# register the document object to the database |
147 |
if ($node_url) { |
$db->put_doc($doc); |
|
$db->put_doc($doc); |
|
|
} else { |
|
|
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
sub file { |
|
|
|
|
|
my $path = $_; |
|
|
my $contents; |
|
148 |
|
|
149 |
return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/); |
$docs++; |
150 |
|
|
151 |
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; |
|
|
|
|
|
print STDERR "# $path $type\n" if ($debug); |
|
152 |
|
|
153 |
if ($pdftotext && -f $path && $type =~ m/pdf/i) { |
sub filter_to_pages { |
154 |
|
my ($path, $mtime, $command) = @_; |
155 |
|
|
156 |
print STDERR "$path {converting}" if ($verbose); |
print STDERR "$path {converting}" if ($verbose); |
157 |
|
|
158 |
open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'"; |
open(F,"$command |") || die "can't open $command with '$path': $!"; |
159 |
my $html; |
my $html; |
160 |
while(<F>) { |
while(<F>) { |
|
# XXX why pdftotext barks if I try to use this is beyond me. |
|
|
#$contents .= $_; |
|
|
|
|
161 |
$html .= $_; |
$html .= $_; |
162 |
} |
} |
163 |
close(F); |
close(F); |
190 |
$page_nr++; |
$page_nr++; |
191 |
} |
} |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
} |
196 |
|
|
197 |
|
sub file { |
198 |
|
|
199 |
|
my $path = $_; |
200 |
|
my $contents; |
201 |
|
|
202 |
|
return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak|.gif)$/); |
203 |
|
return if (! $all && -d $path); |
204 |
|
|
205 |
|
my $mtime = (stat($path))[9] || -1; |
206 |
|
my $mtime_db = eval { $db->get_doc_attr_by_uri("file:///$path", '@mtime') }; |
207 |
|
$mtime_db ||= -2; |
208 |
|
|
209 |
|
if ($mtime == $mtime_db) { |
210 |
|
print STDERR "# same: $path $mtime\n" if ($verbose); |
211 |
|
return unless($force); |
212 |
|
} else { |
213 |
|
print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug); |
214 |
|
} |
215 |
|
|
216 |
|
# skip files on which File::MMagic::XS croaks |
217 |
|
if ($path =~ m#\.au$#) { |
218 |
|
warn "skipped '$path' to prevent File::MMagic::XS bug\n" if ($debug); |
219 |
|
return; |
220 |
|
} |
221 |
|
|
222 |
|
my $type = $mm->checktype_filename($path); |
223 |
|
$type =~ s/\s+/ /gs; |
224 |
|
|
225 |
|
print STDERR "# $path $type\n" if ($debug); |
226 |
|
|
227 |
|
if ($type =~ m/pdf/i) { |
228 |
|
if ($filter->{pdftotext}) { |
229 |
|
filter_to_pages($path, $mtime, qq( $filter->{pdftotext} -htmlmeta "$path" - )); |
230 |
|
} else { |
231 |
|
warn "skipping '$path', no pdftotext filter\n" if ($verbose); |
232 |
|
return; |
233 |
|
} |
234 |
|
} elsif ($type eq 'application/postscript') { |
235 |
|
if ($filter->{pstotext}) { |
236 |
|
filter_to_pages($path, $mtime, qq( $filter->{pstotext} "$path" )); |
237 |
|
} else { |
238 |
|
warn "skipping '$path', no pstotext filter\n" if ($verbose); |
239 |
|
return; |
240 |
|
} |
241 |
} else { |
} else { |
242 |
|
|
243 |
# 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); |
244 |
if (-f $path && $type =~ m/html/ || |
if (-f $path && |
245 |
($type !~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io) |
$type !~ m/html/ && |
246 |
|
$path !~ m/\.(php|pl|txt|info|log|text)$/io |
247 |
) { |
) { |
248 |
dump_contents($db, "<title>$path</title> $type", $mtime, $path); |
dump_contents($db, '', $mtime, $path, -s $path) if ($all); |
249 |
|
return; |
250 |
} |
} |
251 |
|
|
252 |
# skip index files |
# skip index files |
253 |
return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i); |
return if ($path =~ m/index_(?:[a-z]+|symbol)\.html*/i); |
254 |
|
|
255 |
open(F,"$path") || die "can't open file: $path"; |
open(F,"$path") || die "can't open file: $path"; |
256 |
print STDERR "$path ($type)" if ($verbose); |
print STDERR "$path ($type)" if ($verbose); |