/[webpac]/trunk2/lib/WebPAC.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 /trunk2/lib/WebPAC.pm

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

revision 363 by dpavlin, Wed Jun 16 20:05:19 2004 UTC revision 367 by dpavlin, Thu Jun 17 12:05:01 2004 UTC
# Line 1  Line 1 
1  package WebPAC;  package WebPAC;
2    
3    use warnings;
4    use strict;
5    
6  use Carp;  use Carp;
7  use Text::Iconv;  use Text::Iconv;
8  use Config::IniFiles;  use Config::IniFiles;
# Line 77  sub new { Line 80  sub new {
80    
81          $self->{indexer_config_file} = new Config::IniFiles( -file => $self->{config_file} ) || croak "can't open '$self->{config_file}'";          $self->{indexer_config_file} = new Config::IniFiles( -file => $self->{config_file} ) || croak "can't open '$self->{config_file}'";
82    
83            $self->{'utf2cp'} = Text::Iconv->new('UTF-8' ,$self->{'code_page'});
84          return $self;          return $self;
85  }  }
86    
# Line 96  By default, ISIS code page is assumed to Line 100  By default, ISIS code page is assumed to
100  If optional parametar C<limit_mfn> is set, it will read just 500 records  If optional parametar C<limit_mfn> is set, it will read just 500 records
101  from database in example above.  from database in example above.
102    
 Returns number of last record read into memory (size of database, really).  
   
103  C<lookup> argument is an array of lookups to create. Each lookup must have C<key> and  C<lookup> argument is an array of lookups to create. Each lookup must have C<key> and
104  C<val>. Optional parametar C<eval> is perl code to evaluate before storing  C<val>. Optional parametar C<eval> is perl code to evaluate before storing
105  value in index.  value in index.
# Line 109  value in index. Line 111  value in index.
111      'val' => 'v900' },      'val' => 'v900' },
112   ]   ]
113    
114    Returns number of last record read into memory (size of database, really).
115    
116  =cut  =cut
117    
118  sub open_isis {  sub open_isis {
# Line 209  sub open_import_xml { Line 213  sub open_import_xml {
213          my $arg = {@_};          my $arg = {@_};
214          confess "need type to load file from import_xml/" if (! $arg->{'type'});          confess "need type to load file from import_xml/" if (! $arg->{'type'});
215    
216          my $type = $arg->{'type'};          $self->{'type'} = $arg->{'type'};
217    
218          my $type_base = $type;          my $type_base = $arg->{'type'};
219          $type_base =~ s/_.*$//g;          $type_base =~ s/_.*$//g;
220    
221          my $f = "./import_xml/$type.xml";          $self->{'tag'} = $type2tag{$type_base};
222    
223            print STDERR "using type ",$self->{'type'}," tag ",$self->{'tag'},"\n" if ($self->{'debug'});
224    
225            my $f = "./import_xml/".$self->{'type'}.".xml";
226          confess "import_xml file '$f' doesn't exist!" if (! -e "$f");          confess "import_xml file '$f' doesn't exist!" if (! -e "$f");
227    
228          print STDERR "reading '$f'\n" if ($self->{'debug'});          print STDERR "reading '$f'\n" if ($self->{'debug'});
229    
230          $self->{'import_xml'} = XMLin($f,          $self->{'import_xml'} = XMLin($f,
231                  ForceArray => [ $type2tag{$type_base}, 'config', 'format' ],                  ForceArray => [ $self->{'tag'}, 'config', 'format' ],
232                  ForceContent => 1                  ForceContent => 1
233          );          );
234    
# Line 232  sub open_import_xml { Line 240  sub open_import_xml {
240    
241  Create lookup from record using lookup definition.  Create lookup from record using lookup definition.
242    
243     $self->create_lookup($rec, @lookups);
244    
245    Called internally by C<open_*> methods.
246    
247  =cut  =cut
248    
249  sub create_lookup {  sub create_lookup {
# Line 262  sub create_lookup { Line 274  sub create_lookup {
274    
275  Returns value from record.  Returns value from record.
276    
277   $self->get_data(\$rec,$f,$sf,$i,\$found);   my $text = $self->get_data(\$rec,$f,$sf,$i,\$found);
278    
279  Arguments are:  Arguments are:
280  record reference C<$rec>,  record reference C<$rec>,
# Line 270  field C<$f>, Line 282  field C<$f>,
282  optional subfiled C<$sf>,  optional subfiled C<$sf>,
283  index for repeatable values C<$i>.  index for repeatable values C<$i>.
284    
285  Optinal variable C<$found> will be incremeted if thre  Optinal variable C<$found> will be incremeted if there
286  is field.  is field.
287    
288  Returns value or empty string.  Returns value or empty string.
# Line 281  sub get_data { Line 293  sub get_data {
293          my $self = shift;          my $self = shift;
294    
295          my ($rec,$f,$sf,$i,$found) = @_;          my ($rec,$f,$sf,$i,$found) = @_;
296    
297          if ($$rec->{$f}) {          if ($$rec->{$f}) {
298                    return '' if (! $$rec->{$f}->[$i]);
299                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {
300                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
301                          return $$rec->{$f}->[$i]->{$sf};                          return $$rec->{$f}->[$i]->{$sf};
302                  } elsif ($$rec->{$f}->[$i]) {                  } elsif ($$rec->{$f}->[$i]) {
303                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
304                          return $$rec->{$f}->[$i];                          # it still might have subfield, just
305                            # not specified, so we'll dump all
306                            if ($$rec->{$f}->[$i] =~ /HASH/o) {
307                                    my $out;
308                                    foreach my $k (keys %{$$rec->{$f}->[$i]}) {
309                                            $out .= $$rec->{$f}->[$i]->{$k}." ";
310                                    }
311                                    return $out;
312                            } else {
313                                    return $$rec->{$f}->[$i];
314                            }
315                  }                  }
316          } else {          } else {
317                  return '';                  return '';
# Line 300  Workhourse of all: takes record from in- Line 324  Workhourse of all: takes record from in-
324  strings with placeholders and returns string or array of with substituted  strings with placeholders and returns string or array of with substituted
325  values from record.  values from record.
326    
327   $webpac->fill_in($rec,'v250^a');   my $text = $webpac->fill_in($rec,'v250^a');
328    
329  Optional argument is ordinal number for repeatable fields. By default,  Optional argument is ordinal number for repeatable fields. By default,
330  it's assume to be first repeatable field (fields are perl array, so first  it's assume to be first repeatable field (fields are perl array, so first
331  element is 0).  element is 0).
332  Following example will read second value from repeatable field.  Following example will read second value from repeatable field.
333    
334   $webpac->fill_in($rec,'Title: v250^a',1);   my $text = $webpac->fill_in($rec,'Title: v250^a',1);
335    
336  This function B<does not> perform parsing of format to inteligenty skip  This function B<does not> perform parsing of format to inteligenty skip
337  delimiters before fields which aren't used.  delimiters before fields which aren't used.
# Line 354  sub fill_in { Line 378  sub fill_in {
378    
379  Perform lookups on format supplied to it.  Perform lookups on format supplied to it.
380    
381   my $txt = $self->lookup('[v900]');   my $text = $self->lookup('[v900]');
382    
383  Lookups can be nested (like C<[d:[a:[v900]]]>).  Lookups can be nested (like C<[d:[a:[v900]]]>).
384    
# Line 367  sub lookup { Line 391  sub lookup {
391    
392          if ($tmp =~ /\[[^\[\]]+\]/o) {          if ($tmp =~ /\[[^\[\]]+\]/o) {
393                  my @in = ( $tmp );                  my @in = ( $tmp );
 #print "##lookup $tmp\n";  
394                  my @out;                  my @out;
395                  while (my $f = shift @in) {                  while (my $f = shift @in) {
396                          if ($f =~ /\[([^\[\]]+)\]/) {                          if ($f =~ /\[([^\[\]]+)\]/) {
397                                  my $k = $1;                                  my $k = $1;
398                                  if ($self->{'lookup'}->{$k}) {                                  if ($self->{'lookup'}->{$k}) {
 #print "## lookup key = $k\n";  
399                                          foreach my $nv (@{$self->{'lookup'}->{$k}}) {                                          foreach my $nv (@{$self->{'lookup'}->{$k}}) {
400                                                  my $tmp2 = $f;                                                  my $tmp2 = $f;
401                                                  $tmp2 =~ s/\[$k\]/$nv/g;                                                  $tmp2 =~ s/\[$k\]/$nv/g;
402                                                  push @in, $tmp2;                                                  push @in, $tmp2;
 #print "## lookup in => $tmp2\n";  
403                                          }                                          }
404                                  } else {                                  } else {
405                                          undef $f;                                          undef $f;
406                                  }                                  }
407                          } elsif ($f) {                          } elsif ($f) {
408                                  push @out, $f;                                  push @out, $f;
 #print "## lookup out => $f\n";  
409                          }                          }
410                  }                  }
411                  return @out;                  return @out;
# Line 400  Perform smart parsing of string, skippin Line 420  Perform smart parsing of string, skippin
420  defined. It can also eval code in format starting with C<eval{...}> and  defined. It can also eval code in format starting with C<eval{...}> and
421  return output or nothing depending on eval code.  return output or nothing depending on eval code.
422    
423   $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);
424    
425  =cut  =cut
426    
427  sub parse {  sub parse {
428          my $self = shift;          my $self = shift;
429    
430          my ($rec, $format, $i) = @_;          my ($rec, $format_utf8, $i) = @_;
431    
432            return if (! $format_utf8);
433    
434          confess("need HASH as first argument!") if ($rec !~ /HASH/o);          confess("need HASH as first argument!") if ($rec !~ /HASH/o);
435            confess("need utf2cp Text::Iconv object!") if (! $self->{'utf2cp'});
436    
437          $i = 0 if (! $i);          $i = 0 if (! $i);
438    
439            my $format = $self->{'utf2cp'}->convert($format_utf8) || confess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'});
440    
441          my @out;          my @out;
442    
443          my $eval_code;          my $eval_code;
# Line 422  sub parse { Line 447  sub parse {
447          my $prefix;          my $prefix;
448          my $all_found=0;          my $all_found=0;
449    
 #print "## $format\n";  
450          while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))*//s) {          while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))*//s) {
 #print "## [ $1 | $2 | $3 ] $format\n";  
451    
452                  my $del = $1 || '';                  my $del = $1 || '';
453                  $prefix ||= $del if ($all_found == 0);                  $prefix ||= $del if ($all_found == 0);
# Line 445  sub parse { Line 468  sub parse {
468    
469          # add prefix if not there          # add prefix if not there
470          $out = $prefix . $out if ($out !~ m/^\Q$prefix\E/);          $out = $prefix . $out if ($out !~ m/^\Q$prefix\E/);
471            
472          if ($eval_code) {          if ($eval_code) {
473                  my $eval = $self->fill_in($rec,$eval_code,$i);                  my $eval = $self->fill_in($rec,$eval_code,$i);
474                  return if (! eval $eval);                  return if (! eval $eval);
# Line 454  sub parse { Line 477  sub parse {
477          return $out;          return $out;
478  }  }
479    
480    =head2 parse_to_arr
481    
482    Similar to C<parse>, but returns array of all repeatable fields
483    
484     my @arr = $webpac->parse_to_arr($rec,'v250^a');
485    
486    =cut
487    
488    sub parse_to_arr {
489            my $self = shift;
490    
491            my ($rec, $format_utf8) = @_;
492    
493            confess("need HASH as first argument!") if ($rec !~ /HASH/o);
494            return if (! $format_utf8);
495    
496            my $i = 0;
497            my @arr;
498    
499            while (my $v = $self->parse($rec,$format_utf8,$i++)) {
500                    push @arr, $v;
501            }
502    
503            return @arr;
504    }
505    
506    =head2 data_structure
507    
508    Create in-memory data structure which represents layout from C<import_xml>.
509    It is used later to produce output.
510    
511     my $ds = $webpac->data_structure($rec);
512    
513    =cut
514    
515    # private method _sort_by_order
516    # sort subrouting using order="" attribute
517    sub _sort_by_order {
518            my $self = shift;
519    
520            my $va = $self->{'import_xml'}->{'indexer'}->{$a}->{'order'} ||
521                    $self->{'import_xml'}->{'indexer'}->{$a};
522            my $vb = $self->{'import_xml'}->{'indexer'}->{$b}->{'order'} ||
523                    $self->{'import_xml'}->{'indexer'}->{$b};
524    
525            return $va <=> $vb;
526    }
527    
528    sub data_structure {
529            my $self = shift;
530    
531            my $rec = shift;
532            confess("need HASH as first argument!") if ($rec !~ /HASH/o);
533    
534            my @sorted_tags;
535            if ($self->{tags_by_order}) {
536                    @sorted_tags = @{$self->{tags_by_order}};
537            } else {
538                    @sorted_tags = sort { $self->_sort_by_order } keys %{$self->{'import_xml'}->{'indexer'}};
539                    $self->{tags_by_order} = \@sorted_tags;
540            }
541    
542            my $ds;
543    
544            foreach my $field (@sorted_tags) {
545    
546                    my $row;
547    
548    #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}});
549    
550                    foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {
551                            my @v = $self->parse_to_arr($rec,$tag->{'content'});
552    
553                            next if (! @v);
554    
555                            # does tag have type?
556                            if ($tag->{'type'}) {
557                                    push @{$row->{$tag->{'type'}}}, @v;
558                            } else {
559                                    push @{$row->{'display'}}, @v;
560                                    push @{$row->{'swish'}}, @v;
561                            }
562                    }
563    
564                    push @{$ds->{$field}}, $row if ($row);
565    
566            }
567    
568            print "data_structure => ",Dumper($ds);
569    
570    }
571    
572  1;  1;

Legend:
Removed from v.363  
changed lines
  Added in v.367

  ViewVC Help
Powered by ViewVC 1.1.26