/[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 582 - (show annotations)
Tue Jul 4 11:36:21 2006 UTC (17 years, 10 months ago) by dpavlin
File size: 3621 byte(s)
 r802@llin:  dpavlin | 2006-07-04 13:36:05 +0200
 added MARC lint warning summary

1 package WebPAC::Output::MARC;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common/;
7
8 use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net
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.01
19
20 =cut
21
22 our $VERSION = '0.01';
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 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 if (my $path = $self->{path}) {
56 open($self->{fh}, '>', $path) ||
57 $log->logdie("can't open MARC output $path: $!");
58
59 $log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', "\n");
60 } else {
61 $log->logconfess("new called without path");
62 }
63
64 $self->{encoding} ||= 'utf-8';
65
66 $self ? return $self : return undef;
67 }
68
69 =head2 add
70
71 $marc->add(
72 id => $mfn,
73 fields => WebPAC::Normalize::_get_marc_fields(),
74 leader => WebPAC::Normalize::marc_leader(),
75 row => $row,
76 );
77
78 C<row> is optional parametar which is used when dumping original row to
79 error log.
80
81 =cut
82
83 sub add {
84 my $self = shift;
85
86 my $arg = {@_};
87
88 my $log = $self->_get_logger;
89
90 $log->logconfess("add needs fields and id arguments")
91 unless ($arg->{fields} && defined $arg->{id});
92
93 my $marc = new MARC::Record;
94 $marc->encoding( $self->{encoding} );
95
96 my $id = $self->{id};
97
98 $log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY');
99
100 $marc->add_fields( @{ $arg->{fields} } );
101
102 # tweak leader
103 if (my $new_l = $arg->{leader}) {
104
105 my $leader = $marc->leader;
106
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));
111 }
112 $marc->leader( $leader );
113 }
114
115 if ($self->{lint}) {
116 $self->{lint}->check_record( $marc );
117 my @w = $self->{lint}->warnings;
118 if (@w) {
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"
123 );
124 map { $self->{_marc_lint_warnings}->{$_}++ } @w;
125 }
126 }
127
128 if ($self->{dump}) {
129 $log->info("MARC record on record $id\n",
130 "<<<<< Original imput row:\n",dump($self->{row}), "\n",
131 ">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $arg->{fields} ), "\n",
132 );
133 }
134
135 print {$self->{fh}} $marc->as_usmarc;
136
137 }
138
139 =head2 finish
140
141 Close MARC output file
142
143 $marc->finish;
144
145 It will also dump MARC lint warnings summary if called with C<lint>.
146
147 =cut
148
149 sub finish {
150 my $self = shift;
151
152 my $log = $self->get_logger;
153
154 close( $self->{fh} ) or $log->logdie("can't close ", $self->{path}, ": $!");
155
156 if (my $w = $self->{_marc_lint_warnings}) {
157 $log->error("MARC lint warnings summary:\n",
158 join ("\n",
159 map { $w->{$_} . "\t" . $_ }
160 sort { $w->{$b} <=> $w->{$a} } keys %$w
161 )
162 );
163 }
164 }
165
166 =head1 AUTHOR
167
168 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
169
170 =head1 COPYRIGHT & LICENSE
171
172 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
173
174 This program is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
176
177 =cut
178
179 1; # End of WebPAC::Output::MARC

  ViewVC Help
Powered by ViewVC 1.1.26