--- trunk/lib/WebPAC/Normalize.pm 2005/12/15 17:01:10 253 +++ trunk/lib/WebPAC/Normalize.pm 2006/02/15 15:54:12 397 @@ -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.05 +Version 0.08 =cut -our $VERSION = '0.05'; +our $VERSION = '0.08'; =head1 SYNOPSIS @@ -47,6 +49,10 @@ code defined as code ref on format after field substitution to producing output +There is one built-in filter called C which can be use like this: + + filter{regex(s/foo/bar/)} + =item * optional C will be then performed. See C. @@ -119,6 +125,15 @@ $log->debug("using lookup regex: ", $self->{lookup_regex}) if ($r && $l); + if (! $self->{filter} || ! $self->{filter}->{regex}) { + $log->debug("adding built-in filter regex"); + $self->{filter}->{regex} = sub { + my ($val, $regex) = @_; + eval "\$val =~ $regex"; + return $val; + }; + } + $self ? return $self : return undef; } @@ -144,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!"); @@ -157,9 +172,6 @@ $log->debug("cache miss, creating"); } - undef $self->{'currnet_filename'}; - undef $self->{'headline'}; - my @sorted_tags; if ($self->{tags_by_order}) { @sorted_tags = @{$self->{tags_by_order}}; @@ -184,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); + @v = $self->_rec_to_arr($rec,$format,'fill_in'); + } else { + @v = $self->_rec_to_arr($rec,$format,'parse'); + } + if (! @v) { + $log->debug("$field <",$self->{tag},"> format: $format no values"); + next; } else { - @v = $self->parse_to_arr($rec,$format); + $log->debug("$field <",$self->{tag},"> format: $format values: ", join(",", @v)); } - next if (! @v); if ($tag->{'sort'}) { @v = $self->sort_arr(@v); @@ -215,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 @@ -278,12 +293,22 @@ my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); +Filters are implemented here. While simple form of filters looks like this: + + filter{name_of_filter} + +but, filters can also have variable number of parametars like this: + + filter{name_of_filter(param,param,param)} + =cut +my $warn_once; + sub parse { my $self = shift; - my ($rec, $format_utf8, $i) = @_; + my ($rec, $format_utf8, $i, $rec_size) = @_; return if (! $format_utf8); @@ -297,7 +322,7 @@ my @out; - $log->debug("format: $format"); + $log->debug("format: $format [$i]"); my $eval_code; # remove eval{...} from beginning @@ -307,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); + my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found,$rec_size); 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); @@ -349,47 +390,26 @@ return if (! $self->_eval($eval)); } - if ($filter_name && $self->{'filter'}->{$filter_name}) { - $log->debug("about to filter{$filter_name} format: $out"); - $out = $self->{'filter'}->{$filter_name}->($out); - return unless(defined($out)); - $log->debug("filter result: $out"); + if ($filter_name) { + my @filter_args; + if ($filter_name =~ s/(\w+)\((.*)\)/$1/) { + @filter_args = split(/,/, $2); + } + if ($self->{'filter'}->{$filter_name}) { + $log->debug("about to filter{$filter_name} format: $out with arguments: ", join(",", @filter_args)); + unshift @filter_args, $out; + $out = $self->{'filter'}->{$filter_name}->(@filter_args); + return unless(defined($out)); + $log->debug("filter result: $out"); + } elsif (! $warn_once->{$filter_name}) { + $log->warn("trying to use undefined filter $filter_name"); + $warn_once->{$filter_name}++; + } } return $out; } -=head2 parse_to_arr - -Similar to C, but returns array of all repeatable fields - - my @arr = $webpac->parse_to_arr($rec,'v250^a'); - -=cut - -sub parse_to_arr { - my $self = shift; - - my ($rec, $format_utf8) = @_; - - my $log = $self->_get_logger(); - - $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); - return if (! $format_utf8); - - my $i = 0; - my @arr; - - while (my $v = $self->parse($rec,$format_utf8,$i++)) { - push @arr, $v; - } - - $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); - - return @arr; -} - - =head2 fill_in Workhourse of all: takes record from in-memory structure of database and @@ -411,6 +431,11 @@ This method will automatically decode UTF-8 string to local code page if needed. +There is optional parametar C<$record_size> which can be used to get sizes of +all C combinations in this format. + + my $text = $webpac->fill_in($rec,'got: v900^a v900^x',0,\$rec_size); + =cut sub fill_in { @@ -418,10 +443,13 @@ my $log = $self->_get_logger(); - my $rec = shift || $log->logconfess("need data record"); - my $format = shift || $log->logconfess("need format to parse"); + my ($rec,$format,$i,$rec_size) = @_; + + $log->logconfess("need data record") unless ($rec); + $log->logconfess("need format to parse") unless($format); + # iteration (for repeatable fields) - my $i = shift || 0; + $i ||= 0; $log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); @@ -433,6 +461,7 @@ } my $found = 0; + my $just_single = 1; my $eval_code; # remove eval{...} from beginning @@ -442,11 +471,21 @@ # remove filter{...} from beginning $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); - # do actual replacement of placeholders - # repeatable fields - $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; - # non-repeatable fields - $format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; + { + # fix warnings + no warnings 'uninitialized'; + + # do actual replacement of placeholders + # repeatable fields + if ($format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found,$rec_size)/ges) { + $just_single = 0; + } + + # non-repeatable fields + if ($format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found,$rec_size)/ges) { + return if ($i > 0 && $just_single); + } + } if ($found) { $log->debug("format: $format"); @@ -478,31 +517,47 @@ } -=head2 fill_in_to_arr +=head2 _rec_to_arr -Similar to C, but returns array of all repeatable fields. Usable +Similar to C and C, but returns array of all repeatable fields. Usable for fields which have lookups, so they shouldn't be parsed but rather -Ced. +Cd or Ced. Last argument is name of operation: C or C. - my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); + my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]','paste'); =cut -sub fill_in_to_arr { +sub _rec_to_arr { my $self = shift; - my ($rec, $format_utf8) = @_; + my ($rec, $format_utf8, $code) = @_; my $log = $self->_get_logger(); $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); return if (! $format_utf8); + $log->debug("using $code on $format_utf8"); + my $i = 0; + my $max = 0; my @arr; + my $rec_size = {}; - while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { - push @arr, @v; + while ($i <= $max) { + my @v = $self->$code($rec,$format_utf8,$i++,\$rec_size); + if ($rec_size) { + foreach my $f (keys %{ $rec_size }) { + $max = $rec_size->{$f} if ($rec_size->{$f} > $max); + } + $log->debug("max set to $max"); + undef $rec_size; + } + if (@v) { + push @arr, @v; + } else { + push @arr, '' if ($max > $i); + } } $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); @@ -515,50 +570,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,\$rec_size); + +Required arguments are: + +=over 8 -Arguments are: -record reference C<$rec>, -field C<$f>, -optional subfiled C<$sf>, -index for repeatable values C<$i>. +=item C<$rec> -Optinal variable C<$found> will be incremeted if there -is field. +record reference -Returns value or empty string. +=item C<$f> + +field + +=item C<$sf> + +optional subfield + +=item C<$i> + +index offset for repeatable values ( 0 ... $rec_size->{'400^a'} ) + +=item C<$found> + +optional variable that will be incremeted if preset + +=item C<$rec_size> + +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) = @_; + + return '' unless ($$rec->{$f} && ref($$rec->{$f}) eq 'ARRAY'); + + if (defined($$cache)) { + $$cache->{ $f . ( $sf ? '^' . $sf : '' ) } ||= scalar @{ $$rec->{$f} }; + } + + return '' unless ($$rec->{$f}->[$i]); - if ($$rec->{$f}) { - return '' if (! $$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 ''; } }