/[webpac2]/trunk/lib/WebPAC/Normalize.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Normalize.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 252 by dpavlin, Thu Dec 15 17:01:04 2005 UTC revision 397 by dpavlin, Wed Feb 15 15:54:12 2006 UTC
# Line 2  package WebPAC::Normalize; Line 2  package WebPAC::Normalize;
2    
3  use warnings;  use warnings;
4  use strict;  use strict;
5    use blib;
6    use WebPAC::Common;
7  use base 'WebPAC::Common';  use base 'WebPAC::Common';
8  use Data::Dumper;  use Data::Dumper;
9    
# Line 11  WebPAC::Normalize - data mungling for no Line 13  WebPAC::Normalize - data mungling for no
13    
14  =head1 VERSION  =head1 VERSION
15    
16  Version 0.04  Version 0.08
17    
18  =cut  =cut
19    
20  our $VERSION = '0.04';  our $VERSION = '0.08';
21    
22  =head1 SYNOPSIS  =head1 SYNOPSIS
23    
# Line 47  optional C<filter{filter_name}> at B<beg Line 49  optional C<filter{filter_name}> at B<beg
49  code defined as code ref on format after field substitution to producing  code defined as code ref on format after field substitution to producing
50  output  output
51    
52    There is one built-in filter called C<regex> which can be use like this:
53    
54      filter{regex(s/foo/bar/)}
55    
56  =item *  =item *
57    
58  optional C<lookup{...}> will be then performed. See C<WebPAC::Lookups>.  optional C<lookup{...}> will be then performed. See C<WebPAC::Lookups>.
# Line 119  sub new { Line 125  sub new {
125    
126          $log->debug("using lookup regex: ", $self->{lookup_regex}) if ($r && $l);          $log->debug("using lookup regex: ", $self->{lookup_regex}) if ($r && $l);
127    
128            if (! $self->{filter} || ! $self->{filter}->{regex}) {
129                    $log->debug("adding built-in filter regex");
130                    $self->{filter}->{regex} = sub {
131                            my ($val, $regex) = @_;
132                            eval "\$val =~ $regex";
133                            return $val;
134                    };
135            }
136    
137          $self ? return $self : return undef;          $self ? return $self : return undef;
138  }  }
139    
# Line 144  sub data_structure { Line 159  sub data_structure {
159    
160          $log->debug("data_structure rec = ", sub { Dumper($rec) });          $log->debug("data_structure rec = ", sub { Dumper($rec) });
161    
162          $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'}));
163    
164          my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!");          my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!");
165    
# Line 157  sub data_structure { Line 172  sub data_structure {
172                  $log->debug("cache miss, creating");                  $log->debug("cache miss, creating");
173          }          }
174    
         undef $self->{'currnet_filename'};  
         undef $self->{'headline'};  
   
175          my @sorted_tags;          my @sorted_tags;
176          if ($self->{tags_by_order}) {          if ($self->{tags_by_order}) {
177                  @sorted_tags = @{$self->{tags_by_order}};                  @sorted_tags = @{$self->{tags_by_order}};
# Line 184  sub data_structure { Line 196  sub data_structure {
196                          $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH');                          $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH');
197                          $format = $tag->{'value'} || $tag->{'content'};                          $format = $tag->{'value'} || $tag->{'content'};
198    
                         $log->debug("format: $format");  
   
199                          my @v;                          my @v;
200                          if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) {                          if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) {
201                                  @v = $self->fill_in_to_arr($rec,$format);                                  @v = $self->_rec_to_arr($rec,$format,'fill_in');
202                            } else {
203                                    @v = $self->_rec_to_arr($rec,$format,'parse');
204                            }
205                            if (! @v) {
206                                    $log->debug("$field <",$self->{tag},"> format: $format no values");
207                                    next;
208                          } else {                          } else {
209                                  @v = $self->parse_to_arr($rec,$format);                                  $log->debug("$field <",$self->{tag},"> format: $format values: ", join(",", @v));
210                          }                          }
                         next if (! @v);  
211    
212                          if ($tag->{'sort'}) {                          if ($tag->{'sort'}) {
213                                  @v = $self->sort_arr(@v);                                  @v = $self->sort_arr(@v);
# Line 215  sub data_structure { Line 230  sub data_structure {
230    
231                          foreach my $type (@types) {                          foreach my $type (@types) {
232                                  # append to previous line?                                  # append to previous line?
233                                  $log->debug("type: $type ",sub { join(" ",@v) }, " ", $row->{'append'} || 'no append');                                  $log->debug("tag $field / $type [",sub { join(",",@v) }, "] ", $row->{'append'} || 'no append');
234                                  if ($tag->{'append'}) {                                  if ($tag->{'append'}) {
235    
236                                          # I will delimit appended part with                                          # I will delimit appended part with
# Line 278  return output or nothing depending on ev Line 293  return output or nothing depending on ev
293    
294   my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i);   my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i);
295    
296    Filters are implemented here. While simple form of filters looks like this:
297    
298      filter{name_of_filter}
299    
300    but, filters can also have variable number of parametars like this:
301    
302      filter{name_of_filter(param,param,param)}
303    
304  =cut  =cut
305    
306    my $warn_once;
307    
308  sub parse {  sub parse {
309          my $self = shift;          my $self = shift;
310    
311          my ($rec, $format_utf8, $i) = @_;          my ($rec, $format_utf8, $i, $rec_size) = @_;
312    
313          return if (! $format_utf8);          return if (! $format_utf8);
314    
# Line 297  sub parse { Line 322  sub parse {
322    
323          my @out;          my @out;
324    
325          $log->debug("format: $format");          $log->debug("format: $format [$i]");
326    
327          my $eval_code;          my $eval_code;
328          # remove eval{...} from beginning          # remove eval{...} from beginning
# Line 307  sub parse { Line 332  sub parse {
332          # remove filter{...} from beginning          # remove filter{...} from beginning
333          $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s);          $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s);
334    
335            # did we found any (att all) field from format in row?
336            my $found_any;
337            # prefix before first field which we preserve it $found_any
338          my $prefix;          my $prefix;
339          my $all_found=0;  
340            my $f_step = 1;
341    
342          while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) {          while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) {
343    
344                  my $del = $1 || '';                  my $del = $1 || '';
345                  $prefix ||= $del if ($all_found == 0);                  $prefix = $del if ($f_step == 1);
346    
347                    my $fld_type = lc($2);
348    
349                  # repeatable index                  # repeatable index
350                  my $r = $i;                  my $r = $i;
351                  $r = 0 if (lc("$2") eq 's');                  if ($fld_type eq 's') {
352                            if ($found_any->{'v'}) {
353                                    $r = 0;
354                            } else {
355                                    return;
356                            }
357                    }
358    
359                  my $found = 0;                  my $found = 0;
360                  my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found);                  my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found,$rec_size);
361    
362                  if ($found) {                  if ($found) {
363                          push @out, $del;                          $found_any->{$fld_type} += $found;
364    
365                            # we will skip delimiter before first occurence of field!
366                            push @out, $del unless($found_any->{$fld_type} == 1);
367                          push @out, $tmp;                          push @out, $tmp;
                         $all_found += $found;  
368                  }                  }
369                    $f_step++;
370          }          }
371    
372          return if (! $all_found);          # test if any fields found?
373            return if (! $found_any->{'v'} && ! $found_any->{'s'});
374    
375          my $out = join('',@out);          my $out = join('',@out);
376    
# Line 349  sub parse { Line 390  sub parse {
390                  return if (! $self->_eval($eval));                  return if (! $self->_eval($eval));
391          }          }
392                    
393          if ($filter_name && $self->{'filter'}->{$filter_name}) {          if ($filter_name) {
394                  $log->debug("about to filter{$filter_name} format: $out");                  my @filter_args;
395                  $out = $self->{'filter'}->{$filter_name}->($out);                  if ($filter_name =~ s/(\w+)\((.*)\)/$1/) {
396                  return unless(defined($out));                          @filter_args = split(/,/, $2);
397                  $log->debug("filter result: $out");                  }
398                    if ($self->{'filter'}->{$filter_name}) {
399                            $log->debug("about to filter{$filter_name} format: $out with arguments: ", join(",", @filter_args));
400                            unshift @filter_args, $out;
401                            $out = $self->{'filter'}->{$filter_name}->(@filter_args);
402                            return unless(defined($out));
403                            $log->debug("filter result: $out");
404                    } elsif (! $warn_once->{$filter_name}) {
405                            $log->warn("trying to use undefined filter $filter_name");
406                            $warn_once->{$filter_name}++;
407                    }
408          }          }
409    
410          return $out;          return $out;
411  }  }
412    
 =head2 parse_to_arr  
   
 Similar to C<parse>, 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;  
 }  
   
   
413  =head2 fill_in  =head2 fill_in
414    
415  Workhourse of all: takes record from in-memory structure of database and  Workhourse of all: takes record from in-memory structure of database and
# Line 411  delimiters before fields which aren't us Line 431  delimiters before fields which aren't us
431  This method will automatically decode UTF-8 string to local code page  This method will automatically decode UTF-8 string to local code page
432  if needed.  if needed.
433    
434    There is optional parametar C<$record_size> which can be used to get sizes of
435    all C<field^subfield> combinations in this format.
436    
437     my $text = $webpac->fill_in($rec,'got: v900^a v900^x',0,\$rec_size);
438    
439  =cut  =cut
440    
441  sub fill_in {  sub fill_in {
# Line 418  sub fill_in { Line 443  sub fill_in {
443    
444          my $log = $self->_get_logger();          my $log = $self->_get_logger();
445    
446          my $rec = shift || $log->logconfess("need data record");          my ($rec,$format,$i,$rec_size) = @_;
447          my $format = shift || $log->logconfess("need format to parse");  
448            $log->logconfess("need data record") unless ($rec);
449            $log->logconfess("need format to parse") unless($format);
450    
451          # iteration (for repeatable fields)          # iteration (for repeatable fields)
452          my $i = shift || 0;          $i ||= 0;
453    
454          $log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999));          $log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999));
455    
# Line 433  sub fill_in { Line 461  sub fill_in {
461          }          }
462    
463          my $found = 0;          my $found = 0;
464            my $just_single = 1;
465    
466          my $eval_code;          my $eval_code;
467          # remove eval{...} from beginning          # remove eval{...} from beginning
# Line 442  sub fill_in { Line 471  sub fill_in {
471          # remove filter{...} from beginning          # remove filter{...} from beginning
472          $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s);          $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s);
473    
474          # do actual replacement of placeholders          {
475          # repeatable fields                  # fix warnings
476          $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges;                  no warnings 'uninitialized';
477          # non-repeatable fields  
478          $format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges;                  # do actual replacement of placeholders
479                    # repeatable fields
480                    if ($format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found,$rec_size)/ges) {
481                            $just_single = 0;
482                    }
483    
484                    # non-repeatable fields
485                    if ($format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found,$rec_size)/ges) {
486                            return if ($i > 0 && $just_single);
487                    }
488            }
489    
490          if ($found) {          if ($found) {
491                  $log->debug("format: $format");                  $log->debug("format: $format");
# Line 464  sub fill_in { Line 503  sub fill_in {
503                  if ($self->{'lookup'}) {                  if ($self->{'lookup'}) {
504                          if ($self->{'lookup'}->can('lookup')) {                          if ($self->{'lookup'}->can('lookup')) {
505                                  my @lookup = $self->{lookup}->lookup($format);                                  my @lookup = $self->{lookup}->lookup($format);
506                                  $log->debug('lookup $format', join(", ", @lookup));                                  $log->debug("lookup $format", join(", ", @lookup));
507                                  return @lookup;                                  return @lookup;
508                          } else {                          } else {
509                                  $log->warn("Have lookup object but can't invoke lookup method");                                  $log->warn("Have lookup object but can't invoke lookup method");
# Line 478  sub fill_in { Line 517  sub fill_in {
517  }  }
518    
519    
520  =head2 fill_in_to_arr  =head2 _rec_to_arr
521    
522  Similar to C<fill_in>, but returns array of all repeatable fields. Usable  Similar to C<parse> and C<fill_in>, but returns array of all repeatable fields. Usable
523  for fields which have lookups, so they shouldn't be parsed but rather  for fields which have lookups, so they shouldn't be parsed but rather
524  C<fill_id>ed.  C<paste>d or C<fill_id>ed. Last argument is name of operation: C<paste> or C<fill_in>.
525    
526   my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]');   my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]','paste');
527    
528  =cut  =cut
529    
530  sub fill_in_to_arr {  sub _rec_to_arr {
531          my $self = shift;          my $self = shift;
532    
533          my ($rec, $format_utf8) = @_;          my ($rec, $format_utf8, $code) = @_;
534    
535          my $log = $self->_get_logger();          my $log = $self->_get_logger();
536    
537          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
538          return if (! $format_utf8);          return if (! $format_utf8);
539    
540            $log->debug("using $code on $format_utf8");
541    
542          my $i = 0;          my $i = 0;
543            my $max = 0;
544          my @arr;          my @arr;
545            my $rec_size = {};
546    
547          while (my @v = $self->fill_in($rec,$format_utf8,$i++)) {          while ($i <= $max) {
548                  push @arr, @v;                  my @v = $self->$code($rec,$format_utf8,$i++,\$rec_size);
549                    if ($rec_size) {
550                            foreach my $f (keys %{ $rec_size }) {
551                                    $max = $rec_size->{$f} if ($rec_size->{$f} > $max);
552                            }
553                            $log->debug("max set to $max");
554                            undef $rec_size;
555                    }
556                    if (@v) {
557                            push @arr, @v;
558                    } else {
559                            push @arr, '' if ($max > $i);
560                    }
561          }          }
562    
563          $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr);          $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr);
# Line 515  sub fill_in_to_arr { Line 570  sub fill_in_to_arr {
570    
571  Returns value from record.  Returns value from record.
572    
573   my $text = $self->get_data(\$rec,$f,$sf,$i,\$found);   my $text = $self->get_data(\$rec,$f,$sf,$i,\$found,\$rec_size);
574    
575    Required arguments are:
576    
577    =over 8
578    
579  Arguments are:  =item C<$rec>
 record reference C<$rec>,  
 field C<$f>,  
 optional subfiled C<$sf>,  
 index for repeatable values C<$i>.  
580    
581  Optinal variable C<$found> will be incremeted if there  record reference
 is field.  
582    
583  Returns value or empty string.  =item C<$f>
584    
585    field
586    
587    =item C<$sf>
588    
589    optional subfield
590    
591    =item C<$i>
592    
593    index offset for repeatable values ( 0 ... $rec_size->{'400^a'} )
594    
595    =item C<$found>
596    
597    optional variable that will be incremeted if preset
598    
599    =item C<$rec_size>
600    
601    hash to hold maximum occurances of C<field^subfield> combinations
602    (which can be accessed using keys in same format)
603    
604    =back
605    
606    Returns value or empty string, updates C<$found> and C<rec_size>
607    if present.
608    
609  =cut  =cut
610    
611  sub get_data {  sub get_data {
612          my $self = shift;          my $self = shift;
613    
614          my ($rec,$f,$sf,$i,$found) = @_;          my ($rec,$f,$sf,$i,$found,$cache) = @_;
615    
616            return '' unless ($$rec->{$f} && ref($$rec->{$f}) eq 'ARRAY');
617    
618            if (defined($$cache)) {
619                    $$cache->{ $f . ( $sf ? '^' . $sf : '' ) } ||= scalar @{ $$rec->{$f} };
620            }
621    
622            return '' unless ($$rec->{$f}->[$i]);
623    
624          if ($$rec->{$f}) {          {
                 return '' if (! $$rec->{$f}->[$i]);  
625                  no strict 'refs';                  no strict 'refs';
626                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {                  if (defined($sf)) {
627                          $$found++ if (defined($$found));                          $$found++ if (defined($$found) && $$rec->{$f}->[$i]->{$sf});
628                          return $$rec->{$f}->[$i]->{$sf};                          return $$rec->{$f}->[$i]->{$sf};
629                  } elsif (! $sf && $$rec->{$f}->[$i]) {                  } else {
630                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
631                          # it still might have subfield, just                          # it still might have subfields, just
632                          # not specified, so we'll dump all                          # not specified, so we'll dump some debug info
633                          if ($$rec->{$f}->[$i] =~ /HASH/o) {                          if ($$rec->{$f}->[$i] =~ /HASH/o) {
634                                  my $out;                                  my $out;
635                                  foreach my $k (keys %{$$rec->{$f}->[$i]}) {                                  foreach my $k (keys %{$$rec->{$f}->[$i]}) {
636                                          $out .= $$rec->{$f}->[$i]->{$k}." ";                                          $out .= '$' . $k .':' . $$rec->{$f}->[$i]->{$k}." ";
637                                  }                                  }
638                                  return $out;                                  return $out;
639                          } else {                          } else {
640                                  return $$rec->{$f}->[$i];                                  return $$rec->{$f}->[$i];
641                          }                          }
                 } else {  
                         return '';  
642                  }                  }
         } else {  
                 return '';  
643          }          }
644  }  }
645    

Legend:
Removed from v.252  
changed lines
  Added in v.397

  ViewVC Help
Powered by ViewVC 1.1.26