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