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 |
); |
76 |
|
77 |
=cut |
78 |
|
79 |
sub add { |
80 |
my $self = shift; |
81 |
|
82 |
my $arg = {@_}; |
83 |
|
84 |
my $log = $self->_get_logger; |
85 |
|
86 |
$log->logconfess("add needs fields and id arguments") |
87 |
unless ($arg->{fields} && defined $arg->{id}); |
88 |
|
89 |
my $marc = new MARC::Record; |
90 |
$marc->encoding( $self->{encoding} ); |
91 |
|
92 |
my $id = $self->{id}; |
93 |
|
94 |
$log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY'); |
95 |
|
96 |
$marc->add_fields( @{ $arg->{fields} } ); |
97 |
|
98 |
# tweak leader |
99 |
if (my $new_l = $arg->{leader}) { |
100 |
|
101 |
my $leader = $marc->leader; |
102 |
|
103 |
foreach my $o ( keys %$new_l ) { |
104 |
my $insert = $new_l->{$o}; |
105 |
$leader = substr($leader, 0, $o) . |
106 |
$insert . substr($leader, $o+length($insert)); |
107 |
} |
108 |
$marc->leader( $leader ); |
109 |
} |
110 |
|
111 |
if ($self->{lint}) { |
112 |
$self->{lint}->check_record( $marc ); |
113 |
my $err = join( "\n", $self->{lint}->warnings ); |
114 |
$log->error("MARC lint detected warning on record $id\n", |
115 |
"<<<<< Original imput row:\n",dump($arg->{row}), "\n", |
116 |
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $arg->{fields} ), "\n", |
117 |
"!!!!! MARC lint warnings:\n",$err,"\n" |
118 |
) if ($err); |
119 |
} |
120 |
|
121 |
if ($self->{dump}) { |
122 |
$log->info("MARC record on record $id\n", |
123 |
"<<<<< Original imput row:\n",dump($self->{row}), "\n", |
124 |
">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $arg->{fields} ), "\n", |
125 |
); |
126 |
} |
127 |
|
128 |
print {$self->{fh}} $marc->as_usmarc; |
129 |
|
130 |
} |
131 |
|
132 |
=head2 finish |
133 |
|
134 |
Close MARC output file |
135 |
|
136 |
$marc->finish; |
137 |
|
138 |
=cut |
139 |
|
140 |
sub finish { |
141 |
my $self = shift; |
142 |
|
143 |
close( $self->{fh} ) or $self->_get_logger->logdie("can't close ", $self->{path}, ": $!"); |
144 |
} |
145 |
|
146 |
=head1 AUTHOR |
147 |
|
148 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
149 |
|
150 |
=head1 COPYRIGHT & LICENSE |
151 |
|
152 |
Copyright 2006 Dobrica Pavlinusic, All Rights Reserved. |
153 |
|
154 |
This program is free software; you can redistribute it and/or modify it |
155 |
under the same terms as Perl itself. |
156 |
|
157 |
=cut |
158 |
|
159 |
1; # End of WebPAC::Output::MARC |