--- trunk/lib/WebPAC/Normalize.pm 2005/12/19 15:34:47 295 +++ trunk/lib/WebPAC/Normalize.pm 2006/01/08 20:32:06 368 @@ -2,6 +2,8 @@ use warnings; use strict; +use blib; +use WebPAC::Common; use base 'WebPAC::Common'; use Data::Dumper; @@ -11,11 +13,11 @@ =head1 VERSION -Version 0.07 +Version 0.08 =cut -our $VERSION = '0.07'; +our $VERSION = '0.08'; =head1 SYNOPSIS @@ -157,7 +159,7 @@ $log->debug("data_structure rec = ", sub { Dumper($rec) }); - $log->logdie("need unique ID (mfn) in field 000 of record ", sub { Dumper($rec) } ) unless (defined($rec->{'000'})); + $log->logdie("need unique ID (mfn) in field 000 of record " . Dumper($rec) ) unless (defined($rec->{'000'})); my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!"); @@ -194,15 +196,18 @@ $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); $format = $tag->{'value'} || $tag->{'content'}; - $log->debug("format: $format"); - my @v; if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { @v = $self->fill_in_to_arr($rec,$format); } else { @v = $self->parse_to_arr($rec,$format); } - next if (! @v); + if (! @v) { + $log->debug("$field <",$self->{tag},"> format: $format no values"); +# next; + } else { + $log->debug("$field <",$self->{tag},"> format: $format values: ", join(",", @v)); + } if ($tag->{'sort'}) { @v = $self->sort_arr(@v); @@ -225,7 +230,7 @@ foreach my $type (@types) { # append to previous line? - $log->debug("type: $type ",sub { join(" ",@v) }, " ", $row->{'append'} || 'no append'); + $log->debug("tag $field / $type [",sub { join(",",@v) }, "] ", $row->{'append'} || 'no append'); if ($tag->{'append'}) { # I will delimit appended part with @@ -317,7 +322,7 @@ my @out; - $log->debug("format: $format"); + $log->debug("format: $format [$i]"); my $eval_code; # remove eval{...} from beginning @@ -327,29 +332,45 @@ # remove filter{...} from beginning $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); + # did we found any (att all) field from format in row? + my $found_any; + # prefix before first field which we preserve it $found_any my $prefix; - my $all_found=0; + + my $f_step = 1; while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { my $del = $1 || ''; - $prefix ||= $del if ($all_found == 0); + $prefix = $del if ($f_step == 1); + + my $fld_type = lc($2); # repeatable index my $r = $i; - $r = 0 if (lc("$2") eq 's'); + if ($fld_type eq 's') { + if ($found_any->{'v'}) { + $r = 0; + } else { + return; + } + } my $found = 0; my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); if ($found) { - push @out, $del; + $found_any->{$fld_type} += $found; + + # we will skip delimiter before first occurence of field! + push @out, $del unless($found_any->{$fld_type} == 1); push @out, $tmp; - $all_found += $found; } + $f_step++; } - return if (! $all_found); + # test if any fields found? + return if (! $found_any->{'v'} && ! $found_any->{'s'}); my $out = join('',@out); @@ -463,6 +484,7 @@ } my $found = 0; + my $just_single = 1; my $eval_code; # remove eval{...} from beginning @@ -474,9 +496,14 @@ # do actual replacement of placeholders # repeatable fields - $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; + if ($format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges) { + $just_single = 0; + } + # non-repeatable fields - $format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; + if ($format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges) { + return if ($i > 0 && $just_single); + } if ($found) { $log->debug("format: $format"); @@ -531,8 +558,8 @@ my $i = 0; my @arr; - while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { - push @arr, @v; + while (my $v = $self->fill_in($rec,$format_utf8,$i++)) { + push @arr, $v; } $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); @@ -545,50 +572,76 @@ Returns value from record. - my $text = $self->get_data(\$rec,$f,$sf,$i,\$found); + my $text = $self->get_data(\$rec,$f,$sf,$i,\$found,\$fld_occurances); + +Required arguments are: + +=over 8 + +=item C<$rec> + +record reference + +=item C<$f> -Arguments are: -record reference C<$rec>, -field C<$f>, -optional subfiled C<$sf>, -index for repeatable values C<$i>. +field -Optinal variable C<$found> will be incremeted if there -is field. +=item C<$sf> -Returns value or empty string. +optional subfield + +=item C<$i> + +index offset for repeatable values ( 0 ... $#occurances ) + +=item C<$found> + +optional variable that will be incremeted if preset + +=item C<$fld_occurances> + +hash to hold maximum occurances of C combinations +(which can be accessed using keys in same format) + +=back + +Returns value or empty string, updates C<$found> and C +if present. =cut sub get_data { my $self = shift; - my ($rec,$f,$sf,$i,$found) = @_; + my ($rec,$f,$sf,$i,$found,$cache) = @_; - if ($$rec->{$f}) { - return '' if (! $$rec->{$f}->[$i]); + return '' unless ($$rec->{$f} && ref($$rec->{$f}) eq 'ARRAY'); + + if (defined($$cache)) { + $$cache->{"$f\t$sf"} ||= $$#rec->{$f}; + } + + return '' unless ($$rec->{$f}->[$i]); + + { no strict 'refs'; - if ($sf && $$rec->{$f}->[$i]->{$sf}) { - $$found++ if (defined($$found)); + if (defined($sf)) { + $$found++ if (defined($$found) && $$rec->{$f}->[$i]->{$sf}); return $$rec->{$f}->[$i]->{$sf}; - } elsif (! $sf && $$rec->{$f}->[$i]) { + } else { $$found++ if (defined($$found)); - # it still might have subfield, just - # not specified, so we'll dump all + # it still might have subfields, just + # not specified, so we'll dump some debug info if ($$rec->{$f}->[$i] =~ /HASH/o) { my $out; foreach my $k (keys %{$$rec->{$f}->[$i]}) { - $out .= $$rec->{$f}->[$i]->{$k}." "; + $out .= '$' . $k .':' . $$rec->{$f}->[$i]->{$k}." "; } return $out; } else { return $$rec->{$f}->[$i]; } - } else { - return ''; } - } else { - return ''; } }