1 package WebPAC::Output::MARC;
6 use base qw/WebPAC::Common/;
11 use Data::Dump qw/dump/;
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 marc_encoding => 'utf-8',
49 my $log = $self->_get_logger;
52 $self->{lint}= new MARC::Lint or
53 $log->warn("Can't create MARC::Lint object, linting is disabled");
56 $self->{marc_encoding} ||= 'utf-8';
58 if (my $path = $self->{path}) {
59 open($self->{fh}, '>', $path) ||
60 $log->logdie("can't open MARC output $path: $!");
61 binmode($self->{fh}, ':utf8');
63 $log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', " encoding ", $self->{marc_encoding}, "\n");
64 if ( $ENV{MARCXML} ) {
65 open($self->{fh_marcxml}, '>:utf8', "$path.marcxml");
66 print {$self->{fh_marcxml}} qq{<?xml version="1.0" encoding="UTF-8"?>\n<collection>\n};
69 $log->logconfess("new called without path");
72 $self ? return $self : return undef;
79 fields => WebPAC::Normalize::_get_marc_fields(),
80 leader => WebPAC::Normalize::_get_marc_leader(),
84 C<row> is optional parametar which is used when dumping original row to
94 my $log = $self->_get_logger;
96 $log->logconfess("add needs fields and id arguments")
97 unless ($arg->{fields} && defined $arg->{id});
99 my $marc = new MARC::Record;
100 $marc->encoding( $self->{marc_encoding} );
104 $log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY');
106 my $fields = $arg->{fields};
108 $log->debug("original fields = ", sub { dump( $fields ) });
110 # recode fields to marc_encoding
111 foreach my $j ( 0 .. $#$fields ) {
112 foreach my $i ( 0 .. ( ( $#{$fields->[$j]} - 3 ) / 2 ) ) {
113 my $f = $fields->[$j]->[ ($i * 2) + 4 ];
114 $fields->[$j]->[ ($i * 2) + 4 ] = $f;
119 @$fields = sort { $a->[0] <=> $b->[0] } @$fields;
121 $log->debug("recode fields = ", sub { dump( $fields ) });
123 $marc->add_fields( @$fields );
126 if (my $new_l = $arg->{leader}) {
128 my $leader = $marc->leader;
130 foreach my $o ( sort { $a <=> $b } keys %$new_l ) {
131 my $insert = $new_l->{$o};
132 $leader = substr($leader, 0, $o) .
133 $insert . substr($leader, $o+length($insert));
135 $marc->leader( $leader );
139 $self->{lint}->check_record( $marc );
140 my @w = $self->{lint}->warnings;
142 $log->error("MARC lint detected warning on record $id\n",
143 "<<<<< Original input row:\n",dump($arg->{row}), "\n",
144 ">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n",
145 "!!!!! MARC lint warnings:\n",join("\n",@w),"\n"
147 map { $self->{_marc_lint_warnings}->{$_}++ } @w;
152 $log->info("MARC record on record $id\n",
153 "<<<<< Original imput row:\n",dump($arg->{row}), "\n",
154 ">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n",
158 print {$self->{fh}} $marc->as_usmarc;
160 if ( $self->{fh_marcxml} ) {
161 my $xml = $marc->as_xml_record;
162 $xml =~ s/\Q<?xml version="1.0" encoding="UTF-8"?>\E//;
163 print {$self->{fh_marcxml}} $xml;
169 Close MARC output file
173 It will also dump MARC lint warnings summary if called with C<lint>.
180 my $log = $self->get_logger;
182 close( $self->{fh} ) or $log->logdie("can't close ", $self->{path}, ": $!");
184 if ( $self->{fh_marcxml} ) {
185 print {$self->{fh_marcxml}} qq{</collection>\n};
186 $log->info("MARCXML file size ", -s $self->{fh_marcxml}, " bytes");
187 close( $self->{fh_marcxml} );
189 if (my $w = $self->{_marc_lint_warnings}) {
190 $log->error("MARC lint warnings summary:\n",
192 map { $w->{$_} . "\t" . $_ }
193 sort { $w->{$b} <=> $w->{$a} } keys %$w
201 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
203 =head1 COPYRIGHT & LICENSE
205 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
207 This program is free software; you can redistribute it and/or modify it
208 under the same terms as Perl itself.
212 1; # End of WebPAC::Output::MARC