--- trunk/lib/WebPAC/Normalize.pm 2005/07/24 15:03:11 31 +++ trunk/lib/WebPAC/Normalize.pm 2006/01/07 03:28:10 346 @@ -11,11 +11,11 @@ =head1 VERSION -Version 0.01 +Version 0.08 =cut -our $VERSION = '0.01'; +our $VERSION = '0.08'; =head1 SYNOPSIS @@ -47,6 +47,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 +86,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 +119,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; } @@ -122,13 +143,7 @@ This structures are used to produce output. - 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. + my $ds = $webpac->data_structure($rec); =cut @@ -140,18 +155,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); - $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper(@ds) }); - return @ds if ($#ds > 0); + 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}}; @@ -160,7 +178,7 @@ $self->{tags_by_order} = \@sorted_tags; } - my @ds; + my $ds; $log->debug("tags: ",sub { join(", ",@sorted_tags) }); @@ -171,7 +189,10 @@ #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { - my $format = $tag->{'value'} || $tag->{'content'}; + my $format; + + $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); + $format = $tag->{'value'} || $tag->{'content'}; $log->debug("format: $format"); @@ -192,28 +213,19 @@ @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) ); } # default types - my @types = qw(display swish); + my @types = qw(display search); # override by type attribute @types = ( $tag->{'type'} ) if ($tag->{'type'}); foreach my $type (@types) { # append to previous line? - $log->debug("type: $type ",sub { join(" ",@v) }, $row->{'append'} || 'no append'); + $log->debug("type: $type ",sub { join(" ",@v) }, " ", $row->{'append'} || 'no append'); if ($tag->{'append'}) { # I will delimit appended part with @@ -240,14 +252,14 @@ # TODO: name_sigular, name_plural my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; - $row->{'name'} = $name ? $self->_x($name) : $field; + my $row_name = $name ? $self->_x($name) : $field; # post-sort all values in field if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { $log->warn("sort at field tag not implemented"); } - push @ds, $row; + $ds->{$row_name} = $row; $log->debug("row $field: ",sub { Dumper($row) }); } @@ -255,14 +267,16 @@ } $self->{'db'}->save_ds( - ds => \@ds, - current_filename => $self->{'current_filename'}, - headline => $self->{'headline'}, + id => $id, + ds => $ds, + prefix => $self->{prefix}, ) if ($self->{'db'}); - $log->debug("ds: ", sub { Dumper(@ds) }); + $log->debug("ds: ", sub { Dumper($ds) }); + + $log->logconfess("data structure returned is not array any more!") if wantarray; - return @ds; + return $ds; } @@ -274,8 +288,18 @@ 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; @@ -293,7 +317,7 @@ my @out; - $log->debug("format: $format"); + $log->debug("format: $format [$i]"); my $eval_code; # remove eval{...} from beginning @@ -303,29 +327,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); @@ -345,11 +385,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; @@ -429,6 +479,7 @@ } my $found = 0; + my $just_single = 1; my $eval_code; # remove eval{...} from beginning @@ -440,9 +491,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"); @@ -459,7 +515,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"); } @@ -535,19 +593,22 @@ if ($sf && $$rec->{$f}->[$i]->{$sf}) { $$found++ if (defined($$found)); return $$rec->{$f}->[$i]->{$sf}; - } elsif ($$rec->{$f}->[$i]) { + } elsif (! $sf && $$rec->{$f}->[$i]) { $$found++ if (defined($$found)); # it still might have subfield, just # not specified, so we'll dump all if ($$rec->{$f}->[$i] =~ /HASH/o) { my $out; foreach my $k (keys %{$$rec->{$f}->[$i]}) { - $out .= $$rec->{$f}->[$i]->{$k}." "; + my $v = $$rec->{$f}->[$i]->{$k}; + $out .= "$v " if ($v); } return $out; } else { return $$rec->{$f}->[$i]; } + } else { + return ''; } } else { return ''; @@ -671,4 +732,4 @@ =cut -1; # End of WebPAC::DB +1; # End of WebPAC::Normalize