/[webpac2]/trunk/lib/WebPAC/Normalize/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/Normalize/MARC.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1021 - (show annotations)
Sat Nov 10 11:11:16 2007 UTC (16 years, 3 months ago) by dpavlin
File size: 5270 byte(s)
 r1579@llin:  dpavlin | 2007-11-10 11:59:27 +0100
 Begin extraction of MARC functionality from WebPAC::Normalize to
 WebPAC::Normalize::MARC

1 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 $pos_templates;
84 my $fill_in;
85 my @marc_out;
86
87 my ( $marc_template_order, @marc_order ); # = _parse_template( $args->{marc_template} );
88
89 my $templates = $args->{marc_template};
90
91 foreach my $template ( @{ $templates } ) {
92 my $count;
93 my @marc_order;
94 sub my_count {
95 my $sf = shift;
96 my $nr = $count->{$sf}++;
97 push @marc_order, [ $sf, $nr ];
98 return $sf . $nr;
99 }
100 my $pos_template = $template;
101 $pos_template =~ s/($fields_re)/my_count($1)/ge;
102 my $count_key = dump( $count );
103 warn "### template: |$template| -> |$pos_template| count = $count_key marc_order = ",dump( @marc_order ),$/;
104 $pos_templates->{ $count_key } = $pos_template;
105 $marc_template_order->{ $pos_template } = [ @marc_order ];
106 }
107 warn "### from ",dump( $templates ), " created ", dump( $pos_templates ), " and ", dump( $marc_template_order );
108
109 my $m;
110
111 foreach my $r ( @{ $rec->{ $args->{from} } } ) {
112
113 my $i1 = $r->{i1} || ' ';
114 my $i2 = $r->{i2} || ' ';
115 $m = [ $args->{to}, $i1, $i2 ];
116
117 warn "### r = ",dump( $r );
118
119 my ( $new_r, $from_count, $to_count );
120 foreach my $sf ( keys %{$r} ) {
121 # skip everything which isn't one char subfield (e.g. 'subfields')
122 next unless $sf =~ m/^\w$/;
123 my $nr = $from_count->{$sf}++;
124 my $rename_to = $subfields_rename->{ $sf } ||
125 die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename );
126 warn "### rename $sf/$nr to ", dump( $rename_to->[$nr] ), $/;
127 my ( $to_sf, $to_nr ) = @{ $rename_to->[$nr] };
128 $new_r->{ $to_sf }->[ $to_nr ] = [ $sf => $nr ];
129
130 $to_count->{ $to_sf }++;
131 }
132
133 warn "### new_r = ",dump( $new_r );
134
135 my $from_count_key = dump( $to_count );
136
137 warn "### from_count = ",dump( $from_count ), $/;
138 warn "### to_count = ",dump( $to_count ), $/;
139
140 my $template = $pos_templates->{ $from_count_key } ||
141 die "I don't have template for:\n$from_count_key\n## available templates\n", dump( $pos_templates );
142
143 warn "### selected template: |$template|\n";
144
145 $fill_in = {};
146
147 my @templates = split(/\|/, $template );
148 @templates = ( $template );
149
150 foreach my $sf ( @templates ) {
151 sub fill_in {
152 my ( $r, $sf, $nr ) = @_;
153 my ( $from_sf, $from_nr ) = @{ $new_r->{ $sf }->[ $nr ] };
154 my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr";
155 warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ), $/;
156 if ( ref( $v ) eq 'ARRAY' ) {
157 $fill_in->{$sf}->[$nr] = $v->[$from_nr];
158 return $v->[$from_nr];
159 } elsif ( $from_nr == 0 ) {
160 $fill_in->{$sf}->[$nr] = $v;
161 return $v;
162 } else {
163 die "requested subfield $from_sf/$from_nr but it's ",dump( $v );
164 }
165 }
166 warn "#### $sf <<<< $fields_re\n";
167 $sf =~ s/($fields_re)(\d+)/fill_in($r,$1,$2)/ge;
168 warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/;
169 }
170
171 warn "## template: |$template|\n## marc_template_order = ",dump( $marc_template_order );
172
173 foreach my $sf ( @{ $marc_template_order->{$template} } ) {
174 my ( $sf, $nr ) = @$sf;
175 my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr";
176 warn "++ $sf/$nr |$v|\n";
177 push @$m, ( $sf, $v );
178 }
179
180 warn "#### >>>> created marc: ", dump( $m );
181
182 push @marc_out, $m;
183 }
184
185 warn "### marc_template produced: ",dump( @marc_out );
186
187 foreach my $marc ( @marc_out ) {
188 warn "+++ ",dump( $marc );
189 WebPAC::Normalize::_marc_push( $marc );
190 }
191 }
192
193 1;

  ViewVC Help
Powered by ViewVC 1.1.26