/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 662 - (hide annotations)
Fri Sep 8 17:48:43 2006 UTC (17 years, 8 months ago) by dpavlin
File size: 4190 byte(s)
open marc output file with :utf8 layer, with previous fix it fixes encoding in MARC output

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

  ViewVC Help
Powered by ViewVC 1.1.26