7 |
use lib './lib'; |
use lib './lib'; |
8 |
|
|
9 |
use WebPAC::Common 0.02; |
use WebPAC::Common 0.02; |
10 |
use WebPAC::Lookup; |
use WebPAC::Lookup 0.03; |
11 |
use WebPAC::Input 0.03; |
use WebPAC::Input 0.07; |
12 |
use WebPAC::Store 0.03; |
use WebPAC::Store 0.03; |
13 |
use WebPAC::Normalize; |
use WebPAC::Normalize 0.11; |
14 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
15 |
use WebPAC::Validate; |
use WebPAC::Validate; |
16 |
|
use WebPAC::Output::MARC; |
17 |
use YAML qw/LoadFile/; |
use YAML qw/LoadFile/; |
18 |
use Getopt::Long; |
use Getopt::Long; |
19 |
use File::Path; |
use File::Path; |
20 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
21 |
use File::Slurp; |
use File::Slurp; |
|
use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net |
|
|
use MARC::Lint; |
|
22 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
23 |
|
use Storable qw/dclone/; |
24 |
|
|
25 |
|
use Proc::Queue size => 1; |
26 |
|
use POSIX ":sys_wait_h"; # imports WNOHANG |
27 |
|
|
28 |
=head1 NAME |
=head1 NAME |
29 |
|
|
84 |
|
|
85 |
Force dump or input and marc record for debugging. |
Force dump or input and marc record for debugging. |
86 |
|
|
87 |
|
=item --parallel 4 |
88 |
|
|
89 |
|
Run databases in parallel (aproximatly same as number of processors in |
90 |
|
machine if you want to use full load) |
91 |
|
|
92 |
|
=item --only-links |
93 |
|
|
94 |
|
Create just links |
95 |
|
|
96 |
|
=item --merge |
97 |
|
|
98 |
|
Create merged index of databases which have links |
99 |
|
|
100 |
=back |
=back |
101 |
|
|
102 |
=cut |
=cut |
113 |
my ($marc_normalize, $marc_output); |
my ($marc_normalize, $marc_output); |
114 |
my $marc_lint = 1; |
my $marc_lint = 1; |
115 |
my $marc_dump = 0; |
my $marc_dump = 0; |
116 |
|
my $parallel = 0; |
117 |
|
my $only_links = 0; |
118 |
|
my $merge = 0; |
119 |
|
|
120 |
GetOptions( |
GetOptions( |
121 |
"limit=i" => \$limit, |
"limit=i" => \$limit, |
131 |
"marc-output=s" => \$marc_output, |
"marc-output=s" => \$marc_output, |
132 |
"marc-lint!" => \$marc_lint, |
"marc-lint!" => \$marc_lint, |
133 |
"marc-dump!" => \$marc_dump, |
"marc-dump!" => \$marc_dump, |
134 |
|
"parallel=i" => \$parallel, |
135 |
|
"only-links!" => \$only_links, |
136 |
|
"merge" => \$merge, |
137 |
); |
); |
138 |
|
|
139 |
$config = LoadFile($config); |
$config = LoadFile($config); |
145 |
my $log = _new WebPAC::Common()->_get_logger(); |
my $log = _new WebPAC::Common()->_get_logger(); |
146 |
$log->info( "-" x 79 ); |
$log->info( "-" x 79 ); |
147 |
|
|
148 |
|
|
149 |
|
my $estcmd_fh; |
150 |
|
my $estcmd_path = './estcmd-merge.sh'; |
151 |
|
if ($merge) { |
152 |
|
open($estcmd_fh, '>', $estcmd_path) || $log->logdie("can't open $estcmd_path: $!"); |
153 |
|
print $estcmd_fh 'cd /data/estraier/_node/ || exit 1',$/; |
154 |
|
print $estcmd_fh 'sudo /etc/init.d/hyperestraier stop',$/; |
155 |
|
$log->info("created merge batch file $estcmd_path"); |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
my $validate; |
my $validate; |
160 |
$validate = new WebPAC::Validate( |
$validate = new WebPAC::Validate( |
161 |
path => $validate_path, |
path => $validate_path, |
162 |
) if ($validate_path); |
) if ($validate_path); |
163 |
|
|
164 |
|
|
165 |
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
166 |
if ($stats) { |
if ($stats) { |
167 |
$log->debug("option --stats disables update of indexing engine..."); |
$log->debug("option --stats disables update of indexing engine..."); |
177 |
my $start_t = time(); |
my $start_t = time(); |
178 |
|
|
179 |
my @links; |
my @links; |
|
my $indexer; |
|
180 |
|
|
181 |
my $lint = new MARC::Lint if ($marc_lint); |
if ($parallel) { |
182 |
|
$log->info("Using $parallel processes for speedup"); |
183 |
|
Proc::Queue::size($parallel); |
184 |
|
} |
185 |
|
|
186 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
187 |
|
|
188 |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
189 |
next if ($only_database && $database !~ m/$only_database/i); |
next if ($only_database && $database !~ m/$only_database/i); |
190 |
|
|
191 |
|
if ($parallel) { |
192 |
|
my $f=fork; |
193 |
|
if(defined ($f) and $f==0) { |
194 |
|
$log->info("Created processes $$ for speedup"); |
195 |
|
} else { |
196 |
|
next; |
197 |
|
} |
198 |
|
} |
199 |
|
|
200 |
|
my $indexer; |
201 |
if ($use_indexer) { |
if ($use_indexer) { |
202 |
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
203 |
$indexer_config->{database} = $database; |
$indexer_config->{database} = $database; |
204 |
$indexer_config->{clean} = $clean; |
$indexer_config->{clean} = $clean; |
205 |
$indexer_config->{label} = $db_config->{name}; |
$indexer_config->{label} = $db_config->{name}; |
206 |
|
|
207 |
|
# force clean if database has links |
208 |
|
$indexer_config->{clean} = 1 if ($db_config->{links}); |
209 |
|
|
210 |
if ($use_indexer eq 'hyperestraier') { |
if ($use_indexer eq 'hyperestraier') { |
211 |
|
|
212 |
# open Hyper Estraier database |
# open Hyper Estraier database |
229 |
|
|
230 |
|
|
231 |
# |
# |
232 |
|
# store Hyper Estraier links to other databases |
233 |
|
# |
234 |
|
if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { |
235 |
|
foreach my $link (@{ $db_config->{links} }) { |
236 |
|
if ($use_indexer eq 'hyperestraier') { |
237 |
|
if ($merge) { |
238 |
|
print $estcmd_fh 'sudo -u www-data estcmd merge ' . $database . ' ' . $link->{to},$/; |
239 |
|
} else { |
240 |
|
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
241 |
|
push @links, sub { |
242 |
|
$log->info("adding link $database -> $link->{to} [$link->{credit}]"); |
243 |
|
$indexer->add_link( |
244 |
|
from => $database, |
245 |
|
to => $link->{to}, |
246 |
|
credit => $link->{credit}, |
247 |
|
); |
248 |
|
}; |
249 |
|
} |
250 |
|
} else { |
251 |
|
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
252 |
|
} |
253 |
|
} |
254 |
|
} |
255 |
|
next if ($only_links); |
256 |
|
|
257 |
|
|
258 |
|
# |
259 |
# now WebPAC::Store |
# now WebPAC::Store |
260 |
# |
# |
261 |
my $abs_path = abs_path($0); |
my $abs_path = abs_path($0); |
300 |
|
|
301 |
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
302 |
|
|
303 |
my $lookup = new WebPAC::Lookup( |
my $lookup; |
304 |
lookup_file => $input->{lookup}, |
if ($input->{lookup}) { |
305 |
) if ($input->{lookup}); |
$lookup = new WebPAC::Lookup( |
306 |
|
lookup_file => $input->{lookup}, |
307 |
|
); |
308 |
|
delete( $input->{lookup} ); |
309 |
|
} |
310 |
|
|
311 |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
312 |
|
|
316 |
|
|
317 |
my $input_db = new WebPAC::Input( |
my $input_db = new WebPAC::Input( |
318 |
module => $input_module, |
module => $input_module, |
319 |
code_page => $config->{webpac}->{webpac_encoding}, |
encoding => $config->{webpac}->{webpac_encoding}, |
320 |
limit => $limit || $input->{limit}, |
limit => $limit || $input->{limit}, |
321 |
offset => $offset, |
offset => $offset, |
322 |
lookup => $lookup, |
lookup_coderef => sub { |
323 |
|
my $rec = shift || return; |
324 |
|
$lookup->add( $rec ); |
325 |
|
}, |
326 |
recode => $input->{recode}, |
recode => $input->{recode}, |
327 |
stats => $stats, |
stats => $stats, |
328 |
|
modify_records => $input->{modify_records}, |
329 |
); |
); |
330 |
$log->logdie("can't create input using $input_module") unless ($input); |
$log->logdie("can't create input using $input_module") unless ($input); |
331 |
|
|
355 |
|
|
356 |
$log->info("Using $normalize_path for normalization..."); |
$log->info("Using $normalize_path for normalization..."); |
357 |
|
|
358 |
my $marc_fh; |
my $marc = new WebPAC::Output::MARC( |
359 |
if (my $path = $normalize->{output}) { |
path => $normalize->{output}, |
360 |
open($marc_fh, '>', $path) || |
lint => $marc_lint, |
361 |
$log->logdie("can't open MARC output $path: $!"); |
dump => $marc_dump, |
362 |
|
) if ($normalize->{output}); |
|
$log->info("Creating MARC export file $path", $marc_lint ? ' (with lint)' : '', "\n"); |
|
|
} |
|
363 |
|
|
364 |
# reset position in database |
# reset position in database |
365 |
$input_db->seek(1); |
$input_db->seek(1); |
382 |
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
383 |
} |
} |
384 |
|
|
385 |
|
my $ds_config = dclone($db_config); |
386 |
|
|
387 |
|
# default values -> database key |
388 |
|
$ds_config->{_} = $database; |
389 |
|
|
390 |
|
# current mfn |
391 |
|
$ds_config->{_mfn} = $mfn; |
392 |
|
|
393 |
|
# attach current input |
394 |
|
$ds_config->{input} = $input; |
395 |
|
|
396 |
my $ds = WebPAC::Normalize::data_structure( |
my $ds = WebPAC::Normalize::data_structure( |
397 |
row => $row, |
row => $row, |
398 |
rules => $rules, |
rules => $rules, |
399 |
lookup => $lookup ? $lookup->lookup_hash : undef, |
lookup => $lookup ? $lookup->lookup_hash : undef, |
400 |
|
config => $ds_config, |
401 |
marc_encoding => 'utf-8', |
marc_encoding => 'utf-8', |
402 |
); |
); |
403 |
|
|
413 |
type => $config->{$use_indexer}->{type}, |
type => $config->{$use_indexer}->{type}, |
414 |
) if ($indexer && $ds); |
) if ($indexer && $ds); |
415 |
|
|
416 |
if ($marc_fh) { |
if ($marc) { |
417 |
my $marc = new MARC::Record; |
my $i = 0; |
|
$marc->encoding( 'utf-8' ); |
|
|
my @marc_fields = WebPAC::Normalize::_get_marc_fields(); |
|
|
if (! @marc_fields) { |
|
|
$log->warn("MARC record $mfn is empty, skipping"); |
|
|
} else { |
|
|
$marc->add_fields( @marc_fields ); |
|
|
|
|
|
if ($marc_lint) { |
|
|
$lint->check_record( $marc ); |
|
|
my $err = join( "\n", $lint->warnings ); |
|
|
$log->error("MARC lint detected warning on MFN $mfn\n", |
|
|
"Original imput row: ",dump($row), "\n", |
|
|
"Normalized MARC row: ",dump(@marc_fields), "\n", |
|
|
"MARC lint warnings: ",$err,"\n" |
|
|
) if ($err); |
|
|
} |
|
|
|
|
|
if ($marc_dump) { |
|
|
$log->info("MARC record on MFN $mfn\n", |
|
|
"Original imput row: ",dump($row), "\n", |
|
|
"Normalized MARC row: ",dump(@marc_fields), "\n", |
|
|
); |
|
|
} |
|
418 |
|
|
419 |
print $marc_fh $marc->as_usmarc; |
while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) { |
420 |
|
$marc->add( |
421 |
|
id => $mfn . ( $i ? "/$i" : '' ), |
422 |
|
fields => $fields, |
423 |
|
leader => WebPAC::Normalize::marc_leader(), |
424 |
|
row => $row, |
425 |
|
); |
426 |
|
$i++; |
427 |
} |
} |
428 |
|
|
429 |
|
$log->info("Created $i instances of MFN $mfn\n") if ($i > 1); |
430 |
} |
} |
431 |
|
|
432 |
$total_rows++; |
$total_rows++; |
435 |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
436 |
|
|
437 |
# close MARC file |
# close MARC file |
438 |
close($marc_fh) if ($marc_fh); |
$marc->finish if ($marc); |
439 |
|
|
440 |
} |
} |
441 |
|
|
450 |
) |
) |
451 |
); |
); |
452 |
|
|
453 |
# |
|
454 |
# add Hyper Estraier links to other databases |
# end forked process |
455 |
# |
if ($parallel) { |
456 |
if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { |
$log->info("parallel process $$ finished"); |
457 |
foreach my $link (@{ $db_config->{links} }) { |
exit(0); |
|
if ($use_indexer eq 'hyperestraier') { |
|
|
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
|
|
push @links, { |
|
|
from => $database, |
|
|
to => $link->{to}, |
|
|
credit => $link->{credit}, |
|
|
}; |
|
|
} else { |
|
|
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
|
|
} |
|
|
} |
|
458 |
} |
} |
459 |
|
|
460 |
} |
} |
461 |
|
|
462 |
foreach my $link (@links) { |
if ($parallel) { |
463 |
$log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]"); |
# wait all children to finish |
464 |
$indexer->add_link( %{ $link } ); |
sleep(1) while wait != -1; |
465 |
|
$log->info("all parallel processes finished"); |
466 |
|
} |
467 |
|
|
468 |
|
# |
469 |
|
# handle links or merge after indexing |
470 |
|
# |
471 |
|
|
472 |
|
if ($merge) { |
473 |
|
print $estcmd_fh 'sudo /etc/init.d/hyperestraier start',$/; |
474 |
|
close($estcmd_fh); |
475 |
|
chmod 0700, $estcmd_path || $log->warn("can't chmod 0700 $estcmd_path: $!"); |
476 |
|
system $estcmd_path; |
477 |
|
} else { |
478 |
|
foreach my $link (@links) { |
479 |
|
$log->logdie("coderef in link ", Dumper($link), " is ", ref($link), " and not CODE") unless (ref($link) eq 'CODE'); |
480 |
|
$link->(); |
481 |
|
} |
482 |
} |
} |