1 |
dpavlin |
1021 |
package WebPAC::Normalize::MARC; |
2 |
|
|
use Exporter 'import'; |
3 |
|
|
@EXPORT = qw/ |
4 |
|
|
marc_template |
5 |
|
|
/; |
6 |
|
|
|
7 |
|
|
use strict; |
8 |
|
|
use warnings; |
9 |
|
|
|
10 |
|
|
use Data::Dump qw/dump/; |
11 |
|
|
use Carp qw/confess/; |
12 |
|
|
|
13 |
|
|
use WebPAC::Normalize; |
14 |
|
|
|
15 |
|
|
=head1 NAME |
16 |
|
|
|
17 |
|
|
WebPAC::Normalize::MARC - create MARC/ISO2709 records |
18 |
|
|
|
19 |
|
|
=cut |
20 |
|
|
|
21 |
|
|
=head1 FUNCTIONS |
22 |
|
|
|
23 |
|
|
=head2 marc_template |
24 |
|
|
|
25 |
|
|
marc_template( |
26 |
|
|
from => 225, to => 440, |
27 |
|
|
subfields_rename => [ |
28 |
|
|
'a' => 'a', |
29 |
|
|
'x' => 'x', |
30 |
|
|
'v' => 'v', |
31 |
|
|
'h' => 'n', |
32 |
|
|
'i' => 'p', |
33 |
|
|
'w' => 'v', |
34 |
|
|
], |
35 |
|
|
isis_template => [ |
36 |
|
|
'a ; |v. |i', |
37 |
|
|
'a. |i ; |w', |
38 |
|
|
], |
39 |
|
|
marc_template => [ |
40 |
|
|
'a, |x ; |v. |n, |p ; |v', |
41 |
|
|
'a ; |v. |p ; |v', |
42 |
|
|
], |
43 |
|
|
); |
44 |
|
|
|
45 |
|
|
=cut |
46 |
|
|
|
47 |
|
|
sub marc_template { |
48 |
|
|
my $args = {@_}; |
49 |
|
|
warn "## marc_template(",dump($args),")"; |
50 |
|
|
|
51 |
|
|
foreach ( qw/subfields_rename isis_template marc_template/ ) { |
52 |
|
|
# warn "ref($_) = ",ref($args->{$_}); |
53 |
|
|
die "$_ not ARRAY" if defined($args->{$_}) && ref($args->{$_}) ne 'ARRAY'; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
die "marc_template needs isis_template or marc_template" |
57 |
|
|
if ! defined $args->{isis_template} && ! defined $args->{marc_template}; |
58 |
|
|
|
59 |
|
|
my $rec = WebPAC::Normalize::_get_rec() || die "_get_rec?"; |
60 |
|
|
my $r = $rec->{ $args->{from} } || return; |
61 |
|
|
die "record field ", $args->{from}, " isn't array ",dump( $rec ) unless (ref($r) eq 'ARRAY'); |
62 |
|
|
|
63 |
|
|
my @subfields_rename = @{ $args->{subfields_rename} }; |
64 |
|
|
# warn "### subfields_rename [$#subfields_rename] = ",dump( @subfields_rename ); |
65 |
|
|
|
66 |
|
|
confess "need mapping in pairs for subfields_rename" |
67 |
|
|
if $#subfields_rename % 2 != 1; |
68 |
|
|
|
69 |
|
|
my ( $subfields_rename, $from_subfields, $to_subfields ); |
70 |
|
|
while ( my ( $from, $to ) = splice(@subfields_rename, 0, 2) ) { |
71 |
|
|
my ( $f, $t ) = ( |
72 |
|
|
$from_subfields->{ $from }++, |
73 |
|
|
$to_subfields->{ $to }++ |
74 |
|
|
); |
75 |
|
|
$subfields_rename->{ $from }->[ $f ] = [ $to => $t ]; |
76 |
|
|
} |
77 |
|
|
warn "### subfields_rename = ",dump( $subfields_rename ),$/; |
78 |
|
|
warn "### from_subfields = ", dump( $from_subfields ),$/; |
79 |
|
|
warn "### to_subfields = ", dump( $to_subfields ),$/; |
80 |
|
|
|
81 |
|
|
my $fields_re = join('|', keys %$to_subfields ); |
82 |
|
|
|
83 |
|
|
my $fill_in; |
84 |
|
|
my @marc_out; |
85 |
|
|
|
86 |
dpavlin |
1023 |
my $marc_template; |
87 |
|
|
_parse_template( $args->{marc_template}, $marc_template ); |
88 |
dpavlin |
1021 |
|
89 |
dpavlin |
1023 |
sub _parse_template { |
90 |
|
|
my ( $templates, $marc_template_order ) = @_; |
91 |
dpavlin |
1021 |
|
92 |
dpavlin |
1023 |
foreach my $template ( @{ $templates } ) { |
93 |
|
|
our $count = {}; |
94 |
|
|
our @marc_order = (); |
95 |
|
|
sub my_count { |
96 |
|
|
my $sf = shift; |
97 |
|
|
my $nr = $count->{$sf}++; |
98 |
|
|
push @marc_order, [ $sf, $nr ]; |
99 |
|
|
return $sf . $nr; |
100 |
|
|
} |
101 |
|
|
my $pos_template = $template; |
102 |
|
|
$pos_template =~ s/($fields_re)/my_count($1)/ge; |
103 |
|
|
my $count_key = dump( $count ); |
104 |
|
|
warn "### template: |$template| -> |$pos_template| count = $count_key marc_order = ",dump( @marc_order ),$/; |
105 |
|
|
$marc_template->{pos}->{ $count_key } = $pos_template; |
106 |
|
|
$marc_template->{order}->{ $pos_template } = [ @marc_order ]; |
107 |
dpavlin |
1021 |
} |
108 |
dpavlin |
1023 |
warn "### from ",dump( $templates ), " created ", dump( $marc_template ); |
109 |
dpavlin |
1021 |
} |
110 |
|
|
|
111 |
dpavlin |
1023 |
warn "### marc_template = ",dump( $marc_template ); |
112 |
|
|
|
113 |
dpavlin |
1021 |
my $m; |
114 |
|
|
|
115 |
|
|
foreach my $r ( @{ $rec->{ $args->{from} } } ) { |
116 |
|
|
|
117 |
|
|
my $i1 = $r->{i1} || ' '; |
118 |
|
|
my $i2 = $r->{i2} || ' '; |
119 |
|
|
$m = [ $args->{to}, $i1, $i2 ]; |
120 |
|
|
|
121 |
|
|
warn "### r = ",dump( $r ); |
122 |
|
|
|
123 |
|
|
my ( $new_r, $from_count, $to_count ); |
124 |
|
|
foreach my $sf ( keys %{$r} ) { |
125 |
|
|
# skip everything which isn't one char subfield (e.g. 'subfields') |
126 |
|
|
next unless $sf =~ m/^\w$/; |
127 |
|
|
my $nr = $from_count->{$sf}++; |
128 |
|
|
my $rename_to = $subfields_rename->{ $sf } || |
129 |
|
|
die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename ); |
130 |
|
|
warn "### rename $sf/$nr to ", dump( $rename_to->[$nr] ), $/; |
131 |
|
|
my ( $to_sf, $to_nr ) = @{ $rename_to->[$nr] }; |
132 |
|
|
$new_r->{ $to_sf }->[ $to_nr ] = [ $sf => $nr ]; |
133 |
|
|
|
134 |
|
|
$to_count->{ $to_sf }++; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
warn "### new_r = ",dump( $new_r ); |
138 |
|
|
|
139 |
|
|
my $from_count_key = dump( $to_count ); |
140 |
|
|
|
141 |
|
|
warn "### from_count = ",dump( $from_count ), $/; |
142 |
|
|
warn "### to_count = ",dump( $to_count ), $/; |
143 |
|
|
|
144 |
dpavlin |
1023 |
my $template = $marc_template->{pos}->{ $from_count_key } || |
145 |
|
|
die "I don't have template for:\n$from_count_key\n## available templates\n", dump( $marc_template->{pos} ); |
146 |
dpavlin |
1021 |
|
147 |
|
|
warn "### selected template: |$template|\n"; |
148 |
|
|
|
149 |
|
|
$fill_in = {}; |
150 |
|
|
|
151 |
|
|
my @templates = split(/\|/, $template ); |
152 |
|
|
@templates = ( $template ); |
153 |
|
|
|
154 |
|
|
foreach my $sf ( @templates ) { |
155 |
|
|
sub fill_in { |
156 |
|
|
my ( $r, $sf, $nr ) = @_; |
157 |
|
|
my ( $from_sf, $from_nr ) = @{ $new_r->{ $sf }->[ $nr ] }; |
158 |
|
|
my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr"; |
159 |
|
|
warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ), $/; |
160 |
|
|
if ( ref( $v ) eq 'ARRAY' ) { |
161 |
|
|
$fill_in->{$sf}->[$nr] = $v->[$from_nr]; |
162 |
|
|
return $v->[$from_nr]; |
163 |
|
|
} elsif ( $from_nr == 0 ) { |
164 |
|
|
$fill_in->{$sf}->[$nr] = $v; |
165 |
|
|
return $v; |
166 |
|
|
} else { |
167 |
|
|
die "requested subfield $from_sf/$from_nr but it's ",dump( $v ); |
168 |
|
|
} |
169 |
|
|
} |
170 |
|
|
warn "#### $sf <<<< $fields_re\n"; |
171 |
|
|
$sf =~ s/($fields_re)(\d+)/fill_in($r,$1,$2)/ge; |
172 |
|
|
warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/; |
173 |
|
|
} |
174 |
|
|
|
175 |
dpavlin |
1023 |
warn "## template: |$template|\n## marc_template = ",dump( $marc_template ); |
176 |
dpavlin |
1021 |
|
177 |
dpavlin |
1023 |
foreach my $sf ( @{ $marc_template->{order}->{$template} } ) { |
178 |
dpavlin |
1021 |
my ( $sf, $nr ) = @$sf; |
179 |
|
|
my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr"; |
180 |
|
|
warn "++ $sf/$nr |$v|\n"; |
181 |
|
|
push @$m, ( $sf, $v ); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
warn "#### >>>> created marc: ", dump( $m ); |
185 |
|
|
|
186 |
|
|
push @marc_out, $m; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
warn "### marc_template produced: ",dump( @marc_out ); |
190 |
|
|
|
191 |
|
|
foreach my $marc ( @marc_out ) { |
192 |
|
|
warn "+++ ",dump( $marc ); |
193 |
|
|
WebPAC::Normalize::_marc_push( $marc ); |
194 |
|
|
} |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
1; |