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