--- trunk/lib/WebPAC/Normalize.pm 2005/11/20 20:13:39 74 +++ trunk/lib/WebPAC/Normalize.pm 2006/01/08 21:16:27 371 @@ -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.02 +Version 0.08 =cut -our $VERSION = '0.02'; +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. @@ -82,11 +88,15 @@ db => $db_obj, lookup_regex => $lookup->regex, lookup => $lookup_obj, + prefix => 'foobar', ); Parametar C defines user supplied snippets of perl code which can be use with C notation. +C is used to form filename for database record (to support multiple +source files which are joined in one database). + Recommended parametar C is used to enable parsing of lookups in structures. If you pass this parametar, you must also pass C which is C object. @@ -111,6 +121,19 @@ $log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup')); + $log->warn("no prefix defined. please check that!") unless ($self->{'prefix'}); + + $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; } @@ -124,12 +147,6 @@ my $ds = $webpac->data_structure($rec); -B - -This method will also set C<< $webpac->{'currnet_filename'} >> if there is -C<< >> tag and C<< $webpac->{'headline'} >> if there is -C<< >> tag. - =cut sub data_structure { @@ -140,18 +157,21 @@ my $rec = shift; $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); + $log->debug("data_structure rec = ", sub { Dumper($rec) }); + + $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!"); + my $cache_file; if ($self->{'db'}) { - my $ds = $self->{'db'}->load_ds($rec); + my $ds = $self->{'db'}->load_ds( id => $id, prefix => $self->{prefix} ); $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) }); return $ds if ($ds); $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}}; @@ -176,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); @@ -195,15 +218,6 @@ @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; } - if ($field eq 'filename') { - $self->{'current_filename'} = join('',@v); - $log->debug("filename: ",$self->{'current_filename'}); - } elsif ($field eq 'headline') { - $self->{'headline'} .= join('',@v); - $log->debug("headline: ",$self->{'headline'}); - next; # don't return headline in data_structure! - } - # delimiter will join repeatable fields if ($tag->{'delimiter'}) { @v = ( join($tag->{'delimiter'}, @v) ); @@ -216,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 @@ -257,12 +271,10 @@ } - $log->logdie("there is no current_filename defined! Do you have filename tag in conf/normalize/?.xml") unless ($self->{'current_filename'}); - $self->{'db'}->save_ds( + id => $id, ds => $ds, - current_filename => $self->{'current_filename'}, - headline => $self->{'headline'}, + prefix => $self->{prefix}, ) if ($self->{'db'}); $log->debug("ds: ", sub { Dumper($ds) }); @@ -281,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); @@ -300,7 +322,7 @@ my @out; - $log->debug("format: $format"); + $log->debug("format: $format [$i]"); my $eval_code; # remove eval{...} from beginning @@ -310,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); @@ -352,11 +390,21 @@ 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; @@ -383,8 +431,11 @@ my $i = 0; my @arr; - while (my $v = $self->parse($rec,$format_utf8,$i++)) { + my $rec_size = { '_' => '_' }; + + while (my $v = $self->parse($rec,$format_utf8,$i++,\$rec_size)) { push @arr, $v; + warn "parse rec_size = ", Dumper($rec_size); } $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); @@ -414,6 +465,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 { @@ -421,10 +477,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)); @@ -436,6 +495,7 @@ } my $found = 0; + my $just_single = 1; my $eval_code; # remove eval{...} from beginning @@ -447,9 +507,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,$rec_size)/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,$rec_size)/ges) { + return if ($i > 0 && $just_single); + } if ($found) { $log->debug("format: $format"); @@ -466,7 +531,9 @@ # do we have lookups? if ($self->{'lookup'}) { if ($self->{'lookup'}->can('lookup')) { - return $self->{'lookup'}->lookup($format); + my @lookup = $self->{lookup}->lookup($format); + $log->debug("lookup $format", join(", ", @lookup)); + return @lookup; } else { $log->warn("Have lookup object but can't invoke lookup method"); } @@ -502,8 +569,11 @@ my $i = 0; my @arr; - while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { - push @arr, @v; + my $rec_size; + + while (my $v = $self->fill_in($rec,$format_utf8,$i,\$rec_size)) { + push @arr, $v; + warn "rec_size = ", Dumper($rec_size); } $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); @@ -516,50 +586,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 + +=item C<$rec> + +record reference + +=item C<$f> + +field -Arguments are: -record reference C<$rec>, -field C<$f>, -optional subfiled C<$sf>, -index for repeatable values C<$i>. +=item C<$sf> -Optinal variable C<$found> will be incremeted if there -is field. +optional subfield -Returns value or empty string. +=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) = @_; - if ($$rec->{$f}) { - return '' if (! $$rec->{$f}->[$i]); + return '' unless ($$rec->{$f} && ref($$rec->{$f}) eq 'ARRAY'); + + if (defined($$cache)) { + $$cache->{ $f . ( $sf ? '^' . $sf : '' ) } ||= scalar @{ $$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 ''; } } @@ -680,4 +776,4 @@ =cut -1; # End of WebPAC::DB +1; # End of WebPAC::Normalize