1 package WebPAC::Normalize::MARC;
10 use Data::Dump qw/dump/;
13 use WebPAC::Normalize;
17 WebPAC::Normalize::MARC - create MARC/ISO2709 records
26 from => 225, to => 440,
40 'a, |x ; |v. |n, |p ; |v',
49 warn "## marc_template(",dump($args),")";
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';
56 die "marc_template needs isis_template or marc_template"
57 if ! defined $args->{isis_template} && ! defined $args->{marc_template};
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');
63 my @subfields_rename = @{ $args->{subfields_rename} };
64 # warn "### subfields_rename [$#subfields_rename] = ",dump( @subfields_rename );
66 confess "need mapping in pairs for subfields_rename"
67 if $#subfields_rename % 2 != 1;
69 my ( $subfields_rename, $from_subfields, $to_subfields );
70 while ( my ( $from, $to ) = splice(@subfields_rename, 0, 2) ) {
72 $from_subfields->{ $from }++,
73 $to_subfields->{ $to }++
75 $subfields_rename->{ $from }->[ $f ] = [ $to => $t ];
77 warn "### subfields_rename = ",dump( $subfields_rename ),$/;
78 warn "### from_subfields = ", dump( $from_subfields ),$/;
79 warn "### to_subfields = ", dump( $to_subfields ),$/;
81 my $fields_re = join('|', keys %$to_subfields );
87 my ( $marc_template_order, @marc_order ); # = _parse_template( $args->{marc_template} );
89 my $templates = $args->{marc_template};
91 foreach my $template ( @{ $templates } ) {
96 my $nr = $count->{$sf}++;
97 push @marc_order, [ $sf, $nr ];
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 ];
107 warn "### from ",dump( $templates ), " created ", dump( $pos_templates ), " and ", dump( $marc_template_order );
111 foreach my $r ( @{ $rec->{ $args->{from} } } ) {
113 my $i1 = $r->{i1} || ' ';
114 my $i2 = $r->{i2} || ' ';
115 $m = [ $args->{to}, $i1, $i2 ];
117 warn "### r = ",dump( $r );
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 ];
130 $to_count->{ $to_sf }++;
133 warn "### new_r = ",dump( $new_r );
135 my $from_count_key = dump( $to_count );
137 warn "### from_count = ",dump( $from_count ), $/;
138 warn "### to_count = ",dump( $to_count ), $/;
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 );
143 warn "### selected template: |$template|\n";
147 my @templates = split(/\|/, $template );
148 @templates = ( $template );
150 foreach my $sf ( @templates ) {
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;
163 die "requested subfield $from_sf/$from_nr but it's ",dump( $v );
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 ),$/;
171 warn "## template: |$template|\n## marc_template_order = ",dump( $marc_template_order );
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 );
180 warn "#### >>>> created marc: ", dump( $m );
185 warn "### marc_template produced: ",dump( @marc_out );
187 foreach my $marc ( @marc_out ) {
188 warn "+++ ",dump( $marc );
189 WebPAC::Normalize::_marc_push( $marc );