/[webpac2]/trunk/lib/WebPAC/Output/MARC.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/WebPAC/Output/MARC.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1187 - (show annotations)
Wed May 20 14:23:54 2009 UTC (14 years, 11 months ago) by dpavlin
File size: 4077 byte(s)
display marc encoding (which is utf-8 by default)

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

  ViewVC Help
Powered by ViewVC 1.1.26