/[webpac2]/trunk/lib/WebPAC/Normalize.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.pm

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

revision 736 by dpavlin, Thu Oct 5 12:57:51 2006 UTC revision 813 by dpavlin, Sun Apr 1 21:47:47 2007 UTC
# Line 10  use Exporter 'import'; Line 10  use Exporter 'import';
10          tag search display          tag search display
11          marc marc_indicators marc_repeatable_subfield          marc marc_indicators marc_repeatable_subfield
12          marc_compose marc_leader          marc_compose marc_leader
13          marc_duplicate marc_remove          marc_duplicate marc_remove marc_count
14          marc_original_order          marc_original_order
15    
16          rec1 rec2 rec          rec1 rec2 rec
# Line 19  use Exporter 'import'; Line 19  use Exporter 'import';
19          save_into_lookup          save_into_lookup
20    
21          split_rec_on          split_rec_on
22    
23            get set
24            count
25  /;  /;
26    
27  use warnings;  use warnings;
# Line 39  WebPAC::Normalize - describe normalisato Line 42  WebPAC::Normalize - describe normalisato
42    
43  =head1 VERSION  =head1 VERSION
44    
45  Version 0.22  Version 0.27
46    
47  =cut  =cut
48    
49  our $VERSION = '0.22';  our $VERSION = '0.27';
50    
51  =head1 SYNOPSIS  =head1 SYNOPSIS
52    
# Line 164  Return hash formatted as data structure Line 167  Return hash formatted as data structure
167    
168  =cut  =cut
169    
170  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators);  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader);
171  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);
172    
173  sub _get_ds {  sub _get_ds {
# Line 181  Clean data structure hash for next recor Line 184  Clean data structure hash for next recor
184    
185  sub _clean_ds {  sub _clean_ds {
186          my $a = {@_};          my $a = {@_};
187          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = ();          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader) = ();
188          ($marc_record_offset, $marc_fetch_offset) = (0,0);          ($marc_record_offset, $marc_fetch_offset) = (0,0);
189          $marc_encoding = $a->{marc_encoding};          $marc_encoding = $a->{marc_encoding};
190  }  }
# Line 286  will return 42th copy record (if it exis Line 289  will return 42th copy record (if it exis
289    
290  =cut  =cut
291    
292    my $fetch_pos;
293    
294  sub _get_marc_fields {  sub _get_marc_fields {
295    
296          my $arg = {@_};          my $arg = {@_};
297          warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2);          warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2);
298          my $offset = $marc_fetch_offset;          $fetch_pos = $marc_fetch_offset;
299          if ($arg->{offset}) {          if ($arg->{offset}) {
300                  $offset = $arg->{offset};                  $fetch_pos = $arg->{offset};
301          } elsif($arg->{fetch_next}) {          } elsif($arg->{fetch_next}) {
302                  $marc_fetch_offset++;                  $marc_fetch_offset++;
303          }          }
# Line 301  sub _get_marc_fields { Line 306  sub _get_marc_fields {
306    
307          warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2);          warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2);
308    
309          my $marc_rec = $marc_record->[ $offset ];          my $marc_rec = $marc_record->[ $fetch_pos ];
310    
311          warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1);          warn "## _get_marc_fields (at offset: $fetch_pos) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1);
312    
313          return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0);          return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0);
314    
# Line 324  sub _get_marc_fields { Line 329  sub _get_marc_fields {
329    
330          if ($debug) {          if ($debug) {
331                  warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield );                  warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield );
332                  warn "## marc_record[$offset] = ", dump( $marc_rec ), $/;                  warn "## marc_record[$fetch_pos] = ", dump( $marc_rec ), $/;
333                  warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/;                  warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/;
334                  warn "## subfield count = ", dump( $u ), $/;                  warn "## subfield count = ", dump( $u ), $/;
335          }          }
# Line 405  sub _get_marc_fields { Line 410  sub _get_marc_fields {
410          return \@m;          return \@m;
411  }  }
412    
413    =head2 _get_marc_leader
414    
415    Return leader from currently fetched record by L</_get_marc_fields>
416    
417      print WebPAC::Normalize::_get_marc_leader();
418    
419    =cut
420    
421    sub _get_marc_leader {
422            die "no fetch_pos, did you called _get_marc_fields first?" unless ( defined( $fetch_pos ) );
423            return $marc_leader->[ $fetch_pos ];
424    }
425    
426  =head2 _debug  =head2 _debug
427    
428  Change level of debug warnings  Change level of debug warnings
# Line 487  sub marc_leader { Line 505  sub marc_leader {
505          my ($offset,$value) = @_;          my ($offset,$value) = @_;
506    
507          if ($offset) {          if ($offset) {
508                  $out->{' leader'}->{ $offset } = $value;                  $marc_leader->[ $marc_record_offset ]->{ $offset } = $value;
509          } else {          } else {
510                  return $out->{' leader'};                  
511                    if (defined($marc_leader)) {
512                            die "marc_leader not array = ", dump( $marc_leader ) unless (ref($marc_leader) eq 'ARRAY');
513                            return $marc_leader->[ $marc_record_offset ];
514                    } else {
515                            return;
516                    }
517          }          }
518  }  }
519    
# Line 621  sub marc_duplicate { Line 645  sub marc_duplicate {
645           my $m = $marc_record->[ -1 ];           my $m = $marc_record->[ -1 ];
646           die "can't duplicate record which isn't defined" unless ($m);           die "can't duplicate record which isn't defined" unless ($m);
647           push @{ $marc_record }, dclone( $m );           push @{ $marc_record }, dclone( $m );
648           warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1);           push @{ $marc_leader }, dclone( marc_leader() );
649             warn "## marc_duplicate = ", dump(@$marc_leader, @$marc_record), $/ if ($debug > 1);
650           $marc_record_offset = $#{ $marc_record };           $marc_record_offset = $#{ $marc_record };
651           warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1);           warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1);
652    
653  }  }
654    
655  =head2 marc_remove  =head2 marc_remove
# Line 635  Remove some field or subfield from MARC Line 661  Remove some field or subfield from MARC
661    
662  This will erase field C<200> or C<200^a> from current MARC record.  This will erase field C<200> or C<200^a> from current MARC record.
663    
664      marc_remove('*');
665    
666    Will remove all fields in current MARC record.
667    
668  This is useful after calling C<marc_duplicate> or on it's own (but, you  This is useful after calling C<marc_duplicate> or on it's own (but, you
669  should probably just remove that subfield definition if you are not  should probably just remove that subfield definition if you are not
670  using C<marc_duplicate>).  using C<marc_duplicate>).
# Line 652  sub marc_remove { Line 682  sub marc_remove {
682    
683          warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2);          warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2);
684    
685          my $i = 0;          if ($f eq '*') {
686          foreach ( 0 .. $#{ $marc } ) {  
687                  last unless (defined $marc->[$i]);                  delete( $marc_record->[ $marc_record_offset ] );
688                  warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3);                  warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1);
689                  if ($marc->[$i]->[0] eq $f) {  
690                          if (! defined $sf) {          } else {
691                                  # remove whole field  
692                                  splice @$marc, $i, 1;                  my $i = 0;
693                                  warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3);                  foreach ( 0 .. $#{ $marc } ) {
694                                  $i--;                          last unless (defined $marc->[$i]);
695                          } else {                          warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3);
696                                  foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) {                          if ($marc->[$i]->[0] eq $f) {
697                                          my $o = ($j * 2) + 3;                                  if (! defined $sf) {
698                                          if ($marc->[$i]->[$o] eq $sf) {                                          # remove whole field
699                                                  # remove subfield                                          splice @$marc, $i, 1;
700                                                  splice @{$marc->[$i]}, $o, 2;                                          warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3);
701                                                  warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3);                                          $i--;
702                                                  # is record now empty?                                  } else {
703                                                  if ($#{ $marc->[$i] } == 2) {                                          foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) {
704                                                          splice @$marc, $i, 1;                                                  my $o = ($j * 2) + 3;
705                                                          warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3);                                                  if ($marc->[$i]->[$o] eq $sf) {
706                                                          $i--;                                                          # remove subfield
707                                                  };                                                          splice @{$marc->[$i]}, $o, 2;
708                                                            warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3);
709                                                            # is record now empty?
710                                                            if ($#{ $marc->[$i] } == 2) {
711                                                                    splice @$marc, $i, 1;
712                                                                    warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3);
713                                                                    $i--;
714                                                            };
715                                                    }
716                                          }                                          }
717                                  }                                  }
718                          }                          }
719                            $i++;
720                  }                  }
                 $i++;  
         }  
721    
722          warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2);                  warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2);
723    
724          $marc_record->[ $marc_record_offset ] = $marc;                  $marc_record->[ $marc_record_offset ] = $marc;
725            }
726    
727          warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1);          warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1);
728  }  }
# Line 759  sub marc_original_order { Line 797  sub marc_original_order {
797          warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1);          warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1);
798  }  }
799    
800    =head2 marc_count
801    
802    Return number of MARC records created using L</marc_duplicate>.
803    
804      print "created ", marc_count(), " records";
805    
806    =cut
807    
808    sub marc_count {
809            return $#{ $marc_record };
810    }
811    
812    
813  =head1 Functions to extract data from input  =head1 Functions to extract data from input
814    
# Line 880  syntaxtic sugar for Line 930  syntaxtic sugar for
930    @v = rec('200')    @v = rec('200')
931    @v = rec('200','a')    @v = rec('200','a')
932    
933    If rec() returns just single value, it will
934    return scalar, not array.
935    
936  =cut  =cut
937    
938  sub rec {  sub rec {
# Line 889  sub rec { Line 942  sub rec {
942          } elsif ($#_ == 1) {          } elsif ($#_ == 1) {
943                  @out = rec2(@_);                  @out = rec2(@_);
944          }          }
945          if (@out) {          if ($#out == 0 && ! wantarray) {
946                    return $out[0];
947            } elsif (@out) {
948                  return @out;                  return @out;
949          } else {          } else {
950                  return '';                  return '';
# Line 1017  Easy as pie, right? Line 1072  Easy as pie, right?
1072  sub lookup {  sub lookup {
1073          my ($what, $database, $input, $key, $having) = @_;          my ($what, $database, $input, $key, $having) = @_;
1074    
1075          confess "lookup needs 5 arguments: what, database, input, key, having" unless ($#_ == 4);          confess "lookup needs 5 arguments: what, database, input, key, having\n" unless ($#_ == 4);
1076    
1077          warn "## lookup ($database, $input, $key)", $/ if ($debug > 1);          warn "## lookup ($database, $input, $key)", $/ if ($debug > 1);
1078          return unless (defined($lookup->{$database}->{$input}->{$key}));          return unless (defined($lookup->{$database}->{$input}->{$key}));
# Line 1031  sub lookup { Line 1086  sub lookup {
1086    
1087          foreach my $h ( @having ) {          foreach my $h ( @having ) {
1088                  if (defined($lookup->{$database}->{$input}->{$key}->{$h})) {                  if (defined($lookup->{$database}->{$input}->{$key}->{$h})) {
1089                          warn "lookup for $database/$input/$key/$h return ",dump($lookup->{$database}->{$input}->{$key}->{$h}),"\n";                          warn "lookup for $database/$input/$key/$h return ",dump($lookup->{$database}->{$input}->{$key}->{$h}),"\n" if ($debug);
1090                          $mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} };                          $mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} };
1091                  }                  }
1092          }          }
# Line 1040  sub lookup { Line 1095  sub lookup {
1095    
1096          my @mfns = sort keys %$mfns;          my @mfns = sort keys %$mfns;
1097    
1098          warn "# lookup loading $database/$input/$key mfn ", join(",",@mfns)," having ",dump(@having),"\n";          warn "# lookup loading $database/$input/$key mfn ", join(",",@mfns)," having ",dump(@having),"\n" if ($debug);
1099    
1100          my $old_rec = $rec;          my $old_rec = $rec;
1101          my @out;          my @out;
# Line 1048  sub lookup { Line 1103  sub lookup {
1103          foreach my $mfn (@mfns) {          foreach my $mfn (@mfns) {
1104                  $rec = $load_row_coderef->( $database, $input, $mfn );                  $rec = $load_row_coderef->( $database, $input, $mfn );
1105    
1106                  warn "got $database/$input/$mfn = ", dump($rec), $/;                  warn "got $database/$input/$mfn = ", dump($rec), $/ if ($debug);
1107    
1108                  my @vals = $what->();                  my @vals = $what->();
1109    
1110                  push @out, ( @vals );                  push @out, ( @vals );
1111    
1112                  warn "lookup for mfn $mfn returned ", dump(@vals), $/;                  warn "lookup for mfn $mfn returned ", dump(@vals), $/ if ($debug);
1113          }          }
1114    
1115  #       if (ref($lookup->{$k}) eq 'ARRAY') {  #       if (ref($lookup->{$k}) eq 'ARRAY') {
# Line 1065  sub lookup { Line 1120  sub lookup {
1120    
1121          $rec = $old_rec;          $rec = $old_rec;
1122    
1123          warn "## lookup returns = ", dump(@out), $/;          warn "## lookup returns = ", dump(@out), $/ if ($debug);
1124    
1125          return @out;          if ($#out == 0) {
1126                    return $out[0];
1127            } else {
1128                    return @out;
1129            }
1130  }  }
1131    
1132  =head2 save_into_lookup  =head2 save_into_lookup
# Line 1239  sub split_rec_on { Line 1298  sub split_rec_on {
1298          }          }
1299  }  }
1300    
1301    my $hash;
1302    
1303    =head2 set
1304    
1305      set( key => 'value' );
1306    
1307    =cut
1308    
1309    sub set {
1310            my ($k,$v) = @_;
1311            warn "## set ( $k => ", dump($v), " )", $/ if ( $debug );
1312            $hash->{$k} = $v;
1313    };
1314    
1315    =head2 get
1316    
1317      get( 'key' );
1318    
1319    =cut
1320    
1321    sub get {
1322            my $k = shift || return;
1323            my $v = $hash->{$k};
1324            warn "## get $k = ", dump( $v ), $/ if ( $debug );
1325            return $v;
1326    }
1327    
1328    =head2 count
1329    
1330      if ( count( @result ) == 1 ) {
1331            # do something if only 1 result is there
1332      }
1333    
1334    =cut
1335    
1336    sub count {
1337            warn "## count ",dump(@_),$/ if ( $debug );
1338            return @_ . '';
1339    }
1340    
1341  # END  # END
1342  1;  1;

Legend:
Removed from v.736  
changed lines
  Added in v.813

  ViewVC Help
Powered by ViewVC 1.1.26