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/;
14 WebPAC::Output::MARC - Create MARC records from C<marc_*> normalisation rules
22 our $VERSION = '0.01';
26 Create MARC records from C<marc_*> normalisation rules described in
34 my $marc = new WebPAC::Output::MARC(
35 path => '/path/to/output.marc',
48 my $log = $self->_get_logger;
51 $self->{lint}= new MARC::Lint or
52 $log->warn("Can't create MARC::Lint object, linting is disabled");
55 if (my $path = $self->{path}) {
56 open($self->{fh}, '>', $path) ||
57 $log->logdie("can't open MARC output $path: $!");
59 $log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', "\n");
61 $log->logconfess("new called without path");
64 $self->{encoding} ||= 'utf-8';
66 $self ? return $self : return undef;
73 fields => WebPAC::Normalize::_get_marc_fields(),
74 leader => WebPAC::Normalize::marc_leader(),
78 C<row> is optional parametar which is used when dumping original row to
88 my $log = $self->_get_logger;
90 $log->logconfess("add needs fields and id arguments")
91 unless ($arg->{fields} && defined $arg->{id});
93 my $marc = new MARC::Record;
94 $marc->encoding( $self->{encoding} );
98 $log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY');
100 $marc->add_fields( @{ $arg->{fields} } );
103 if (my $new_l = $arg->{leader}) {
105 my $leader = $marc->leader;
107 foreach my $o ( keys %$new_l ) {
108 my $insert = $new_l->{$o};
109 $leader = substr($leader, 0, $o) .
110 $insert . substr($leader, $o+length($insert));
112 $marc->leader( $leader );
116 $self->{lint}->check_record( $marc );
117 my @w = $self->{lint}->warnings;
119 $log->error("MARC lint detected warning on record $id\n",
120 "<<<<< Original imput row:\n",dump($arg->{row}), "\n",
121 ">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $arg->{fields} ), "\n",
122 "!!!!! MARC lint warnings:\n",join("\n",@w),"\n"
124 map { $self->{_marc_lint_warnings}->{$_}++ } @w;
129 $log->info("MARC record on record $id\n",
130 "<<<<< Original imput row:\n",dump($arg->{row}), "\n",
131 ">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $arg->{fields} ), "\n",
135 print {$self->{fh}} $marc->as_usmarc;
141 Close MARC output file
145 It will also dump MARC lint warnings summary if called with C<lint>.
152 my $log = $self->get_logger;
154 close( $self->{fh} ) or $log->logdie("can't close ", $self->{path}, ": $!");
156 if (my $w = $self->{_marc_lint_warnings}) {
157 $log->error("MARC lint warnings summary:\n",
159 map { $w->{$_} . "\t" . $_ }
160 sort { $w->{$b} <=> $w->{$a} } keys %$w
168 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
170 =head1 COPYRIGHT & LICENSE
172 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
174 This program is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
179 1; # End of WebPAC::Output::MARC