/[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

Diff of /trunk/lib/WebPAC/Normalize/MARC.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1023 by dpavlin, Sat Nov 10 11:13:58 2007 UTC revision 1028 by dpavlin, Sun Nov 11 14:22:57 2007 UTC
# Line 12  use Carp qw/confess/; Line 12  use Carp qw/confess/;
12    
13  use WebPAC::Normalize;  use WebPAC::Normalize;
14    
15    my $debug = 0;
16    
17  =head1 NAME  =head1 NAME
18    
19  WebPAC::Normalize::MARC - create MARC/ISO2709 records  WebPAC::Normalize::MARC - create MARC/ISO2709 records
# Line 42  WebPAC::Normalize::MARC - create MARC/IS Line 44  WebPAC::Normalize::MARC - create MARC/IS
44                  ],                  ],
45          );          );
46    
47    Returns number of records produced.
48    
49  =cut  =cut
50    
51  sub marc_template {  sub marc_template {
52          my $args = {@_};          my $args = {@_};
53          warn "## marc_template(",dump($args),")";          warn "## marc_template(",dump($args),")",$/ if $debug;
54    
55          foreach ( qw/subfields_rename isis_template marc_template/ ) {          foreach ( qw/subfields_rename isis_template marc_template/ ) {
56  #               warn "ref($_) = ",ref($args->{$_});  #               warn "ref($_) = ",ref($args->{$_})  if $debug;
57                  die "$_ not ARRAY" if defined($args->{$_}) && ref($args->{$_}) ne 'ARRAY';                  die "$_ not ARRAY" if defined($args->{$_}) && ref($args->{$_}) ne 'ARRAY';
58          }          }
59    
# Line 61  sub marc_template { Line 65  sub marc_template {
65          die "record field ", $args->{from}, " isn't array ",dump( $rec ) unless (ref($r) eq 'ARRAY');          die "record field ", $args->{from}, " isn't array ",dump( $rec ) unless (ref($r) eq 'ARRAY');
66    
67          my @subfields_rename = @{ $args->{subfields_rename} };          my @subfields_rename = @{ $args->{subfields_rename} };
68  #       warn "### subfields_rename [$#subfields_rename] = ",dump( @subfields_rename );  #       warn "### subfields_rename [$#subfields_rename] = ",dump( @subfields_rename )  if $debug;
69    
70          confess "need mapping in pairs for subfields_rename"          confess "need mapping in pairs for subfields_rename"
71                  if $#subfields_rename % 2 != 1;                  if $#subfields_rename % 2 != 1;
# Line 74  sub marc_template { Line 78  sub marc_template {
78                  );                  );
79                  $subfields_rename->{ $from }->[ $f ] = [ $to => $t ];                  $subfields_rename->{ $from }->[ $f ] = [ $to => $t ];
80          }          }
81          warn "### subfields_rename = ",dump( $subfields_rename ),$/;          warn "### subfields_rename = ",dump( $subfields_rename ),$/ if $debug;
82          warn "### from_subfields = ", dump( $from_subfields ),$/;          warn "### from_subfields = ", dump( $from_subfields ),$/ if $debug;
83          warn "### to_subfields = ", dump( $to_subfields ),$/;          warn "### to_subfields = ", dump( $to_subfields ),$/ if $debug;
84    
85          my $fields_re = join('|', keys %$to_subfields );          our $_template;
86    
87          my $fill_in;          $_template->{isis}->{fields_re} = join('|', keys %$from_subfields );
88          my @marc_out;          $_template->{marc}->{fields_re} = join('|', keys %$to_subfields );
89    
90          my $marc_template;          my @marc_out;
         _parse_template( $args->{marc_template}, $marc_template );  
91    
92          sub _parse_template {          sub _parse_template {
93                  my ( $templates, $marc_template_order ) = @_;                  my ( $name, $templates ) = @_;
94    
95                    my $fields_re = $_template->{$name}->{fields_re} || die "can't find $name in ",dump( $_template->{$name}->{fields_re} );
96    
97                  foreach my $template ( @{ $templates } ) {                  foreach my $template ( @{ $templates } ) {
98                          our $count = {};                          our $count = {};
99                          our @marc_order = ();                          our @order = ();
100                          sub my_count {                          sub my_count {
101                                  my $sf = shift;                                  my $sf = shift;
102                                  my $nr = $count->{$sf}++;                                  my $nr = $count->{$sf}++;
103                                  push @marc_order, [ $sf, $nr ];                                  push @order, [ $sf, $nr ];
104                                  return $sf . $nr;                                  return $sf . $nr;
105                          }                          }
106                          my $pos_template = $template;                          my $pos_template = $template;
107                          $pos_template =~ s/($fields_re)/my_count($1)/ge;                          $pos_template =~ s/($fields_re)/my_count($1)/ge;
108                          my $count_key = dump( $count );                          my $count_key = dump( $count );
109                          warn "### template: |$template| -> |$pos_template| count = $count_key marc_order = ",dump( @marc_order ),$/;                          warn "### template: |$template| -> |$pos_template| count = $count_key order = ",dump( @order ),$/ if $debug;
110                          $marc_template->{pos}->{ $count_key } = $pos_template;                          $_template->{$name}->{pos}->{ $count_key } = $pos_template;
111                          $marc_template->{order}->{ $pos_template } = [ @marc_order ];                          $_template->{$name}->{order}->{ $pos_template } = [ @order ];
112                  }                  }
113                  warn "### from ",dump( $templates ), " created ", dump( $marc_template );                  warn "### from ",dump( $templates ), " using $fields_re created ", dump( $_template ),$/ if $debug;
114          }          }
115    
116          warn "### marc_template = ",dump( $marc_template );          _parse_template( 'marc', $args->{marc_template} );
117            _parse_template( 'isis', $args->{isis_template} );
118            warn "### _template = ",dump( $_template ),$/ if $debug;
119    
120          my $m;          my $m;
121    
# Line 118  sub marc_template { Line 125  sub marc_template {
125                  my $i2 = $r->{i2} || ' ';                  my $i2 = $r->{i2} || ' ';
126                  $m = [ $args->{to}, $i1, $i2 ];                  $m = [ $args->{to}, $i1, $i2 ];
127    
128                  warn "### r = ",dump( $r );                  warn "### r = ",dump( $r ),$/ if $debug;
129    
130                  my ( $new_r, $from_count, $to_count );                  my ( $from_mapping, $to_mapping, $from_count, $to_count );
131                  foreach my $sf ( keys %{$r} ) {                  foreach my $sf ( keys %{$r} ) {
132                          # skip everything which isn't one char subfield (e.g. 'subfields')                          # skip everything which isn't one char subfield (e.g. 'subfields')
133                          next unless $sf =~ m/^\w$/;                          next unless $sf =~ m/^\w$/;
134                          my $nr = $from_count->{$sf}++;                          my $nr = $from_count->{$sf}++;
135                          my $rename_to = $subfields_rename->{ $sf } ||                          my $rename_to = $subfields_rename->{ $sf } ||
136                                  die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename );                                  die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename );
137                          warn "### rename $sf/$nr to ", dump( $rename_to->[$nr] ), $/;                          warn "### rename $sf/$nr to ", dump( $rename_to->[$nr] ),$/ if $debug;
138                          my ( $to_sf, $to_nr ) = @{ $rename_to->[$nr] };                          my ( $to_sf, $to_nr ) = @{ $rename_to->[$nr] };
139                          $new_r->{ $to_sf }->[ $to_nr ] = [ $sf => $nr ];                          $from_mapping->{ $sf }->[ $nr ] = [ $to_sf => $to_nr ];
140                            $to_mapping->{ $to_sf }->[ $to_nr ] = [ $sf => $nr ];
141    
142                          $to_count->{ $to_sf }++;                          $to_count->{ $to_sf }++;
143                  }                  }
144    
145                  warn "### new_r = ",dump( $new_r );                  warn "### to_mapping = ",dump( $to_mapping ),$/ if $debug;
146    
147                  my $from_count_key = dump( $to_count );                  my $count_key = {
148                            from => dump( $from_count ),
149                            to   => dump( $to_count),
150                    };
151    
152                    warn "### count_key = ",dump( $count_key ),$/ if $debug;
153    
154                    my $processed_templates = 0;
155    
156                    # this defines order of traversal
157                    foreach ( qw/isis:from marc:to/ ) {
158                            my ($name,$count_name) = split(/:/);
159    
160                            my $ckey = $count_key->{$count_name} || die "can't find count_key $count_name in ",dump( $count_key );
161    
162                            my $template = $_template->{$name}->{pos}->{ $ckey } || next;
163                            $processed_templates++;
164    
165                            warn "### traverse $name $count_name selected template: |$template|\n",$/ if $debug;
166    
167                            our $fill_in = {};
168    
169                            my @templates = split(/\|/, $template );
170                            @templates = ( $template );
171    
172                            foreach my $sf ( @templates ) {
173                                    sub fill_in {
174                                            my ( $name, $r, $sf, $nr ) = @_;
175                                            my ( $from_sf, $from_nr );
176                                            if ( $name eq 'marc' ) {
177                                                    ( $from_sf, $from_nr ) = @{ $to_mapping->{$sf}->[$nr] };
178                                            } else {
179                                                    ( $from_sf, $from_nr ) = ( $sf, $nr );
180                                            }
181                                            my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr";
182                                            warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ),$/ if $debug;
183                                            if ( ref( $v ) eq 'ARRAY' ) {
184                                                    $fill_in->{$sf}->[$nr] = $v->[$from_nr];
185                                                    return $v->[$from_nr];
186                                            } elsif ( $from_nr == 0 ) {
187                                                    $fill_in->{$sf}->[$nr] = $v;
188                                                    return $v;
189                                            } else {
190                                                    die "requested subfield $from_sf/$from_nr but it's ",dump( $v );
191                                            }
192                                    }
193                                    my $fields_re = $_template->{$name}->{fields_re} || die "can't find $name in ",dump( $_template->{$name}->{fields_re} );
194                                    warn "#### $sf <<<< $fields_re\n",$/ if $debug;
195                                    $sf =~ s/($fields_re)(\d+)/fill_in($name,$r,$1,$2)/ge;
196                                    warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/ if $debug;
197                            }
198    
199                  warn "### from_count = ",dump( $from_count ), $/;                          warn "## template: |$template|\n## _template->$name = ",dump( $_template->{$name} ),$/ if $debug;
                 warn "### to_count   = ",dump( $to_count ), $/;  
200    
201                  my $template = $marc_template->{pos}->{ $from_count_key } ||                          foreach my $sf ( @{ $_template->{$name}->{order}->{$template} } ) {
202                          die "I don't have template for:\n$from_count_key\n## available templates\n", dump( $marc_template->{pos} );                                  my ( $sf, $nr ) = @$sf;
203                                    my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr";
204                  warn "### selected template: |$template|\n";                                  if ( $name eq 'isis') {
205                                            ( $sf, $nr ) = @{ $from_mapping->{$sf}->[$nr] };
                 $fill_in = {};  
   
                 my @templates = split(/\|/, $template );  
                 @templates = ( $template );  
   
                 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 );  
206                                  }                                  }
207                                    warn "++ $sf/$nr |$v|\n" if $debug;
208                                    push @$m, ( $sf, $v );
209                          }                          }
                         warn "#### $sf <<<< $fields_re\n";  
                         $sf =~ s/($fields_re)(\d+)/fill_in($r,$1,$2)/ge;  
                         warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/;  
                 }  
210    
211                  warn "## template: |$template|\n## marc_template = ",dump( $marc_template );                          warn "#### >>>> created MARC record: ", dump( $m ),$/ if $debug;
212    
213                  foreach my $sf ( @{ $marc_template->{order}->{$template} } ) {                          push @marc_out, $m;
                         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 );  
214                  }                  }
215            
216                  warn "#### >>>> created marc: ", dump( $m );                  die "I don't have template for fields ",dump( $count_key ), "\n## available templates\n", dump( $_template ) unless $processed_templates;
217                    warn ">>> $processed_templates templates applied to data\n",$/ if $debug;
                 push @marc_out, $m;  
218          }          }
219    
220          warn "### marc_template produced: ",dump( @marc_out );  
221            my $recs = 0;
222    
223          foreach my $marc ( @marc_out ) {          foreach my $marc ( @marc_out ) {
224                  warn "+++ ",dump( $marc );                  warn "+++ ",dump( $marc ),$/ if $debug;
225                  WebPAC::Normalize::_marc_push( $marc );                  WebPAC::Normalize::_marc_push( $marc );
226                    $recs++;
227          }          }
228    
229            warn "### marc_template produced $recs MARC records: ",dump( @marc_out ),$/ if $debug;
230    
231            return $recs;
232  }  }
233    
234  1;  1;

Legend:
Removed from v.1023  
changed lines
  Added in v.1028

  ViewVC Help
Powered by ViewVC 1.1.26