1 |
#!/usr/bin/perl -w |
2 |
use strict; |
3 |
use File::Find; |
4 |
use Getopt::Long; |
5 |
use File::Which; |
6 |
use Search::Estraier; |
7 |
use Text::Iconv; |
8 |
#use File::MMagic; |
9 |
use File::MMagic::XS qw/:compat/; |
10 |
use Time::HiRes qw/time/; |
11 |
|
12 |
my $collection; # name which will be inserted |
13 |
my $path_add; # add additional info in path |
14 |
my $verbose; |
15 |
my $exclude; |
16 |
|
17 |
#$verbose = 1; |
18 |
my $debug = 0; |
19 |
my $force = 0; |
20 |
|
21 |
my $result = GetOptions( |
22 |
"collection=s" => \$collection, |
23 |
"path=s" => \$path_add, |
24 |
"verbose!" => \$verbose, |
25 |
"debug!" => \$debug, |
26 |
"exclude=s" => \$exclude, |
27 |
"force!" => \$force, |
28 |
); |
29 |
|
30 |
my ($node_url,$dir) = @ARGV; |
31 |
|
32 |
die <<"_END_OF_USAGE_" if (! $node_url || ! $dir); |
33 |
usage: $0 http://localhost:1978/node/my_dir /path/to/directory |
34 |
|
35 |
options: |
36 |
--collection="name of collection" |
37 |
--path=/path/to/add/at/end |
38 |
--exclude=regex_to_exclude |
39 |
--verbose |
40 |
--force |
41 |
--debug |
42 |
_END_OF_USAGE_ |
43 |
|
44 |
if (! -e $dir) { |
45 |
warn "directory $dir doesn't exist, skipping\n"; |
46 |
exit 1; |
47 |
} |
48 |
|
49 |
#my $basedir = $0; |
50 |
#$basedir =~ s,/[^/]+$,/,; |
51 |
#require "$basedir/filter.pm"; |
52 |
|
53 |
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'); |
66 |
my $mm = new File::MMagic::XS(); |
67 |
|
68 |
my $iconv = new Text::Iconv('iso-8859-2', 'utf-8'); |
69 |
|
70 |
select(STDERR); $|=1; |
71 |
select(STDOUT); $|=1; |
72 |
|
73 |
my $db = new Search::Estraier::Node; |
74 |
$db->set_url($node_url); |
75 |
$db->set_auth('admin', 'admin'); |
76 |
|
77 |
find({ wanted => \&file, |
78 |
follow => 1, |
79 |
follow_skip => 2, |
80 |
no_chdir => 1, |
81 |
}, $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; |
87 |
|
88 |
sub dump_contents { |
89 |
my ($db,$contents,$mtime,$path,$size) = @_; |
90 |
|
91 |
return unless (defined($contents)); # don't die on empty files |
92 |
|
93 |
if ($exclude && $path =~ m/$exclude/i) { |
94 |
print STDERR "skip: $path\n" if ($verbose); |
95 |
return; |
96 |
} |
97 |
|
98 |
use bytes; |
99 |
if (! $size) { |
100 |
$size = length $contents; |
101 |
} |
102 |
|
103 |
print STDERR " [$size]" if ($verbose); |
104 |
|
105 |
# create a document object |
106 |
my $doc = new Search::Estraier::Document; |
107 |
|
108 |
my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is); |
109 |
|
110 |
# chop long titles to 100 chars |
111 |
$title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); |
112 |
# use path if no title is found |
113 |
$title ||= $path; |
114 |
|
115 |
# add attributes to the document object |
116 |
$doc->add_attr('@uri', "file:///$path"); |
117 |
$doc->add_attr('@title', $iconv->convert($title)); |
118 |
$doc->add_attr('@size', $size); |
119 |
$doc->add_attr('@mtime', $mtime); |
120 |
|
121 |
if ($contents) { |
122 |
# html2text |
123 |
$contents =~ s#<[^>]+/*>##gs; |
124 |
$contents =~ s#\s\s+# #gs; |
125 |
|
126 |
$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); |
134 |
|
135 |
# register the document object to the database |
136 |
$db->put_doc($doc); |
137 |
|
138 |
$docs++; |
139 |
|
140 |
} |
141 |
|
142 |
sub filter_to_pages { |
143 |
my ($path, $mtime, $command) = @_; |
144 |
|
145 |
print STDERR "$path {converting}" if ($verbose); |
146 |
|
147 |
open(F,"$command |") || die "can't open $command with '$path': $!"; |
148 |
my $html; |
149 |
while(<F>) { |
150 |
$html .= $_; |
151 |
} |
152 |
close(F); |
153 |
|
154 |
return if (! $html); |
155 |
|
156 |
my $file_only = $path; |
157 |
$file_only =~ s/^.*\/([^\/]+)$/$1/g; |
158 |
|
159 |
my ($pre_html,$pages,$post_html) = ('<html><head><title>$path :: page ##page_nr##</title></head><body><pre>',$html,'</pre></body></html>'); |
160 |
|
161 |
($pre_html,$pages,$post_html) = ($1,$2,$3) if ($html =~ m/^(<html>.+?<pre>)(.+)(<\/pre>.+?)$/si); |
162 |
|
163 |
if ($collection) { |
164 |
$pre_html =~ s/<title>(.+?)<\/title>/<title>$collection :: page ##page_nr##<\/title>/si; |
165 |
} else { |
166 |
$pre_html =~ s/<title>(.+?)<\/title>/<title>$1 :: page ##page_nr##<\/title>/si || |
167 |
$pre_html =~ s/<title><\/title>/<title>$file_only :: page ##page_nr##<\/title>/si; |
168 |
} |
169 |
|
170 |
# save empty entry as a placeholder |
171 |
dump_contents($db, ' ', $mtime, "$path"); |
172 |
|
173 |
my $page_nr = 1; |
174 |
foreach my $page (split(/\f/s,$pages)) { |
175 |
print STDERR " $page_nr" if ($verbose); |
176 |
my $pre_tmp = $pre_html; |
177 |
$pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s; |
178 |
dump_contents($db, $pre_tmp . $page . $post_html, $mtime, "$path#$page_nr") if ($page !~ m/^\s*$/s); |
179 |
$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 { |
229 |
|
230 |
# return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i); |
231 |
if (-f $path && $type =~ m/html/ || |
232 |
($path !~ m/\.(php|pl|txt|info|log|text)$/io) |
233 |
) { |
234 |
dump_contents($db, '', $mtime, $path, -s $path); |
235 |
return; |
236 |
} |
237 |
|
238 |
# skip index files |
239 |
return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i); |
240 |
|
241 |
open(F,"$path") || die "can't open file: $path"; |
242 |
print STDERR "$path ($type)" if ($verbose); |
243 |
while(<F>) { |
244 |
$contents .= "$_"; |
245 |
} |
246 |
$contents .= "\n\n"; |
247 |
|
248 |
#$contents = filter($contents,$collection); |
249 |
|
250 |
# add optional components to path |
251 |
$path .= " $path_add" if ($path_add); |
252 |
|
253 |
dump_contents($db, $contents, $mtime, $path); |
254 |
} |
255 |
|
256 |
print STDERR "\n" if ($verbose); |
257 |
# die "zero size content in '$path'" if (! $contents); |
258 |
|
259 |
} |
260 |
|