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; |
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 |
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 |
|
|
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-arc-lint>. |
79 |
|
|
80 |
=back |
=back |
81 |
|
|
82 |
=cut |
=cut |
90 |
my $only_filter; |
my $only_filter; |
91 |
my $stats = 0; |
my $stats = 0; |
92 |
my $validate_path; |
my $validate_path; |
93 |
|
my ($marc_normalize, $marc_output); |
94 |
|
my $marc_lint = 1; |
95 |
|
|
96 |
GetOptions( |
GetOptions( |
97 |
"limit=i" => \$limit, |
"limit=i" => \$limit, |
103 |
"debug" => \$debug, |
"debug" => \$debug, |
104 |
"stats" => \$stats, |
"stats" => \$stats, |
105 |
"validate=s" => \$validate_path, |
"validate=s" => \$validate_path, |
106 |
|
"marc-normalize=s" => \$marc_normalize, |
107 |
|
"marc-output=s" => \$marc_output, |
108 |
|
"marc-lint!" => \$marc_lint, |
109 |
); |
); |
110 |
|
|
111 |
$config = LoadFile($config); |
$config = LoadFile($config); |
112 |
|
|
113 |
print "config = ",Dumper($config) if ($debug); |
print "config = ",dump($config) if ($debug); |
114 |
|
|
115 |
die "no databases in config file!\n" unless ($config->{databases}); |
die "no databases in config file!\n" unless ($config->{databases}); |
116 |
|
|
130 |
$log->info("using $use_indexer indexing engine..."); |
$log->info("using $use_indexer indexing engine..."); |
131 |
} |
} |
132 |
|
|
133 |
|
# disable indexing when creating marc |
134 |
|
$use_indexer = undef if ($marc_normalize); |
135 |
|
|
136 |
my $total_rows = 0; |
my $total_rows = 0; |
137 |
my $start_t = time(); |
my $start_t = time(); |
138 |
|
|
139 |
my @links; |
my @links; |
140 |
my $indexer; |
my $indexer; |
141 |
|
|
142 |
|
my $lint = new MARC::Lint if ($marc_lint); |
143 |
|
|
144 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
145 |
|
|
146 |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
248 |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
249 |
@{ $input->{normalize} } : ( $input->{normalize} ); |
@{ $input->{normalize} } : ( $input->{normalize} ); |
250 |
|
|
251 |
|
if ($marc_normalize) { |
252 |
|
@norm_array = ( { |
253 |
|
path => $marc_normalize, |
254 |
|
output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc', |
255 |
|
} ); |
256 |
|
} |
257 |
|
|
258 |
foreach my $normalize (@norm_array) { |
foreach my $normalize (@norm_array) { |
259 |
|
|
260 |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
266 |
$log->info("Using $normalize_path for normalization..."); |
$log->info("Using $normalize_path for normalization..."); |
267 |
|
|
268 |
my $marc_fh; |
my $marc_fh; |
269 |
if (my $path = $normalize->{marc21}) { |
if (my $path = $normalize->{output}) { |
270 |
open($marc_fh, '>', $path) || |
open($marc_fh, '>', $path) || |
271 |
$log->logdie("can't open MARC output $path: $!"); |
$log->logdie("can't open MARC output $path: $!"); |
272 |
|
|
273 |
$log->info("Creating MARC export file $path\n"); |
$log->info("Creating MARC export file $path", $marc_lint ? ' (with lint)' : '', "\n"); |
274 |
} |
} |
275 |
|
|
276 |
# reset position in database |
# reset position in database |
317 |
if ($marc_fh) { |
if ($marc_fh) { |
318 |
my $marc = new MARC::Record; |
my $marc = new MARC::Record; |
319 |
$marc->encoding( 'utf-8' ); |
$marc->encoding( 'utf-8' ); |
320 |
$marc->add_fields( WebPAC::Normalize::_get_marc_fields() ); |
my @marc_fields = WebPAC::Normalize::_get_marc_fields(); |
321 |
print $marc_fh $marc->as_usmarc; |
if (! @marc_fields) { |
322 |
|
$log->warn("MARC record $mfn is empty, skipping"); |
323 |
|
} else { |
324 |
|
$marc->add_fields( @marc_fields ); |
325 |
|
if ($marc_lint) { |
326 |
|
$lint->check_record( $marc ); |
327 |
|
my $err = join( "\n", $lint->warnings ); |
328 |
|
$log->error("MARC lint detected warning on MFN $mfn\n", |
329 |
|
"Original imput row: ",dump($row), "\n", |
330 |
|
"Normalized MARC row: ",dump(@marc_fields), "\n", |
331 |
|
"MARC lint warnings: ",$err,"\n" |
332 |
|
) if ($err); |
333 |
|
} |
334 |
|
print $marc_fh $marc->as_usmarc; |
335 |
|
} |
336 |
} |
} |
337 |
|
|
338 |
$total_rows++; |
$total_rows++; |
359 |
# |
# |
360 |
# add Hyper Estraier links to other databases |
# add Hyper Estraier links to other databases |
361 |
# |
# |
362 |
if (ref($db_config->{links}) eq 'ARRAY') { |
if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { |
363 |
foreach my $link (@{ $db_config->{links} }) { |
foreach my $link (@{ $db_config->{links} }) { |
364 |
if ($use_indexer eq 'hyperestraier') { |
if ($use_indexer eq 'hyperestraier') { |
365 |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |