7 |
use Data::Dumper; |
use Data::Dumper; |
8 |
use lib './lib'; |
use lib './lib'; |
9 |
|
|
10 |
|
use WebPAC::Common 0.02; |
11 |
use WebPAC::Lookup; |
use WebPAC::Lookup; |
12 |
use WebPAC::Input::ISIS; |
use WebPAC::Input 0.03; |
13 |
use WebPAC::DB 0.02; |
use WebPAC::Store 0.03; |
14 |
use WebPAC::Normalize::XML; |
use WebPAC::Normalize::XML; |
15 |
|
use WebPAC::Normalize::Set; |
16 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
17 |
use WebPAC::Output::Estraier; |
use YAML qw/LoadFile/; |
18 |
|
use Getopt::Long; |
19 |
|
use File::Path; |
20 |
|
use Time::HiRes qw/time/; |
21 |
|
use File::Slurp; |
22 |
|
|
23 |
my $limit = shift @ARGV; |
=head1 NAME |
24 |
|
|
25 |
my $abs_path = abs_path($0); |
run.pl - start WebPAC indexing |
|
$abs_path =~ s#/[^/]*$#/#; |
|
26 |
|
|
27 |
my $isis_file = '/data/isis_data/ps/LIBRI/LIBRI'; |
B<this command will probably go away. Don't get used to it!> |
28 |
|
|
29 |
my $lookup = new WebPAC::Lookup( |
Options: |
|
lookup_file => "$abs_path/conf/lookup/isis.pm", |
|
|
); |
|
30 |
|
|
31 |
my $isis = new WebPAC::Input::ISIS( |
=over 4 |
|
code_page => 'ISO-8859-2', # application encoding |
|
|
limit_mfn => $limit, |
|
|
); |
|
32 |
|
|
33 |
my $maxmfn = $isis->open( |
=item --offset 42 |
|
filename => $isis_file, |
|
|
code_page => '852', # database encoding |
|
|
); |
|
34 |
|
|
35 |
my $path = './db/'; |
start loading (all) databases at offset 42 |
36 |
|
|
37 |
my $db = new WebPAC::DB( |
=item --limit 100 |
|
path => $path, |
|
|
); |
|
38 |
|
|
39 |
my $n = new WebPAC::Normalize::XML( |
limit loading to 100 records |
|
# filter => { 'foo' => sub { shift } }, |
|
|
db => $db, |
|
|
lookup_regex => $lookup->regex, |
|
|
lookup => $lookup, |
|
|
); |
|
40 |
|
|
41 |
$n->open( |
=item --clean |
|
tag => 'isis', |
|
|
xml_file => "$abs_path/conf/normalize/isis_ffzg.xml", |
|
|
); |
|
42 |
|
|
43 |
my $out = new WebPAC::Output::TT( |
remove database and Hyper Estraier index before indexing |
44 |
include_path => "$abs_path/conf/output/tt", |
|
45 |
filters => { foo => sub { shift } }, |
=item --only=database_name |
46 |
); |
|
47 |
|
reindex just single database (legacy name is --one) |
48 |
|
|
49 |
|
=item --config conf/config.yml |
50 |
|
|
51 |
|
path to YAML configuration file |
52 |
|
|
53 |
|
=item --force-set |
54 |
|
|
55 |
|
force conversion C<normalize->path> in C<config.yml> from |
56 |
|
C<.xml> to C<.pl> |
57 |
|
|
58 |
|
=back |
59 |
|
|
60 |
|
=cut |
61 |
|
|
62 |
|
my $offset; |
63 |
|
my $limit; |
64 |
|
|
65 |
|
my $clean = 0; |
66 |
|
my $config = 'conf/config.yml'; |
67 |
|
my $debug = 0; |
68 |
|
my $only_db_name; |
69 |
|
my $force_set = 0; |
70 |
|
|
71 |
my $est = new WebPAC::Output::Estraier( |
GetOptions( |
72 |
url => 'http://localhost:1978/node/webpac2', |
"limit=i" => \$limit, |
73 |
user => 'admin', |
"offset=i" => \$offset, |
74 |
passwd => 'admin', |
"clean" => \$clean, |
75 |
database => 'ps', |
"one=s" => \$only_db_name, |
76 |
|
"only=s" => \$only_db_name, |
77 |
|
"config" => \$config, |
78 |
|
"debug" => \$debug, |
79 |
|
"force-set" => \$force_set, |
80 |
); |
); |
81 |
|
|
82 |
|
$config = LoadFile($config); |
83 |
|
|
84 |
|
print "config = ",Dumper($config) if ($debug); |
85 |
|
|
86 |
|
die "no databases in config file!\n" unless ($config->{databases}); |
87 |
|
|
88 |
|
my $log = _new WebPAC::Common()->_get_logger(); |
89 |
|
|
90 |
|
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
91 |
|
$log->info("using $use_indexer indexing engine..."); |
92 |
|
|
93 |
my $total_rows = 0; |
my $total_rows = 0; |
94 |
|
my $start_t = time(); |
95 |
|
|
96 |
|
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
97 |
|
|
98 |
|
next if ($only_db_name && $database !~ m/$only_db_name/i); |
99 |
|
|
100 |
|
my $indexer; |
101 |
|
|
102 |
for ( 0 ... $isis->size ) { |
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
103 |
|
$indexer_config->{database} = $database; |
104 |
|
$indexer_config->{clean} = $clean; |
105 |
|
$indexer_config->{label} = $db_config->{name}; |
106 |
|
|
107 |
my $row = $isis->fetch || next; |
# important: clean database just once! |
108 |
|
$clean = 0; |
109 |
|
|
110 |
my $mfn = $row->{'000'}->[0] || die "can't find MFN"; |
if ($use_indexer eq 'hyperestraier') { |
111 |
|
|
112 |
my $ds = $n->data_structure($row); |
# open Hyper Estraier database |
113 |
|
use WebPAC::Output::Estraier '0.10'; |
114 |
|
$indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); |
115 |
|
|
116 |
|
} elsif ($use_indexer eq 'kinosearch') { |
117 |
|
|
118 |
# print STDERR Dumper($row, $ds); |
# open KinoSearch |
119 |
|
use WebPAC::Output::KinoSearch; |
120 |
# my $html = $out->apply( |
$indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path}); |
121 |
# template => 'html_ffzg.tt', |
$indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } ); |
122 |
# data => $ds, |
|
123 |
# ); |
} else { |
124 |
# |
$log->logdie("unknown use_indexer: $use_indexer"); |
125 |
# # create test output |
} |
126 |
# |
|
127 |
# my $file = sprintf('out/%02d.html', $mfn ); |
$log->logide("can't continue without valid indexer") unless ($indexer); |
128 |
# open(my $fh, '>', $file) or die "can't open $file: $!"; |
|
129 |
# print $fh $html; |
# |
130 |
# close($fh); |
# now WebPAC::Store |
131 |
# |
# |
132 |
# $html =~ s#\s*[\n\r]+\s*##gs; |
my $abs_path = abs_path($0); |
133 |
# |
$abs_path =~ s#/[^/]*$#/#; |
134 |
# print STDERR $html; |
|
135 |
|
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
136 |
$est->add( |
|
137 |
id => $mfn, |
if ($clean) { |
138 |
ds => $ds, |
$log->info("creating new database $database in $db_path"); |
139 |
type => 'search', |
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
140 |
|
} else { |
141 |
|
$log->debug("working on $database in $db_path"); |
142 |
|
} |
143 |
|
|
144 |
|
my $db = new WebPAC::Store( |
145 |
|
path => $db_path, |
146 |
|
database => $database, |
147 |
|
debug => $debug, |
148 |
); |
); |
149 |
|
|
|
$total_rows++; |
|
150 |
|
|
151 |
}; |
# |
152 |
|
# now, iterate through input formats |
153 |
|
# |
154 |
|
|
155 |
|
my @inputs; |
156 |
|
if (ref($db_config->{input}) eq 'ARRAY') { |
157 |
|
@inputs = @{ $db_config->{input} }; |
158 |
|
} elsif ($db_config->{input}) { |
159 |
|
push @inputs, $db_config->{input}; |
160 |
|
} else { |
161 |
|
$log->info("database $database doesn't have inputs defined"); |
162 |
|
} |
163 |
|
|
164 |
|
my @supported_inputs = keys %{ $config->{webpac}->{inputs} }; |
165 |
|
|
166 |
|
foreach my $input (@inputs) { |
167 |
|
|
168 |
|
my $type = lc($input->{type}); |
169 |
|
|
170 |
|
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
171 |
|
|
172 |
|
my $lookup = new WebPAC::Lookup( |
173 |
|
lookup_file => $input->{lookup}, |
174 |
|
); |
175 |
|
|
176 |
|
my $input_module = $config->{webpac}->{inputs}->{$type}; |
177 |
|
|
178 |
|
$log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'"); |
179 |
|
|
180 |
|
my $input_db = new WebPAC::Input( |
181 |
|
module => $input_module, |
182 |
|
code_page => $config->{webpac}->{webpac_encoding}, |
183 |
|
limit => $limit || $input->{limit}, |
184 |
|
offset => $offset, |
185 |
|
lookup => $lookup, |
186 |
|
recode => $input->{recode}, |
187 |
|
); |
188 |
|
$log->logdie("can't create input using $input_module") unless ($input); |
189 |
|
|
190 |
|
my $maxmfn = $input_db->open( |
191 |
|
path => $input->{path}, |
192 |
|
code_page => $input->{encoding}, # database encoding |
193 |
|
); |
194 |
|
|
195 |
|
my $n = new WebPAC::Normalize::XML( |
196 |
|
# filter => { 'foo' => sub { shift } }, |
197 |
|
db => $db, |
198 |
|
lookup_regex => $lookup->regex, |
199 |
|
lookup => $lookup, |
200 |
|
prefix => $input->{name}, |
201 |
|
); |
202 |
|
|
203 |
|
my $rules; |
204 |
|
my $normalize_path = $input->{normalize}->{path}; |
205 |
|
|
206 |
|
if ($force_set) { |
207 |
|
my $new_norm_path = $normalize_path; |
208 |
|
$new_norm_path =~ s/\.xml$/.pl/; |
209 |
|
if (-e $new_norm_path) { |
210 |
|
$log->info("--force-set replaced $normalize_path with $new_norm_path"); |
211 |
|
$normalize_path = $new_norm_path; |
212 |
|
} else { |
213 |
|
$log->warn("--force-set failed on $new_norm_path, fallback to $normalize_path"); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
|
if ($normalize_path =~ m/\.xml$/i) { |
218 |
|
$n->open( |
219 |
|
tag => $input->{normalize}->{tag}, |
220 |
|
xml_file => $normalize_path, |
221 |
|
); |
222 |
|
} elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) { |
223 |
|
$n->open_yaml( |
224 |
|
path => $normalize_path, |
225 |
|
tag => $input->{normalize}->{tag}, |
226 |
|
); |
227 |
|
} elsif ($normalize_path =~ m/\.(?:pl)$/i) { |
228 |
|
$n = undef; |
229 |
|
$log->info("using WebPAC::Normalize::Set to process $normalize_path"); |
230 |
|
$rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
231 |
|
} |
232 |
|
|
233 |
|
foreach my $pos ( 0 ... $input_db->size ) { |
234 |
|
|
235 |
|
my $row = $input_db->fetch || next; |
236 |
|
|
237 |
|
my $mfn = $row->{'000'}->[0]; |
238 |
|
|
239 |
|
if (! $mfn || $mfn !~ m#^\d+$#) { |
240 |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
241 |
|
$mfn = $pos; |
242 |
|
push @{ $row->{'000'} }, $pos; |
243 |
|
} |
244 |
|
|
245 |
|
my $ds = $n ? $n->data_structure($row) : |
246 |
|
WebPAC::Normalize::Set::data_structure( |
247 |
|
row => $row, |
248 |
|
rules => $rules, |
249 |
|
lookup => $lookup->lookup_hash, |
250 |
|
); |
251 |
|
|
252 |
|
$indexer->add( |
253 |
|
id => $input->{name} . "/" . $mfn, |
254 |
|
ds => $ds, |
255 |
|
type => $config->{$use_indexer}->{type}, |
256 |
|
); |
257 |
|
|
258 |
|
$total_rows++; |
259 |
|
} |
260 |
|
|
261 |
|
}; |
262 |
|
|
263 |
|
eval { $indexer->finish } if ($indexer->can('finish')); |
264 |
|
|
265 |
|
my $dt = time() - $start_t; |
266 |
|
$log->info("$total_rows records indexed in " . |
267 |
|
sprintf("%.2f sec [%.2f rec/sec]", |
268 |
|
$dt, ($total_rows / $dt) |
269 |
|
) |
270 |
|
); |
271 |
|
|
272 |
|
# |
273 |
|
# add Hyper Estraier links to other databases |
274 |
|
# |
275 |
|
if (ref($db_config->{links}) eq 'ARRAY') { |
276 |
|
foreach my $link (@{ $db_config->{links} }) { |
277 |
|
if ($use_indexer eq 'hyperestraier') { |
278 |
|
$log->info("adding link $database -> $link->{to} [$link->{credit}]"); |
279 |
|
$indexer->add_link( |
280 |
|
from => $database, |
281 |
|
to => $link->{to}, |
282 |
|
credit => $link->{credit}, |
283 |
|
); |
284 |
|
} else { |
285 |
|
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
286 |
|
} |
287 |
|
} |
288 |
|
} |
289 |
|
|
290 |
my $log = $lookup->_get_logger; |
} |
291 |
|
|
|
$log->info("$total_rows records indexed"); |
|