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',
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 $self->{marc_encoding} ||= 'utf-8';
59 if (my $path = $self->{path}) {
60 open($self->{fh}, '>', $path . '.marc') ||
61 $log->logdie("can't open MARC output $path: $!");
62 binmode($self->{fh}, ':utf8');
64 $log->info("Creating MARC export file $path.marc", $self->{lint} ? ' (with lint)' : '', " encoding ", $self->{marc_encoding}, "\n");
65 if ( $self->{marcxml} || $ENV{MARCXML} ) {
66 open($self->{fh_marcxml}, '>:utf8', "$path.marcxml") ||
67 $log->logdie("can't open MARCXML output $path.marcxml: $!");
68 $log->info("Creating MARCXML export file $path.marcxml");
69 print {$self->{fh_marcxml}} qq{<?xml version="1.0" encoding="UTF-8"?>\n<collection>\n};
72 $log->logconfess("new called without path");
75 $self ? return $self : return undef;
82 fields => WebPAC::Normalize::_get_marc_fields(),
83 leader => WebPAC::Normalize::_get_marc_leader(),
87 C<row> is optional parametar which is used when dumping original row to
97 my $log = $self->_get_logger;
99 $log->logconfess("add needs fields and id arguments")
100 unless ($arg->{fields} && defined $arg->{id});
102 my $marc = new MARC::Record;
103 $marc->encoding( $self->{marc_encoding} );
107 $log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY');
109 my $fields = $arg->{fields};
111 $log->debug("original fields = ", sub { dump( $fields ) });
113 # recode fields to marc_encoding
114 foreach my $j ( 0 .. $#$fields ) {
115 foreach my $i ( 0 .. ( ( $#{$fields->[$j]} - 3 ) / 2 ) ) {
116 my $f = $fields->[$j]->[ ($i * 2) + 4 ];
117 $fields->[$j]->[ ($i * 2) + 4 ] = $f;
122 @$fields = sort { $a->[0] <=> $b->[0] } @$fields;
124 $log->debug("recode fields = ", sub { dump( $fields ) });
126 $marc->add_fields( @$fields );
129 if (my $new_l = $arg->{leader}) {
131 my $leader = $marc->leader;
133 foreach my $o ( sort { $a <=> $b } keys %$new_l ) {
134 my $insert = $new_l->{$o};
135 $leader = substr($leader, 0, $o) .
136 $insert . substr($leader, $o+length($insert));
138 $marc->leader( $leader );
142 $self->{lint}->check_record( $marc );
143 my @w = $self->{lint}->warnings;
145 $log->error("MARC lint detected warning on record $id\n",
146 "<<<<< Original input row:\n",dump($arg->{row}), "\n",
147 ">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n",
148 "!!!!! MARC lint warnings:\n",join("\n",@w),"\n"
150 map { $self->{_marc_lint_warnings}->{$_}++ } @w;
155 $log->info("MARC record on record $id\n",
156 "<<<<< Original imput row:\n",dump($arg->{row}), "\n",
157 ">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n",
161 print {$self->{fh}} $marc->as_usmarc;
163 if ( $self->{fh_marcxml} ) {
164 my $xml = $marc->as_xml_record;
165 $xml =~ s/\Q<?xml version="1.0" encoding="UTF-8"?>\E//;
166 print {$self->{fh_marcxml}} $xml;
172 Close MARC output file
176 It will also dump MARC lint warnings summary if called with C<lint>.
183 my $log = $self->get_logger;
185 close( $self->{fh} ) or $log->logdie("can't close ", $self->{path}, ".marc: $!");
187 if ( $self->{fh_marcxml} ) {
188 print {$self->{fh_marcxml}} qq{</collection>\n};
189 $log->info("MARCXML file ", $self->{path}, ".marcxml ", -s $self->{fh_marcxml}, " bytes");
190 close( $self->{fh_marcxml} );
192 if (my $w = $self->{_marc_lint_warnings}) {
193 $log->error("MARC lint warnings summary:\n",
195 map { $w->{$_} . "\t" . $_ }
196 sort { $w->{$b} <=> $w->{$a} } keys %$w
204 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
206 =head1 COPYRIGHT & LICENSE
208 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
210 This program is free software; you can redistribute it and/or modify it
211 under the same terms as Perl itself.
215 1; # End of WebPAC::Output::MARC