1 |
dpavlin |
64 |
#!/usr/bin/perl -w |
2 |
|
|
use strict; |
3 |
|
|
use File::Find; |
4 |
|
|
use Getopt::Long; |
5 |
|
|
use File::Which; |
6 |
|
|
use HyperEstraier; |
7 |
|
|
use Search::Estraier; |
8 |
|
|
use Text::Iconv; |
9 |
|
|
#use File::MMagic; |
10 |
|
|
use File::MMagic::XS qw/:compat/; |
11 |
|
|
|
12 |
|
|
# do we use Node API? |
13 |
|
|
my $node_url; |
14 |
|
|
|
15 |
|
|
my $collection; # name which will be inserted |
16 |
|
|
my $path_add; # add additional info in path |
17 |
|
|
my $verbose; |
18 |
|
|
my $exclude; |
19 |
|
|
|
20 |
|
|
#$verbose = 1; |
21 |
|
|
my $debug = 0; |
22 |
|
|
my $force = 0; |
23 |
|
|
my $native = 0; |
24 |
|
|
|
25 |
|
|
my $result = GetOptions( |
26 |
|
|
"collection=s" => \$collection, |
27 |
|
|
"path=s" => \$path_add, |
28 |
|
|
"verbose!" => \$verbose, |
29 |
|
|
"debug!" => \$debug, |
30 |
|
|
"exclude=s" => \$exclude, |
31 |
|
|
"node=s" => \$node_url, |
32 |
|
|
"force!" => \$force, |
33 |
|
|
"native!" => \$native, |
34 |
|
|
); |
35 |
|
|
|
36 |
|
|
my $dir = shift @ARGV || die "usage: $0 [dir]"; |
37 |
|
|
|
38 |
|
|
if (! -e $dir) { |
39 |
|
|
warn "directory $dir doesn't exist, skipping\n"; |
40 |
|
|
exit 1; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
#my $basedir = $0; |
44 |
|
|
#$basedir =~ s,/[^/]+$,/,; |
45 |
|
|
#require "$basedir/filter.pm"; |
46 |
|
|
|
47 |
|
|
my $pdftotext = which('pdftotext'); |
48 |
|
|
|
49 |
|
|
#my $mm = new File::MMagic('/usr/share/misc/file/magic'); |
50 |
|
|
my $mm = new File::MMagic::XS(); |
51 |
|
|
|
52 |
|
|
my $iconv = new Text::Iconv('iso-8859-2', 'utf-8'); |
53 |
|
|
|
54 |
|
|
select(STDERR); $|=1; |
55 |
|
|
select(STDOUT); $|=1; |
56 |
|
|
|
57 |
|
|
print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose); |
58 |
|
|
|
59 |
|
|
my $db; |
60 |
|
|
if ($node_url) { |
61 |
|
|
if ($native) { |
62 |
|
|
$db = HyperEstraier::Node->new($node_url); |
63 |
|
|
} else { |
64 |
|
|
$db = new Search::Estraier::Node; |
65 |
|
|
$db->set_url($node_url); |
66 |
|
|
} |
67 |
|
|
$db->set_auth('admin', 'admin'); |
68 |
|
|
} else { |
69 |
|
|
# open the database |
70 |
|
|
$db = HyperEstraier::Database->new(); |
71 |
|
|
$db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
72 |
|
|
|
73 |
|
|
sub signal { |
74 |
|
|
my($sig) = @_; |
75 |
|
|
print "\nCaught a SIG$sig--syncing database and shutting down\n"; |
76 |
|
|
$db->sync(); |
77 |
|
|
exit(0); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
$SIG{'INT'} = \&signal; |
81 |
|
|
$SIG{'QUIT'} = \&signal; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
find({ wanted => \&file, |
85 |
|
|
follow => 1, |
86 |
|
|
follow_skip => 2, |
87 |
|
|
no_chdir => 1, |
88 |
|
|
}, $dir); |
89 |
|
|
|
90 |
|
|
unless ($node_url) { |
91 |
|
|
print "--- sync\n"; |
92 |
|
|
$db->sync(); |
93 |
|
|
|
94 |
|
|
print "--- optimize...\n"; |
95 |
|
|
$db->optimize(0); |
96 |
|
|
} |
97 |
|
|
exit; |
98 |
|
|
|
99 |
|
|
sub dump_contents($$$$) { |
100 |
|
|
my ($db,$contents,$mtime,$path) = @_; |
101 |
|
|
|
102 |
|
|
return unless ($contents); # don't die on empty files |
103 |
|
|
|
104 |
|
|
if ($exclude && $path =~ m/$exclude/i) { |
105 |
|
|
print STDERR "skip: $path\n" if ($verbose); |
106 |
|
|
return; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
use bytes; |
110 |
|
|
my $size = length $contents; |
111 |
|
|
|
112 |
|
|
print STDERR " [$size]" if ($verbose); |
113 |
|
|
|
114 |
|
|
# create a document object |
115 |
|
|
my $doc; |
116 |
|
|
if ($native) { |
117 |
|
|
$doc = HyperEstraier::Document->new; |
118 |
|
|
} else { |
119 |
|
|
$doc = new Search::Estraier::Document; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is); |
123 |
|
|
|
124 |
|
|
# chop long titles to 100 chars |
125 |
|
|
$title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); |
126 |
|
|
# use path if no title is found |
127 |
|
|
$title ||= $path; |
128 |
|
|
|
129 |
|
|
# add attributes to the document object |
130 |
|
|
$doc->add_attr('@uri', "file:///$path"); |
131 |
|
|
$doc->add_attr('@title', $iconv->convert($title)); |
132 |
|
|
$doc->add_attr('@size', $size); |
133 |
|
|
$doc->add_attr('@mtime', $mtime); |
134 |
|
|
|
135 |
|
|
# html2text |
136 |
|
|
$contents =~ s#<[^>]+/*>##gs; |
137 |
|
|
$contents =~ s#\s\s+# #gs; |
138 |
|
|
|
139 |
|
|
$doc->add_text($iconv->convert($contents)); |
140 |
|
|
|
141 |
|
|
# print $doc->dump_draft if ($verbose); |
142 |
|
|
|
143 |
|
|
# register the document object to the database |
144 |
|
|
if ($node_url) { |
145 |
|
|
$db->put_doc($doc); |
146 |
|
|
} else { |
147 |
|
|
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
sub file { |
153 |
|
|
|
154 |
|
|
my $path = $_; |
155 |
|
|
my $contents; |
156 |
|
|
|
157 |
|
|
return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/); |
158 |
|
|
|
159 |
|
|
my $mtime = (stat($path))[9] || -1; |
160 |
|
|
my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2; |
161 |
|
|
|
162 |
|
|
if ($mtime == $mtime_db) { |
163 |
|
|
print STDERR "# same: $path $mtime\n" if ($verbose); |
164 |
|
|
return unless($force); |
165 |
|
|
} else { |
166 |
|
|
print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
# skip files on which File::MMagic::XS croaks |
170 |
|
|
return if ($path =~ m#\.au$#); |
171 |
|
|
|
172 |
|
|
my $type = $mm->checktype_filename($path); |
173 |
|
|
$type =~ s/\s+/ /gs; |
174 |
|
|
|
175 |
|
|
print STDERR "# $path $type\n" if ($debug); |
176 |
|
|
|
177 |
|
|
if ($pdftotext && -f $path && $type =~ m/pdf/i) { |
178 |
|
|
|
179 |
|
|
print STDERR "$path {converting}" if ($verbose); |
180 |
|
|
|
181 |
|
|
open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'"; |
182 |
|
|
my $html; |
183 |
|
|
while(<F>) { |
184 |
|
|
# XXX why pdftotext barks if I try to use this is beyond me. |
185 |
|
|
#$contents .= $_; |
186 |
|
|
|
187 |
|
|
$html .= $_; |
188 |
|
|
} |
189 |
|
|
close(F); |
190 |
|
|
|
191 |
|
|
return if (! $html); |
192 |
|
|
|
193 |
|
|
my $file_only = $path; |
194 |
|
|
$file_only =~ s/^.*\/([^\/]+)$/$1/g; |
195 |
|
|
|
196 |
|
|
my ($pre_html,$pages,$post_html) = ('<html><head><title>$path :: page ##page_nr##</title></head><body><pre>',$html,'</pre></body></html>'); |
197 |
|
|
|
198 |
|
|
($pre_html,$pages,$post_html) = ($1,$2,$3) if ($html =~ m/^(<html>.+?<pre>)(.+)(<\/pre>.+?)$/si); |
199 |
|
|
|
200 |
|
|
if ($collection) { |
201 |
|
|
$pre_html =~ s/<title>(.+?)<\/title>/<title>$collection :: page ##page_nr##<\/title>/si; |
202 |
|
|
} else { |
203 |
|
|
$pre_html =~ s/<title>(.+?)<\/title>/<title>$1 :: page ##page_nr##<\/title>/si || |
204 |
|
|
$pre_html =~ s/<title><\/title>/<title>$file_only :: page ##page_nr##<\/title>/si; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
# save empty entry as a placeholder |
208 |
|
|
dump_contents($db, ' ', $mtime, "$path"); |
209 |
|
|
|
210 |
|
|
my $page_nr = 1; |
211 |
|
|
foreach my $page (split(/\f/s,$pages)) { |
212 |
|
|
print STDERR " $page_nr" if ($verbose); |
213 |
|
|
my $pre_tmp = $pre_html; |
214 |
|
|
$pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s; |
215 |
|
|
dump_contents($db, $pre_tmp . $page . $post_html, $mtime, "$path#$page_nr") if ($page !~ m/^\s*$/s); |
216 |
|
|
$page_nr++; |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
} else { |
220 |
|
|
|
221 |
|
|
# return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i); |
222 |
dpavlin |
77 |
if (-f $path && $type =~ m/html/ || |
223 |
|
|
($type !~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io) |
224 |
|
|
) { |
225 |
|
|
dump_contents($db, "<title>$path</title> $type", $mtime, $path); |
226 |
|
|
} |
227 |
dpavlin |
64 |
|
228 |
|
|
# skip index files |
229 |
|
|
return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i); |
230 |
|
|
|
231 |
|
|
open(F,"$path") || die "can't open file: $path"; |
232 |
|
|
print STDERR "$path ($type)" if ($verbose); |
233 |
|
|
while(<F>) { |
234 |
|
|
$contents .= "$_"; |
235 |
|
|
} |
236 |
|
|
$contents .= "\n\n"; |
237 |
|
|
|
238 |
|
|
#$contents = filter($contents,$collection); |
239 |
|
|
|
240 |
|
|
# add optional components to path |
241 |
|
|
$path .= " $path_add" if ($path_add); |
242 |
|
|
|
243 |
|
|
dump_contents($db, $contents, $mtime, $path); |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
print STDERR "\n" if ($verbose); |
247 |
|
|
# die "zero size content in '$path'" if (! $contents); |
248 |
|
|
|
249 |
|
|
} |
250 |
|
|
|