/[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 454 by dpavlin, Wed Sep 15 22:36:23 2004 UTC revision 705 by dpavlin, Wed Jul 13 22:34:52 2005 UTC
# Line 13  use Time::HiRes qw(time); Line 13  use Time::HiRes qw(time);
13    
14  use Data::Dumper;  use Data::Dumper;
15    
16    my ($have_biblio_isis, $have_openisis) = (0,0);
17    
18    eval "use Biblio::Isis 0.13;";
19    unless ($@) {
20            $have_biblio_isis = 1
21    } else {
22            eval "use OpenIsis;";
23            $have_openisis = 1 unless ($@);
24    }
25    
26  #my $LOOKUP_REGEX = '\[[^\[\]]+\]';  #my $LOOKUP_REGEX = '\[[^\[\]]+\]';
27  #my $LOOKUP_REGEX_SAVE = '\[([^\[\]]+)\]';  #my $LOOKUP_REGEX_SAVE = '\[([^\[\]]+)\]';
28  my $LOOKUP_REGEX = 'lookup{[^\{\}]+}';  my $LOOKUP_REGEX = 'lookup{[^\{\}]+}';
# Line 36  Create new instance of WebPAC using conf Line 46  Create new instance of WebPAC using conf
46          config_file => 'name.conf',          config_file => 'name.conf',
47          code_page => 'ISO-8859-2',          code_page => 'ISO-8859-2',
48          low_mem => 1,          low_mem => 1,
49            filter => {
50                    'lower' => sub { lc($_[0]) },
51            },
52   );   );
53    
54  Default C<code_page> is C<ISO-8859-2>.  Default C<code_page> is C<ISO-8859-2>.
55    
56  Default is not to use C<low_mem> options (see L<MEMORY USAGE> below).  Default is not to use C<low_mem> options (see L<MEMORY USAGE> below).
57    
58    There is optinal parametar C<filter> which specify different filters which
59    can be applied using C<filter{name}> notation.
60    Same filters can be used in Template Toolkit files.
61    
62  This method will also read configuration files  This method will also read configuration files
63  C<global.conf> (used by indexer and Web font-end)  C<global.conf> (used by indexer and Web font-end)
64  and configuration file specified by C<config_file>  and configuration file specified by C<config_file>
# Line 106  sub new { Line 123  sub new {
123          # create Template toolkit instance          # create Template toolkit instance
124          $self->{'tt'} = Template->new(          $self->{'tt'} = Template->new(
125                  INCLUDE_PATH => ($self->{'global_config_file'}->{'output_template'} || './output_template'),                  INCLUDE_PATH => ($self->{'global_config_file'}->{'output_template'} || './output_template'),
126  #               FILTERS => {                  FILTERS => $self->{'filter'},
 #                       'foo' => \&foo_filter,  
 #               },  
127                  EVAL_PERL => 1,                  EVAL_PERL => 1,
128          );          );
129    
# Line 138  sub new { Line 153  sub new {
153                  $self->{'db'} = $db;                  $self->{'db'} = $db;
154          }          }
155    
156            $log->debug("filters defined: ",Dumper($self->{'filter'}));
157    
158          return $self;          return $self;
159  }  }
160    
161  =head2 open_isis  =head2 open_isis
162    
163  Open CDS/ISIS database using OpenIsis module and read all records to memory.  Open CDS/ISIS, WinISIS or IsisMarc database using IsisDB or OpenIsis module
164    and read all records to memory.
165    
166   $webpac->open_isis(   $webpac->open_isis(
167          filename => '/data/ISIS/ISIS',          filename => '/data/ISIS/ISIS',
# Line 191  sub open_isis { Line 209  sub open_isis {
209          $self->{'isis_filename'} = $arg->{'filename'};          $self->{'isis_filename'} = $arg->{'filename'};
210          $self->{'isis_code_page'} = $code_page;          $self->{'isis_code_page'} = $code_page;
211    
         use OpenIsis;  
   
212          #$self->{'isis_code_page'} = $code_page;          #$self->{'isis_code_page'} = $code_page;
213    
214          # create Text::Iconv object          # create Text::Iconv object
# Line 201  sub open_isis { Line 217  sub open_isis {
217          $log->info("reading ISIS database '",$arg->{'filename'},"'");          $log->info("reading ISIS database '",$arg->{'filename'},"'");
218          $log->debug("isis code page: $code_page");          $log->debug("isis code page: $code_page");
219    
220          my $isis_db = OpenIsis::open($arg->{'filename'});          my ($isis_db,$maxmfn);
221    
222            if ($have_openisis) {
223                    $log->debug("using OpenIsis perl bindings");
224                    $isis_db = OpenIsis::open($arg->{'filename'});
225                    $maxmfn = OpenIsis::maxRowid( $isis_db ) || 1;
226            } elsif ($have_biblio_isis) {
227                    $log->debug("using Biblio::Isis");
228                    use Biblio::Isis;
229                    $isis_db = new Biblio::Isis(
230                            isisdb => $arg->{'filename'},
231                            include_deleted => 1,
232                            hash_filter => sub {
233                                    my $l = shift || return;
234                                    $l = $cp->convert($l);
235                                    return $l;
236                            },
237                    );
238                    $maxmfn = $isis_db->{'maxmfn'};
239            } else {
240                    $log->logdie("Can't find supported ISIS library for perl. I suggent that you install Bilbio::Isis from CPAN.");
241            }
242    
243    
         my $maxmfn = OpenIsis::maxRowid( $isis_db ) || 1;  
244          my $startmfn = 1;          my $startmfn = 1;
245    
246          if (my $s = $self->{'start_mfn'}) {          if (my $s = $self->{'start_mfn'}) {
# Line 215  sub open_isis { Line 252  sub open_isis {
252    
253          $maxmfn = $startmfn + $self->{limit_mfn} if ($self->{limit_mfn});          $maxmfn = $startmfn + $self->{limit_mfn} if ($self->{limit_mfn});
254    
255          $log->info("processing ",($maxmfn-$startmfn)." records...");          $log->info("processing ",($maxmfn-$startmfn)." records using ",( $have_openisis ? 'OpenIsis' : 'Biblio::Isis'));
256    
257    
258          # read database          # read database
259          for (my $mfn = $startmfn; $mfn <= $maxmfn; $mfn++) {          for (my $mfn = $startmfn; $mfn <= $maxmfn; $mfn++) {
# Line 225  sub open_isis { Line 263  sub open_isis {
263    
264                  my $rec;                  my $rec;
265    
266                  # read record                  if ($have_openisis) {
267                  my $row = OpenIsis::read( $isis_db, $mfn );  
268                  foreach my $k (keys %{$row}) {                          # read record using OpenIsis
269                          if ($k ne "mfn") {                          my $row = OpenIsis::read( $isis_db, $mfn );
270                                  foreach my $l (@{$row->{$k}}) {                          foreach my $k (keys %{$row}) {
271                                          $l = $cp->convert($l);                                  if ($k ne "mfn") {
272                                          # has subfields?                                          foreach my $l (@{$row->{$k}}) {
273                                          my $val;                                                  $l = $cp->convert($l);
274                                          if ($l =~ m/\^/) {                                                  # has subfields?
275                                                  foreach my $t (split(/\^/,$l)) {                                                  my $val;
276                                                          next if (! $t);                                                  if ($l =~ m/\^/) {
277                                                          $val->{substr($t,0,1)} = substr($t,1);                                                          foreach my $t (split(/\^/,$l)) {
278                                                                    next if (! $t);
279                                                                    $val->{substr($t,0,1)} = substr($t,1);
280                                                            }
281                                                    } else {
282                                                            $val = $l;
283                                                  }                                                  }
                                         } else {  
                                                 $val = $l;  
                                         }  
284    
285                                          push @{$rec->{$k}}, $val;                                                  push @{$rec->{$k}}, $val;
286                                            }
287                                    } else {
288                                            push @{$rec->{'000'}}, $mfn;
289                                  }                                  }
                         } else {  
                                 push @{$rec->{'000'}}, $mfn;  
290                          }                          }
291    
292                    } elsif ($have_biblio_isis) {
293                            $rec = $isis_db->to_hash($mfn);
294                    } else {
295                            $log->logdie("hum? implementation missing?");
296                  }                  }
297    
298                  $log->confess("record $mfn empty?") unless ($rec);                  $log->confess("record $mfn empty?") unless ($rec);
# Line 357  sub progress_bar { Line 402  sub progress_bar {
402          # reset on re-run          # reset on re-run
403          if ($p < $self->{'last_pcnt'}) {          if ($p < $self->{'last_pcnt'}) {
404                  $self->{'last_pcnt'} = $p;                  $self->{'last_pcnt'} = $p;
405                  $self->{'last_t'} = time();                  $self->{'start_t'} = time();
                 $self->{'last_curr'} = undef;  
406          }          }
407    
408          if ($p != $self->{'last_pcnt'}) {          if ($p != $self->{'last_pcnt'}) {
409    
                 my $last_curr = $self->{'last_curr'} || $curr;  
410                  my $t = time();                  my $t = time();
411                  my $rate = ($curr - $last_curr) / (($t - $self->{'last_t'} || 1));                  my $rate = ($curr / ($t - $self->{'start_t'} || 1));
412                  my $eta = ($max-$curr) / ($rate || 1);                  my $eta = ($max-$curr) / ($rate || 1);
413                  printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta));                  printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta));
414                  $self->{'last_pcnt'} = $p;                  $self->{'last_pcnt'} = $p;
                 $self->{'last_t'} = time();  
415                  $self->{'last_curr'} = $curr;                  $self->{'last_curr'} = $curr;
416          }          }
417          print STDERR "\n" if ($p == 100);          print STDERR "\n" if ($p == 100);
# Line 560  sub fill_in { Line 602  sub fill_in {
602          # iteration (for repeatable fields)          # iteration (for repeatable fields)
603          my $i = shift || 0;          my $i = shift || 0;
604    
605            $log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999));
606    
607          # FIXME remove for speedup?          # FIXME remove for speedup?
608          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
609    
# Line 573  sub fill_in { Line 617  sub fill_in {
617          # remove eval{...} from beginning          # remove eval{...} from beginning
618          $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s);          $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s);
619    
620            my $filter_name;
621            # remove filter{...} from beginning
622            $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s);
623    
624          # do actual replacement of placeholders          # do actual replacement of placeholders
625            # repeatable fields
626          $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges;          $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges;
627            # non-repeatable fields
628            $format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges;
629    
630          if ($found) {          if ($found) {
631                  $log->debug("format: $format");                  $log->debug("format: $format");
# Line 582  sub fill_in { Line 633  sub fill_in {
633                          my $eval = $self->fill_in($rec,$eval_code,$i);                          my $eval = $self->fill_in($rec,$eval_code,$i);
634                          return if (! $self->_eval($eval));                          return if (! $self->_eval($eval));
635                  }                  }
636                    if ($filter_name && $self->{'filter'}->{$filter_name}) {
637                            $log->debug("filter '$filter_name' for $format");
638                            $format = $self->{'filter'}->{$filter_name}->($format);
639                            return unless(defined($format));
640                            $log->debug("filter result: $format");
641                    }
642                  # do we have lookups?                  # do we have lookups?
643                  if ($format =~ /$LOOKUP_REGEX/o) {                  if ($format =~ /$LOOKUP_REGEX/o) {
644                          $log->debug("format '$format' has lookup");                          $log->debug("format '$format' has lookup");
# Line 674  sub parse { Line 731  sub parse {
731          # remove eval{...} from beginning          # remove eval{...} from beginning
732          $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s);          $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s);
733    
734            my $filter_name;
735            # remove filter{...} from beginning
736            $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s);
737    
738          my $prefix;          my $prefix;
739          my $all_found=0;          my $all_found=0;
740    
741          while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))?//s) {          while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) {
742    
743                  my $del = $1 || '';                  my $del = $1 || '';
744                  $prefix ||= $del if ($all_found == 0);                  $prefix ||= $del if ($all_found == 0);
745    
746                    # repeatable index
747                    my $r = $i;
748                    $r = 0 if (lc("$2") eq 's');
749    
750                  my $found = 0;                  my $found = 0;
751                  my $tmp = $self->get_data(\$rec,$2,$3,$i,\$found);                  my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found);
752    
753                  if ($found) {                  if ($found) {
754                          push @out, $del;                          push @out, $del;
# Line 707  sub parse { Line 772  sub parse {
772          }          }
773    
774          if ($eval_code) {          if ($eval_code) {
775                  my $eval = $self->fill_in($rec,$eval_code,$i);                  my $eval = $self->fill_in($rec,$eval_code,$i) || return;
776                  $log->debug("about to eval{",$eval,"} format: $out");                  $log->debug("about to eval{$eval} format: $out");
777                  return if (! $self->_eval($eval));                  return if (! $self->_eval($eval));
778          }          }
779            
780            if ($filter_name && $self->{'filter'}->{$filter_name}) {
781                    $log->debug("about to filter{$filter_name} format: $out");
782                    $out = $self->{'filter'}->{$filter_name}->($out);
783                    return unless(defined($out));
784                    $log->debug("filter result: $out");
785            }
786    
787          return $out;          return $out;
788  }  }
# Line 860  sub data_structure { Line 932  sub data_structure {
932    
933                          if ($tag->{'sort'}) {                          if ($tag->{'sort'}) {
934                                  @v = $self->sort_arr(@v);                                  @v = $self->sort_arr(@v);
                                 $log->warn("sort within tag is usually not what you want!");  
935                          }                          }
936    
937                          # use format?                          # use format?
# Line 896  sub data_structure { Line 967  sub data_structure {
967                                          # delimiter (or ,)                                          # delimiter (or ,)
968                                          my $d = $tag->{'delimiter'};                                          my $d = $tag->{'delimiter'};
969                                          # default delimiter                                          # default delimiter
970                                          $d ||= ", ";                                          $d ||= " ";
971    
972                                          my $last = pop @{$row->{$type}};                                          my $last = pop @{$row->{$type}};
973                                          $d = "" if (! $last);                                          $d = "" if (! $last);
# Line 1068  sub _eval { Line 1139  sub _eval {
1139    
1140          $log->debug("eval: ",$code," [",$ret,"]");          $log->debug("eval: ",$code," [",$ret,"]");
1141    
1142          return $ret || 0;          return $ret || undef;
1143  }  }
1144    
1145  =head2 _sort_by_order  =head2 _sort_by_order

Legend:
Removed from v.454  
changed lines
  Added in v.705

  ViewVC Help
Powered by ViewVC 1.1.26