/[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 373 by dpavlin, Sun Jan 8 22:09:33 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';
8  use Data::Dumper;  use Data::Dumper;
 use Storable;  
9    
10  =head1 NAME  =head1 NAME
11    
# 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.01  Version 0.08
17    
18  =cut  =cut
19    
20  our $VERSION = '0.01';  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 79  Create new normalisation object Line 85  Create new normalisation object
85                          return length($_);                          return length($_);
86                  }, ...                  }, ...
87          },          },
88          cache_data_structure => './cache/ds/',          db => $db_obj,
89          lookup_regex => $lookup->regex,          lookup_regex => $lookup->regex,
90            lookup => $lookup_obj,
91            prefix => 'foobar',
92    );    );
93    
94  Parametar C<filter> defines user supplied snippets of perl code which can  Parametar C<filter> defines user supplied snippets of perl code which can
95  be use with C<filter{...}> notation.  be use with C<filter{...}> notation.
96    
97  Optional parameter C<cache_data_structure> defines path to directory  C<prefix> is used to form filename for database record (to support multiple
98  in which cache file for C<data_structure> call will be created.  source files which are joined in one database).
99    
100  Recommended parametar C<lookup_regex> is used to enable parsing of lookups  Recommended parametar C<lookup_regex> is used to enable parsing of lookups
101  in structures.  in structures. If you pass this parametar, you must also pass C<lookup>
102    which is C<WebPAC::Lookup> object.
103    
104  =cut  =cut
105    
# Line 99  sub new { Line 108  sub new {
108          my $self = {@_};          my $self = {@_};
109          bless($self, $class);          bless($self, $class);
110    
111          $self->setup_cache_dir( $self->{'cache_data_structure'} );          my $r = $self->{'lookup_regex'} ? 1 : 0;
112            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.  
113    
114   $setup_cache_dir('./cache/ds/');          my $log = $self->_get_logger();
   
 If you pass false or zero value to this function, it will disable  
 cacheing.  
   
 =cut  
115    
116  sub setup_cache_dir {          # those two must be in pair
117          my $self = shift;          if ( ($r & $l) != ($r || $l) ) {
118                    my $log = $self->_get_logger();
119                    $log->logdie("lookup_regex and lookup must be in pair");
120            }
121    
122          my $dir = shift;          $log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup'));
123    
124          my $log = $self->_get_logger();          $log->warn("no prefix defined. please check that!") unless ($self->{'prefix'});
125    
126          if ($dir) {          $log->debug("using lookup regex: ", $self->{lookup_regex}) if ($r && $l);
                 my $msg;  
                 if (! -e $dir) {  
                         $msg = "doesn't exist";  
                 } elsif (! -d $dir) {  
                         $msg = "is not directory";  
                 } elsif (! -w $dir) {  
                         $msg = "not writable";  
                 }  
127    
128                  if ($msg) {          if (! $self->{filter} || ! $self->{filter}->{regex}) {
129                          undef $self->{'cache_data_structure'};                  $log->debug("adding built-in filter regex");
130                          $log->warn("cache_data_structure $dir $msg, disabling...");                  $self->{filter}->{regex} = sub {
131                  } else {                          my ($val, $regex) = @_;
132                          $log->debug("using cache dir $dir");                          eval "\$val =~ $regex";
133                  }                          return $val;
134          } else {                  };
                 $log->debug("disabling cache");  
                 undef $self->{'cache_data_structure'};  
135          }          }
136    
137            $self ? return $self : return undef;
138  }  }
139    
140    
# Line 152  C<conf/normalize/*.xml>. Line 145  C<conf/normalize/*.xml>.
145    
146  This structures are used to produce output.  This structures are used to produce output.
147    
148   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.  
149    
150  =cut  =cut
151    
# Line 170  sub data_structure { Line 157  sub data_structure {
157          my $rec = shift;          my $rec = shift;
158          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
159    
160            $log->debug("data_structure rec = ", sub { Dumper($rec) });
161    
162            $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!");
165    
166          my $cache_file;          my $cache_file;
167    
168          if (my $cache_path = $self->{'cache_data_structure'}) {          if ($self->{'db'}) {
169                  my $id = $rec->{'000'};                  my $ds = $self->{'db'}->load_ds( id => $id, prefix => $self->{prefix} );
170                  $id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o);                  $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) });
171                  unless (defined($id)) {                  return $ds if ($ds);
172                          $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'};  
                                         }  
                                 }  
                         }  
                 }  
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 214  sub data_structure { Line 180  sub data_structure {
180                  $self->{tags_by_order} = \@sorted_tags;                  $self->{tags_by_order} = \@sorted_tags;
181          }          }
182    
183          my @ds;          my $ds;
184    
185          $log->debug("tags: ",sub { join(", ",@sorted_tags) });          $log->debug("tags: ",sub { join(", ",@sorted_tags) });
186    
# Line 225  sub data_structure { Line 191  sub data_structure {
191  #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'}});
192    
193                  foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {                  foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {
194                          my $format = $tag->{'value'} || $tag->{'content'};                          my $format;
195    
196                          $log->debug("format: $format");                          $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH');
197                            $format = $tag->{'value'} || $tag->{'content'};
198    
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 246  sub data_structure { Line 218  sub data_structure {
218                                  @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v;                                  @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v;
219                          }                          }
220    
                         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!  
                         }  
   
221                          # delimiter will join repeatable fields                          # delimiter will join repeatable fields
222                          if ($tag->{'delimiter'}) {                          if ($tag->{'delimiter'}) {
223                                  @v = ( join($tag->{'delimiter'}, @v) );                                  @v = ( join($tag->{'delimiter'}, @v) );
224                          }                          }
225    
226                          # default types                          # default types
227                          my @types = qw(display swish);                          my @types = qw(display search);
228                          # override by type attribute                          # override by type attribute
229                          @types = ( $tag->{'type'} ) if ($tag->{'type'});                          @types = ( $tag->{'type'} ) if ($tag->{'type'});
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 294  sub data_structure { Line 257  sub data_structure {
257    
258                          # TODO: name_sigular, name_plural                          # TODO: name_sigular, name_plural
259                          my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'};                          my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'};
260                          $row->{'name'} = $name ? $self->_x($name) : $field;                          my $row_name = $name ? $self->_x($name) : $field;
261    
262                          # post-sort all values in field                          # post-sort all values in field
263                          if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) {                          if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) {
264                                  $log->warn("sort at field tag not implemented");                                  $log->warn("sort at field tag not implemented");
265                          }                          }
266    
267                          push @ds, $row;                          $ds->{$row_name} = $row;
268    
269                          $log->debug("row $field: ",sub { Dumper($row) });                          $log->debug("row $field: ",sub { Dumper($row) });
270                  }                  }
271    
272          }          }
273    
274          if ($cache_file) {          $self->{'db'}->save_ds(
275                  store {                  id => $id,
276                          ds => \@ds,                  ds => $ds,
277                          current_filename => $self->{'current_filename'},                  prefix => $self->{prefix},
278                          headline => $self->{'headline'},          ) if ($self->{'db'});
                 }, $cache_file;  
                 $log->debug("created storable cache file $cache_file");  
         }  
279    
280          return @ds;          $log->debug("ds: ", sub { Dumper($ds) });
281    
282            $log->logconfess("data structure returned is not array any more!") if wantarray;
283    
284            return $ds;
285    
286  }  }
287    
# Line 329  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 348  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 358  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 400  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 462  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 469  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 484  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 495  sub fill_in { Line 473  sub fill_in {
473    
474          # do actual replacement of placeholders          # do actual replacement of placeholders
475          # repeatable fields          # repeatable fields
476          $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) {
477                    $just_single = 0;
478            }
479    
480          # non-repeatable fields          # non-repeatable fields
481          $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) {
482                    return if ($i > 0 && $just_single);
483            }
484    
485          if ($found) {          if ($found) {
486                  $log->debug("format: $format");                  $log->debug("format: $format");
# Line 513  sub fill_in { Line 496  sub fill_in {
496                  }                  }
497                  # do we have lookups?                  # do we have lookups?
498                  if ($self->{'lookup'}) {                  if ($self->{'lookup'}) {
499                          return $self->lookup($format);                          if ($self->{'lookup'}->can('lookup')) {
500                                    my @lookup = $self->{lookup}->lookup($format);
501                                    $log->debug("lookup $format", join(", ", @lookup));
502                                    return @lookup;
503                            } else {
504                                    $log->warn("Have lookup object but can't invoke lookup method");
505                            }
506                  } else {                  } else {
507                          return $format;                          return $format;
508                  }                  }
# Line 523  sub fill_in { Line 512  sub fill_in {
512  }  }
513    
514    
515  =head2 fill_in_to_arr  =head2 _rec_to_arr
516    
517  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
518  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
519  C<fill_id>ed.  C<paste>d or C<fill_id>ed. Last argument is name of operation: C<paste> or C<fill_in>.
520    
521   my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]');   my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]','paste');
522    
523  =cut  =cut
524    
525  sub fill_in_to_arr {  sub _rec_to_arr {
526          my $self = shift;          my $self = shift;
527    
528          my ($rec, $format_utf8) = @_;          my ($rec, $format_utf8, $code) = @_;
529    
530          my $log = $self->_get_logger();          my $log = $self->_get_logger();
531    
532          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
533          return if (! $format_utf8);          return if (! $format_utf8);
534    
535            $log->debug("using $code on $format_utf8");
536    
537          my $i = 0;          my $i = 0;
538            my $max = 0;
539          my @arr;          my @arr;
540            my $rec_size = {};
541    
542          while (my @v = $self->fill_in($rec,$format_utf8,$i++)) {          while ($i <= $max) {
543                  push @arr, @v;                  my @v = $self->$code($rec,$format_utf8,$i++,\$rec_size);
544                    if ($rec_size) {
545                            foreach my $f (keys %{ $rec_size }) {
546                                    $max = $rec_size->{$f} if ($rec_size->{$f} > $max);
547                            }
548                            $log->debug("max set to $max");
549                            undef $rec_size;
550                    }
551                    if (@v) {
552                            push @arr, @v;
553                    } else {
554                            push @arr, '';
555                    }
556          }          }
557    
558          $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 560  sub fill_in_to_arr { Line 565  sub fill_in_to_arr {
565    
566  Returns value from record.  Returns value from record.
567    
568   my $text = $self->get_data(\$rec,$f,$sf,$i,\$found);   my $text = $self->get_data(\$rec,$f,$sf,$i,\$found,\$rec_size);
569    
570    Required arguments are:
571    
572    =over 8
573    
574    =item C<$rec>
575    
576    record reference
577    
578    =item C<$f>
579    
580    field
581    
582    =item C<$sf>
583    
584    optional subfield
585    
586  Arguments are:  =item C<$i>
 record reference C<$rec>,  
 field C<$f>,  
 optional subfiled C<$sf>,  
 index for repeatable values C<$i>.  
587    
588  Optinal variable C<$found> will be incremeted if there  index offset for repeatable values ( 0 ... $rec_size->{'400^a'} )
 is field.  
589    
590  Returns value or empty string.  =item C<$found>
591    
592    optional variable that will be incremeted if preset
593    
594    =item C<$rec_size>
595    
596    hash to hold maximum occurances of C<field^subfield> combinations
597    (which can be accessed using keys in same format)
598    
599    =back
600    
601    Returns value or empty string, updates C<$found> and C<rec_size>
602    if present.
603    
604  =cut  =cut
605    
606  sub get_data {  sub get_data {
607          my $self = shift;          my $self = shift;
608    
609          my ($rec,$f,$sf,$i,$found) = @_;          my ($rec,$f,$sf,$i,$found,$cache) = @_;
610    
611            return '' unless ($$rec->{$f} && ref($$rec->{$f}) eq 'ARRAY');
612    
613          if ($$rec->{$f}) {          if (defined($$cache)) {
614                  return '' if (! $$rec->{$f}->[$i]);                  $$cache->{ $f . ( $sf ? '^' . $sf : '' ) } ||= scalar @{ $$rec->{$f} };
615            }
616    
617            return '' unless ($$rec->{$f}->[$i]);
618    
619            {
620                  no strict 'refs';                  no strict 'refs';
621                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {                  if (defined($sf)) {
622                          $$found++ if (defined($$found));                          $$found++ if (defined($$found) && $$rec->{$f}->[$i]->{$sf});
623                          return $$rec->{$f}->[$i]->{$sf};                          return $$rec->{$f}->[$i]->{$sf};
624                  } elsif ($$rec->{$f}->[$i]) {                  } else {
625                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
626                          # it still might have subfield, just                          # it still might have subfields, just
627                          # not specified, so we'll dump all                          # not specified, so we'll dump some debug info
628                          if ($$rec->{$f}->[$i] =~ /HASH/o) {                          if ($$rec->{$f}->[$i] =~ /HASH/o) {
629                                  my $out;                                  my $out;
630                                  foreach my $k (keys %{$$rec->{$f}->[$i]}) {                                  foreach my $k (keys %{$$rec->{$f}->[$i]}) {
631                                          $out .= $$rec->{$f}->[$i]->{$k}." ";                                          $out .= '$' . $k .':' . $$rec->{$f}->[$i]->{$k}." ";
632                                  }                                  }
633                                  return $out;                                  return $out;
634                          } else {                          } else {
635                                  return $$rec->{$f}->[$i];                                  return $$rec->{$f}->[$i];
636                          }                          }
637                  }                  }
         } else {  
                 return '';  
638          }          }
639  }  }
640    
# Line 639  sub apply_format { Line 672  sub apply_format {
672          $log->debug("using format $name [$format] on $data to produce: $out");          $log->debug("using format $name [$format] on $data to produce: $out");
673    
674          if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) {          if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) {
675                  return $self->lookup($out);                  return $self->{'lookup'}->lookup($out);
676          } else {          } else {
677                  return $out;                  return $out;
678          }          }
# Line 722  under the same terms as Perl itself. Line 755  under the same terms as Perl itself.
755    
756  =cut  =cut
757    
758  1; # End of WebPAC::DB  1; # End of WebPAC::Normalize

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

  ViewVC Help
Powered by ViewVC 1.1.26