--- trunk/lib/WebPAC/Normalize/MARC.pm 2007/11/10 11:13:58 1023 +++ trunk/lib/WebPAC/Normalize/MARC.pm 2007/11/12 10:16:16 1035 @@ -12,6 +12,8 @@ use WebPAC::Normalize; +my $debug = 0; + =head1 NAME WebPAC::Normalize::MARC - create MARC/ISO2709 records @@ -42,14 +44,16 @@ ], ); +Returns number of records produced. + =cut sub marc_template { my $args = {@_}; - warn "## marc_template(",dump($args),")"; + warn "## marc_template(",dump($args),")",$/ if $debug; foreach ( qw/subfields_rename isis_template marc_template/ ) { -# warn "ref($_) = ",ref($args->{$_}); +# warn "ref($_) = ",ref($args->{$_}) if $debug; die "$_ not ARRAY" if defined($args->{$_}) && ref($args->{$_}) ne 'ARRAY'; } @@ -61,12 +65,13 @@ die "record field ", $args->{from}, " isn't array ",dump( $rec ) unless (ref($r) eq 'ARRAY'); my @subfields_rename = @{ $args->{subfields_rename} }; -# warn "### subfields_rename [$#subfields_rename] = ",dump( @subfields_rename ); +# warn "### subfields_rename [$#subfields_rename] = ",dump( @subfields_rename ) if $debug; confess "need mapping in pairs for subfields_rename" if $#subfields_rename % 2 != 1; - my ( $subfields_rename, $from_subfields, $to_subfields ); + my ( $subfields_rename, $from_subfields ); + our $to_subfields = {}; while ( my ( $from, $to ) = splice(@subfields_rename, 0, 2) ) { my ( $f, $t ) = ( $from_subfields->{ $from }++, @@ -74,41 +79,44 @@ ); $subfields_rename->{ $from }->[ $f ] = [ $to => $t ]; } - warn "### subfields_rename = ",dump( $subfields_rename ),$/; - warn "### from_subfields = ", dump( $from_subfields ),$/; - warn "### to_subfields = ", dump( $to_subfields ),$/; + warn "### subfields_rename = ",dump( $subfields_rename ),$/ if $debug; + warn "### from_subfields = ", dump( $from_subfields ),$/ if $debug; + warn "### to_subfields = ", dump( $to_subfields ),$/ if $debug; - my $fields_re = join('|', keys %$to_subfields ); + our $_template; - my $fill_in; - my @marc_out; + $_template->{isis}->{fields_re} = join('|', keys %$from_subfields ); + $_template->{marc}->{fields_re} = join('|', keys %$to_subfields ); - my $marc_template; - _parse_template( $args->{marc_template}, $marc_template ); + my @marc_out; sub _parse_template { - my ( $templates, $marc_template_order ) = @_; + my ( $name, $templates ) = @_; + + my $fields_re = $_template->{$name}->{fields_re} || die "can't find $name in ",dump( $_template->{$name}->{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 ),$/; - $marc_template->{pos}->{ $count_key } = $pos_template; - $marc_template->{order}->{ $pos_template } = [ @marc_order ]; + warn "### template: |$template| -> |$pos_template| count = $count_key order = ",dump( @order ),$/ if $debug; + $_template->{$name}->{pos}->{ $count_key } = $pos_template; + $_template->{$name}->{order}->{ $pos_template } = [ @order ]; } - warn "### from ",dump( $templates ), " created ", dump( $marc_template ); + warn "### from ",dump( $templates ), " using $fields_re created ", dump( $_template ),$/ if $debug; } - warn "### marc_template = ",dump( $marc_template ); + _parse_template( 'marc', $args->{marc_template} ); + _parse_template( 'isis', $args->{isis_template} ); + warn "### _template = ",dump( $_template ),$/ if $debug; my $m; @@ -118,80 +126,117 @@ my $i2 = $r->{i2} || ' '; $m = [ $args->{to}, $i1, $i2 ]; - warn "### r = ",dump( $r ); + warn "### r = ",dump( $r ),$/ if $debug; - my ( $new_r, $from_count, $to_count ); - foreach my $sf ( keys %{$r} ) { + my ( $from_mapping, $to_mapping, $from_count, $to_count ); + foreach my $from_sf ( keys %{$r} ) { # skip everything which isn't one char subfield (e.g. 'subfields') - next unless $sf =~ m/^\w$/; - my $nr = $from_count->{$sf}++; - my $rename_to = $subfields_rename->{ $sf } || - die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename ); - warn "### rename $sf/$nr to ", dump( $rename_to->[$nr] ), $/; - my ( $to_sf, $to_nr ) = @{ $rename_to->[$nr] }; - $new_r->{ $to_sf }->[ $to_nr ] = [ $sf => $nr ]; - - $to_count->{ $to_sf }++; - } - - 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 = $marc_template->{pos}->{ $from_count_key } || - die "I don't have template for:\n$from_count_key\n## available templates\n", dump( $marc_template->{pos} ); + next unless $from_sf =~ m/^\w$/; + my $from_nr = $from_count->{$from_sf}++; + my $rename_to = $subfields_rename->{ $from_sf } || + die "can't find subfield rename for $from_sf/$from_nr in ", dump( $subfields_rename ); + my ( $to_sf, $to_nr ) = @{ $rename_to->[$from_nr] }; + $to_mapping->{ $to_sf }->[ $to_nr ] = [ $from_sf => $from_nr ]; - warn "### selected template: |$template|\n"; + my $to_nr2 = $to_count->{ $to_sf }++; + $from_mapping->{ $from_sf }->[ $from_nr ] = [ $to_sf => $to_nr2 ]; - $fill_in = {}; + warn "### from $from_sf/$from_nr -> $to_sf/$to_nr\tto $from_sf/$from_nr -> $to_sf/$to_nr2\n" if $debug; + } - my @templates = split(/\|/, $template ); - @templates = ( $template ); + warn "### from_mapping = ",dump( $from_mapping ), "\n### to_mapping = ",dump( $to_mapping ),$/ if $debug; - 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 ) { + my $count_key = { + from => dump( $from_count ), + to => dump( $to_count), + }; + + warn "### count_key = ",dump( $count_key ),$/ if $debug; + + my $processed_templates = 0; + + # this defines order of traversal + foreach ( qw/isis:from marc:to/ ) { + my ($name,$count_name) = split(/:/); + + 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 "### traverse $name $count_name selected template: |$template|\n" if $debug; + + our $fill_in = {}; + + my @templates = split(/\|/, $template ); + @templates = ( $template ) unless @templates; + + warn "### templates = ",dump( @templates ),$/ if $debug; + + foreach my $sf ( @templates ) { + sub fill_in { + my ( $name, $r, $pre, $sf, $nr, $post ) = @_; + warn "#### fill_in( $name, r, '$pre', $sf, $nr, '$post' )\n" if $debug; + my ( $from_sf, $from_nr ); + if ( $name eq 'marc' ) { + die "no $sf/$nr in to_mapping: ",dump( $to_mapping ), " form record ",dump( $r ) unless defined $to_mapping->{$sf}->[$nr]; + ( $from_sf, $from_nr ) = @{ $to_mapping->{$sf}->[$nr] }; + } else { + ( $from_sf, $from_nr ) = ( $sf, $nr ); + } + my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr"; + if ( ref( $v ) eq 'ARRAY' ) { + $v = $pre . $v->[$from_nr] . $post; + } elsif ( $from_nr == 0 ) { + $v = $pre . $v . $post; + } else { + die "requested subfield $from_sf/$from_nr but it's ",dump( $v ); + } + warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ),$/ if $debug; $fill_in->{$sf}->[$nr] = $v; return $v; - } else { - die "requested subfield $from_sf/$from_nr but it's ",dump( $v ); } + my $fields_re = $_template->{$name}->{fields_re} || die "can't find $name in ",dump( $_template->{$name}->{fields_re} ); + warn "#### $sf <<<< $fields_re\n" if $debug; + $sf =~ s/^(.*?)($fields_re)(\d+)(.*?)$/fill_in($name,$r,$1,$2,$3,$4)/ge; + warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/ if $debug; } - 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## marc_template = ",dump( $marc_template ); + warn "## template: |$template|\n## _template->$name = ",dump( $_template->{$name} ),$/ if $debug; - foreach my $sf ( @{ $marc_template->{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 ); - } + 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"; + if ( $name eq 'isis') { + ( $sf, $nr ) = @{ $from_mapping->{$sf}->[$nr] }; + } + warn "++ $sf/$nr |$v|\n" if $debug; + push @$m, ( $sf, $v ); + } + + warn "#### >>>> created MARC record: ", dump( $m ),$/ if $debug; - warn "#### >>>> created marc: ", dump( $m ); + push @marc_out, $m; - push @marc_out, $m; + last; + } + + 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",$/ if $debug; } - warn "### marc_template produced: ",dump( @marc_out ); + + my $recs = 0; foreach my $marc ( @marc_out ) { - warn "+++ ",dump( $marc ); + warn "+++ ",dump( $marc ),$/ if $debug; WebPAC::Normalize::_marc_push( $marc ); + $recs++; } + + warn "### marc_template produced $recs MARC records: ",dump( @marc_out ),$/ if $debug; + + return $recs; } 1;