moved clean into WebPAC::Output::Estraier, cleanup
[webpac2] / run.pl
diff --git a/run.pl b/run.pl
index d0c4ae5..4f99408 100755 (executable)
--- a/run.pl
+++ b/run.pl
@@ -7,92 +7,211 @@ 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::DB;
+use WebPAC::Input 0.03;
+use WebPAC::Store 0.03;
 use WebPAC::Normalize::XML;
 use WebPAC::Output::TT;
-use WebPAC::Output::Estraier;
+use WebPAC::Output::Estraier 0.08;
+use YAML qw/LoadFile/;
+use Getopt::Long;
+use File::Path;
 
-my $limit = shift @ARGV;
+=head1 NAME
 
-my $abs_path = abs_path($0);
-$abs_path =~ s#/[^/]*$#/#;
+run.pl - start WebPAC indexing
 
-my $isis_file = '/data/isis_data/ps/LIBRI/LIBRI';
+B<this command will probably go away. Don't get used to it!>
 
-my $lookup = new WebPAC::Lookup(
-       lookup_file => "$abs_path/conf/lookup/isis.pm",
-);
+Options:
 
-my $isis = new WebPAC::Input::ISIS(
-       code_page => 'ISO-8859-2',      # application encoding
-       limit_mfn => $limit,
-);
+=over 4
 
-my $maxmfn = $isis->open(
-       filename => $isis_file,
-       code_page => '852',             # database encoding
-);
+=item --offset 42
 
-my $path = './db/';
+start loading (all) databases at offset 42
 
-my $db = new WebPAC::DB(
-       path => $path,
-);
+=item --limit 100
 
-my $n = new WebPAC::Normalize::XML(
-#      filter => { 'foo' => sub { shift } },
-       db => $db,
-       lookup_regex => $lookup->regex,
-       lookup => $lookup,
-);
+limit loading to 100 records
 
-$n->open(
-       tag => 'isis',
-       xml_file => "$abs_path/conf/normalize/isis_ffzg.xml",
-);
+=item --clean
 
-my $out = new WebPAC::Output::TT(
-       include_path => "$abs_path/conf/output/tt",
-       filters => { foo => sub { shift } },
-);
+remove database and Hyper Estraier index before indexing
+
+=item --config conf/config.yml
+
+path to YAML configuration file
+
+=back
+
+=cut
 
-my $est = new WebPAC::Output::Estraier(
-       url => 'http://localhost:1978/node/webpac2',
-       user => 'admin',
-       passwd => 'admin',
-       database => 'ps',
+my $offset;
+my $limit;
+
+my $clean = 0;
+my $config = 'conf/config.yml';
+my $debug = 0;
+
+GetOptions(
+       "limit=i" => \$limit,
+       "offset=i" => \$offset,
+       "clean" => \$clean,
+       "config" => \$config,
+       "debug" => \$debug,
 );
 
-while (my $row = $isis->fetch) {
+$config = LoadFile($config);
 
-       my $mfn = $row->{'000'}->[0] || die "can't find MFN";
+print "config = ",Dumper($config) if ($debug);
 
-       my $ds = $n->data_structure($row);
+die "no databases in config file!\n" unless ($config->{databases});
 
-#      print STDERR Dumper($row, $ds);
+my $total_rows = 0;
 
-       my $html = $out->apply(
-               template => 'html_ffzg.tt',
-               data => $ds,
-       );
+while (my ($database, $db_config) = each %{ $config->{databases} }) {
+
+       my $log = _new WebPAC::Common()->_get_logger();
+
+       #
+       # open Hyper Estraier database
+       #
 
-       # create test output
+       my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");
+       $est_config->{database} = $database;
+       $est_config->{clean} = $clean;
 
-       my $file = sprintf('out/%02d.html', $mfn );
-       open(my $fh, '>', $file) or die "can't open $file: $!";
-       print $fh $html;
-       close($fh);
+       my $est = new WebPAC::Output::Estraier( %{ $est_config } );
 
-       $html =~ s#\s*[\n\r]+\s*##gs;
+       #
+       # now WebPAC::Store
+       #
+       my $abs_path = abs_path($0);
+       $abs_path =~ s#/[^/]*$#/#;
 
-#      print STDERR $html;
+       my $db_path = $config->{webpac}->{db_path} . '/' . $database;
 
-       $est->add(
-               id => $mfn,
-               ds => $ds,
-               type => 'search',
+       if ($clean) {
+               $log->info("creating new database $database in $db_path");
+               rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
+       } else {
+               $log->info("working on $database in $db_path");
+       }
+
+       my $db = new WebPAC::Store(
+               path => $db_path,
+               database => $database,
+               debug => $debug,
        );
 
-};
+
+       #
+       # now, iterate through input formats
+       #
+
+       my @inputs;
+       if (ref($db_config->{input}) eq 'ARRAY') {
+               @inputs = @{ $db_config->{input} };
+       } 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) {
+
+               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 => $limit || $input->{limit},
+                       offset => $offset,
+                       lookup => $lookup,
+               );
+               $log->logdie("can't create input using $input_module") unless ($input);
+
+               my $maxmfn = $input_db->open(
+                       path => $input->{path},
+                       code_page => $input->{encoding},        # database encoding
+               );
+
+               my $n = new WebPAC::Normalize::XML(
+               #       filter => { 'foo' => sub { shift } },
+                       db => $db,
+                       lookup_regex => $lookup->regex,
+                       lookup => $lookup,
+                       prefix => $input->{name},
+               );
+
+               my $normalize_path = $input->{normalize}->{path};
+
+               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 = $input_db->fetch || next;
+
+                       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,
+                               ds => $ds,
+                               type => $config->{hyperestraier}->{type},
+                       );
+
+                       $total_rows++;
+               }
+
+       };
+
+       $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},
+                       );
+               }
+       }
+
+}
+