/[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 572 by dpavlin, Mon Jul 3 14:32:40 2006 UTC revision 595 by dpavlin, Mon Jul 10 10:16:11 2006 UTC
# Line 8  use Exporter 'import'; Line 8  use Exporter 'import';
8          tag search display          tag search display
9          marc marc_indicators marc_repeatable_subfield          marc marc_indicators marc_repeatable_subfield
10          marc_compose marc_leader          marc_compose marc_leader
11            marc_duplicate marc_remove
12    
13          rec1 rec2 rec          rec1 rec2 rec
14          regex prefix suffix surround          regex prefix suffix surround
# Line 22  use strict; Line 23  use strict;
23  #use base qw/WebPAC::Common/;  #use base qw/WebPAC::Common/;
24  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
25  use Encode qw/from_to/;  use Encode qw/from_to/;
26    use Storable qw/dclone/;
27    
28  # debugging warn(s)  # debugging warn(s)
29  my $debug = 0;  my $debug = 0;
# Line 33  WebPAC::Normalize - describe normalisato Line 35  WebPAC::Normalize - describe normalisato
35    
36  =head1 VERSION  =head1 VERSION
37    
38  Version 0.09  Version 0.14
39    
40  =cut  =cut
41    
42  our $VERSION = '0.09';  our $VERSION = '0.14';
43    
44  =head1 SYNOPSIS  =head1 SYNOPSIS
45    
# Line 67  Return data structure Line 69  Return data structure
69          row => $row,          row => $row,
70          rules => $normalize_pl_config,          rules => $normalize_pl_config,
71          marc_encoding => 'utf-8',          marc_encoding => 'utf-8',
72            config => $config,
73    );    );
74    
75  Options C<lookup>, C<row>, C<rules> and C<log> are mandatory while all  Options C<lookup>, C<row>, C<rules> and C<log> are mandatory while all
# Line 88  sub data_structure { Line 91  sub data_structure {
91          no strict 'subs';          no strict 'subs';
92          _set_lookup( $arg->{lookup} );          _set_lookup( $arg->{lookup} );
93          _set_rec( $arg->{row} );          _set_rec( $arg->{row} );
94            _set_config( $arg->{config} );
95          _clean_ds( %{ $arg } );          _clean_ds( %{ $arg } );
96          eval "$arg->{rules}";          eval "$arg->{rules}";
97          die "error evaling $arg->{rules}: $@\n" if ($@);          die "error evaling $arg->{rules}: $@\n" if ($@);
# Line 109  sub _set_rec { Line 113  sub _set_rec {
113          $rec = shift or die "no record hash";          $rec = shift or die "no record hash";
114  }  }
115    
116    =head2 _set_config
117    
118    Set current config hash
119    
120      _set_config( $config );
121    
122    Magic keys are:
123    
124    =over 4
125    
126    =item _
127    
128    Code of current database
129    
130    =item _mfn
131    
132    Current MFN
133    
134    =back
135    
136    =cut
137    
138    my $config;
139    
140    sub _set_config {
141            $config = shift;
142    }
143    
144  =head2 _get_ds  =head2 _get_ds
145    
146  Return hash formatted as data structure  Return hash formatted as data structure
# Line 117  Return hash formatted as data structure Line 149  Return hash formatted as data structure
149    
150  =cut  =cut
151    
152  my ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators);  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators);
153    my ($marc_record_offset, $marc_fetch_offset) = (0, 0);
154    
155  sub _get_ds {  sub _get_ds {
156          return $out;          return $out;
# Line 134  Clean data structure hash for next recor Line 167  Clean data structure hash for next recor
167  sub _clean_ds {  sub _clean_ds {
168          my $a = {@_};          my $a = {@_};
169          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = ();          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = ();
170            ($marc_record_offset, $marc_fetch_offset) = (0,0);
171          $marc_encoding = $a->{marc_encoding};          $marc_encoding = $a->{marc_encoding};
172  }  }
173    
# Line 182  and following rules: Line 216  and following rules:
216  which might not be what you have in mind. If you need repeatable subfield,  which might not be what you have in mind. If you need repeatable subfield,
217  define it using C<marc_repeatable_subfield> like this:  define it using C<marc_repeatable_subfield> like this:
218    
219  ....    marc_repeatable_subfield('900','a');
220      marc('900','a', rec('200','a') );
221      marc('900','b', rec('200','b') );
222      marc('900','c', rec('200','c') );
223    
224    will create:
225    
226      900a-1 900a-2 900a-3 900b-1 900c-1
227      900b-2
228    
229    There is also support for returning next or specific using:
230    
231      while (my $mf = WebPAC::Normalize:_get_marc_fields( fetch_next => 1 ) ) {
232            # do something with $mf
233      }
234    
235    will always return fields from next MARC record or
236    
237      my $mf = WebPAC::Normalize::_get_marc_fields( offset => 42 );
238    
239    will return 42th copy record (if it exists).
240    
241  =cut  =cut
242    
243  sub _get_marc_fields {  sub _get_marc_fields {
244    
245          return if (! $marc_record || ref($marc_record) ne 'ARRAY' || $#{ $marc_record } < 0);          my $arg = {@_};
246            warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2);
247            my $offset = $marc_fetch_offset;
248            if ($arg->{offset}) {
249                    $offset = $arg->{offset};
250            } elsif($arg->{fetch_next}) {
251                    $marc_fetch_offset++;
252            }
253    
254            return if (! $marc_record || ref($marc_record) ne 'ARRAY');
255    
256            warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2);
257    
258            my $marc_rec = $marc_record->[ $offset ];
259    
260            warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1);
261    
262            return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0);
263    
264          # first, sort all existing fields          # first, sort all existing fields
265          # XXX might not be needed, but modern perl might randomize elements in hash          # XXX might not be needed, but modern perl might randomize elements in hash
266          my @sorted_marc_record = sort {          my @sorted_marc_record = sort {
267                  $a->[0] . ( $a->[3] || '' ) cmp $b->[0] . ( $b->[3] || '')                  $a->[0] . ( $a->[3] || '' ) cmp $b->[0] . ( $b->[3] || '')
268          } @{ $marc_record };          } @{ $marc_rec };
269    
270          @sorted_marc_record = @{ $marc_record };        ### FIXME disable sorting          @sorted_marc_record = @{ $marc_rec };   ### FIXME disable sorting
271                    
272          # output marc fields          # output marc fields
273          my @m;          my @m;
# Line 206  sub _get_marc_fields { Line 277  sub _get_marc_fields {
277          map { $u->{ $_->[0] . ( $_->[3] || '')  }++ } @sorted_marc_record;          map { $u->{ $_->[0] . ( $_->[3] || '')  }++ } @sorted_marc_record;
278    
279          if ($debug) {          if ($debug) {
280                  warn "## marc_repeatable_subfield ", dump( $marc_repeatable_subfield ), $/;                  warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield );
281                  warn "## marc_record ", dump( $marc_record ), $/;                  warn "## marc_record[$offset] = ", dump( $marc_rec ), $/;
282                  warn "## sorted_marc_record ", dump( \@sorted_marc_record ), $/;                  warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/;
283                  warn "## subfield count ", dump( $u ), $/;                  warn "## subfield count = ", dump( $u ), $/;
284          }          }
285    
286          my $len = $#sorted_marc_record;          my $len = $#sorted_marc_record;
# Line 227  sub _get_marc_fields { Line 298  sub _get_marc_fields {
298                  # mark it visited                  # mark it visited
299                  $visited->{$i}++;                  $visited->{$i}++;
300    
301                  my $row = $sorted_marc_record[$i];                  my $row = dclone( $sorted_marc_record[$i] );
302    
303                  # field and subfield which is key for                  # field and subfield which is key for
304                  # marc_repeatable_subfield and u                  # marc_repeatable_subfield and u
# Line 285  sub _get_marc_fields { Line 356  sub _get_marc_fields {
356                  warn "## saved/3 ", dump( $field ),$/ if ($debug);                  warn "## saved/3 ", dump( $field ),$/ if ($debug);
357          }          }
358    
359          return @m;          return \@m;
360  }  }
361    
362  =head2 _debug  =head2 _debug
# Line 400  sub marc { Line 471  sub marc {
471                  from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding);                  from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding);
472                  my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' ');                  my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' ');
473                  if (defined $sf) {                  if (defined $sf) {
474                          push @{ $marc_record }, [ $f, $i1, $i2, $sf => $v ];                          push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $i1, $i2, $sf => $v ];
475                  } else {                  } else {
476                          push @{ $marc_record }, [ $f, $v ];                          push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ];
477                  }                  }
478          }          }
479  }  }
# Line 463  sub marc_compose { Line 534  sub marc_compose {
534          my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' ');          my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' ');
535          my $m = [ $f, $i1, $i2 ];          my $m = [ $f, $i1, $i2 ];
536    
537            warn "### marc_compose input subfields = ", dump(@_),$/ if ($debug > 2);
538    
539          while (@_) {          while (@_) {
540                  my $sf = shift or die "marc_compose $f needs subfield";                  my $sf = shift or die "marc_compose $f needs subfield";
541                  my $v = shift;                  my $v = shift;
# Line 473  sub marc_compose { Line 546  sub marc_compose {
546                  warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1);                  warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1);
547          }          }
548    
549          warn "## marc_compose(d) ", dump( $m ),$/ if ($debug > 1);          warn "## marc_compose current marc = ", dump( $m ),$/ if ($debug > 1);
550    
551          push @{ $marc_record }, $m if ($#{$m} > 2);          push @{ $marc_record->[ $marc_record_offset ] }, $m if ($#{$m} > 2);
552  }  }
553    
554    =head2 marc_duplicate
555    
556    Generate copy of current MARC record and continue working on copy
557    
558      marc_duplicate();
559    
560    Copies can be accessed using C<< _get_marc_fields( fetch_next => 1 ) >> or
561    C<< _get_marc_fields( offset => 42 ) >>.
562    
563    =cut
564    
565    sub marc_duplicate {
566             my $m = $marc_record->[ -1 ];
567             die "can't duplicate record which isn't defined" unless ($m);
568             push @{ $marc_record }, dclone( $m );
569             warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1);
570             $marc_record_offset = $#{ $marc_record };
571             warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1);
572    }
573    
574    =head2 marc_remove
575    
576    Remove some field or subfield from MARC record.
577    
578      marc_remove('200');
579      marc_remove('200','a');
580    
581    This will erase field C<200> or C<200^a> from current MARC record.
582    
583    This is useful after calling C<marc_duplicate> or on it's own (but, you
584    should probably just remove that subfield definition if you are not
585    using C<marc_duplicate>).
586    
587    FIXME: support fields < 10.
588    
589    =cut
590    
591    sub marc_remove {
592            my ($f, $sf) = @_;
593    
594            die "marc_remove needs record number" unless defined($f);
595    
596            my $marc = $marc_record->[ $marc_record_offset ];
597    
598            warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2);
599    
600            my $i = 0;
601            foreach ( 0 .. $#{ $marc } ) {
602                    last unless (defined $marc->[$i]);
603                    warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3);
604                    if ($marc->[$i]->[0] eq $f) {
605                            if (! defined $sf) {
606                                    # remove whole field
607                                    splice @$marc, $i, 1;
608                                    warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3);
609                                    $i--;
610                            } else {
611                                    foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) {
612                                            my $o = ($j * 2) + 3;
613                                            if ($marc->[$i]->[$o] eq $sf) {
614                                                    # remove subfield
615                                                    splice @{$marc->[$i]}, $o, 2;
616                                                    warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3);
617                                                    # is record now empty?
618                                                    if ($#{ $marc->[$i] } == 2) {
619                                                            splice @$marc, $i, 1;
620                                                            warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3);
621                                                            $i--;
622                                                    };
623                                            }
624                                    }
625                            }
626                    }
627                    $i++;
628            }
629    
630            warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2);
631    
632            $marc_record->[ $marc_record_offset ] = $marc;
633    
634            warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1);
635    }
636    
637  =head1 Functions to extract data from input  =head1 Functions to extract data from input
638    
# Line 524  sub rec2 { Line 679  sub rec2 {
679          my $f = shift;          my $f = shift;
680          return unless (defined($rec && $rec->{$f}));          return unless (defined($rec && $rec->{$f}));
681          my $sf = shift;          my $sf = shift;
682          return map { $_->{$sf} } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} };          return map {
683                    if (ref($_->{$sf}) eq 'ARRAY') {
684                            @{ $_->{$sf} };
685                    } else {
686                            $_->{$sf};
687                    }
688            } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} };
689  }  }
690    
691  =head2 rec  =head2 rec
# Line 537  syntaxtic sugar for Line 698  syntaxtic sugar for
698  =cut  =cut
699    
700  sub rec {  sub rec {
701            my @out;
702          if ($#_ == 0) {          if ($#_ == 0) {
703                  return rec1(@_);                  @out = rec1(@_);
704          } elsif ($#_ == 1) {          } elsif ($#_ == 1) {
705                  return rec2(@_);                  @out = rec2(@_);
706            }
707            if (@out) {
708                    return @out;
709            } else {
710                    return '';
711          }          }
712  }  }
713    
# Line 573  Prefix all values with a string Line 740  Prefix all values with a string
740  =cut  =cut
741    
742  sub prefix {  sub prefix {
743          my $p = shift or die "prefix needs string as first argument";          my $p = shift or return;
744          return map { $p . $_ } grep { defined($_) } @_;          return map { $p . $_ } grep { defined($_) } @_;
745  }  }
746    
# Line 636  sub lookup { Line 803  sub lookup {
803          }          }
804  }  }
805    
806    =head2 config
807    
808    Consult config values stored in C<config.yml>
809    
810      # return database code (key under databases in yaml)
811      $database_code = config();    # use _ from hash
812      $database_name = config('name');
813      $database_input_name = config('input name');
814      $tag = config('input normalize tag');
815    
816    Up to three levels are supported.
817    
818    =cut
819    
820    sub config {
821            return unless ($config);
822    
823            my $p = shift;
824    
825            $p ||= '';
826    
827            my $v;
828    
829            warn "### getting config($p)\n" if ($debug > 1);
830    
831            my @p = split(/\s+/,$p);
832            if ($#p < 0) {
833                    $v = $config->{ '_' };  # special, database code
834            } else {
835    
836                    my $c = dclone( $config );
837    
838                    foreach my $k (@p) {
839                            warn "### k: $k c = ",dump($c),$/ if ($debug > 1);
840                            if (ref($c) eq 'ARRAY') {
841                                    $c = shift @$c;
842                                    warn "config($p) taking first occurence of '$k', probably not what you wanted!\n";
843                                    last;
844                            }
845    
846                            if (! defined($c->{$k}) ) {
847                                    $c = undef;
848                                    last;
849                            } else {
850                                    $c = $c->{$k};
851                            }
852                    }
853                    $v = $c if ($c);
854    
855            }
856    
857            warn "## config( '$p' ) = ",dump( $v ),$/ if ($v && $debug);
858            warn "config( '$p' ) is empty\n" if (! $v);
859    
860            return $v;
861    }
862    
863    =head2 id
864    
865    Returns unique id of this record
866    
867      $id = id();
868    
869    Returns C<42/2> for 2nd occurence of MFN 42.
870    
871    =cut
872    
873    sub id {
874            my $mfn = $config->{_mfn} || die "no _mfn in config data";
875            return $mfn . $#{$marc_record} ? $#{$marc_record} + 1 : '';
876    }
877    
878  =head2 join_with  =head2 join_with
879    
880  Joins walues with some delimiter  Joins walues with some delimiter
# Line 646  Joins walues with some delimiter Line 885  Joins walues with some delimiter
885    
886  sub join_with {  sub join_with {
887          my $d = shift;          my $d = shift;
888          return join($d, grep { defined($_) && $_ ne '' } @_);          warn "### join_with('$d',",dump(@_),")\n" if ($debug > 2);
889            my $v = join($d, grep { defined($_) && $_ ne '' } @_);
890            return '' unless defined($v);
891            return $v;
892  }  }
893    
894  =head2 split_rec_on  =head2 split_rec_on

Legend:
Removed from v.572  
changed lines
  Added in v.595

  ViewVC Help
Powered by ViewVC 1.1.26