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 MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net |
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 |
=back |
=back |
75 |
|
|
76 |
=cut |
=cut |
84 |
my $only_filter; |
my $only_filter; |
85 |
my $stats = 0; |
my $stats = 0; |
86 |
my $validate_path; |
my $validate_path; |
87 |
|
my ($marc_normalize, $marc_output); |
88 |
|
|
89 |
GetOptions( |
GetOptions( |
90 |
"limit=i" => \$limit, |
"limit=i" => \$limit, |
96 |
"debug" => \$debug, |
"debug" => \$debug, |
97 |
"stats" => \$stats, |
"stats" => \$stats, |
98 |
"validate=s" => \$validate_path, |
"validate=s" => \$validate_path, |
99 |
|
"marc-normalize=s" => \$marc_normalize, |
100 |
|
"marc-output=s" => \$marc_output, |
101 |
); |
); |
102 |
|
|
103 |
$config = LoadFile($config); |
$config = LoadFile($config); |
122 |
$log->info("using $use_indexer indexing engine..."); |
$log->info("using $use_indexer indexing engine..."); |
123 |
} |
} |
124 |
|
|
125 |
|
# disable indexing when creating marc |
126 |
|
$use_indexer = undef if ($marc_normalize); |
127 |
|
|
128 |
my $total_rows = 0; |
my $total_rows = 0; |
129 |
my $start_t = time(); |
my $start_t = time(); |
130 |
|
|
235 |
%{ $input }, |
%{ $input }, |
236 |
); |
); |
237 |
|
|
238 |
my $rules; |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
239 |
my $normalize_path = $input->{normalize}->{path}; |
@{ $input->{normalize} } : ( $input->{normalize} ); |
240 |
|
|
241 |
|
if ($marc_normalize) { |
242 |
|
@norm_array = ( { |
243 |
|
path => $marc_normalize, |
244 |
|
output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc', |
245 |
|
} ); |
246 |
|
} |
247 |
|
|
248 |
|
foreach my $normalize (@norm_array) { |
249 |
|
|
250 |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
251 |
|
|
252 |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
253 |
|
|
254 |
foreach my $pos ( 0 ... $input_db->size ) { |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
255 |
|
|
256 |
my $row = $input_db->fetch || next; |
$log->info("Using $normalize_path for normalization..."); |
257 |
|
|
258 |
my $mfn = $row->{'000'}->[0]; |
my $marc_fh; |
259 |
|
if (my $path = $normalize->{output}) { |
260 |
|
open($marc_fh, '>', $path) || |
261 |
|
$log->logdie("can't open MARC output $path: $!"); |
262 |
|
|
263 |
if (! $mfn || $mfn !~ m#^\d+$#) { |
$log->info("Creating MARC export file $path\n"); |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
|
|
$mfn = $pos; |
|
|
push @{ $row->{'000'} }, $pos; |
|
264 |
} |
} |
265 |
|
|
266 |
|
# reset position in database |
267 |
|
$input_db->seek(1); |
268 |
|
|
269 |
if ($validate) { |
foreach my $pos ( 0 ... $input_db->size ) { |
270 |
my @errors = $validate->validate_errors( $row ); |
|
271 |
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
my $row = $input_db->fetch || next; |
272 |
|
|
273 |
|
my $mfn = $row->{'000'}->[0]; |
274 |
|
|
275 |
|
if (! $mfn || $mfn !~ m#^\d+$#) { |
276 |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
277 |
|
$mfn = $pos; |
278 |
|
push @{ $row->{'000'} }, $pos; |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
|
if ($validate) { |
283 |
|
my @errors = $validate->validate_errors( $row ); |
284 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
|
my $ds = WebPAC::Normalize::data_structure( |
289 |
|
row => $row, |
290 |
|
rules => $rules, |
291 |
|
lookup => $lookup ? $lookup->lookup_hash : undef, |
292 |
|
marc_encoding => 'utf-8', |
293 |
|
); |
294 |
|
|
295 |
|
$db->save_ds( |
296 |
|
id => $mfn, |
297 |
|
ds => $ds, |
298 |
|
prefix => $input->{name}, |
299 |
|
) if ($ds && !$stats); |
300 |
|
|
301 |
|
$indexer->add( |
302 |
|
id => $input->{name} . "/" . $mfn, |
303 |
|
ds => $ds, |
304 |
|
type => $config->{$use_indexer}->{type}, |
305 |
|
) if ($indexer && $ds); |
306 |
|
|
307 |
|
if ($marc_fh) { |
308 |
|
my $marc = new MARC::Record; |
309 |
|
$marc->encoding( 'utf-8' ); |
310 |
|
$marc->add_fields( WebPAC::Normalize::_get_marc_fields() ); |
311 |
|
print $marc_fh $marc->as_usmarc; |
312 |
|
} |
313 |
|
|
314 |
|
$total_rows++; |
315 |
} |
} |
316 |
|
|
317 |
|
$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); |
|
318 |
|
|
319 |
$total_rows++; |
# close MARC file |
320 |
} |
close($marc_fh) if ($marc_fh); |
321 |
|
|
322 |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
} |
323 |
|
|
324 |
}; |
} |
325 |
|
|
326 |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
327 |
|
|
335 |
# |
# |
336 |
# add Hyper Estraier links to other databases |
# add Hyper Estraier links to other databases |
337 |
# |
# |
338 |
if (ref($db_config->{links}) eq 'ARRAY') { |
if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { |
339 |
foreach my $link (@{ $db_config->{links} }) { |
foreach my $link (@{ $db_config->{links} }) { |
340 |
if ($use_indexer eq 'hyperestraier') { |
if ($use_indexer eq 'hyperestraier') { |
341 |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |