--- trunk/lib/WebPAC/Output/Estraier.pm 2005/12/14 23:08:11 246 +++ trunk/lib/WebPAC/Output/Estraier.pm 2005/12/16 01:04:20 255 @@ -17,11 +17,11 @@ =head1 VERSION -Version 0.04 +Version 0.05 =cut -our $VERSION = '0.04'; +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}", @@ -198,63 +198,133 @@ 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; -=head2 est_ua + my $args = {@_}; + my $log = $self->_get_logger; -Make C object with Super User priviledges + my $action = $args->{action} || $log->logconfess("no action specified"); - my $ua = $self->est_ua( user => 'admin', passwd => 'admin' ); + $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 + + $est->add_link( + from => 'ps', + to => 'webpac2', + credit => 10000, + ); =cut -sub est_ua { +sub add_link { my $self = shift; - return $self->{_master_ua} if ($self->{_master_ua}); + my $args = {@_}; + my $log = $self->_get_logger; - $self->{_master_ua} = LWP::UserAgent->new( ) || sub { - my $log = $self->_get_logger; - $log->logdie("can't create LWP::UserAgent: $!"); - }; + my @labels = $self->master( action => 'nodelist' ); - $self->{_master_ua}->credentials('localhost:1978','Super User', $self->{user} => $self->{passwd}); + $log->debug("got labels: ", join("|", @labels)); - return $self->{_master_ua}; + @labels = grep(/^$args->{to}/, @labels); + + my (undef,$label) = split(/\t/, shift @labels); + + if (! $label) { + $log->warn("can't find label for $args->{to}, skipping link creaton"); + return; + } + + $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}, + ); } -sub est_master { +=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(Dumper($args)); - - my $action = $args->{action} || $log->logconfess("no action specified"); - - $log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action}); + $log->debug("estcall: ",Dumper($args)); - my $url = $self->{masterurl} . '/master?action=' . $action; + foreach my $p (qw/rest_url validate action/) { + $log->die("ectcall needs $p parametar") unless ($args->{$p}); + } - foreach my $arg (@{ $estmaster_actions->{$action} }) { - $log->logdie("missing parametar $arg for action $action") unless ($args->{$arg}); - $url .= '&' . $arg . '=' . uri_escape( $args->{$arg} ); + 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 = '&'; } + $url_args =~ s#^\&#?# if ($url =~ m#\?#); + $url .= $url_args; + $log->debug("calling $url"); my $res = $self->est_ua()->get($url); @@ -262,7 +332,7 @@ 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); return; @@ -270,6 +340,30 @@ } +=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<< >>