/[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 1024 by dpavlin, Sat Nov 10 11:37:47 2007 UTC revision 1035 by dpavlin, Mon Nov 12 10:16:16 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;
72                    
73          my ( $subfields_rename, $from_subfields, $to_subfields );          my ( $subfields_rename, $from_subfields );
74            our $to_subfields = {};
75          while ( my ( $from, $to ) = splice(@subfields_rename, 0, 2) ) {          while ( my ( $from, $to ) = splice(@subfields_rename, 0, 2) ) {
76                  my ( $f, $t ) = (                  my ( $f, $t ) = (
77                          $from_subfields->{ $from }++,                          $from_subfields->{ $from }++,
# Line 74  sub marc_template { Line 79  sub marc_template {
79                  );                  );
80                  $subfields_rename->{ $from }->[ $f ] = [ $to => $t ];                  $subfields_rename->{ $from }->[ $f ] = [ $to => $t ];
81          }          }
82          warn "### subfields_rename = ",dump( $subfields_rename ),$/;          warn "### subfields_rename = ",dump( $subfields_rename ),$/ if $debug;
83          warn "### from_subfields = ", dump( $from_subfields ),$/;          warn "### from_subfields = ", dump( $from_subfields ),$/ if $debug;
84          warn "### to_subfields = ", dump( $to_subfields ),$/;          warn "### to_subfields = ", dump( $to_subfields ),$/ if $debug;
85    
86            our $_template;
87    
88          my $fields_re = join('|', keys %$to_subfields );          $_template->{isis}->{fields_re} = join('|', keys %$from_subfields );
89            $_template->{marc}->{fields_re} = join('|', keys %$to_subfields );
90    
91          my @marc_out;          my @marc_out;
           
         our $_template;  
92    
93          sub _parse_template {          sub _parse_template {
94                  my ( $name, $templates ) = @_;                  my ( $name, $templates ) = @_;
95    
96                    my $fields_re = $_template->{$name}->{fields_re} || die "can't find $name in ",dump( $_template->{$name}->{fields_re} );
97    
98                  foreach my $template ( @{ $templates } ) {                  foreach my $template ( @{ $templates } ) {
99                          our $count = {};                          our $count = {};
100                          our @marc_order = ();                          our @order = ();
101                          sub my_count {                          sub my_count {
102                                  my $sf = shift;                                  my $sf = shift;
103                                  my $nr = $count->{$sf}++;                                  my $nr = $count->{$sf}++;
104                                  push @marc_order, [ $sf, $nr ];                                  push @order, [ $sf, $nr ];
105                                  return $sf . $nr;                                  return $sf . $nr;
106                          }                          }
107                          my $pos_template = $template;                          my $pos_template = $template;
108                          $pos_template =~ s/($fields_re)/my_count($1)/ge;                          $pos_template =~ s/($fields_re)/my_count($1)/ge;
109                          my $count_key = dump( $count );                          my $count_key = dump( $count );
110                          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;
111                          $_template->{$name}->{pos}->{ $count_key } = $pos_template;                          $_template->{$name}->{pos}->{ $count_key } = $pos_template;
112                          $_template->{$name}->{order}->{ $pos_template } = [ @marc_order ];                          $_template->{$name}->{order}->{ $pos_template } = [ @order ];
113                  }                  }
114                  warn "### from ",dump( $templates ), " created ", dump( $_template );                  warn "### from ",dump( $templates ), " using $fields_re created ", dump( $_template ),$/ if $debug;
115          }          }
116    
117          _parse_template( 'marc', $args->{marc_template} );          _parse_template( 'marc', $args->{marc_template} );
118          _parse_template( 'isis', $args->{isis_template} );          _parse_template( 'isis', $args->{isis_template} );
119          warn "### _template = ",dump( $_template );          warn "### _template = ",dump( $_template ),$/ if $debug;
120    
121          my $m;          my $m;
122    
# Line 118  sub marc_template { Line 126  sub marc_template {
126                  my $i2 = $r->{i2} || ' ';                  my $i2 = $r->{i2} || ' ';
127                  $m = [ $args->{to}, $i1, $i2 ];                  $m = [ $args->{to}, $i1, $i2 ];
128    
129                  warn "### r = ",dump( $r );                  warn "### r = ",dump( $r ),$/ if $debug;
130    
131                  my ( $new_r, $from_count, $to_count );                  my ( $from_mapping, $to_mapping, $from_count, $to_count );
132                  foreach my $sf ( keys %{$r} ) {                  foreach my $from_sf ( keys %{$r} ) {
133                          # skip everything which isn't one char subfield (e.g. 'subfields')                          # skip everything which isn't one char subfield (e.g. 'subfields')
134                          next unless $sf =~ m/^\w$/;                          next unless $from_sf =~ m/^\w$/;
135                          my $nr = $from_count->{$sf}++;                          my $from_nr = $from_count->{$from_sf}++;
136                          my $rename_to = $subfields_rename->{ $sf } ||                          my $rename_to = $subfields_rename->{ $from_sf } ||
137                                  die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename );                                  die "can't find subfield rename for $from_sf/$from_nr in ", dump( $subfields_rename );
138                          warn "### rename $sf/$nr to ", dump( $rename_to->[$nr] ), $/;                          my ( $to_sf, $to_nr ) = @{ $rename_to->[$from_nr] };
139                          my ( $to_sf, $to_nr ) = @{ $rename_to->[$nr] };                          $to_mapping->{ $to_sf }->[ $to_nr ] = [ $from_sf => $from_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 ), $/;  
140    
141                  my $template = $_template->{marc}->{pos}->{ $from_count_key } ||                          my $to_nr2 = $to_count->{ $to_sf }++;
142                          die "I don't have template for:\n$from_count_key\n## available templates\n", dump( $_template );                          $from_mapping->{ $from_sf }->[ $from_nr ] = [ $to_sf => $to_nr2 ];
143    
144                  warn "### selected template: |$template|\n";                          warn "### from $from_sf/$from_nr -> $to_sf/$to_nr\tto $from_sf/$from_nr -> $to_sf/$to_nr2\n" if $debug;
145                    }
                 our $fill_in = {};  
146    
147                  my @templates = split(/\|/, $template );                  warn "### from_mapping = ",dump( $from_mapping ), "\n### to_mapping = ",dump( $to_mapping ),$/ if $debug;
                 @templates = ( $template );  
148    
149                  foreach my $sf ( @templates ) {                  my $count_key = {
150                          sub fill_in {                          from => dump( $from_count ),
151                                  my ( $r, $sf, $nr ) = @_;                          to   => dump( $to_count),
152                                  my ( $from_sf, $from_nr ) = @{ $new_r->{ $sf }->[ $nr ] };                  };
153                                  my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr";  
154                                  warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ), $/;                  warn "### count_key = ",dump( $count_key ),$/ if $debug;
155                                  if ( ref( $v ) eq 'ARRAY' ) {  
156                                          $fill_in->{$sf}->[$nr] = $v->[$from_nr];                  my $processed_templates = 0;
157                                          return $v->[$from_nr];  
158                                  } elsif ( $from_nr == 0 ) {                  # this defines order of traversal
159                    foreach ( qw/isis:from marc:to/ ) {
160                            my ($name,$count_name) = split(/:/);
161    
162                            my $ckey = $count_key->{$count_name} || die "can't find count_key $count_name in ",dump( $count_key );
163    
164                            my $template = $_template->{$name}->{pos}->{ $ckey } || next;
165                            $processed_templates++;
166    
167                            warn "### traverse $name $count_name selected template: |$template|\n" if $debug;
168    
169                            our $fill_in = {};
170    
171                            my @templates = split(/\|/, $template );
172                            @templates = ( $template ) unless @templates;
173    
174                            warn "### templates = ",dump( @templates ),$/ if $debug;
175    
176                            foreach my $sf ( @templates ) {
177                                    sub fill_in {
178                                            my ( $name, $r, $pre, $sf, $nr, $post ) = @_;
179                                            warn "#### fill_in( $name, r, '$pre', $sf, $nr, '$post' )\n" if $debug;
180                                            my ( $from_sf, $from_nr );
181                                            if ( $name eq 'marc' ) {
182                                                    die "no $sf/$nr in to_mapping: ",dump( $to_mapping ), " form record ",dump( $r ) unless defined $to_mapping->{$sf}->[$nr];
183                                                    ( $from_sf, $from_nr ) = @{ $to_mapping->{$sf}->[$nr] };
184                                            } else {
185                                                    ( $from_sf, $from_nr ) = ( $sf, $nr );
186                                            }
187                                            my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr";
188                                            if ( ref( $v ) eq 'ARRAY' ) {
189                                                    $v = $pre . $v->[$from_nr] . $post;
190                                            } elsif ( $from_nr == 0 ) {
191                                                    $v = $pre . $v . $post;
192                                            } else {
193                                                    die "requested subfield $from_sf/$from_nr but it's ",dump( $v );
194                                            }
195                                            warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ),$/ if $debug;
196                                          $fill_in->{$sf}->[$nr] = $v;                                          $fill_in->{$sf}->[$nr] = $v;
197                                          return $v;                                          return $v;
                                 } else {  
                                         die "requested subfield $from_sf/$from_nr but it's ",dump( $v );  
198                                  }                                  }
199                                    my $fields_re = $_template->{$name}->{fields_re} || die "can't find $name in ",dump( $_template->{$name}->{fields_re} );
200                                    warn "#### $sf <<<< $fields_re\n" if $debug;
201                                    $sf =~ s/^(.*?)($fields_re)(\d+)(.*?)$/fill_in($name,$r,$1,$2,$3,$4)/ge;
202                                    warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/ if $debug;
203                          }                          }
                         warn "#### $sf <<<< $fields_re\n";  
                         $sf =~ s/($fields_re)(\d+)/fill_in($r,$1,$2)/ge;  
                         warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/;  
                 }  
204    
205                  warn "## template: |$template|\n## _template->marc = ",dump( $_template->{marc} );                          warn "## template: |$template|\n## _template->$name = ",dump( $_template->{$name} ),$/ if $debug;
206    
207                  foreach my $sf ( @{ $_template->{marc}->{order}->{$template} } ) {                          foreach my $sf ( @{ $_template->{$name}->{order}->{$template} } ) {
208                          my ( $sf, $nr ) = @$sf;                                  my ( $sf, $nr ) = @$sf;
209                          my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr";                                  my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr";
210                          warn "++ $sf/$nr |$v|\n";                                  if ( $name eq 'isis') {
211                          push @$m, ( $sf, $v );                                          ( $sf, $nr ) = @{ $from_mapping->{$sf}->[$nr] };
212                  }                                  }
213                                    warn "++ $sf/$nr |$v|\n" if $debug;
214                                    push @$m, ( $sf, $v );
215                            }
216    
217                  warn "#### >>>> created marc: ", dump( $m );                          warn "#### >>>> created MARC record: ", dump( $m ),$/ if $debug;
218    
219                  push @marc_out, $m;                          push @marc_out, $m;
220    
221                            last;
222                    }
223            
224                    die "I don't have template for fields ",dump( $count_key ), "\n## available templates\n", dump( $_template ) unless $processed_templates;
225                    warn ">>> $processed_templates templates applied to data\n",$/ if $debug;
226          }          }
227    
228          warn "### marc_template produced: ",dump( @marc_out );  
229            my $recs = 0;
230    
231          foreach my $marc ( @marc_out ) {          foreach my $marc ( @marc_out ) {
232                  warn "+++ ",dump( $marc );                  warn "+++ ",dump( $marc ),$/ if $debug;
233                  WebPAC::Normalize::_marc_push( $marc );                  WebPAC::Normalize::_marc_push( $marc );
234                    $recs++;
235          }          }
236    
237            warn "### marc_template produced $recs MARC records: ",dump( @marc_out ),$/ if $debug;
238    
239            return $recs;
240  }  }
241    
242  1;  1;

Legend:
Removed from v.1024  
changed lines
  Added in v.1035

  ViewVC Help
Powered by ViewVC 1.1.26