r11519@llin: dpavlin | 2005-12-05 00:20:06 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 5 Dec 2005 17:47:04 +0000 (17:47 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 5 Dec 2005 17:47:04 +0000 (17:47 +0000)
 partial changes to support multiple databases

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@210 07558da8-63fa-0310-ba24-9fe276d99e06

Makefile.PL
conf/log.conf
lib/WebPAC/Output/Estraier.pm
run.pl

index b61a01c..398467a 100644 (file)
@@ -24,6 +24,7 @@ WriteMakefile(
        'File::Temp' => 0,
        'List::Util' => 0,
        'URI::Escape' => 0,
+       'LWP::Simple' => 0,
     },
     dist                => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
     clean               => { FILES => 'WebPAC-* pod2html Makefile tags' },
@@ -52,8 +53,8 @@ sf:
 run:
        rm -f log
        test ! -e conf/config.yml && ln -s /data/Webpacus/config.yml conf/ || true
-       estcall raw -auth admin admin 'http://localhost:1978/master?action=nodedel&name=webpac2' || true
-       estcall raw -auth admin admin 'http://localhost:1978/master?action=nodeadd&name=webpac2&label=WebPAC%20test' || true
+       #estcall raw -auth admin admin 'http://localhost:1978/master?action=nodedel&name=webpac2' || true
+       #estcall raw -auth admin admin 'http://localhost:1978/master?action=nodeadd&name=webpac2&label=WebPAC%20test' || true
        ./run.pl
 
 MAKE_MORE
index 24aac8f..e7dc254 100644 (file)
@@ -28,7 +28,7 @@ log4perl.logger.main=INFO
 #log4perl.logger.WebPAC.Normalize.parse_to_arr=DEBUG
 
 #log4perl.logger.WebPAC.Output.TT=DEBUG
-#log4perl.logger.WebPAC.Output.Estraier=DEBUG
+log4perl.logger.WebPAC.Output.Estraier=DEBUG
 #log4perl.logger.WebPAC.Search.Estraier=DEBUG
 
 #log4perl.logger.WebPAC.Common=DEBUG
index 871237f..83eae34 100644 (file)
@@ -8,6 +8,8 @@ use base qw/WebPAC::Common/;
 use HyperEstraier;
 use Text::Iconv;
 use Data::Dumper;
+use LWP::Simple;
+use URI::Escape;
 
 =head1 NAME
 
@@ -15,11 +17,11 @@ WebPAC::Output::Estraier - Create Hyper Estraier full text index
 
 =head1 VERSION
 
-Version 0.01
+Version 0.02
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 =head1 SYNOPSIS
 
@@ -33,7 +35,7 @@ type C<search>.
 Connect to Hyper Estraier index using HTTP
 
  my $est = new WebPAC::Output::Estraier(
-       url => 'http://localhost:1978/node/webpac2',
+       masterurl => 'http://localhost:1978/',
        user => 'admin',
        passwd => 'admin',
        database => 'demo',
@@ -44,7 +46,7 @@ Options are:
 
 =over 4
 
-=item url
+=item masterurl
 
 URI to C<estmaster> node
 
@@ -79,14 +81,31 @@ sub new {
 
        my $log = $self->_get_logger;
 
-       foreach my $p (qw/url user passwd/) {
+       $log->debug("self: ", sub { Dumper($self) });
+
+       foreach my $p (qw/masterurl user passwd database/) {
                $log->logdie("need $p") unless ($self->{$p});
        }
 
-       $log->info("opening Hyper Estraier index $self->{'url'}");
+       my $url = $self->{masterurl} . '/node/' . $self->{database};
+       $url =~ s#//#/#g;
+       $self->{url} = $url;
+
+       $log->info("opening Hyper Estraier index $self->{url}");
 
-       $self->{'db'} = HyperEstraier::Node->new($self->{'url'});
-       $self->{'db'}->set_auth($self->{'user'}, $self->{'passwd'});
+       my @nodes = $self->est_master( action => 'nodelist' );
+
+       if (! grep(/$self->{database}/, @nodes)) {
+               $log->info("creating index $url");
+               $self->est_master(
+                       action => 'nodeadd',
+                       name => $self->{database},
+                       label => "WebPAC $self->{database}",
+               ) || $log->logdie("can't create Hyper Estraier node $self->{database}");
+       }
+
+       $self->{'db'} = HyperEstraier::Node->new($self->{url});
+       $self->{'db'}->set_auth($self->{'user'}, $self->{passwd});
 
        my $encoding = $self->{'encoding'} || 'ISO-8859-2';
        $log->info("using encoding $encoding");
@@ -179,6 +198,52 @@ sub add {
        return 1;
 }
 
+=head2 est_master
+
+Issue administrative commands to C<estmaster> process and receive response
+as array of lines
+
+  my $nodelist = $self->est_master( action => nodelist );
+
+=cut
+
+my $estmaster_actions = {
+       userdel => [ qw/name/ ],
+       nodelist => [],
+       nodeadd => [ qw/name label/ ],
+       nodedel => [ qw/name/ ],
+};
+
+sub est_master {
+       my $self = shift;
+       my $args = {@_};
+       my $log = $self->_get_logger;
+
+       $log->debug(Dumper($args));
+
+       my $action = $args->{action} || $log->logconfess("no action specified");
+
+       $log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action});
+
+       my $url = $self->{masterurl} . '/master?action=' . $action;
+
+       foreach my $arg (@{ $estmaster_actions->{$action} }) {
+               $log->logdie("missing parametar $arg for action $action") unless ($args->{$arg});
+               $url .= '&' . $arg . '=' . uri_escape( $args->{$arg} );
+       }
+
+       $log->debug("calling $url");
+
+       my $tsv = get($url);
+
+       if (! $tsv) {
+               $log->warn("unable to call $url");
+               return;
+       }
+
+       return split(/\n/, $tsv);
+}
+
 =head1 AUTHOR
 
 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
diff --git a/run.pl b/run.pl
index 6e47b6a..5a09b6b 100755 (executable)
--- a/run.pl
+++ b/run.pl
@@ -12,8 +12,9 @@ use WebPAC::Input::ISIS;
 use WebPAC::Store 0.03;
 use WebPAC::Normalize::XML;
 use WebPAC::Output::TT;
-use WebPAC::Output::Estraier;
+use WebPAC::Output::Estraier 0.02;
 use YAML qw/LoadFile/;
+use LWP::Simple;
 
 my $limit = shift @ARGV;
 
@@ -21,94 +22,107 @@ my $config = LoadFile('conf/config.yml');
 
 print "config = ",Dumper($config);
 
-my $type = lc($config->{input}->{type});
+die "no databases in config file!\n" unless ($config->{databases});
 
-die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis');
+while (my ($database, $db_config) = each %{ $config->{databases} }) {
 
-my $abs_path = abs_path($0);
-$abs_path =~ s#/[^/]*$#/#;
+       my $type = lc($db_config->{input}->{type});
 
-my $lookup = new WebPAC::Lookup(
-       lookup_file => $config->{input}->{lookup},
-);
+       die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis');
 
+       my $abs_path = abs_path($0);
+       $abs_path =~ s#/[^/]*$#/#;
 
+       my $lookup = new WebPAC::Lookup(
+               lookup_file => $db_config->{input}->{lookup},
+       );
+
+       my $db_path = $config->{webpac}->{db_path} . '/' . $database;
+
+
+       my $log = $lookup->_get_logger;
+       $log->info("working on $database in $db_path");
+
+       my $isis = new WebPAC::Input::ISIS(
+               code_page => $config->{webpac}->{webpac_encoding},
+               limit_mfn => $db_config->{input}->{limit},
+       );
+
+       my $maxmfn = $isis->open(
+               filename => $db_config->{input}->{path},
+               code_page => $db_config->{input}->{encoding},   # database encoding
+       );
 
-my $isis = new WebPAC::Input::ISIS(
-       code_page => $config->{webpac}->{webpac_encoding},
-       limit_mfn => $config->{input}->{limit},
-);
+       my $path = './db/';
 
-my $maxmfn = $isis->open(
-       filename => $config->{input}->{path},
-       code_page => $config->{input}->{encoding},      # database encoding
-);
+       my $db = new WebPAC::Store(
+               path => $db_path,
+       );
 
-my $path = './db/';
+       my $n = new WebPAC::Normalize::XML(
+       #       filter => { 'foo' => sub { shift } },
+               db => $db,
+               lookup_regex => $lookup->regex,
+               lookup => $lookup,
+       );
 
-my $db = new WebPAC::Store(
-       path => $config->{webpac}->{db_path},
-);
+       $n->open(
+               tag => $db_config->{normalize}->{tag},
+               xml_file => $db_config->{normalize}->{path},
+       );
 
-my $n = new WebPAC::Normalize::XML(
-#      filter => { 'foo' => sub { shift } },
-       db => $db,
-       lookup_regex => $lookup->regex,
-       lookup => $lookup,
-);
+       my $out = new WebPAC::Output::TT(
+               include_path => $config->{webpac}->{template_path},
+               filters => { foo => sub { shift } },
+       );
 
-$n->open(
-       tag => $config->{normalize}->{tag},
-       xml_file => $config->{normalize}->{path},
-);
+       my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");
+       $est_config->{database} = $database;
 
-my $out = new WebPAC::Output::TT(
-       include_path => $config->{webpac}->{template_path},
-       filters => { foo => sub { shift } },
-);
+       $log->info("using HyperEstraier URL $est_config->{url}");
 
-my $est = new WebPAC::Output::Estraier(
-       %{ $config->{hyperestraier} }
-);
+       my $est = new WebPAC::Output::Estraier(
+               %{ $est_config },
+       );
 
-my $total_rows = 0;
+       my $total_rows = 0;
 
-for ( 0 ... $isis->size ) {
+       for ( 0 ... $isis->size ) {
 
-       my $row = $isis->fetch || next;
+               my $row = $isis->fetch || next;
 
-       my $mfn = $row->{'000'}->[0] || die "can't find MFN";
+               my $mfn = $row->{'000'}->[0] || die "can't find MFN";
 
-       my $ds = $n->data_structure($row);
+               my $ds = $n->data_structure($row);
 
-#      print STDERR Dumper($row, $ds);
+       #       print STDERR Dumper($row, $ds);
 
-#      my $html = $out->apply(
-#              template => 'html_ffzg.tt',
-#              data => $ds,
-#      );
-#
-#      # create test output
-#
-#      my $file = sprintf('out/%02d.html', $mfn );
-#      open(my $fh, '>', $file) or die "can't open $file: $!";
-#      print $fh $html;
-#      close($fh);
-#
-#      $html =~ s#\s*[\n\r]+\s*##gs;
-#
-#      print STDERR $html;
+       #       my $html = $out->apply(
+       #               template => 'html_ffzg.tt',
+       #               data => $ds,
+       #       );
+       #
+       #       # create test output
+       #
+       #       my $file = sprintf('out/%02d.html', $mfn );
+       #       open(my $fh, '>', $file) or die "can't open $file: $!";
+       #       print $fh $html;
+       #       close($fh);
+       #
+       #       $html =~ s#\s*[\n\r]+\s*##gs;
+       #
+       #       print STDERR $html;
 
-       $est->add(
-               id => $mfn,
-               ds => $ds,
-               type => $config->{hyperestraier}->{type},
-       );
+               $est->add(
+                       id => $mfn,
+                       ds => $ds,
+                       type => $config->{hyperestraier}->{type},
+               );
 
-       $total_rows++;
+               $total_rows++;
 
-};
+       };
 
-my $log = $lookup->_get_logger;
+       $log->info("$total_rows records indexed");
+}
 
-$log->info("$total_rows records indexed");