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 );
88 my ( $name, $templates ) = @_;
90 foreach my $template ( @{ $templates } ) {
95 my $nr = $count->{$sf}++;
96 push @marc_order, [ $sf, $nr ];
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 ];
106 warn "### from ",dump( $templates ), " created ", dump( $_template );
109 _parse_template( 'marc', $args->{marc_template} );
110 _parse_template( 'isis', $args->{isis_template} );
111 warn "### _template = ",dump( $_template );
115 foreach my $r ( @{ $rec->{ $args->{from} } } ) {
117 my $i1 = $r->{i1} || ' ';
118 my $i2 = $r->{i2} || ' ';
119 $m = [ $args->{to}, $i1, $i2 ];
121 warn "### r = ",dump( $r );
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 ];
134 $to_count->{ $to_sf }++;
137 warn "### new_r = ",dump( $new_r );
139 my $from_count_key = dump( $to_count );
141 warn "### from_count = ",dump( $from_count ), $/;
142 warn "### to_count = ",dump( $to_count ), $/;
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 );
147 warn "### selected template: |$template|\n";
151 my @templates = split(/\|/, $template );
152 @templates = ( $template );
154 foreach my $sf ( @templates ) {
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;
167 die "requested subfield $from_sf/$from_nr but it's ",dump( $v );
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 ),$/;
175 warn "## template: |$template|\n## _template->marc = ",dump( $_template->{marc} );
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 );
184 warn "#### >>>> created marc: ", dump( $m );
189 warn "### marc_template produced: ",dump( @marc_out );
191 foreach my $marc ( @marc_out ) {
192 warn "+++ ",dump( $marc );
193 WebPAC::Normalize::_marc_push( $marc );