--- trunk/lib/WebPAC/Normalize.pm 2006/07/03 21:08:07 574 +++ trunk/lib/WebPAC/Normalize.pm 2006/07/23 17:33:11 601 @@ -35,11 +35,11 @@ =head1 VERSION -Version 0.10 +Version 0.14 =cut -our $VERSION = '0.10'; +our $VERSION = '0.14'; =head1 SYNOPSIS @@ -69,6 +69,7 @@ row => $row, rules => $normalize_pl_config, marc_encoding => 'utf-8', + config => $config, ); Options C, C, C and C are mandatory while all @@ -90,6 +91,7 @@ no strict 'subs'; _set_lookup( $arg->{lookup} ); _set_rec( $arg->{row} ); + _set_config( $arg->{config} ); _clean_ds( %{ $arg } ); eval "$arg->{rules}"; die "error evaling $arg->{rules}: $@\n" if ($@); @@ -111,6 +113,34 @@ $rec = shift or die "no record hash"; } +=head2 _set_config + +Set current config hash + + _set_config( $config ); + +Magic keys are: + +=over 4 + +=item _ + +Code of current database + +=item _mfn + +Current MFN + +=back + +=cut + +my $config; + +sub _set_config { + $config = shift; +} + =head2 _get_ds Return hash formatted as data structure @@ -326,7 +356,7 @@ warn "## saved/3 ", dump( $field ),$/ if ($debug); } - return @m; + return \@m; } =head2 _debug @@ -504,6 +534,8 @@ my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); my $m = [ $f, $i1, $i2 ]; + warn "### marc_compose input subfields = ", dump(@_),$/ if ($debug > 2); + while (@_) { my $sf = shift or die "marc_compose $f needs subfield"; my $v = shift; @@ -514,7 +546,7 @@ warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1); } - warn "## marc_compose(d) ", dump( $m ),$/ if ($debug > 1); + warn "## marc_compose current marc = ", dump( $m ),$/ if ($debug > 1); push @{ $marc_record->[ $marc_record_offset ] }, $m if ($#{$m} > 2); } @@ -565,7 +597,8 @@ warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); - foreach my $i ( 0 .. $#{ $marc } ) { + my $i = 0; + foreach ( 0 .. $#{ $marc } ) { last unless (defined $marc->[$i]); warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); if ($marc->[$i]->[0] eq $f) { @@ -591,6 +624,7 @@ } } } + $i++; } warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); @@ -645,7 +679,14 @@ my $f = shift; return unless (defined($rec && $rec->{$f})); my $sf = shift; - return map { $_->{$sf} } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; + warn "rec2($f,$sf) = ", dump( $rec->{$f} ), $/ if ($debug > 1); + return map { + if (ref($_->{$sf}) eq 'ARRAY') { + @{ $_->{$sf} }; + } else { + $_->{$sf}; + } + } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; } =head2 rec @@ -658,10 +699,16 @@ =cut sub rec { + my @out; if ($#_ == 0) { - return rec1(@_); + @out = rec1(@_); } elsif ($#_ == 1) { - return rec2(@_); + @out = rec2(@_); + } + if (@out) { + return @out; + } else { + return ''; } } @@ -694,7 +741,7 @@ =cut sub prefix { - my $p = shift or die "prefix needs string as first argument"; + my $p = shift or return; return map { $p . $_ } grep { defined($_) } @_; } @@ -757,6 +804,78 @@ } } +=head2 config + +Consult config values stored in C + + # return database code (key under databases in yaml) + $database_code = config(); # use _ from hash + $database_name = config('name'); + $database_input_name = config('input name'); + $tag = config('input normalize tag'); + +Up to three levels are supported. + +=cut + +sub config { + return unless ($config); + + my $p = shift; + + $p ||= ''; + + my $v; + + warn "### getting config($p)\n" if ($debug > 1); + + my @p = split(/\s+/,$p); + if ($#p < 0) { + $v = $config->{ '_' }; # special, database code + } else { + + my $c = dclone( $config ); + + foreach my $k (@p) { + warn "### k: $k c = ",dump($c),$/ if ($debug > 1); + if (ref($c) eq 'ARRAY') { + $c = shift @$c; + warn "config($p) taking first occurence of '$k', probably not what you wanted!\n"; + last; + } + + if (! defined($c->{$k}) ) { + $c = undef; + last; + } else { + $c = $c->{$k}; + } + } + $v = $c if ($c); + + } + + warn "## config( '$p' ) = ",dump( $v ),$/ if ($v && $debug); + warn "config( '$p' ) is empty\n" if (! $v); + + return $v; +} + +=head2 id + +Returns unique id of this record + + $id = id(); + +Returns C<42/2> for 2nd occurence of MFN 42. + +=cut + +sub id { + my $mfn = $config->{_mfn} || die "no _mfn in config data"; + return $mfn . $#{$marc_record} ? $#{$marc_record} + 1 : ''; +} + =head2 join_with Joins walues with some delimiter @@ -767,7 +886,10 @@ sub join_with { my $d = shift; - return join($d, grep { defined($_) && $_ ne '' } @_); + warn "### join_with('$d',",dump(@_),")\n" if ($debug > 2); + my $v = join($d, grep { defined($_) && $_ ne '' } @_); + return '' unless defined($v); + return $v; } =head2 split_rec_on