5 |
use File::Which; |
use File::Which; |
6 |
use HyperEstraier; |
use HyperEstraier; |
7 |
use Text::Iconv; |
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 |
my $collection; # name which will be inserted |
15 |
my $path_add; # add additional info in path |
my $path_add; # add additional info in path |
17 |
my $exclude; |
my $exclude; |
18 |
|
|
19 |
#$verbose = 1; |
#$verbose = 1; |
20 |
|
my $debug = 0; |
21 |
|
|
22 |
my $result = GetOptions( |
my $result = GetOptions( |
23 |
"collection=s" => \$collection, |
"collection=s" => \$collection, |
24 |
"path=s" => \$path_add, |
"path=s" => \$path_add, |
25 |
"verbose!" => \$verbose, |
"verbose!" => \$verbose, |
26 |
"debug!" => \$verbose, |
"debug!" => \$debug, |
27 |
"exclude=s" => \$exclude, |
"exclude=s" => \$exclude, |
28 |
|
"node=s" => \$node_url, |
29 |
); |
); |
30 |
|
|
31 |
my $dir = shift @ARGV || die "usage: $0 [dir]"; |
my $dir = shift @ARGV || die "usage: $0 [dir]"; |
36 |
|
|
37 |
my $pdftotext = which('pdftotext'); |
my $pdftotext = which('pdftotext'); |
38 |
|
|
39 |
|
#my $mm = new File::MMagic('/usr/share/misc/file/magic'); |
40 |
|
my $mm = new File::MMagic::XS(); |
41 |
|
|
42 |
my $iconv = new Text::Iconv('iso-8859-2', 'utf-8'); |
my $iconv = new Text::Iconv('iso-8859-2', 'utf-8'); |
43 |
|
|
44 |
select(STDERR); $|=1; |
select(STDERR); $|=1; |
46 |
|
|
47 |
print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose); |
print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose); |
48 |
|
|
49 |
|
my $db; |
50 |
|
if ($node_url) { |
51 |
|
$db = HyperEstraier::Node->new($node_url); |
52 |
|
$db->set_auth('admin', 'admin'); |
53 |
|
} else { |
54 |
|
# open the database |
55 |
|
$db = HyperEstraier::Database->new(); |
56 |
|
$db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
57 |
|
|
58 |
|
sub signal { |
59 |
|
my($sig) = @_; |
60 |
|
print "\nCaught a SIG$sig--syncing database and shutting down\n"; |
61 |
|
$db->sync(); |
62 |
|
exit(0); |
63 |
|
} |
64 |
|
|
65 |
# open the database |
$SIG{'INT'} = \&signal; |
66 |
my $db = HyperEstraier::Database->new(); |
$SIG{'QUIT'} = \&signal; |
|
$db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
|
|
|
|
|
sub signal { |
|
|
my($sig) = @_; |
|
|
print "\nCaught a SIG$sig--syncing database and shutting down\n"; |
|
|
$db->sync(); |
|
|
exit(0); |
|
67 |
} |
} |
68 |
|
|
|
$SIG{'INT'} = \&signal; |
|
|
$SIG{'QUIT'} = \&signal; |
|
|
|
|
69 |
find({ wanted => \&file, |
find({ wanted => \&file, |
70 |
follow => 1, |
follow => 1, |
71 |
no_chdir => 1 |
follow_skip => 2, |
72 |
|
no_chdir => 1, |
73 |
}, $dir); |
}, $dir); |
74 |
|
|
75 |
print "--- sync\n"; |
unless ($node_url) { |
76 |
$db->sync(); |
print "--- sync\n"; |
77 |
|
$db->sync(); |
78 |
|
|
79 |
print "--- optimize...\n"; |
print "--- optimize...\n"; |
80 |
$db->optimize(0); |
$db->optimize(0); |
81 |
|
} |
82 |
exit; |
exit; |
83 |
|
|
84 |
sub dump_contents($$$$) { |
sub dump_contents($$$$) { |
101 |
|
|
102 |
my $title = $1 if ($contents =~ m#<title>(.+)</title>#is); |
my $title = $1 if ($contents =~ m#<title>(.+)</title>#is); |
103 |
|
|
104 |
|
# chop long titles to 100 chars |
105 |
|
$title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); |
106 |
|
# use path if no title is found |
107 |
|
$title ||= $path; |
108 |
|
|
109 |
# add attributes to the document object |
# add attributes to the document object |
110 |
$doc->add_attr('@uri', "file:///$path"); |
$doc->add_attr('@uri', "file:///$path"); |
111 |
$doc->add_attr('@title', $title || $path); |
$doc->add_attr('@title', $iconv->convert($title)); |
112 |
$doc->add_attr('@size', $size); |
$doc->add_attr('@size', $size); |
113 |
$doc->add_attr('@mtime', $mtime); |
$doc->add_attr('@mtime', $mtime); |
114 |
|
|
121 |
# print $doc->dump_draft if ($verbose); |
# print $doc->dump_draft if ($verbose); |
122 |
|
|
123 |
# register the document object to the database |
# register the document object to the database |
124 |
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
if ($node_url) { |
125 |
|
$db->put_doc($doc); |
126 |
|
} else { |
127 |
|
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
128 |
|
} |
129 |
|
|
130 |
} |
} |
131 |
|
|
134 |
my $path = $_; |
my $path = $_; |
135 |
my $contents; |
my $contents; |
136 |
|
|
137 |
return if (-l $path); |
return if (-l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/); |
138 |
|
|
139 |
|
my $mtime = (stat($path))[9]; |
140 |
|
my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2; |
141 |
|
|
142 |
|
if ($mtime == $mtime_db) { |
143 |
|
print STDERR "# same: $path $mtime\n" if ($verbose); |
144 |
|
return; |
145 |
|
} else { |
146 |
|
print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug); |
147 |
|
} |
148 |
|
|
149 |
|
# skip files on which File::MMagic::XS croaks |
150 |
|
return if ($path =~ m#\.au$#); |
151 |
|
|
152 |
|
my $type = $mm->checktype_filename($path); |
153 |
|
$type =~ s/\s+/ /gs; |
154 |
|
|
155 |
|
print STDERR "# $path $type\n" if ($debug); |
156 |
|
|
157 |
if ($pdftotext && -f $path && $path =~ m/\.pdf$/i) { |
if ($pdftotext && -f $path && $type =~ m/pdf/i) { |
158 |
|
|
159 |
print STDERR "$path {converting}" if ($verbose); |
print STDERR "$path {converting}" if ($verbose); |
160 |
|
|
189 |
print STDERR " $page_nr" if ($verbose); |
print STDERR " $page_nr" if ($verbose); |
190 |
my $pre_tmp = $pre_html; |
my $pre_tmp = $pre_html; |
191 |
$pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s; |
$pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s; |
192 |
dump_contents($db, $pre_tmp . $page . $post_html,time(), $path) if ($page !~ m/^\s*$/s); |
dump_contents($db, $pre_tmp . $page . $post_html, $mtime, $path) if ($page !~ m/^\s*$/s); |
193 |
$page_nr++; |
$page_nr++; |
194 |
} |
} |
195 |
|
|
196 |
} else { |
} else { |
197 |
|
|
198 |
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); |
199 |
|
return unless (-f $path && $type =~ m/html/ || |
200 |
|
($type =~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io) |
201 |
|
); |
202 |
|
|
203 |
# skip index files |
# skip index files |
204 |
return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i); |
return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i); |
205 |
|
|
206 |
open(F,"$path") || die "can't open file: $path"; |
open(F,"$path") || die "can't open file: $path"; |
207 |
print STDERR "$path" if ($verbose); |
print STDERR "$path ($type)" if ($verbose); |
208 |
while(<F>) { |
while(<F>) { |
209 |
$contents .= "$_"; |
$contents .= "$_"; |
210 |
} |
} |
215 |
# add optional components to path |
# add optional components to path |
216 |
$path .= " $path_add" if ($path_add); |
$path .= " $path_add" if ($path_add); |
217 |
|
|
218 |
dump_contents($db, $contents,time(), $path); |
dump_contents($db, $contents, $mtime, $path); |
219 |
} |
} |
220 |
|
|
221 |
print STDERR "\n" if ($verbose); |
print STDERR "\n" if ($verbose); |