/[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 18 by dpavlin, Sun Jul 17 14:53:37 2005 UTC revision 252 by dpavlin, Thu Dec 15 17:01:04 2005 UTC
# Line 2  package WebPAC::Normalize; Line 2  package WebPAC::Normalize;
2    
3  use warnings;  use warnings;
4  use strict;  use strict;
5    use base 'WebPAC::Common';
6  use Data::Dumper;  use Data::Dumper;
7    
8  =head1 NAME  =head1 NAME
# Line 10  WebPAC::Normalize - data mungling for no Line 11  WebPAC::Normalize - data mungling for no
11    
12  =head1 VERSION  =head1 VERSION
13    
14  Version 0.01  Version 0.04
15    
16  =cut  =cut
17    
18  our $VERSION = '0.01';  our $VERSION = '0.04';
19    
20  =head1 SYNOPSIS  =head1 SYNOPSIS
21    
# Line 78  Create new normalisation object Line 79  Create new normalisation object
79                          return length($_);                          return length($_);
80                  }, ...                  }, ...
81          },          },
82          db => $webpac_db_obj,          db => $db_obj,
83          lookup_regex => $lookup->regex,          lookup_regex => $lookup->regex,
84            lookup => $lookup_obj,
85            prefix => 'foobar',
86    );    );
87    
88  Parametar C<filter> defines user supplied snippets of perl code which can  Parametar C<filter> defines user supplied snippets of perl code which can
89  be use with C<filter{...}> notation.  be use with C<filter{...}> notation.
90    
91    C<prefix> is used to form filename for database record (to support multiple
92    source files which are joined in one database).
93    
94  Recommended parametar C<lookup_regex> is used to enable parsing of lookups  Recommended parametar C<lookup_regex> is used to enable parsing of lookups
95  in structures.  in structures. If you pass this parametar, you must also pass C<lookup>
96    which is C<WebPAC::Lookup> object.
97    
98  =cut  =cut
99    
# Line 95  sub new { Line 102  sub new {
102          my $self = {@_};          my $self = {@_};
103          bless($self, $class);          bless($self, $class);
104    
105            my $r = $self->{'lookup_regex'} ? 1 : 0;
106            my $l = $self->{'lookup'} ? 1 : 0;
107    
108            my $log = $self->_get_logger();
109    
110            # those two must be in pair
111            if ( ($r & $l) != ($r || $l) ) {
112                    my $log = $self->_get_logger();
113                    $log->logdie("lookup_regex and lookup must be in pair");
114            }
115    
116            $log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup'));
117    
118            $log->warn("no prefix defined. please check that!") unless ($self->{'prefix'});
119    
120            $log->debug("using lookup regex: ", $self->{lookup_regex}) if ($r && $l);
121    
122          $self ? return $self : return undef;          $self ? return $self : return undef;
123  }  }
124    
# Line 106  C<conf/normalize/*.xml>. Line 130  C<conf/normalize/*.xml>.
130    
131  This structures are used to produce output.  This structures are used to produce output.
132    
133   my @ds = $webpac->data_structure($rec);   my $ds = $webpac->data_structure($rec);
   
 B<Note: historical oddity follows>  
   
 This method will also set C<< $webpac->{'currnet_filename'} >> if there is  
 C<< <filename> >> tag and C<< $webpac->{'headline'} >> if there is  
 C<< <headline> >> tag.  
134    
135  =cut  =cut
136    
# Line 124  sub data_structure { Line 142  sub data_structure {
142          my $rec = shift;          my $rec = shift;
143          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
144    
145            $log->debug("data_structure rec = ", sub { Dumper($rec) });
146    
147            $log->logdie("need unique ID (mfn) in field 000 of record ", sub { Dumper($rec) } ) unless (defined($rec->{'000'}));
148    
149            my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!");
150    
151          my $cache_file;          my $cache_file;
152    
153          if ($self->{'db'}) {          if ($self->{'db'}) {
154                  my @ds = $self->{'db'}->get_ds($rec);                  my $ds = $self->{'db'}->load_ds( id => $id, prefix => $self->{prefix} );
155                  return @ds if (@ds);                  $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) });
156                    return $ds if ($ds);
157                    $log->debug("cache miss, creating");
158          }          }
159    
160          undef $self->{'currnet_filename'};          undef $self->{'currnet_filename'};
# Line 142  sub data_structure { Line 168  sub data_structure {
168                  $self->{tags_by_order} = \@sorted_tags;                  $self->{tags_by_order} = \@sorted_tags;
169          }          }
170    
171          my @ds;          my $ds;
172    
173          $log->debug("tags: ",sub { join(", ",@sorted_tags) });          $log->debug("tags: ",sub { join(", ",@sorted_tags) });
174    
# Line 153  sub data_structure { Line 179  sub data_structure {
179  #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}});  #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}});
180    
181                  foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {                  foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {
182                          my $format = $tag->{'value'} || $tag->{'content'};                          my $format;
183    
184                            $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH');
185                            $format = $tag->{'value'} || $tag->{'content'};
186    
187                          $log->debug("format: $format");                          $log->debug("format: $format");
188    
# Line 174  sub data_structure { Line 203  sub data_structure {
203                                  @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v;                                  @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v;
204                          }                          }
205    
                         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!  
                         }  
   
206                          # delimiter will join repeatable fields                          # delimiter will join repeatable fields
207                          if ($tag->{'delimiter'}) {                          if ($tag->{'delimiter'}) {
208                                  @v = ( join($tag->{'delimiter'}, @v) );                                  @v = ( join($tag->{'delimiter'}, @v) );
209                          }                          }
210    
211                          # default types                          # default types
212                          my @types = qw(display swish);                          my @types = qw(display search);
213                          # override by type attribute                          # override by type attribute
214                          @types = ( $tag->{'type'} ) if ($tag->{'type'});                          @types = ( $tag->{'type'} ) if ($tag->{'type'});
215    
216                          foreach my $type (@types) {                          foreach my $type (@types) {
217                                  # append to previous line?                                  # append to previous line?
218                                  $log->debug("type: $type ",sub { join(" ",@v) }, $row->{'append'} || 'no append');                                  $log->debug("type: $type ",sub { join(" ",@v) }, " ", $row->{'append'} || 'no append');
219                                  if ($tag->{'append'}) {                                  if ($tag->{'append'}) {
220    
221                                          # I will delimit appended part with                                          # I will delimit appended part with
# Line 222  sub data_structure { Line 242  sub data_structure {
242    
243                          # TODO: name_sigular, name_plural                          # TODO: name_sigular, name_plural
244                          my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'};                          my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'};
245                          $row->{'name'} = $name ? $self->_x($name) : $field;                          my $row_name = $name ? $self->_x($name) : $field;
246    
247                          # post-sort all values in field                          # post-sort all values in field
248                          if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) {                          if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) {
249                                  $log->warn("sort at field tag not implemented");                                  $log->warn("sort at field tag not implemented");
250                          }                          }
251    
252                          push @ds, $row;                          $ds->{$row_name} = $row;
253    
254                          $log->debug("row $field: ",sub { Dumper($row) });                          $log->debug("row $field: ",sub { Dumper($row) });
255                  }                  }
256    
257          }          }
258    
259          $self->{'db'}->put_gs(          $self->{'db'}->save_ds(
260                  ds => \@ds,                  id => $id,
261                  current_filename => $self->{'current_filename'},                  ds => $ds,
262                  headline => $self->{'headline'},                  prefix => $self->{prefix},
263          ) if ($self->{'db'});          ) if ($self->{'db'});
264    
265          return @ds;          $log->debug("ds: ", sub { Dumper($ds) });
266    
267            $log->logconfess("data structure returned is not array any more!") if wantarray;
268    
269            return $ds;
270    
271  }  }
272    
# Line 438  sub fill_in { Line 462  sub fill_in {
462                  }                  }
463                  # do we have lookups?                  # do we have lookups?
464                  if ($self->{'lookup'}) {                  if ($self->{'lookup'}) {
465                          return $self->lookup($format);                          if ($self->{'lookup'}->can('lookup')) {
466                                    my @lookup = $self->{lookup}->lookup($format);
467                                    $log->debug('lookup $format', join(", ", @lookup));
468                                    return @lookup;
469                            } else {
470                                    $log->warn("Have lookup object but can't invoke lookup method");
471                            }
472                  } else {                  } else {
473                          return $format;                          return $format;
474                  }                  }
# Line 511  sub get_data { Line 541  sub get_data {
541                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {
542                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
543                          return $$rec->{$f}->[$i]->{$sf};                          return $$rec->{$f}->[$i]->{$sf};
544                  } elsif ($$rec->{$f}->[$i]) {                  } elsif (! $sf && $$rec->{$f}->[$i]) {
545                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
546                          # it still might have subfield, just                          # it still might have subfield, just
547                          # not specified, so we'll dump all                          # not specified, so we'll dump all
# Line 524  sub get_data { Line 554  sub get_data {
554                          } else {                          } else {
555                                  return $$rec->{$f}->[$i];                                  return $$rec->{$f}->[$i];
556                          }                          }
557                    } else {
558                            return '';
559                  }                  }
560          } else {          } else {
561                  return '';                  return '';
# Line 564  sub apply_format { Line 596  sub apply_format {
596          $log->debug("using format $name [$format] on $data to produce: $out");          $log->debug("using format $name [$format] on $data to produce: $out");
597    
598          if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) {          if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) {
599                  return $self->lookup($out);                  return $self->{'lookup'}->lookup($out);
600          } else {          } else {
601                  return $out;                  return $out;
602          }          }
# Line 647  under the same terms as Perl itself. Line 679  under the same terms as Perl itself.
679    
680  =cut  =cut
681    
682  1; # End of WebPAC::DB  1; # End of WebPAC::Normalize

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

  ViewVC Help
Powered by ViewVC 1.1.26