5 |
|
|
6 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common/; |
7 |
|
|
8 |
use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net |
use MARC::Record; |
9 |
use MARC::Lint; |
use MARC::Lint; |
10 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
11 |
|
|
15 |
|
|
16 |
=head1 VERSION |
=head1 VERSION |
17 |
|
|
18 |
Version 0.01 |
Version 0.04 |
19 |
|
|
20 |
=cut |
=cut |
21 |
|
|
22 |
our $VERSION = '0.01'; |
our $VERSION = '0.04'; |
23 |
|
|
24 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
25 |
|
|
33 |
|
|
34 |
my $marc = new WebPAC::Output::MARC( |
my $marc = new WebPAC::Output::MARC( |
35 |
path => '/path/to/output.marc', |
path => '/path/to/output.marc', |
36 |
encoding => 'utf-8', |
marc_encoding => 'utf-8', |
37 |
lint => 1, |
lint => 1, |
38 |
dump => 0, |
dump => 0, |
39 |
) |
) |
52 |
$log->warn("Can't create MARC::Lint object, linting is disabled"); |
$log->warn("Can't create MARC::Lint object, linting is disabled"); |
53 |
} |
} |
54 |
|
|
55 |
|
$self->{marc_encoding} ||= 'utf-8'; |
56 |
|
|
57 |
if (my $path = $self->{path}) { |
if (my $path = $self->{path}) { |
58 |
open($self->{fh}, '>', $path) || |
open($self->{fh}, '>', $path) || |
59 |
$log->logdie("can't open MARC output $path: $!"); |
$log->logdie("can't open MARC output $path: $!"); |
60 |
|
binmode($self->{fh}, ':utf8'); |
61 |
|
|
62 |
$log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', "\n"); |
$log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', " encoding ", $self->{marc_encoding}, "\n"); |
63 |
} else { |
} else { |
64 |
$log->logconfess("new called without path"); |
$log->logconfess("new called without path"); |
65 |
} |
} |
66 |
|
|
|
$self->{encoding} ||= 'utf-8'; |
|
|
|
|
67 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
68 |
} |
} |
69 |
|
|
72 |
$marc->add( |
$marc->add( |
73 |
id => $mfn, |
id => $mfn, |
74 |
fields => WebPAC::Normalize::_get_marc_fields(), |
fields => WebPAC::Normalize::_get_marc_fields(), |
75 |
leader => WebPAC::Normalize::marc_leader(), |
leader => WebPAC::Normalize::_get_marc_leader(), |
76 |
row => $row, |
row => $row, |
77 |
); |
); |
78 |
|
|
92 |
unless ($arg->{fields} && defined $arg->{id}); |
unless ($arg->{fields} && defined $arg->{id}); |
93 |
|
|
94 |
my $marc = new MARC::Record; |
my $marc = new MARC::Record; |
95 |
$marc->encoding( $self->{encoding} ); |
$marc->encoding( $self->{marc_encoding} ); |
96 |
|
|
97 |
my $id = $self->{id}; |
my $id = $arg->{id}; |
98 |
|
|
99 |
$log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY'); |
$log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY'); |
100 |
|
|
101 |
$marc->add_fields( @{ $arg->{fields} } ); |
my $fields = $arg->{fields}; |
102 |
|
|
103 |
|
$log->debug("original fields = ", sub { dump( $fields ) }); |
104 |
|
|
105 |
|
# recode fields to marc_encoding |
106 |
|
foreach my $j ( 0 .. $#$fields ) { |
107 |
|
foreach my $i ( 0 .. ( ( $#{$fields->[$j]} - 3 ) / 2 ) ) { |
108 |
|
my $f = $fields->[$j]->[ ($i * 2) + 4 ]; |
109 |
|
$fields->[$j]->[ ($i * 2) + 4 ] = $f; |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
# sort fields |
114 |
|
@$fields = sort { $a->[0] <=> $b->[0] } @$fields; |
115 |
|
|
116 |
|
$log->debug("recode fields = ", sub { dump( $fields ) }); |
117 |
|
|
118 |
|
$marc->add_fields( @$fields ); |
119 |
|
|
120 |
# tweak leader |
# tweak leader |
121 |
if (my $new_l = $arg->{leader}) { |
if (my $new_l = $arg->{leader}) { |
135 |
my @w = $self->{lint}->warnings; |
my @w = $self->{lint}->warnings; |
136 |
if (@w) { |
if (@w) { |
137 |
$log->error("MARC lint detected warning on record $id\n", |
$log->error("MARC lint detected warning on record $id\n", |
138 |
"<<<<< Original imput row:\n",dump($arg->{row}), "\n", |
"<<<<< Original input row:\n",dump($arg->{row}), "\n", |
139 |
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $arg->{fields} ), "\n", |
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n", |
140 |
"!!!!! MARC lint warnings:\n",join("\n",@w),"\n" |
"!!!!! MARC lint warnings:\n",join("\n",@w),"\n" |
141 |
); |
); |
142 |
map { $self->{_marc_lint_warnings}->{$_}++ } @w; |
map { $self->{_marc_lint_warnings}->{$_}++ } @w; |
145 |
|
|
146 |
if ($self->{dump}) { |
if ($self->{dump}) { |
147 |
$log->info("MARC record on record $id\n", |
$log->info("MARC record on record $id\n", |
148 |
"<<<<< Original imput row:\n",dump($self->{row}), "\n", |
"<<<<< Original imput row:\n",dump($arg->{row}), "\n", |
149 |
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $arg->{fields} ), "\n", |
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n", |
150 |
); |
); |
151 |
} |
} |
152 |
|
|