4 |
|
|
5 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
6 |
use File::Temp qw/tempdir/; |
use File::Temp qw/tempdir/; |
|
use Data::Dumper; |
|
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; |
11 |
use WebPAC::Input 0.03; |
use WebPAC::Input 0.03; |
12 |
use WebPAC::Store 0.03; |
use WebPAC::Store 0.03; |
13 |
use WebPAC::Normalize::XML; |
use WebPAC::Normalize; |
|
use WebPAC::Normalize::Set; |
|
14 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
15 |
use WebPAC::Validate; |
use WebPAC::Validate; |
16 |
use YAML qw/LoadFile/; |
use YAML qw/LoadFile/; |
18 |
use File::Path; |
use File::Path; |
19 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
20 |
use File::Slurp; |
use File::Slurp; |
21 |
|
use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net |
22 |
|
use MARC::Lint; |
23 |
|
use Data::Dump qw/dump/; |
24 |
|
|
25 |
=head1 NAME |
=head1 NAME |
26 |
|
|
55 |
|
|
56 |
path to YAML configuration file |
path to YAML configuration file |
57 |
|
|
|
=item --force-set |
|
|
|
|
|
force conversion C<< normalize->path >> in C<config.yml> from |
|
|
C<.xml> to C<.pl> |
|
|
|
|
58 |
=item --stats |
=item --stats |
59 |
|
|
60 |
disable indexing and dump statistics about field and subfield |
disable indexing and dump statistics about field and subfield |
64 |
|
|
65 |
turn on extra validation of imput records, see L<WebPAC::Validation> |
turn on extra validation of imput records, see L<WebPAC::Validation> |
66 |
|
|
67 |
|
=item --marc-normalize conf/normalize/mapping.pl |
68 |
|
|
69 |
|
This option specifies normalisation file for MARC creation |
70 |
|
|
71 |
|
=item --marc-output out/marc/test.marc |
72 |
|
|
73 |
|
Optional path to output file |
74 |
|
|
75 |
|
=item --marc-lint |
76 |
|
|
77 |
|
By default turned on if C<--marc-normalize> is used. You can disable lint |
78 |
|
messages with C<--no-marc-lint>. |
79 |
|
|
80 |
|
=item --marc-dump |
81 |
|
|
82 |
|
Force dump or input and marc record for debugging. |
83 |
|
|
84 |
=back |
=back |
85 |
|
|
86 |
=cut |
=cut |
92 |
my $config = 'conf/config.yml'; |
my $config = 'conf/config.yml'; |
93 |
my $debug = 0; |
my $debug = 0; |
94 |
my $only_filter; |
my $only_filter; |
|
my $force_set = 0; |
|
95 |
my $stats = 0; |
my $stats = 0; |
96 |
my $validate_path; |
my $validate_path; |
97 |
|
my ($marc_normalize, $marc_output); |
98 |
|
my $marc_lint = 1; |
99 |
|
my $marc_dump = 0; |
100 |
|
|
101 |
GetOptions( |
GetOptions( |
102 |
"limit=i" => \$limit, |
"limit=i" => \$limit, |
105 |
"one=s" => \$only_filter, |
"one=s" => \$only_filter, |
106 |
"only=s" => \$only_filter, |
"only=s" => \$only_filter, |
107 |
"config" => \$config, |
"config" => \$config, |
108 |
"debug" => \$debug, |
"debug+" => \$debug, |
|
"force-set" => \$force_set, |
|
109 |
"stats" => \$stats, |
"stats" => \$stats, |
110 |
"validate=s" => \$validate_path, |
"validate=s" => \$validate_path, |
111 |
|
"marc-normalize=s" => \$marc_normalize, |
112 |
|
"marc-output=s" => \$marc_output, |
113 |
|
"marc-lint!" => \$marc_lint, |
114 |
|
"marc-dump!" => \$marc_dump, |
115 |
); |
); |
116 |
|
|
117 |
$config = LoadFile($config); |
$config = LoadFile($config); |
118 |
|
|
119 |
print "config = ",Dumper($config) if ($debug); |
print "config = ",dump($config) if ($debug); |
120 |
|
|
121 |
die "no databases in config file!\n" unless ($config->{databases}); |
die "no databases in config file!\n" unless ($config->{databases}); |
122 |
|
|
136 |
$log->info("using $use_indexer indexing engine..."); |
$log->info("using $use_indexer indexing engine..."); |
137 |
} |
} |
138 |
|
|
139 |
|
# disable indexing when creating marc |
140 |
|
$use_indexer = undef if ($marc_normalize); |
141 |
|
|
142 |
my $total_rows = 0; |
my $total_rows = 0; |
143 |
my $start_t = time(); |
my $start_t = time(); |
144 |
|
|
145 |
|
my @links; |
146 |
|
my $indexer; |
147 |
|
|
148 |
|
my $lint = new MARC::Lint if ($marc_lint); |
149 |
|
|
150 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
151 |
|
|
152 |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
153 |
next if ($only_database && $database !~ m/$only_database/i); |
next if ($only_database && $database !~ m/$only_database/i); |
154 |
|
|
|
my $indexer; |
|
|
|
|
155 |
if ($use_indexer) { |
if ($use_indexer) { |
156 |
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"); |
157 |
$indexer_config->{database} = $database; |
$indexer_config->{database} = $database; |
230 |
|
|
231 |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
232 |
|
|
233 |
$log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module lookup '$input->{lookup}'"); |
$log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module", |
234 |
|
$input->{lookup} ? "lookup '$input->{lookup}'" : "" |
235 |
|
); |
236 |
|
|
237 |
my $input_db = new WebPAC::Input( |
my $input_db = new WebPAC::Input( |
238 |
module => $input_module, |
module => $input_module, |
248 |
my $maxmfn = $input_db->open( |
my $maxmfn = $input_db->open( |
249 |
path => $input->{path}, |
path => $input->{path}, |
250 |
code_page => $input->{encoding}, # database encoding |
code_page => $input->{encoding}, # database encoding |
251 |
|
%{ $input }, |
252 |
); |
); |
253 |
|
|
254 |
my $n = new WebPAC::Normalize::XML( |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
255 |
# filter => { 'foo' => sub { shift } }, |
@{ $input->{normalize} } : ( $input->{normalize} ); |
|
db => $db, |
|
|
lookup_regex => $lookup ? $lookup->regex : undef, |
|
|
lookup => $lookup, |
|
|
prefix => $input->{name}, |
|
|
); |
|
|
|
|
|
my $rules; |
|
|
my $normalize_path = $input->{normalize}->{path}; |
|
256 |
|
|
257 |
if ($force_set) { |
if ($marc_normalize) { |
258 |
my $new_norm_path = $normalize_path; |
@norm_array = ( { |
259 |
$new_norm_path =~ s/\.xml$/.pl/; |
path => $marc_normalize, |
260 |
if (-e $new_norm_path) { |
output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc', |
261 |
$log->debug("--force-set replaced $normalize_path with $new_norm_path"); |
} ); |
|
$normalize_path = $new_norm_path; |
|
|
} else { |
|
|
$log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path"); |
|
|
} |
|
262 |
} |
} |
263 |
|
|
264 |
if ($normalize_path =~ m/\.xml$/i) { |
foreach my $normalize (@norm_array) { |
|
$n->open( |
|
|
tag => $input->{normalize}->{tag}, |
|
|
xml_file => $normalize_path, |
|
|
); |
|
|
} elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) { |
|
|
$n->open_yaml( |
|
|
path => $normalize_path, |
|
|
tag => $input->{normalize}->{tag}, |
|
|
); |
|
|
} elsif ($normalize_path =~ m/\.(?:pl)$/i) { |
|
|
$n = undef; |
|
|
$log->info("using WebPAC::Normalize::Set to process $normalize_path"); |
|
|
$rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
|
|
} |
|
265 |
|
|
266 |
foreach my $pos ( 0 ... $input_db->size ) { |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
267 |
|
|
268 |
my $row = $input_db->fetch || next; |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
269 |
|
|
270 |
my $mfn = $row->{'000'}->[0]; |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
271 |
|
|
272 |
if (! $mfn || $mfn !~ m#^\d+$#) { |
$log->info("Using $normalize_path for normalization..."); |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
|
|
$mfn = $pos; |
|
|
push @{ $row->{'000'} }, $pos; |
|
|
} |
|
273 |
|
|
274 |
|
my $marc_fh; |
275 |
|
if (my $path = $normalize->{output}) { |
276 |
|
open($marc_fh, '>', $path) || |
277 |
|
$log->logdie("can't open MARC output $path: $!"); |
278 |
|
|
279 |
if ($validate) { |
$log->info("Creating MARC export file $path", $marc_lint ? ' (with lint)' : '', "\n"); |
|
my @errors = $validate->validate_errors( $row ); |
|
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
|
280 |
} |
} |
281 |
|
|
282 |
|
# reset position in database |
283 |
my $ds; |
$input_db->seek(1); |
284 |
if ($n) { |
|
285 |
$ds = $n->data_structure($row); |
foreach my $pos ( 0 ... $input_db->size ) { |
286 |
} else { |
|
287 |
$ds = WebPAC::Normalize::Set::data_structure( |
my $row = $input_db->fetch || next; |
288 |
|
|
289 |
|
my $mfn = $row->{'000'}->[0]; |
290 |
|
|
291 |
|
if (! $mfn || $mfn !~ m#^\d+$#) { |
292 |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
293 |
|
$mfn = $pos; |
294 |
|
push @{ $row->{'000'} }, $pos; |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
if ($validate) { |
299 |
|
my @errors = $validate->validate_errors( $row ); |
300 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
301 |
|
} |
302 |
|
|
303 |
|
my $ds = WebPAC::Normalize::data_structure( |
304 |
row => $row, |
row => $row, |
305 |
rules => $rules, |
rules => $rules, |
306 |
lookup => $lookup ? $lookup->lookup_hash : undef, |
lookup => $lookup ? $lookup->lookup_hash : undef, |
307 |
|
marc_encoding => 'utf-8', |
308 |
); |
); |
309 |
|
|
310 |
$db->save_ds( |
$db->save_ds( |
312 |
ds => $ds, |
ds => $ds, |
313 |
prefix => $input->{name}, |
prefix => $input->{name}, |
314 |
) if ($ds && !$stats); |
) if ($ds && !$stats); |
315 |
|
|
316 |
|
$indexer->add( |
317 |
|
id => $input->{name} . "/" . $mfn, |
318 |
|
ds => $ds, |
319 |
|
type => $config->{$use_indexer}->{type}, |
320 |
|
) if ($indexer && $ds); |
321 |
|
|
322 |
|
if ($marc_fh) { |
323 |
|
my $marc = new MARC::Record; |
324 |
|
$marc->encoding( 'utf-8' ); |
325 |
|
my @marc_fields = WebPAC::Normalize::_get_marc_fields(); |
326 |
|
if (! @marc_fields) { |
327 |
|
$log->warn("MARC record $mfn is empty, skipping"); |
328 |
|
} else { |
329 |
|
$marc->add_fields( @marc_fields ); |
330 |
|
|
331 |
|
# tweak leader |
332 |
|
if (my $new_l = WebPAC::Normalize::marc_leader()) { |
333 |
|
|
334 |
|
my $leader = $marc->leader; |
335 |
|
|
336 |
|
foreach my $o ( keys %$new_l ) { |
337 |
|
my $insert = $new_l->{$o}; |
338 |
|
$leader = substr($leader, 0, $o) . |
339 |
|
$insert . substr($leader, $o+length($insert)); |
340 |
|
} |
341 |
|
$marc->leader( $leader ); |
342 |
|
} |
343 |
|
|
344 |
|
if ($marc_lint) { |
345 |
|
$lint->check_record( $marc ); |
346 |
|
my $err = join( "\n", $lint->warnings ); |
347 |
|
$log->error("MARC lint detected warning on MFN $mfn\n", |
348 |
|
"<<<<< Original imput row:\n",dump($row), "\n", |
349 |
|
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump(@marc_fields), "\n", |
350 |
|
"!!!!! MARC lint warnings:\n",$err,"\n" |
351 |
|
) if ($err); |
352 |
|
} |
353 |
|
|
354 |
|
if ($marc_dump) { |
355 |
|
$log->info("MARC record on MFN $mfn\n", |
356 |
|
"<<<<< Original imput row:\n",dump($row), "\n", |
357 |
|
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump(@marc_fields), "\n", |
358 |
|
); |
359 |
|
} |
360 |
|
|
361 |
|
print $marc_fh $marc->as_usmarc; |
362 |
|
} |
363 |
|
} |
364 |
|
|
365 |
|
$total_rows++; |
366 |
} |
} |
367 |
|
|
368 |
$indexer->add( |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
|
id => $input->{name} . "/" . $mfn, |
|
|
ds => $ds, |
|
|
type => $config->{$use_indexer}->{type}, |
|
|
) if ($indexer); |
|
369 |
|
|
370 |
$total_rows++; |
# close MARC file |
371 |
} |
close($marc_fh) if ($marc_fh); |
372 |
|
|
373 |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
} |
374 |
|
|
375 |
}; |
} |
376 |
|
|
377 |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
378 |
|
|
386 |
# |
# |
387 |
# add Hyper Estraier links to other databases |
# add Hyper Estraier links to other databases |
388 |
# |
# |
389 |
if (ref($db_config->{links}) eq 'ARRAY') { |
if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { |
390 |
foreach my $link (@{ $db_config->{links} }) { |
foreach my $link (@{ $db_config->{links} }) { |
391 |
if ($use_indexer eq 'hyperestraier') { |
if ($use_indexer eq 'hyperestraier') { |
392 |
$log->info("adding link $database -> $link->{to} [$link->{credit}]"); |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
393 |
$indexer->add_link( |
push @links, { |
394 |
from => $database, |
from => $database, |
395 |
to => $link->{to}, |
to => $link->{to}, |
396 |
credit => $link->{credit}, |
credit => $link->{credit}, |
397 |
); |
}; |
398 |
} else { |
} else { |
399 |
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
400 |
} |
} |
403 |
|
|
404 |
} |
} |
405 |
|
|
406 |
|
foreach my $link (@links) { |
407 |
|
$log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]"); |
408 |
|
$indexer->add_link( %{ $link } ); |
409 |
|
} |