1 |
package WebPAC::Output::MARC; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use base qw/WebPAC::Common/; |
7 |
|
8 |
use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net |
9 |
use MARC::Lint; |
10 |
use Data::Dump qw/dump/; |
11 |
use Encode qw/from_to decode/; |
12 |
|
13 |
=head1 NAME |
14 |
|
15 |
WebPAC::Output::MARC - Create MARC records from C<marc_*> normalisation rules |
16 |
|
17 |
=head1 VERSION |
18 |
|
19 |
Version 0.02 |
20 |
|
21 |
=cut |
22 |
|
23 |
our $VERSION = '0.02'; |
24 |
|
25 |
=head1 SYNOPSIS |
26 |
|
27 |
Create MARC records from C<marc_*> normalisation rules described in |
28 |
L<WebPAC::Normalize>. |
29 |
|
30 |
|
31 |
=head1 FUNCTIONS |
32 |
|
33 |
=head2 new |
34 |
|
35 |
my $marc = new WebPAC::Output::MARC( |
36 |
path => '/path/to/output.marc', |
37 |
native_encoding => 'iso-8859-2', |
38 |
marc_encoding => 'utf-8', |
39 |
lint => 1, |
40 |
dump => 0, |
41 |
) |
42 |
|
43 |
=cut |
44 |
|
45 |
sub new { |
46 |
my $class = shift; |
47 |
my $self = {@_}; |
48 |
bless($self, $class); |
49 |
|
50 |
my $log = $self->_get_logger; |
51 |
|
52 |
if ($self->{lint}) { |
53 |
$self->{lint}= new MARC::Lint or |
54 |
$log->warn("Can't create MARC::Lint object, linting is disabled"); |
55 |
} |
56 |
|
57 |
if (my $path = $self->{path}) { |
58 |
open($self->{fh}, '>', $path) || |
59 |
$log->logdie("can't open MARC output $path: $!"); |
60 |
|
61 |
$log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', "\n"); |
62 |
} else { |
63 |
$log->logconfess("new called without path"); |
64 |
} |
65 |
|
66 |
$self->{native_encoding} ||= 'iso-8859-2'; |
67 |
$self->{marc_encoding} ||= 'utf-8'; |
68 |
|
69 |
$self ? return $self : return undef; |
70 |
} |
71 |
|
72 |
=head2 add |
73 |
|
74 |
$marc->add( |
75 |
id => $mfn, |
76 |
fields => WebPAC::Normalize::_get_marc_fields(), |
77 |
leader => WebPAC::Normalize::marc_leader(), |
78 |
row => $row, |
79 |
); |
80 |
|
81 |
C<row> is optional parametar which is used when dumping original row to |
82 |
error log. |
83 |
|
84 |
=cut |
85 |
|
86 |
sub add { |
87 |
my $self = shift; |
88 |
|
89 |
my $arg = {@_}; |
90 |
|
91 |
my $log = $self->_get_logger; |
92 |
|
93 |
$log->logconfess("add needs fields and id arguments") |
94 |
unless ($arg->{fields} && defined $arg->{id}); |
95 |
|
96 |
my $marc = new MARC::Record; |
97 |
$marc->encoding( $self->{marc_encoding} ); |
98 |
|
99 |
my $id = $arg->{id}; |
100 |
|
101 |
$log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY'); |
102 |
|
103 |
my $fields = $arg->{fields}; |
104 |
|
105 |
$log->debug("original fields = ", sub { dump( $fields ) }); |
106 |
|
107 |
# recode fields to marc_encoding |
108 |
foreach my $j ( 0 .. $#$fields ) { |
109 |
foreach my $i ( 0 .. ( ( $#{$fields->[$j]} - 3 ) / 2 ) ) { |
110 |
my $f = $fields->[$j]->[ ($i * 2) + 4 ]; |
111 |
$f = decode( $self->{native_encoding}, $f ); |
112 |
$fields->[$j]->[ ($i * 2) + 4 ] = $f; |
113 |
} |
114 |
} |
115 |
|
116 |
$log->debug("recode fields = ", sub { dump( $fields ) }); |
117 |
|
118 |
$marc->add_fields( @$fields ); |
119 |
|
120 |
# tweak leader |
121 |
if (my $new_l = $arg->{leader}) { |
122 |
|
123 |
my $leader = $marc->leader; |
124 |
|
125 |
foreach my $o ( keys %$new_l ) { |
126 |
my $insert = $new_l->{$o}; |
127 |
$leader = substr($leader, 0, $o) . |
128 |
$insert . substr($leader, $o+length($insert)); |
129 |
} |
130 |
$marc->leader( $leader ); |
131 |
} |
132 |
|
133 |
if ($self->{lint}) { |
134 |
$self->{lint}->check_record( $marc ); |
135 |
my @w = $self->{lint}->warnings; |
136 |
if (@w) { |
137 |
$log->error("MARC lint detected warning on record $id\n", |
138 |
"<<<<< Original input row:\n",dump($arg->{row}), "\n", |
139 |
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $arg->{fields} ), "\n", |
140 |
"!!!!! MARC lint warnings:\n",join("\n",@w),"\n" |
141 |
); |
142 |
map { $self->{_marc_lint_warnings}->{$_}++ } @w; |
143 |
} |
144 |
} |
145 |
|
146 |
if ($self->{dump}) { |
147 |
$log->info("MARC record on record $id\n", |
148 |
"<<<<< Original imput row:\n",dump($arg->{row}), "\n", |
149 |
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $arg->{fields} ), "\n", |
150 |
); |
151 |
} |
152 |
|
153 |
{ |
154 |
use bytes; |
155 |
print {$self->{fh}} $marc->as_usmarc; |
156 |
} |
157 |
|
158 |
} |
159 |
|
160 |
=head2 finish |
161 |
|
162 |
Close MARC output file |
163 |
|
164 |
$marc->finish; |
165 |
|
166 |
It will also dump MARC lint warnings summary if called with C<lint>. |
167 |
|
168 |
=cut |
169 |
|
170 |
sub finish { |
171 |
my $self = shift; |
172 |
|
173 |
my $log = $self->get_logger; |
174 |
|
175 |
close( $self->{fh} ) or $log->logdie("can't close ", $self->{path}, ": $!"); |
176 |
|
177 |
if (my $w = $self->{_marc_lint_warnings}) { |
178 |
$log->error("MARC lint warnings summary:\n", |
179 |
join ("\n", |
180 |
map { $w->{$_} . "\t" . $_ } |
181 |
sort { $w->{$b} <=> $w->{$a} } keys %$w |
182 |
) |
183 |
); |
184 |
} |
185 |
} |
186 |
|
187 |
=head1 AUTHOR |
188 |
|
189 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
190 |
|
191 |
=head1 COPYRIGHT & LICENSE |
192 |
|
193 |
Copyright 2006 Dobrica Pavlinusic, All Rights Reserved. |
194 |
|
195 |
This program is free software; you can redistribute it and/or modify it |
196 |
under the same terms as Perl itself. |
197 |
|
198 |
=cut |
199 |
|
200 |
1; # End of WebPAC::Output::MARC |