X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=lib%2FWebPAC%2FNormalize%2FMARC.pm;h=b931f4582ccf723ea175a29c66689a2ff16e4f6e;hb=59ffd3d64d936929c7365f94ef952044fa2330d8;hp=6aa45ed6a2541e03009b293f6649fef2d2758e75;hpb=43902a5137590e45a90040ef8401f327452fee3b;p=webpac2 diff --git a/lib/WebPAC/Normalize/MARC.pm b/lib/WebPAC/Normalize/MARC.pm index 6aa45ed..b931f45 100644 --- a/lib/WebPAC/Normalize/MARC.pm +++ b/lib/WebPAC/Normalize/MARC.pm @@ -78,32 +78,37 @@ sub marc_template { warn "### from_subfields = ", dump( $from_subfields ),$/; warn "### to_subfields = ", dump( $to_subfields ),$/; - my $fields_re = join('|', keys %$to_subfields ); + our $_template; + + $_template->{fields_re} = { + isis => join('|', keys %$from_subfields ), + marc => join('|', keys %$to_subfields ), + }; my @marc_out; - - our $_template; sub _parse_template { my ( $name, $templates ) = @_; + my $fields_re = $_template->{fields_re}->{ $name } || die "can't find $name in ",dump( $_template->{fields_re} ); + foreach my $template ( @{ $templates } ) { our $count = {}; - our @marc_order = (); + our @order = (); sub my_count { my $sf = shift; my $nr = $count->{$sf}++; - push @marc_order, [ $sf, $nr ]; + push @order, [ $sf, $nr ]; return $sf . $nr; } my $pos_template = $template; $pos_template =~ s/($fields_re)/my_count($1)/ge; my $count_key = dump( $count ); - warn "### template: |$template| -> |$pos_template| count = $count_key marc_order = ",dump( @marc_order ),$/; + warn "### template: |$template| -> |$pos_template| count = $count_key order = ",dump( @order ),$/; $_template->{$name}->{pos}->{ $count_key } = $pos_template; - $_template->{$name}->{order}->{ $pos_template } = [ @marc_order ]; + $_template->{$name}->{order}->{ $pos_template } = [ @order ]; } - warn "### from ",dump( $templates ), " created ", dump( $_template ); + warn "### from ",dump( $templates ), " using $fields_re created ", dump( $_template ); } _parse_template( 'marc', $args->{marc_template} ); @@ -136,57 +141,81 @@ sub marc_template { warn "### new_r = ",dump( $new_r ); - my $from_count_key = dump( $to_count ); - - warn "### from_count = ",dump( $from_count ), $/; - warn "### to_count = ",dump( $to_count ), $/; - - my $template = $_template->{marc}->{pos}->{ $from_count_key } || - die "I don't have template for:\n$from_count_key\n## available templates\n", dump( $_template ); - - warn "### selected template: |$template|\n"; + my $count_key = { + from => dump( $from_count ), + to => dump( $to_count), + }; + + warn "### count_key = ",dump( $count_key ), $/; + + my $processed_templates = 0; + + # this defines order of traversal + foreach ( qw/isis:from marc:to/ ) { + my ($name,$count_name) = split(/:/); + warn "## traverse $name $count_name\n"; + + my $ckey = $count_key->{$count_name} || die "can't find count_key $count_name in ",dump( $count_key ); + + my $template = $_template->{$name}->{pos}->{ $ckey } || next; + $processed_templates++; + + warn "### selected template: |$template|\n"; + + our $fill_in = {}; + + my @templates = split(/\|/, $template ); + @templates = ( $template ); + + foreach my $sf ( @templates ) { + sub fill_in { + my ( $name, $r, $sf, $nr ) = @_; + my ( $from_sf, $from_nr, $v ); + if ( $name eq 'marc' ) { + ( $from_sf, $from_nr ) = @{ $new_r->{$sf}->[$nr] }; + } else { + ( $from_sf, $from_nr ) = ( $sf, $nr ); + } + my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr"; + warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ), $/; + if ( ref( $v ) eq 'ARRAY' ) { + $fill_in->{$sf}->[$nr] = $v->[$from_nr]; + return $v->[$from_nr]; + } elsif ( $from_nr == 0 ) { + $fill_in->{$sf}->[$nr] = $v; + return $v; + } else { + die "requested subfield $from_sf/$from_nr but it's ",dump( $v ); + } + } + my $fields_re = $_template->{fields_re}->{ $name } || die "can't find $name in ",dump( $_template->{fields_re} ); + warn "#### $sf <<<< $fields_re\n"; + $sf =~ s/($fields_re)(\d+)/fill_in($name,$r,$1,$2)/ge; + warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/; + } - our $fill_in = {}; + warn "## template: |$template|\n## _template->$name = ",dump( $_template->{$name} ); - my @templates = split(/\|/, $template ); - @templates = ( $template ); + $sf_pos = $#m; - foreach my $sf ( @templates ) { - sub fill_in { - my ( $r, $sf, $nr ) = @_; - my ( $from_sf, $from_nr ) = @{ $new_r->{ $sf }->[ $nr ] }; - my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr"; - warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ), $/; - if ( ref( $v ) eq 'ARRAY' ) { - $fill_in->{$sf}->[$nr] = $v->[$from_nr]; - return $v->[$from_nr]; - } elsif ( $from_nr == 0 ) { - $fill_in->{$sf}->[$nr] = $v; - return $v; - } else { - die "requested subfield $from_sf/$from_nr but it's ",dump( $v ); - } + foreach my $sf ( @{ $_template->{$name}->{order}->{$template} } ) { + my ( $sf, $nr ) = @$sf; + my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr"; + warn "++ $sf/$nr |$v|\n"; + push @$m, ( $sf, $v ); } - warn "#### $sf <<<< $fields_re\n"; - $sf =~ s/($fields_re)(\d+)/fill_in($r,$1,$2)/ge; - warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/; - } - warn "## template: |$template|\n## _template->marc = ",dump( $_template->{marc} ); + warn "#### >>>> created MARC record: ", dump( $m ); - foreach my $sf ( @{ $_template->{marc}->{order}->{$template} } ) { - my ( $sf, $nr ) = @$sf; - my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr"; - warn "++ $sf/$nr |$v|\n"; - push @$m, ( $sf, $v ); + push @marc_out, $m; } - - warn "#### >>>> created marc: ", dump( $m ); - - push @marc_out, $m; + + die "I don't have template for fields ",dump( $count_key ), "\n## available templates\n", dump( $_template ) unless $processed_templates; + warn ">>> $processed_templates templates applied to data\n"; } - warn "### marc_template produced: ",dump( @marc_out ); + + warn "### marc_template produced following MARC records: ",dump( @marc_out ); foreach my $marc ( @marc_out ) { warn "+++ ",dump( $marc );