/[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 1024 - (show annotations)
Sat Nov 10 11:37:47 2007 UTC (16 years, 5 months ago) by dpavlin
File size: 5350 byte(s)
 r1586@llin:  dpavlin | 2007-11-10 12:37:44 +0100
 more re-factoring to support isis_template

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 @marc_out;
84
85 our $_template;
86
87 sub _parse_template {
88 my ( $name, $templates ) = @_;
89
90 foreach my $template ( @{ $templates } ) {
91 our $count = {};
92 our @marc_order = ();
93 sub my_count {
94 my $sf = shift;
95 my $nr = $count->{$sf}++;
96 push @marc_order, [ $sf, $nr ];
97 return $sf . $nr;
98 }
99 my $pos_template = $template;
100 $pos_template =~ s/($fields_re)/my_count($1)/ge;
101 my $count_key = dump( $count );
102 warn "### template: |$template| -> |$pos_template| count = $count_key marc_order = ",dump( @marc_order ),$/;
103 $_template->{$name}->{pos}->{ $count_key } = $pos_template;
104 $_template->{$name}->{order}->{ $pos_template } = [ @marc_order ];
105 }
106 warn "### from ",dump( $templates ), " created ", dump( $_template );
107 }
108
109 _parse_template( 'marc', $args->{marc_template} );
110 _parse_template( 'isis', $args->{isis_template} );
111 warn "### _template = ",dump( $_template );
112
113 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 my $template = $_template->{marc}->{pos}->{ $from_count_key } ||
145 die "I don't have template for:\n$from_count_key\n## available templates\n", dump( $_template );
146
147 warn "### selected template: |$template|\n";
148
149 our $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 warn "## template: |$template|\n## _template->marc = ",dump( $_template->{marc} );
176
177 foreach my $sf ( @{ $_template->{marc}->{order}->{$template} } ) {
178 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;

  ViewVC Help
Powered by ViewVC 1.1.26