--- trunk/lib/WebPAC/Output/Estraier.pm 2005/12/05 17:47:16 212 +++ trunk/lib/WebPAC/Output/Estraier.pm 2005/12/16 01:04:20 255 @@ -17,11 +17,11 @@ =head1 VERSION -Version 0.03 +Version 0.05 =cut -our $VERSION = '0.03'; +our $VERSION = '0.05'; =head1 SYNOPSIS @@ -92,13 +92,13 @@ $log->info("opening Hyper Estraier index $self->{url}"); - my $nodes = $self->est_master( action => 'nodelist' ); + my $nodes = $self->master( action => 'nodelist' ); $log->debug("nodes found: $nodes"); if ($nodes !~ m/^$self->{database}\t/sm) { $log->info("creating index $url"); - $self->est_master( + $self->master( action => 'nodeadd', name => $self->{database}, label => "WebPAC $self->{database}", @@ -153,9 +153,9 @@ } my $type = $args->{'type'}; - my $mfn = $args->{'id'}; + my $id = $args->{'id'}; - my $uri = "file:///$type/$database/$mfn"; + my $uri = "file:///$type/$database/$id"; $log->debug("creating $uri"); my $doc = HyperEstraier::Document->new; @@ -198,61 +198,172 @@ return 1; } -=head2 est_master +# +# REST parametars validation data +# + +my $estraier_rest = { + master => { + userdel => [ qw/name/ ], + nodelist => [], + nodeadd => [ qw/name label/ ], + nodedel => [ qw/name/ ], + }, + node => { + _set_link => [ qw/url label credit/ ], + }, +}; + +=head2 master Issue administrative commands to C process and receive response as array of lines - my $nodelist = $self->est_master( action => nodelist ); + my $nodelist = $est->master( action => 'nodelist' ); =cut -my $estmaster_actions = { - userdel => [ qw/name/ ], - nodelist => [], - nodeadd => [ qw/name label/ ], - nodedel => [ qw/name/ ], -}; +sub master { + my $self = shift; + + my $args = {@_}; + my $log = $self->_get_logger; + + my $action = $args->{action} || $log->logconfess("no action specified"); + + $log->logdie("action '$action' isn't supported") unless ($estraier_rest->{master}->{$action}); + + $log->debug("master action: $action"); + + return $self->estcall( + validate => 'master', + rest_url => $self->{masterurl} . '/master?action=' . $action , + action => $action, + ); +} + +=head2 add_link -sub est_master { + $est->add_link( + from => 'ps', + to => 'webpac2', + credit => 10000, + ); + +=cut + +sub add_link { my $self = shift; + my $args = {@_}; my $log = $self->_get_logger; - $log->debug(Dumper($args)); + my @labels = $self->master( action => 'nodelist' ); - my $action = $args->{action} || $log->logconfess("no action specified"); + $log->debug("got labels: ", join("|", @labels)); - $log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action}); + @labels = grep(/^$args->{to}/, @labels); - my $url = $self->{masterurl} . '/master?action=' . $action; + my (undef,$label) = split(/\t/, shift @labels); - foreach my $arg (@{ $estmaster_actions->{$action} }) { - $log->logdie("missing parametar $arg for action $action") unless ($args->{$arg}); - $url .= '&' . $arg . '=' . uri_escape( $args->{$arg} ); + if (! $label) { + $log->warn("can't find label for $args->{to}, skipping link creaton"); + return; } - $log->debug("calling $url"); + $log->debug("using label $label for $args->{to}"); + + return $self->estcall( + validate => 'node', + action => '_set_link', + rest_url => $self->{masterurl} . '/node/' . $args->{from} . '/_set_link' , + url => $self->{masterurl} . '/node/' . $args->{to}, + label => $label, + credit => $args->{credit}, + ); +} - if (! $self->{_master_ua}) { - $self->{_master_ua} = LWP::UserAgent->new( ) || $log->logdie("can't create LWP::UserAgent: $!"); - $self->{_master_ua}->credentials('localhost:1978','Super User', $self->{user} => $self->{passwd}); +=head2 estcall + +Workhourse which does actual calls to Hyper Estraier + + $self->estcall( + rest_url => '/master?action=' . $action, + validate => 'master', + # ... + ); + +C is relative URL to C and C is entry into +internal hash which will check if all parametars are available before +calling function. + +=cut + +sub estcall { + my $self = shift; + my $args = {@_}; + my $log = $self->_get_logger; + + $log->debug("estcall: ",Dumper($args)); + + foreach my $p (qw/rest_url validate action/) { + $log->die("ectcall needs $p parametar") unless ($args->{$p}); + } + + my $url = $args->{rest_url}; + my $del = '?'; + $del = '&' if ($url =~ m#\?#); + + my $url_args; + + foreach my $arg (@{ $estraier_rest->{ $args->{validate} }->{ $args->{action} } }) { + $log->logdie("missing parametar $arg for action $args->{action}") unless ($args->{$arg}); + $url_args .= $del . $arg . '=' . uri_escape( $args->{$arg} ); + $del = '&'; } - my $res = $self->{_master_ua}->get($url); + $url_args =~ s#^\&#?# if ($url =~ m#\?#); + $url .= $url_args; + + $log->debug("calling $url"); + + my $res = $self->est_ua()->get($url); if ($res->is_success) { #$log->debug( $res->content ); return split(/\n/, $res->content) if wantarray; - return $res->content; + return $res->content || 0E0; } else { $log->warn("unable to call $url: " . $res->status_line); - #$log->debug(Dumper($res, $self->{'_master_ua'})); return; } } +=head2 est_ua + +This is helper function to create C object with Super User +priviledges + + my $ua = $self->est_ua( user => 'admin', passwd => 'admin' ); + +=cut + +sub est_ua { + my $self = shift; + + return $self->{_master_ua} if ($self->{_master_ua}); + + $self->{_master_ua} = LWP::UserAgent->new( ) || sub { + my $log = $self->_get_logger; + $log->logdie("can't create LWP::UserAgent: $!"); + }; + + $self->{_master_ua}->credentials('localhost:1978','Super User', $self->{user} => $self->{passwd}); + + return $self->{_master_ua}; +} + =head1 AUTHOR Dobrica Pavlinusic, C<< >>