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 |
|
|
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 |
50 |
#$basedir =~ s,/[^/]+$,/,; |
#$basedir =~ s,/[^/]+$,/,; |
51 |
#require "$basedir/filter.pm"; |
#require "$basedir/filter.pm"; |
52 |
|
|
53 |
my $pdftotext = which('pdftotext'); |
my $docs = 0; |
54 |
|
my $start_t = time(); |
55 |
|
|
56 |
|
my $filter; |
57 |
|
foreach my $f (qw/pdftotext pstotext/) { |
58 |
|
my $w = which($f); |
59 |
|
if ($f) { |
60 |
|
$filter->{$f} = $w; |
61 |
|
print STDERR "using $f filter at $w\n" if ($verbose); |
62 |
|
} |
63 |
|
} |
64 |
|
|
65 |
#my $mm = new File::MMagic('/usr/share/misc/file/magic'); |
#my $mm = new File::MMagic('/usr/share/misc/file/magic'); |
66 |
my $mm = new File::MMagic::XS(); |
my $mm = new File::MMagic::XS(); |
70 |
select(STDERR); $|=1; |
select(STDERR); $|=1; |
71 |
select(STDOUT); $|=1; |
select(STDOUT); $|=1; |
72 |
|
|
|
print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose); |
|
|
|
|
73 |
my $db = new Search::Estraier::Node; |
my $db = new Search::Estraier::Node; |
74 |
$db->set_url($node_url); |
$db->set_url($node_url); |
75 |
$db->set_auth('admin', 'admin'); |
$db->set_auth('admin', 'admin'); |
80 |
no_chdir => 1, |
no_chdir => 1, |
81 |
}, $dir); |
}, $dir); |
82 |
|
|
83 |
|
my $dur = (time() - $start_t) || 1; |
84 |
|
printf STDERR "%d documents in %.2fs [%.2f docs/s]\n", $docs, $dur, ($docs / $dur); |
85 |
|
|
86 |
exit; |
exit; |
87 |
|
|
88 |
sub dump_contents($$$$) { |
sub dump_contents { |
89 |
my ($db,$contents,$mtime,$path) = @_; |
my ($db,$contents,$mtime,$path,$size) = @_; |
90 |
|
|
91 |
return unless ($contents); # don't die on empty files |
return unless (defined($contents)); # don't die on empty files |
92 |
|
|
93 |
if ($exclude && $path =~ m/$exclude/i) { |
if ($exclude && $path =~ m/$exclude/i) { |
94 |
print STDERR "skip: $path\n" if ($verbose); |
print STDERR "skip: $path\n" if ($verbose); |
96 |
} |
} |
97 |
|
|
98 |
use bytes; |
use bytes; |
99 |
my $size = length $contents; |
if (! $size) { |
100 |
|
$size = length $contents; |
101 |
|
} |
102 |
|
|
103 |
print STDERR " [$size]" if ($verbose); |
print STDERR " [$size]" if ($verbose); |
104 |
|
|
118 |
$doc->add_attr('@size', $size); |
$doc->add_attr('@size', $size); |
119 |
$doc->add_attr('@mtime', $mtime); |
$doc->add_attr('@mtime', $mtime); |
120 |
|
|
121 |
# html2text |
if ($contents) { |
122 |
$contents =~ s#<[^>]+/*>##gs; |
# html2text |
123 |
$contents =~ s#\s\s+# #gs; |
$contents =~ s#<[^>]+/*>##gs; |
124 |
|
$contents =~ s#\s\s+# #gs; |
125 |
|
|
126 |
$doc->add_text($iconv->convert($contents)); |
$doc->add_text($iconv->convert($contents)); |
127 |
|
} |
128 |
|
# store path |
129 |
|
$doc->add_hidden_text($path); |
130 |
|
# boost title |
131 |
|
$doc->add_hidden_text($title); |
132 |
|
|
133 |
# print $doc->dump_draft if ($verbose); |
# print $doc->dump_draft if ($verbose); |
134 |
|
|
135 |
# register the document object to the database |
# register the document object to the database |
136 |
$db->put_doc($doc); |
$db->put_doc($doc); |
137 |
|
|
138 |
} |
$docs++; |
|
|
|
|
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; |
|
139 |
|
|
140 |
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); |
|
141 |
|
|
142 |
if ($pdftotext && -f $path && $type =~ m/pdf/i) { |
sub filter_to_pages { |
143 |
|
my ($path, $mtime, $command) = @_; |
144 |
|
|
145 |
print STDERR "$path {converting}" if ($verbose); |
print STDERR "$path {converting}" if ($verbose); |
146 |
|
|
147 |
open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'"; |
open(F,"$command |") || die "can't open $command with '$path': $!"; |
148 |
my $html; |
my $html; |
149 |
while(<F>) { |
while(<F>) { |
|
# XXX why pdftotext barks if I try to use this is beyond me. |
|
|
#$contents .= $_; |
|
|
|
|
150 |
$html .= $_; |
$html .= $_; |
151 |
} |
} |
152 |
close(F); |
close(F); |
179 |
$page_nr++; |
$page_nr++; |
180 |
} |
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
} |
185 |
|
|
186 |
|
sub file { |
187 |
|
|
188 |
|
my $path = $_; |
189 |
|
my $contents; |
190 |
|
|
191 |
|
return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/); |
192 |
|
|
193 |
|
my $mtime = (stat($path))[9] || -1; |
194 |
|
my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2; |
195 |
|
|
196 |
|
if ($mtime == $mtime_db) { |
197 |
|
print STDERR "# same: $path $mtime\n" if ($verbose); |
198 |
|
return unless($force); |
199 |
|
} else { |
200 |
|
print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug); |
201 |
|
} |
202 |
|
|
203 |
|
# skip files on which File::MMagic::XS croaks |
204 |
|
if ($path =~ m#\.au$#) { |
205 |
|
warn "skipped '$path' to prevent File::MMagic::XS bug\n" if ($debug); |
206 |
|
return; |
207 |
|
} |
208 |
|
|
209 |
|
my $type = $mm->checktype_filename($path); |
210 |
|
$type =~ s/\s+/ /gs; |
211 |
|
|
212 |
|
print STDERR "# $path $type\n" if ($debug); |
213 |
|
|
214 |
|
if ($type =~ m/pdf/i) { |
215 |
|
if ($filter->{pdftotext}) { |
216 |
|
filter_to_pages($path, $mtime, qq( $filter->{pdftotext} -htmlmeta "$path" - )); |
217 |
|
} else { |
218 |
|
warn "skipping '$path', no pdftotext filter\n" if ($verbose); |
219 |
|
return; |
220 |
|
} |
221 |
|
} elsif ($type eq 'application/postscript') { |
222 |
|
if ($filter->{pstotext}) { |
223 |
|
filter_to_pages($path, $mtime, qq( $filter->{pstotext} "$path" )); |
224 |
|
} else { |
225 |
|
warn "skipping '$path', no pstotext filter\n" if ($verbose); |
226 |
|
return; |
227 |
|
} |
228 |
} else { |
} else { |
229 |
|
|
230 |
# 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); |
231 |
if (-f $path && $type =~ m/html/ || |
if (-f $path && $type =~ m/html/ || |
232 |
($type !~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io) |
($path !~ m/\.(php|pl|txt|info|log|text)$/io) |
233 |
) { |
) { |
234 |
dump_contents($db, "<title>$path</title> $type", $mtime, $path); |
dump_contents($db, '', $mtime, $path, -s $path); |
235 |
|
return; |
236 |
} |
} |
237 |
|
|
238 |
# skip index files |
# skip index files |