r11790@llin: dpavlin | 2005-12-19 06:35:06 +0100
[webpac2] / run.pl
diff --git a/run.pl b/run.pl
index 1137964..f69d51f 100755 (executable)
--- a/run.pl
+++ b/run.pl
@@ -7,12 +7,13 @@ use File::Temp qw/tempdir/;
 use Data::Dumper;
 use lib './lib';
 
+use WebPAC::Common 0.02;
 use WebPAC::Lookup;
-use WebPAC::Input::ISIS;
+use WebPAC::Input 0.03;
 use WebPAC::Store 0.03;
 use WebPAC::Normalize::XML;
 use WebPAC::Output::TT;
-use WebPAC::Output::Estraier 0.02;
+use WebPAC::Output::Estraier 0.05;
 use YAML qw/LoadFile/;
 use LWP::Simple;
 
@@ -28,21 +29,29 @@ my $total_rows = 0;
 
 while (my ($database, $db_config) = each %{ $config->{databases} }) {
 
-       my $type = lc($db_config->{input}->{type});
+       my $log = _new WebPAC::Common()->_get_logger();
 
-       die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis');
+       #
+       # open Hyper Estraier database
+       #
 
-       my $abs_path = abs_path($0);
-       $abs_path =~ s#/[^/]*$#/#;
+       my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");
+       $est_config->{database} = $database;
 
-       my $lookup = new WebPAC::Lookup(
-               lookup_file => $db_config->{input}->{lookup},
+       $log->info("using HyperEstraier URL $est_config->{masterurl}");
+
+       my $est = new WebPAC::Output::Estraier(
+               %{ $est_config },
        );
 
-       my $db_path = $config->{webpac}->{db_path} . '/' . $database;
+       #
+       # now WebPAC::Store
+       #
+       my $abs_path = abs_path($0);
+       $abs_path =~ s#/[^/]*$#/#;
 
+       my $db_path = $config->{webpac}->{db_path} . '/' . $database;
 
-       my $log = $lookup->_get_logger;
        $log->info("working on $database in $db_path");
 
        my $db = new WebPAC::Store(
@@ -51,14 +60,6 @@ while (my ($database, $db_config) = each %{ $config->{databases} }) {
                debug => 1,
        );
 
-       my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");
-       $est_config->{database} = $database;
-
-       $log->info("using HyperEstraier URL $est_config->{masterurl}");
-
-       my $est = new WebPAC::Output::Estraier(
-               %{ $est_config },
-       );
 
        #
        # now, iterate through input formats
@@ -67,20 +68,38 @@ while (my ($database, $db_config) = each %{ $config->{databases} }) {
        my @inputs;
        if (ref($db_config->{input}) eq 'ARRAY') {
                @inputs = @{ $db_config->{input} };
-       } else {
+       } elsif ($db_config->{input}) {
                push @inputs, $db_config->{input};
+       } else {
+               $log->info("database $database doesn't have inputs defined");
        }
 
+       my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
+
        foreach my $input (@inputs) {
-               $log->info("working on input $input->{path} [$input->{type}]");
 
-               my $isis = new WebPAC::Input::ISIS(
+               my $type = lc($input->{type});
+
+               die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
+
+               my $lookup = new WebPAC::Lookup(
+                       lookup_file => $input->{lookup},
+               );
+
+               my $input_module = $config->{webpac}->{inputs}->{$type};
+
+               $log->info("working on input $input->{path} [$input->{type}] using $input_module");
+
+               my $input_db = new WebPAC::Input(
+                       module => $input_module,
                        code_page => $config->{webpac}->{webpac_encoding},
-                       limit_mfn => $input->{limit},
+                       limit => $input->{limit},
+                       lookup => $lookup,
                );
+               $log->logdie("can't create input using $input_module") unless ($input);
 
-               my $maxmfn = $isis->open(
-                       filename => $input->{path},
+               my $maxmfn = $input_db->open(
+                       path => $input->{path},
                        code_page => $input->{encoding},        # database encoding
                );
 
@@ -92,21 +111,36 @@ while (my ($database, $db_config) = each %{ $config->{databases} }) {
                        prefix => $input->{name},
                );
 
-               $n->open(
-                       tag => $input->{normalize}->{tag},
-                       xml_file => $input->{normalize}->{path},
-               );
+               my $normalize_path = $input->{normalize}->{path};
 
-               for ( 0 ... $isis->size ) {
+               if ($normalize_path =~ m/\.xml$/i) {
+                       $n->open(
+                               tag => $input->{normalize}->{tag},
+                               xml_file => $input->{normalize}->{path},
+                       );
+               } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {
+                       $n->open_yaml(
+                               path => $normalize_path,
+                               tag => $input->{normalize}->{tag},
+                       );
+               }
+
+               foreach my $pos ( 0 ... $input_db->size ) {
 
-                       my $row = $isis->fetch || next;
+                       my $row = $input_db->fetch || next;
 
-                       my $mfn = $row->{'000'}->[0] || die "can't find MFN";
+                       my $mfn = $row->{'000'}->[0];
+
+                       if (! $mfn || $mfn !~ m#^\d+$#) {
+                               $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
+                               $mfn = $pos;
+                               push @{ $row->{'000'} }, $pos;
+                       }
 
                        my $ds = $n->data_structure($row);
 
                        $est->add(
-                               id => $input->{name} . "#" . $mfn,
+                               id => $input->{name} . "/" . $mfn,
                                ds => $ds,
                                type => $config->{hyperestraier}->{type},
                        );
@@ -117,5 +151,20 @@ while (my ($database, $db_config) = each %{ $config->{databases} }) {
        };
 
        $log->info("$total_rows records indexed");
+
+       #
+       # add Hyper Estraier links to other databases
+       #
+       if (ref($db_config->{links}) eq 'ARRAY') {
+               foreach my $link (@{ $db_config->{links} }) {
+                       $log->info("adding link $database -> $link->{to} [$link->{credit}]");
+                       $est->add_link(
+                               from => $database,
+                               to => $link->{to},
+                               credit => $link->{credit},
+                       );
+               }
+       }
+
 }