/[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 730 by dpavlin, Fri Sep 29 20:18:34 2006 UTC revision 752 by dpavlin, Sun Oct 8 18:21:26 2006 UTC
# Line 2  package WebPAC::Normalize; Line 2  package WebPAC::Normalize;
2  use Exporter 'import';  use Exporter 'import';
3  @EXPORT = qw/  @EXPORT = qw/
4          _set_rec _set_lookup          _set_rec _set_lookup
5          _set_load_ds          _set_load_row
6          _get_ds _clean_ds          _get_ds _clean_ds
7          _debug          _debug
8          _pack_subfields_hash          _pack_subfields_hash
# Line 39  WebPAC::Normalize - describe normalisato Line 39  WebPAC::Normalize - describe normalisato
39    
40  =head1 VERSION  =head1 VERSION
41    
42  Version 0.21  Version 0.23
43    
44  =cut  =cut
45    
46  our $VERSION = '0.21';  our $VERSION = '0.23';
47    
48  =head1 SYNOPSIS  =head1 SYNOPSIS
49    
# Line 74  Return data structure Line 74  Return data structure
74          rules => $normalize_pl_config,          rules => $normalize_pl_config,
75          marc_encoding => 'utf-8',          marc_encoding => 'utf-8',
76          config => $config,          config => $config,
77          load_ds_coderef => sub {          load_row_coderef => sub {
78                  my ($database,$input,$mfn) = shift;                  my ($database,$input,$mfn) = shift;
79                  $store->load_ds( database => $database, input => $input, id => $mfn );                  $store->load_row( database => $database, input => $input, id => $mfn );
80          },          },
81    );    );
82    
83  Options C<row>, C<rules> and C<log> are mandatory while all  Options C<row>, C<rules> and C<log> are mandatory while all
84  other are optional.  other are optional.
85    
86  C<load_ds_coderef> is closure only used when executing lookups, so they will  C<load_row_coderef> is closure only used when executing lookups, so they will
87  die if it's not defined.  die if it's not defined.
88    
89  This function will B<die> if normalizastion can't be evaled.  This function will B<die> if normalizastion can't be evaled.
# Line 93  C<WebPAC::Normalize::data_structure>. Line 93  C<WebPAC::Normalize::data_structure>.
93    
94  =cut  =cut
95    
96  my $load_ds_coderef;  my $load_row_coderef;
97    
98  sub data_structure {  sub data_structure {
99          my $arg = {@_};          my $arg = {@_};
# Line 106  sub data_structure { Line 106  sub data_structure {
106          _set_rec( $arg->{row} );          _set_rec( $arg->{row} );
107          _set_config( $arg->{config} ) if defined($arg->{config});          _set_config( $arg->{config} ) if defined($arg->{config});
108          _clean_ds( %{ $arg } );          _clean_ds( %{ $arg } );
109          $load_ds_coderef = $arg->{load_ds_coderef};          $load_row_coderef = $arg->{load_row_coderef};
110    
111          eval "$arg->{rules}";          eval "$arg->{rules}";
112          die "error evaling $arg->{rules}: $@\n" if ($@);          die "error evaling $arg->{rules}: $@\n" if ($@);
# Line 164  Return hash formatted as data structure Line 164  Return hash formatted as data structure
164    
165  =cut  =cut
166    
167  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators);  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader);
168  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);
169    
170  sub _get_ds {  sub _get_ds {
# Line 181  Clean data structure hash for next recor Line 181  Clean data structure hash for next recor
181    
182  sub _clean_ds {  sub _clean_ds {
183          my $a = {@_};          my $a = {@_};
184          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = ();          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader) = ();
185          ($marc_record_offset, $marc_fetch_offset) = (0,0);          ($marc_record_offset, $marc_fetch_offset) = (0,0);
186          $marc_encoding = $a->{marc_encoding};          $marc_encoding = $a->{marc_encoding};
187  }  }
# Line 212  sub _get_lookup { Line 212  sub _get_lookup {
212          return $lookup;          return $lookup;
213  }  }
214    
215  =head2 _set_load_ds  =head2 _set_load_row
216    
217  Setup code reference which will return L<data_structure> from  Setup code reference which will return L<data_structure> from
218  L<WebPAC::Store>  L<WebPAC::Store>
219    
220    _set_load_ds(sub {    _set_load_row(sub {
221                  my ($database,$input,$mfn) = @_;                  my ($database,$input,$mfn) = @_;
222                  $store->load_ds( database => $database, input => $input, id => $mfn );                  $store->load_row( database => $database, input => $input, id => $mfn );
223    });    });
224    
225  =cut  =cut
226    
227  sub _set_load_ds {  sub _set_load_row {
228          my $coderef = shift;          my $coderef = shift;
229          confess "argument isn't CODE" unless ref($coderef) eq 'CODE';          confess "argument isn't CODE" unless ref($coderef) eq 'CODE';
230    
231          $load_ds_coderef = $coderef;          $load_row_coderef = $coderef;
232  }  }
233    
234  =head2 _get_marc_fields  =head2 _get_marc_fields
# Line 487  sub marc_leader { Line 487  sub marc_leader {
487          my ($offset,$value) = @_;          my ($offset,$value) = @_;
488    
489          if ($offset) {          if ($offset) {
490                  $out->{' leader'}->{ $offset } = $value;                  $leader->{ $offset } = $value;
491          } else {          } else {
492                  return $out->{' leader'};                  return $leader;
493          }          }
494  }  }
495    
# Line 880  syntaxtic sugar for Line 880  syntaxtic sugar for
880    @v = rec('200')    @v = rec('200')
881    @v = rec('200','a')    @v = rec('200','a')
882    
883    If rec() returns just single value, it will
884    return scalar, not array.
885    
886  =cut  =cut
887    
888  sub rec {  sub rec {
# Line 889  sub rec { Line 892  sub rec {
892          } elsif ($#_ == 1) {          } elsif ($#_ == 1) {
893                  @out = rec2(@_);                  @out = rec2(@_);
894          }          }
895          if (@out) {          if ($#out == 0 && ! wantarray) {
896                    return $out[0];
897            } elsif (@out) {
898                  return @out;                  return @out;
899          } else {          } else {
900                  return '';                  return '';
# Line 1022  sub lookup { Line 1027  sub lookup {
1027          warn "## lookup ($database, $input, $key)", $/ if ($debug > 1);          warn "## lookup ($database, $input, $key)", $/ if ($debug > 1);
1028          return unless (defined($lookup->{$database}->{$input}->{$key}));          return unless (defined($lookup->{$database}->{$input}->{$key}));
1029    
1030          confess "lookup really need load_ds_coderef added to data_structure\n" unless ($load_ds_coderef);          confess "lookup really need load_row_coderef added to data_structure\n" unless ($load_row_coderef);
1031    
1032          my $mfns;          my $mfns;
1033          my @having = $having->();          my @having = $having->();
# Line 1031  sub lookup { Line 1036  sub lookup {
1036    
1037          foreach my $h ( @having ) {          foreach my $h ( @having ) {
1038                  if (defined($lookup->{$database}->{$input}->{$key}->{$h})) {                  if (defined($lookup->{$database}->{$input}->{$key}->{$h})) {
1039                          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);
1040                          $mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} };                          $mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} };
1041                  }                  }
1042          }          }
# Line 1040  sub lookup { Line 1045  sub lookup {
1045    
1046          my @mfns = sort keys %$mfns;          my @mfns = sort keys %$mfns;
1047    
1048          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);
1049    
1050          my $old_rec = $rec;          my $old_rec = $rec;
1051          my @out;          my @out;
1052    
1053          foreach my $mfn (@mfns) {          foreach my $mfn (@mfns) {
1054                  $rec = $load_ds_coderef->( $database, $input, $mfn );                  $rec = $load_row_coderef->( $database, $input, $mfn );
1055    
1056                  warn "got $database/$input/$mfn = ", dump($rec), $/;                  warn "got $database/$input/$mfn = ", dump($rec), $/ if ($debug);
1057    
1058                  my @vals = $what->();                  my @vals = $what->();
1059    
1060                  push @out, ( @vals );                  push @out, ( @vals );
1061    
1062                  warn "lookup for mfn $mfn returned ", dump(@vals), $/;                  warn "lookup for mfn $mfn returned ", dump(@vals), $/ if ($debug);
1063          }          }
1064    
1065  #       if (ref($lookup->{$k}) eq 'ARRAY') {  #       if (ref($lookup->{$k}) eq 'ARRAY') {
# Line 1065  sub lookup { Line 1070  sub lookup {
1070    
1071          $rec = $old_rec;          $rec = $old_rec;
1072    
1073          warn "## lookup returns = ", dump(@out), $/;          warn "## lookup returns = ", dump(@out), $/ if ($debug);
1074    
1075          return @out;          if ($#out == 0) {
1076                    return $out[0];
1077            } else {
1078                    return @out;
1079            }
1080  }  }
1081    
1082  =head2 save_into_lookup  =head2 save_into_lookup

Legend:
Removed from v.730  
changed lines
  Added in v.752

  ViewVC Help
Powered by ViewVC 1.1.26