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; |
13 |
use WebPAC::Normalize; |
use WebPAC::Normalize; |
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; |
22 |
|
use Data::Dump qw/dump/; |
23 |
|
|
24 |
=head1 NAME |
=head1 NAME |
25 |
|
|
63 |
|
|
64 |
turn on extra validation of imput records, see L<WebPAC::Validation> |
turn on extra validation of imput records, see L<WebPAC::Validation> |
65 |
|
|
66 |
|
=item --marc-normalize conf/normalize/mapping.pl |
67 |
|
|
68 |
|
This option specifies normalisation file for MARC creation |
69 |
|
|
70 |
|
=item --marc-output out/marc/test.marc |
71 |
|
|
72 |
|
Optional path to output file |
73 |
|
|
74 |
|
=item --marc-lint |
75 |
|
|
76 |
|
By default turned on if C<--marc-normalize> is used. You can disable lint |
77 |
|
messages with C<--no-marc-lint>. |
78 |
|
|
79 |
|
=item --marc-dump |
80 |
|
|
81 |
|
Force dump or input and marc record for debugging. |
82 |
|
|
83 |
=back |
=back |
84 |
|
|
85 |
=cut |
=cut |
93 |
my $only_filter; |
my $only_filter; |
94 |
my $stats = 0; |
my $stats = 0; |
95 |
my $validate_path; |
my $validate_path; |
96 |
|
my ($marc_normalize, $marc_output); |
97 |
|
my $marc_lint = 1; |
98 |
|
my $marc_dump = 0; |
99 |
|
|
100 |
GetOptions( |
GetOptions( |
101 |
"limit=i" => \$limit, |
"limit=i" => \$limit, |
104 |
"one=s" => \$only_filter, |
"one=s" => \$only_filter, |
105 |
"only=s" => \$only_filter, |
"only=s" => \$only_filter, |
106 |
"config" => \$config, |
"config" => \$config, |
107 |
"debug" => \$debug, |
"debug+" => \$debug, |
108 |
"stats" => \$stats, |
"stats" => \$stats, |
109 |
"validate=s" => \$validate_path, |
"validate=s" => \$validate_path, |
110 |
|
"marc-normalize=s" => \$marc_normalize, |
111 |
|
"marc-output=s" => \$marc_output, |
112 |
|
"marc-lint!" => \$marc_lint, |
113 |
|
"marc-dump!" => \$marc_dump, |
114 |
); |
); |
115 |
|
|
116 |
$config = LoadFile($config); |
$config = LoadFile($config); |
117 |
|
|
118 |
print "config = ",Dumper($config) if ($debug); |
print "config = ",dump($config) if ($debug); |
119 |
|
|
120 |
die "no databases in config file!\n" unless ($config->{databases}); |
die "no databases in config file!\n" unless ($config->{databases}); |
121 |
|
|
135 |
$log->info("using $use_indexer indexing engine..."); |
$log->info("using $use_indexer indexing engine..."); |
136 |
} |
} |
137 |
|
|
138 |
|
# disable indexing when creating marc |
139 |
|
$use_indexer = undef if ($marc_normalize); |
140 |
|
|
141 |
my $total_rows = 0; |
my $total_rows = 0; |
142 |
my $start_t = time(); |
my $start_t = time(); |
143 |
|
|
248 |
%{ $input }, |
%{ $input }, |
249 |
); |
); |
250 |
|
|
251 |
my $rules; |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
252 |
my $normalize_path = $input->{normalize}->{path}; |
@{ $input->{normalize} } : ( $input->{normalize} ); |
253 |
|
|
254 |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
if ($marc_normalize) { |
255 |
|
@norm_array = ( { |
256 |
|
path => $marc_normalize, |
257 |
|
output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc', |
258 |
|
} ); |
259 |
|
} |
260 |
|
|
261 |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
foreach my $normalize (@norm_array) { |
262 |
|
|
263 |
foreach my $pos ( 0 ... $input_db->size ) { |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
264 |
|
|
265 |
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 ); |
266 |
|
|
267 |
my $mfn = $row->{'000'}->[0]; |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
268 |
|
|
269 |
if (! $mfn || $mfn !~ m#^\d+$#) { |
$log->info("Using $normalize_path for normalization..."); |
270 |
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
|
271 |
$mfn = $pos; |
my $marc = new WebPAC::Output::MARC( |
272 |
push @{ $row->{'000'} }, $pos; |
path => $normalize->{output}, |
273 |
} |
lint => $marc_lint, |
274 |
|
dump => $marc_dump, |
275 |
|
) if ($normalize->{output}); |
276 |
|
|
277 |
|
# reset position in database |
278 |
|
$input_db->seek(1); |
279 |
|
|
280 |
|
foreach my $pos ( 0 ... $input_db->size ) { |
281 |
|
|
282 |
|
my $row = $input_db->fetch || next; |
283 |
|
|
284 |
if ($validate) { |
my $mfn = $row->{'000'}->[0]; |
285 |
my @errors = $validate->validate_errors( $row ); |
|
286 |
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
if (! $mfn || $mfn !~ m#^\d+$#) { |
287 |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
288 |
|
$mfn = $pos; |
289 |
|
push @{ $row->{'000'} }, $pos; |
290 |
|
} |
291 |
|
|
292 |
|
|
293 |
|
if ($validate) { |
294 |
|
my @errors = $validate->validate_errors( $row ); |
295 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
296 |
|
} |
297 |
|
|
298 |
|
my $ds = WebPAC::Normalize::data_structure( |
299 |
|
row => $row, |
300 |
|
rules => $rules, |
301 |
|
lookup => $lookup ? $lookup->lookup_hash : undef, |
302 |
|
marc_encoding => 'utf-8', |
303 |
|
); |
304 |
|
|
305 |
|
$db->save_ds( |
306 |
|
id => $mfn, |
307 |
|
ds => $ds, |
308 |
|
prefix => $input->{name}, |
309 |
|
) if ($ds && !$stats); |
310 |
|
|
311 |
|
$indexer->add( |
312 |
|
id => $input->{name} . "/" . $mfn, |
313 |
|
ds => $ds, |
314 |
|
type => $config->{$use_indexer}->{type}, |
315 |
|
) if ($indexer && $ds); |
316 |
|
|
317 |
|
if ($marc) { |
318 |
|
my $i = 0; |
319 |
|
|
320 |
|
while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) { |
321 |
|
$marc->add( |
322 |
|
id => $mfn . ( $i ? "/$i" : '' ), |
323 |
|
fields => $fields, |
324 |
|
leader => WebPAC::Normalize::marc_leader(), |
325 |
|
row => $row, |
326 |
|
); |
327 |
|
$i++; |
328 |
|
} |
329 |
|
|
330 |
|
$log->info("Created $i instances of MFN $mfn\n") if ($i > 1); |
331 |
|
} |
332 |
|
|
333 |
|
$total_rows++; |
334 |
} |
} |
335 |
|
|
336 |
|
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
|
my $ds = WebPAC::Normalize::data_structure( |
|
|
row => $row, |
|
|
rules => $rules, |
|
|
lookup => $lookup ? $lookup->lookup_hash : undef, |
|
|
); |
|
|
|
|
|
$db->save_ds( |
|
|
id => $mfn, |
|
|
ds => $ds, |
|
|
prefix => $input->{name}, |
|
|
) if ($ds && !$stats); |
|
|
|
|
|
$indexer->add( |
|
|
id => $input->{name} . "/" . $mfn, |
|
|
ds => $ds, |
|
|
type => $config->{$use_indexer}->{type}, |
|
|
) if ($indexer); |
|
337 |
|
|
338 |
$total_rows++; |
# close MARC file |
339 |
} |
$marc->finish if ($marc); |
340 |
|
|
341 |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
} |
342 |
|
|
343 |
}; |
} |
344 |
|
|
345 |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
346 |
|
|
354 |
# |
# |
355 |
# add Hyper Estraier links to other databases |
# add Hyper Estraier links to other databases |
356 |
# |
# |
357 |
if (ref($db_config->{links}) eq 'ARRAY') { |
if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { |
358 |
foreach my $link (@{ $db_config->{links} }) { |
foreach my $link (@{ $db_config->{links} }) { |
359 |
if ($use_indexer eq 'hyperestraier') { |
if ($use_indexer eq 'hyperestraier') { |
360 |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |