/[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 15 by dpavlin, Sun Jul 17 10:42:23 2005 UTC revision 217 by dpavlin, Mon Dec 5 17:47:51 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;
 use Storable;  
7    
8  =head1 NAME  =head1 NAME
9    
# Line 11  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.03
15    
16  =cut  =cut
17    
18  our $VERSION = '0.01';  our $VERSION = '0.03';
19    
20  =head1 SYNOPSIS  =head1 SYNOPSIS
21    
# Line 79  Create new normalisation object Line 79  Create new normalisation object
79                          return length($_);                          return length($_);
80                  }, ...                  }, ...
81          },          },
82          cache_data_structure => './cache/ds/',          db => $db_obj,
83          lookup_regex => $lookup->regex,          lookup_regex => $lookup->regex,
84            lookup => $lookup_obj,
85    );    );
86    
87  Parametar C<filter> defines user supplied snippets of perl code which can  Parametar C<filter> defines user supplied snippets of perl code which can
88  be use with C<filter{...}> notation.  be use with C<filter{...}> notation.
89    
 Optional parameter C<cache_data_structure> defines path to directory  
 in which cache file for C<data_structure> call will be created.  
   
90  Recommended parametar C<lookup_regex> is used to enable parsing of lookups  Recommended parametar C<lookup_regex> is used to enable parsing of lookups
91  in structures.  in structures. If you pass this parametar, you must also pass C<lookup>
92    which is C<WebPAC::Lookup> object.
93    
94  =cut  =cut
95    
# Line 99  sub new { Line 98  sub new {
98          my $self = {@_};          my $self = {@_};
99          bless($self, $class);          bless($self, $class);
100    
101          $self->setup_cache_dir( $self->{'cache_data_structure'} );          my $r = $self->{'lookup_regex'} ? 1 : 0;
102            my $l = $self->{'lookup'} ? 1 : 0;
         $self ? return $self : return undef;  
 }  
   
 =head2 setup_cache_dir  
   
 Check if specified cache directory exist, and if not, disable caching.  
   
  $setup_cache_dir('./cache/ds/');  
   
 If you pass false or zero value to this function, it will disable  
 cacheing.  
   
 =cut  
   
 sub setup_cache_dir {  
         my $self = shift;  
   
         my $dir = shift;  
103    
104          my $log = $self->_get_logger();          my $log = $self->_get_logger();
105    
106          if ($dir) {          # those two must be in pair
107                  my $msg;          if ( ($r & $l) != ($r || $l) ) {
108                  if (! -e $dir) {                  my $log = $self->_get_logger();
109                          $msg = "doesn't exist";                  $log->logdie("lookup_regex and lookup must be in pair");
                 } elsif (! -d $dir) {  
                         $msg = "is not directory";  
                 } elsif (! -w $dir) {  
                         $msg = "not writable";  
                 }  
   
                 if ($msg) {  
                         undef $self->{'cache_data_structure'};  
                         $log->warn("cache_data_structure $dir $msg, disabling...");  
                 } else {  
                         $log->debug("using cache dir $dir");  
                 }  
         } else {  
                 $log->debug("disabling cache");  
                 undef $self->{'cache_data_structure'};  
110          }          }
111    
112            $log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup'));
113    
114            $self ? return $self : return undef;
115  }  }
116    
117    
# Line 152  C<conf/normalize/*.xml>. Line 122  C<conf/normalize/*.xml>.
122    
123  This structures are used to produce output.  This structures are used to produce output.
124    
125   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.  
126    
127  =cut  =cut
128    
# Line 170  sub data_structure { Line 134  sub data_structure {
134          my $rec = shift;          my $rec = shift;
135          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
136    
137            $log->debug("data_structure rec = ", sub { Dumper($rec) });
138    
139            $log->logdie("need unique ID (mfn) in field 000 of record ", sub { Dumper($rec) } ) unless (defined($rec->{'000'}));
140    
141            my $mfn = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!");
142    
143          my $cache_file;          my $cache_file;
144    
145          if (my $cache_path = $self->{'cache_data_structure'}) {          if ($self->{'db'}) {
146                  my $id = $rec->{'000'};                  my $ds = $self->{'db'}->load_ds( id => $mfn );
147                  $id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o);                  $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) });
148                  unless (defined($id)) {                  return $ds if ($ds);
149                          $log->warn("Can't use cache_data_structure on records without unique identifier in field 000");                  $log->debug("cache miss, creating");
                         undef $self->{'cache_data_structure'};  
                 } else {  
                         $cache_file = "$cache_path/$id";  
                         if (-r $cache_file) {  
                                 my $ds_ref = retrieve($cache_file);  
                                 if ($ds_ref) {  
                                         $log->debug("cache hit: $cache_file");  
                                         my $ok = 1;  
                                         foreach my $f (qw(current_filename headline)) {  
                                                 if ($ds_ref->{$f}) {  
                                                         $self->{$f} = $ds_ref->{$f};  
                                                 } else {  
                                                         $ok = 0;  
                                                 }  
                                         };  
                                         if ($ok && $ds_ref->{'ds'}) {  
                                                 return @{ $ds_ref->{'ds'} };  
                                         } else {  
                                                 $log->warn("cache_data_structure $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!");  
                                                 undef $self->{'cache_data_structure'};  
                                         }  
                                 }  
                         }  
                 }  
150          }          }
151    
152          undef $self->{'currnet_filename'};          undef $self->{'currnet_filename'};
# Line 214  sub data_structure { Line 160  sub data_structure {
160                  $self->{tags_by_order} = \@sorted_tags;                  $self->{tags_by_order} = \@sorted_tags;
161          }          }
162    
163          my @ds;          my $ds;
164    
165          $log->debug("tags: ",sub { join(", ",@sorted_tags) });          $log->debug("tags: ",sub { join(", ",@sorted_tags) });
166    
# Line 225  sub data_structure { Line 171  sub data_structure {
171  #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'}});
172    
173                  foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {                  foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {
174                          my $format = $tag->{'value'} || $tag->{'content'};                          my $format;
175    
176                            $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH');
177                            $format = $tag->{'value'} || $tag->{'content'};
178    
179                          $log->debug("format: $format");                          $log->debug("format: $format");
180    
# Line 246  sub data_structure { Line 195  sub data_structure {
195                                  @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v;                                  @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v;
196                          }                          }
197    
                         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!  
                         }  
   
198                          # delimiter will join repeatable fields                          # delimiter will join repeatable fields
199                          if ($tag->{'delimiter'}) {                          if ($tag->{'delimiter'}) {
200                                  @v = ( join($tag->{'delimiter'}, @v) );                                  @v = ( join($tag->{'delimiter'}, @v) );
201                          }                          }
202    
203                          # default types                          # default types
204                          my @types = qw(display swish);                          my @types = qw(display search);
205                          # override by type attribute                          # override by type attribute
206                          @types = ( $tag->{'type'} ) if ($tag->{'type'});                          @types = ( $tag->{'type'} ) if ($tag->{'type'});
207    
# Line 294  sub data_structure { Line 234  sub data_structure {
234    
235                          # TODO: name_sigular, name_plural                          # TODO: name_sigular, name_plural
236                          my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'};                          my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'};
237                          $row->{'name'} = $name ? $self->_x($name) : $field;                          my $row_name = $name ? $self->_x($name) : $field;
238    
239                          # post-sort all values in field                          # post-sort all values in field
240                          if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) {                          if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) {
241                                  $log->warn("sort at field tag not implemented");                                  $log->warn("sort at field tag not implemented");
242                          }                          }
243    
244                          push @ds, $row;                          $ds->{$row_name} = $row;
245    
246                          $log->debug("row $field: ",sub { Dumper($row) });                          $log->debug("row $field: ",sub { Dumper($row) });
247                  }                  }
248    
249          }          }
250    
251          if ($cache_file) {          $self->{'db'}->save_ds(
252                  store {                  id => $mfn,
253                          ds => \@ds,                  ds => $ds,
254                          current_filename => $self->{'current_filename'},          ) if ($self->{'db'});
255                          headline => $self->{'headline'},  
256                  }, $cache_file;          $log->debug("ds: ", sub { Dumper($ds) });
                 $log->debug("created storable cache file $cache_file");  
         }  
257    
258          return @ds;          $log->logconfess("data structure returned is not array any more!") if wantarray;
259    
260            return $ds;
261    
262  }  }
263    
# Line 513  sub fill_in { Line 453  sub fill_in {
453                  }                  }
454                  # do we have lookups?                  # do we have lookups?
455                  if ($self->{'lookup'}) {                  if ($self->{'lookup'}) {
456                          return $self->lookup($format);                          if ($self->{'lookup'}->can('lookup')) {
457                                    return $self->{'lookup'}->lookup($format);
458                            } else {
459                                    $log->warn("Have lookup object but can't invoke lookup method");
460                            }
461                  } else {                  } else {
462                          return $format;                          return $format;
463                  }                  }
# Line 586  sub get_data { Line 530  sub get_data {
530                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {
531                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
532                          return $$rec->{$f}->[$i]->{$sf};                          return $$rec->{$f}->[$i]->{$sf};
533                  } elsif ($$rec->{$f}->[$i]) {                  } elsif (! $sf && $$rec->{$f}->[$i]) {
534                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
535                          # it still might have subfield, just                          # it still might have subfield, just
536                          # not specified, so we'll dump all                          # not specified, so we'll dump all
# Line 599  sub get_data { Line 543  sub get_data {
543                          } else {                          } else {
544                                  return $$rec->{$f}->[$i];                                  return $$rec->{$f}->[$i];
545                          }                          }
546                    } else {
547                            return '';
548                  }                  }
549          } else {          } else {
550                  return '';                  return '';
# Line 639  sub apply_format { Line 585  sub apply_format {
585          $log->debug("using format $name [$format] on $data to produce: $out");          $log->debug("using format $name [$format] on $data to produce: $out");
586    
587          if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) {          if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) {
588                  return $self->lookup($out);                  return $self->{'lookup'}->lookup($out);
589          } else {          } else {
590                  return $out;                  return $out;
591          }          }
# Line 722  under the same terms as Perl itself. Line 668  under the same terms as Perl itself.
668    
669  =cut  =cut
670    
671  1; # End of WebPAC::DB  1; # End of WebPAC::Normalize

Legend:
Removed from v.15  
changed lines
  Added in v.217

  ViewVC Help
Powered by ViewVC 1.1.26