1 package WebPAC::Output::MARC;
6 use base qw/WebPAC::Common/;
8 use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net
10 use Data::Dump qw/dump/;
11 use Encode qw/from_to decode/;
15 WebPAC::Output::MARC - Create MARC records from C<marc_*> normalisation rules
23 our $VERSION = '0.04';
27 Create MARC records from C<marc_*> normalisation rules described in
35 my $marc = new WebPAC::Output::MARC(
36 path => '/path/to/output.marc',
37 native_encoding => 'iso-8859-2',
38 marc_encoding => 'utf-8',
50 my $log = $self->_get_logger;
53 $self->{lint}= new MARC::Lint or
54 $log->warn("Can't create MARC::Lint object, linting is disabled");
57 if (my $path = $self->{path}) {
58 open($self->{fh}, '>', $path) ||
59 $log->logdie("can't open MARC output $path: $!");
60 binmode($self->{fh}, ':utf8');
62 $log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', "\n");
64 $log->logconfess("new called without path");
67 $self->{native_encoding} ||= 'iso-8859-2';
68 $self->{marc_encoding} ||= 'utf-8';
70 $self ? return $self : return undef;
77 fields => WebPAC::Normalize::_get_marc_fields(),
78 leader => WebPAC::Normalize::_get_marc_leader(),
82 C<row> is optional parametar which is used when dumping original row to
92 my $log = $self->_get_logger;
94 $log->logconfess("add needs fields and id arguments")
95 unless ($arg->{fields} && defined $arg->{id});
97 my $marc = new MARC::Record;
98 $marc->encoding( $self->{marc_encoding} );
102 $log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY');
104 my $fields = $arg->{fields};
106 $log->debug("original fields = ", sub { dump( $fields ) });
108 # recode fields to marc_encoding
109 foreach my $j ( 0 .. $#$fields ) {
110 foreach my $i ( 0 .. ( ( $#{$fields->[$j]} - 3 ) / 2 ) ) {
111 my $f = $fields->[$j]->[ ($i * 2) + 4 ];
112 $f = decode( $self->{native_encoding}, $f );
113 $fields->[$j]->[ ($i * 2) + 4 ] = $f;
118 @$fields = sort { $a->[0] <=> $b->[0] } @$fields;
120 $log->debug("recode fields = ", sub { dump( $fields ) });
122 $marc->add_fields( @$fields );
125 if (my $new_l = $arg->{leader}) {
127 my $leader = $marc->leader;
129 foreach my $o ( keys %$new_l ) {
130 my $insert = $new_l->{$o};
131 $leader = substr($leader, 0, $o) .
132 $insert . substr($leader, $o+length($insert));
134 $marc->leader( $leader );
138 $self->{lint}->check_record( $marc );
139 my @w = $self->{lint}->warnings;
141 $log->error("MARC lint detected warning on record $id\n",
142 "<<<<< Original input row:\n",dump($arg->{row}), "\n",
143 ">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n",
144 "!!!!! MARC lint warnings:\n",join("\n",@w),"\n"
146 map { $self->{_marc_lint_warnings}->{$_}++ } @w;
151 $log->info("MARC record on record $id\n",
152 "<<<<< Original imput row:\n",dump($arg->{row}), "\n",
153 ">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n",
157 print {$self->{fh}} $marc->as_usmarc;
163 Close MARC output file
167 It will also dump MARC lint warnings summary if called with C<lint>.
174 my $log = $self->get_logger;
176 close( $self->{fh} ) or $log->logdie("can't close ", $self->{path}, ": $!");
178 if (my $w = $self->{_marc_lint_warnings}) {
179 $log->error("MARC lint warnings summary:\n",
181 map { $w->{$_} . "\t" . $_ }
182 sort { $w->{$b} <=> $w->{$a} } keys %$w
190 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
192 =head1 COPYRIGHT & LICENSE
194 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
196 This program is free software; you can redistribute it and/or modify it
197 under the same terms as Perl itself.
201 1; # End of WebPAC::Output::MARC