11 |
use WebPAC::Lookup; |
use WebPAC::Lookup; |
12 |
use WebPAC::Input 0.03; |
use WebPAC::Input 0.03; |
13 |
use WebPAC::Store 0.03; |
use WebPAC::Store 0.03; |
14 |
use WebPAC::Normalize::XML; |
use WebPAC::Normalize; |
|
use WebPAC::Normalize::Set; |
|
15 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
16 |
use WebPAC::Validate; |
use WebPAC::Validate; |
17 |
use YAML qw/LoadFile/; |
use YAML qw/LoadFile/; |
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 |
|
|
54 |
|
|
55 |
path to YAML configuration file |
path to YAML configuration file |
56 |
|
|
|
=item --force-set |
|
|
|
|
|
force conversion C<< normalize->path >> in C<config.yml> from |
|
|
C<.xml> to C<.pl> |
|
|
|
|
57 |
=item --stats |
=item --stats |
58 |
|
|
59 |
disable indexing and dump statistics about field and subfield |
disable indexing and dump statistics about field and subfield |
74 |
my $config = 'conf/config.yml'; |
my $config = 'conf/config.yml'; |
75 |
my $debug = 0; |
my $debug = 0; |
76 |
my $only_filter; |
my $only_filter; |
|
my $force_set = 0; |
|
77 |
my $stats = 0; |
my $stats = 0; |
78 |
my $validate_path; |
my $validate_path; |
79 |
|
|
85 |
"only=s" => \$only_filter, |
"only=s" => \$only_filter, |
86 |
"config" => \$config, |
"config" => \$config, |
87 |
"debug" => \$debug, |
"debug" => \$debug, |
|
"force-set" => \$force_set, |
|
88 |
"stats" => \$stats, |
"stats" => \$stats, |
89 |
"validate=s" => \$validate_path, |
"validate=s" => \$validate_path, |
90 |
); |
); |
221 |
%{ $input }, |
%{ $input }, |
222 |
); |
); |
223 |
|
|
224 |
my $n = new WebPAC::Normalize::XML( |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
225 |
# filter => { 'foo' => sub { shift } }, |
@{ $input->{normalize} } : ( $input->{normalize} ); |
|
db => $db, |
|
|
lookup_regex => $lookup ? $lookup->regex : undef, |
|
|
lookup => $lookup, |
|
|
prefix => $input->{name}, |
|
|
); |
|
226 |
|
|
227 |
my $rules; |
foreach my $normalize (@norm_array) { |
|
my $normalize_path = $input->{normalize}->{path}; |
|
228 |
|
|
229 |
if ($force_set) { |
my $rules; |
230 |
my $new_norm_path = $normalize_path; |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
|
$new_norm_path =~ s/\.xml$/.pl/; |
|
|
if (-e $new_norm_path) { |
|
|
$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"); |
|
|
} |
|
|
} |
|
231 |
|
|
232 |
if ($normalize_path =~ m/\.xml$/i) { |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
|
$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: $!"; |
|
|
} |
|
233 |
|
|
234 |
foreach my $pos ( 0 ... $input_db->size ) { |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
235 |
|
|
236 |
my $row = $input_db->fetch || next; |
$log->info("Using $normalize_path for normalization..."); |
237 |
|
|
238 |
my $mfn = $row->{'000'}->[0]; |
my $marc_fh; |
239 |
|
if (my $path = $normalize->{marc21}) { |
240 |
|
open($marc_fh, '>', $path) || |
241 |
|
$log->logdie("can't open MARC output $path: $!"); |
242 |
|
|
243 |
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; |
|
244 |
} |
} |
245 |
|
|
246 |
|
# reset position in database |
247 |
|
$input_db->seek(1); |
248 |
|
|
249 |
if ($validate) { |
foreach my $pos ( 0 ... $input_db->size ) { |
250 |
my @errors = $validate->validate_errors( $row ); |
|
251 |
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
my $row = $input_db->fetch || next; |
252 |
} |
|
253 |
|
my $mfn = $row->{'000'}->[0]; |
254 |
|
|
255 |
|
if (! $mfn || $mfn !~ m#^\d+$#) { |
256 |
my $ds; |
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
257 |
if ($n) { |
$mfn = $pos; |
258 |
$ds = $n->data_structure($row); |
push @{ $row->{'000'} }, $pos; |
259 |
} else { |
} |
260 |
$ds = WebPAC::Normalize::Set::data_structure( |
|
261 |
|
|
262 |
|
if ($validate) { |
263 |
|
my @errors = $validate->validate_errors( $row ); |
264 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
my $ds = WebPAC::Normalize::data_structure( |
269 |
row => $row, |
row => $row, |
270 |
rules => $rules, |
rules => $rules, |
271 |
lookup => $lookup ? $lookup->lookup_hash : undef, |
lookup => $lookup ? $lookup->lookup_hash : undef, |
272 |
|
marc_encoding => 'utf-8', |
273 |
); |
); |
274 |
|
|
275 |
$db->save_ds( |
$db->save_ds( |
277 |
ds => $ds, |
ds => $ds, |
278 |
prefix => $input->{name}, |
prefix => $input->{name}, |
279 |
) if ($ds && !$stats); |
) if ($ds && !$stats); |
280 |
|
|
281 |
|
$indexer->add( |
282 |
|
id => $input->{name} . "/" . $mfn, |
283 |
|
ds => $ds, |
284 |
|
type => $config->{$use_indexer}->{type}, |
285 |
|
) if ($indexer && $ds); |
286 |
|
|
287 |
|
if ($marc_fh) { |
288 |
|
my $marc = new MARC::Record; |
289 |
|
$marc->encoding( 'utf-8' ); |
290 |
|
$marc->add_fields( WebPAC::Normalize::_get_marc21_fields() ); |
291 |
|
print $marc_fh $marc->as_usmarc; |
292 |
|
} |
293 |
|
|
294 |
|
$total_rows++; |
295 |
} |
} |
296 |
|
|
297 |
$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); |
|
298 |
|
|
299 |
$total_rows++; |
# close MARC file |
300 |
} |
close($marc_fh) if ($marc_fh); |
301 |
|
|
302 |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
} |
303 |
|
|
304 |
}; |
} |
305 |
|
|
306 |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
307 |
|
|