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 |
|
my $force = 0; |
22 |
|
|
23 |
my $result = GetOptions( |
my $result = GetOptions( |
24 |
"collection=s" => \$collection, |
"collection=s" => \$collection, |
25 |
"path=s" => \$path_add, |
"path=s" => \$path_add, |
26 |
"verbose!" => \$verbose, |
"verbose!" => \$verbose, |
27 |
"debug!" => \$verbose, |
"debug!" => \$debug, |
28 |
"exclude=s" => \$exclude, |
"exclude=s" => \$exclude, |
29 |
|
"node=s" => \$node_url, |
30 |
|
"force!" => \$force, |
31 |
); |
); |
32 |
|
|
33 |
my $dir = shift @ARGV || die "usage: $0 [dir]"; |
my $dir = shift @ARGV || die "usage: $0 [dir]"; |
34 |
|
|
35 |
|
if (! -e $dir) { |
36 |
|
warn "directory $dir doesn't exist, skipping\n"; |
37 |
|
exit 1; |
38 |
|
} |
39 |
|
|
40 |
#my $basedir = $0; |
#my $basedir = $0; |
41 |
#$basedir =~ s,/[^/]+$,/,; |
#$basedir =~ s,/[^/]+$,/,; |
42 |
#require "$basedir/filter.pm"; |
#require "$basedir/filter.pm"; |
43 |
|
|
44 |
my $pdftotext = which('pdftotext'); |
my $pdftotext = which('pdftotext'); |
45 |
|
|
46 |
|
#my $mm = new File::MMagic('/usr/share/misc/file/magic'); |
47 |
|
my $mm = new File::MMagic::XS(); |
48 |
|
|
49 |
my $iconv = new Text::Iconv('iso-8859-2', 'utf-8'); |
my $iconv = new Text::Iconv('iso-8859-2', 'utf-8'); |
50 |
|
|
51 |
select(STDERR); $|=1; |
select(STDERR); $|=1; |
53 |
|
|
54 |
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); |
55 |
|
|
56 |
|
my $db; |
57 |
|
if ($node_url) { |
58 |
|
$db = HyperEstraier::Node->new($node_url); |
59 |
|
$db->set_auth('admin', 'admin'); |
60 |
|
} else { |
61 |
|
# open the database |
62 |
|
$db = HyperEstraier::Database->new(); |
63 |
|
$db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
64 |
|
|
65 |
|
sub signal { |
66 |
|
my($sig) = @_; |
67 |
|
print "\nCaught a SIG$sig--syncing database and shutting down\n"; |
68 |
|
$db->sync(); |
69 |
|
exit(0); |
70 |
|
} |
71 |
|
|
72 |
# open the database |
$SIG{'INT'} = \&signal; |
73 |
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); |
|
74 |
} |
} |
75 |
|
|
|
$SIG{'INT'} = \&signal; |
|
|
$SIG{'QUIT'} = \&signal; |
|
|
|
|
76 |
find({ wanted => \&file, |
find({ wanted => \&file, |
77 |
follow => 1, |
follow => 1, |
78 |
no_chdir => 1 |
follow_skip => 2, |
79 |
|
no_chdir => 1, |
80 |
}, $dir); |
}, $dir); |
81 |
|
|
82 |
print "--- sync\n"; |
unless ($node_url) { |
83 |
$db->sync(); |
print "--- sync\n"; |
84 |
|
$db->sync(); |
85 |
|
|
86 |
print "--- optimize...\n"; |
print "--- optimize...\n"; |
87 |
$db->optimize(0); |
$db->optimize(0); |
88 |
|
} |
89 |
exit; |
exit; |
90 |
|
|
91 |
sub dump_contents($$$$) { |
sub dump_contents($$$$) { |
106 |
# create a document object |
# create a document object |
107 |
my $doc = HyperEstraier::Document->new; |
my $doc = HyperEstraier::Document->new; |
108 |
|
|
109 |
my $title = $1 if ($contents =~ m#<title>(.+)</title>#is); |
my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is); |
110 |
|
|
111 |
|
# chop long titles to 100 chars |
112 |
|
$title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); |
113 |
|
# use path if no title is found |
114 |
|
$title ||= $path; |
115 |
|
|
116 |
# add attributes to the document object |
# add attributes to the document object |
117 |
$doc->add_attr('@uri', "file:///$path"); |
$doc->add_attr('@uri', "file:///$path"); |
118 |
$doc->add_attr('@title', $title || $path); |
$doc->add_attr('@title', $iconv->convert($title)); |
119 |
$doc->add_attr('@size', $size); |
$doc->add_attr('@size', $size); |
120 |
$doc->add_attr('@mtime', $mtime); |
$doc->add_attr('@mtime', $mtime); |
121 |
|
|
128 |
# print $doc->dump_draft if ($verbose); |
# print $doc->dump_draft if ($verbose); |
129 |
|
|
130 |
# register the document object to the database |
# register the document object to the database |
131 |
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
if ($node_url) { |
132 |
|
$db->put_doc($doc); |
133 |
|
} else { |
134 |
|
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
135 |
|
} |
136 |
|
|
137 |
} |
} |
138 |
|
|
141 |
my $path = $_; |
my $path = $_; |
142 |
my $contents; |
my $contents; |
143 |
|
|
144 |
return if (-l $path); |
return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/); |
145 |
|
|
146 |
|
my $mtime = (stat($path))[9] || -1; |
147 |
|
my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2; |
148 |
|
|
149 |
|
if ($mtime == $mtime_db) { |
150 |
|
print STDERR "# same: $path $mtime\n" if ($verbose); |
151 |
|
return unless($force); |
152 |
|
} else { |
153 |
|
print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug); |
154 |
|
} |
155 |
|
|
156 |
|
# skip files on which File::MMagic::XS croaks |
157 |
|
return if ($path =~ m#\.au$#); |
158 |
|
|
159 |
|
my $type = $mm->checktype_filename($path); |
160 |
|
$type =~ s/\s+/ /gs; |
161 |
|
|
162 |
|
print STDERR "# $path $type\n" if ($debug); |
163 |
|
|
164 |
if ($pdftotext && -f $path && $path =~ m/\.pdf$/i) { |
if ($pdftotext && -f $path && $type =~ m/pdf/i) { |
165 |
|
|
166 |
print STDERR "$path {converting}" if ($verbose); |
print STDERR "$path {converting}" if ($verbose); |
167 |
|
|
196 |
print STDERR " $page_nr" if ($verbose); |
print STDERR " $page_nr" if ($verbose); |
197 |
my $pre_tmp = $pre_html; |
my $pre_tmp = $pre_html; |
198 |
$pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s; |
$pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s; |
199 |
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#$page_nr") if ($page !~ m/^\s*$/s); |
200 |
$page_nr++; |
$page_nr++; |
201 |
} |
} |
202 |
|
|
203 |
} else { |
} else { |
204 |
|
|
205 |
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); |
206 |
|
return unless (-f $path && $type =~ m/html/ || |
207 |
|
($type =~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io) |
208 |
|
); |
209 |
|
|
210 |
# skip index files |
# skip index files |
211 |
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); |
212 |
|
|
213 |
open(F,"$path") || die "can't open file: $path"; |
open(F,"$path") || die "can't open file: $path"; |
214 |
print STDERR "$path" if ($verbose); |
print STDERR "$path ($type)" if ($verbose); |
215 |
while(<F>) { |
while(<F>) { |
216 |
$contents .= "$_"; |
$contents .= "$_"; |
217 |
} |
} |
222 |
# add optional components to path |
# add optional components to path |
223 |
$path .= " $path_add" if ($path_add); |
$path .= " $path_add" if ($path_add); |
224 |
|
|
225 |
dump_contents($db, $contents,time(), $path); |
dump_contents($db, $contents, $mtime, $path); |
226 |
} |
} |
227 |
|
|
228 |
print STDERR "\n" if ($verbose); |
print STDERR "\n" if ($verbose); |