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