/[webpac2]/trunk/lib/WebPAC/Output/Estraier.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Output/Estraier.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 254 by dpavlin, Wed Dec 14 23:08:11 2005 UTC revision 255 by dpavlin, Fri Dec 16 01:04:20 2005 UTC
# Line 17  WebPAC::Output::Estraier - Create Hyper Line 17  WebPAC::Output::Estraier - Create Hyper
17    
18  =head1 VERSION  =head1 VERSION
19    
20  Version 0.04  Version 0.05
21    
22  =cut  =cut
23    
24  our $VERSION = '0.04';  our $VERSION = '0.05';
25    
26  =head1 SYNOPSIS  =head1 SYNOPSIS
27    
# Line 92  sub new { Line 92  sub new {
92    
93          $log->info("opening Hyper Estraier index $self->{url}");          $log->info("opening Hyper Estraier index $self->{url}");
94    
95          my $nodes = $self->est_master( action => 'nodelist' );          my $nodes = $self->master( action => 'nodelist' );
96    
97          $log->debug("nodes found: $nodes");          $log->debug("nodes found: $nodes");
98    
99          if ($nodes !~ m/^$self->{database}\t/sm) {          if ($nodes !~ m/^$self->{database}\t/sm) {
100                  $log->info("creating index $url");                  $log->info("creating index $url");
101                  $self->est_master(                  $self->master(
102                          action => 'nodeadd',                          action => 'nodeadd',
103                          name => $self->{database},                          name => $self->{database},
104                          label => "WebPAC $self->{database}",                          label => "WebPAC $self->{database}",
# Line 198  sub add { Line 198  sub add {
198          return 1;          return 1;
199  }  }
200    
201  =head2 est_master  #
202    # REST parametars validation data
203    #
204    
205    my $estraier_rest = {
206            master => {
207                    userdel => [ qw/name/ ],
208                    nodelist => [],
209                    nodeadd => [ qw/name label/ ],
210                    nodedel => [ qw/name/ ],
211            },
212            node => {
213                    _set_link => [ qw/url label credit/ ],
214            },
215    };
216    
217    =head2 master
218    
219  Issue administrative commands to C<estmaster> process and receive response  Issue administrative commands to C<estmaster> process and receive response
220  as array of lines  as array of lines
221    
222    my $nodelist = $self->est_master( action => nodelist );    my $nodelist = $est->master( action => 'nodelist' );
223    
224  =cut  =cut
225    
226  my $estmaster_actions = {  sub master {
227          userdel => [ qw/name/ ],          my $self = shift;
         nodelist => [],  
         nodeadd => [ qw/name label/ ],  
         nodedel => [ qw/name/ ],  
 };  
228    
229  =head2 est_ua          my $args = {@_};
230            my $log = $self->_get_logger;
231    
232  Make C<LWP::UserAgent> object with Super User priviledges          my $action = $args->{action} || $log->logconfess("no action specified");
233    
234    my $ua = $self->est_ua( user => 'admin', passwd => 'admin' );          $log->logdie("action '$action' isn't supported") unless ($estraier_rest->{master}->{$action});
235    
236            $log->debug("master action: $action");
237    
238            return $self->estcall(
239                    validate => 'master',
240                    rest_url => $self->{masterurl} . '/master?action=' . $action ,
241                    action => $action,
242            );
243    }
244    
245    =head2 add_link
246    
247      $est->add_link(
248            from => 'ps',
249            to => 'webpac2',
250            credit => 10000,
251      );
252    
253  =cut  =cut
254    
255  sub est_ua {  sub add_link {
256          my $self = shift;          my $self = shift;
257    
258          return $self->{_master_ua} if ($self->{_master_ua});          my $args = {@_};
259            my $log = $self->_get_logger;
260    
261          $self->{_master_ua} = LWP::UserAgent->new( ) || sub {          my @labels = $self->master( action => 'nodelist' );
                 my $log = $self->_get_logger;  
                 $log->logdie("can't create LWP::UserAgent: $!");  
         };  
262    
263          $self->{_master_ua}->credentials('localhost:1978','Super User', $self->{user} => $self->{passwd});          $log->debug("got labels: ", join("|", @labels));
264    
265          return $self->{_master_ua};          @labels = grep(/^$args->{to}/, @labels);
266    
267            my (undef,$label) = split(/\t/, shift @labels);
268    
269            if (! $label) {
270                    $log->warn("can't find label for $args->{to}, skipping link creaton");
271                    return;
272            }
273    
274            $log->debug("using label $label for $args->{to}");
275    
276            return $self->estcall(
277                    validate => 'node',
278                    action => '_set_link',
279                    rest_url => $self->{masterurl} . '/node/' . $args->{from} . '/_set_link' ,
280                    url => $self->{masterurl} . '/node/' . $args->{to},
281                    label => $label,
282                    credit => $args->{credit},
283            );
284  }  }
285    
286  sub est_master {  =head2 estcall
287    
288    Workhourse which does actual calls to Hyper Estraier
289    
290      $self->estcall(
291            rest_url => '/master?action=' . $action,
292            validate => 'master',
293            # ...
294      );
295    
296    C<rest_url> is relative URL to C<estmaster> and C<validate> is entry into
297    internal hash which will check if all parametars are available before
298    calling function.
299    
300    =cut
301    
302    sub estcall {
303          my $self = shift;          my $self = shift;
304          my $args = {@_};          my $args = {@_};
305          my $log = $self->_get_logger;          my $log = $self->_get_logger;
306    
307          $log->debug(Dumper($args));          $log->debug("estcall: ",Dumper($args));
   
         my $action = $args->{action} || $log->logconfess("no action specified");  
   
         $log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action});  
308    
309          my $url = $self->{masterurl} . '/master?action=' . $action;          foreach my $p (qw/rest_url validate action/) {
310                    $log->die("ectcall needs $p parametar") unless ($args->{$p});
311            }
312    
313          foreach my $arg (@{ $estmaster_actions->{$action} }) {          my $url = $args->{rest_url};
314                  $log->logdie("missing parametar $arg for action $action") unless ($args->{$arg});          my $del = '?';
315                  $url .= '&' . $arg . '=' . uri_escape( $args->{$arg} );          $del = '&' if ($url =~ m#\?#);
316    
317            my $url_args;
318    
319            foreach my $arg (@{ $estraier_rest->{ $args->{validate} }->{ $args->{action} } }) {
320                    $log->logdie("missing parametar $arg for action $args->{action}") unless ($args->{$arg});
321                    $url_args .= $del . $arg . '=' . uri_escape( $args->{$arg} );
322                    $del = '&';
323          }          }
324    
325            $url_args =~ s#^\&#?# if ($url =~ m#\?#);
326            $url .= $url_args;
327    
328          $log->debug("calling $url");          $log->debug("calling $url");
329    
330          my $res = $self->est_ua()->get($url);          my $res = $self->est_ua()->get($url);
# Line 262  sub est_master { Line 332  sub est_master {
332          if ($res->is_success) {          if ($res->is_success) {
333                  #$log->debug( $res->content );                  #$log->debug( $res->content );
334                  return split(/\n/, $res->content) if wantarray;                  return split(/\n/, $res->content) if wantarray;
335                  return $res->content;                  return $res->content || 0E0;
336          } else {          } else {
337                  $log->warn("unable to call $url: " . $res->status_line);                  $log->warn("unable to call $url: " . $res->status_line);
338                  return;                  return;
# Line 270  sub est_master { Line 340  sub est_master {
340    
341  }  }
342    
343    =head2 est_ua
344    
345    This is helper function to create C<LWP::UserAgent> object with Super User
346    priviledges
347    
348      my $ua = $self->est_ua( user => 'admin', passwd => 'admin' );
349    
350    =cut
351    
352    sub est_ua {
353            my $self = shift;
354    
355            return $self->{_master_ua} if ($self->{_master_ua});
356    
357            $self->{_master_ua} = LWP::UserAgent->new( ) || sub {
358                    my $log = $self->_get_logger;
359                    $log->logdie("can't create LWP::UserAgent: $!");
360            };
361    
362            $self->{_master_ua}->credentials('localhost:1978','Super User', $self->{user} => $self->{passwd});
363    
364            return $self->{_master_ua};
365    }
366    
367  =head1 AUTHOR  =head1 AUTHOR
368    
369  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

Legend:
Removed from v.254  
changed lines
  Added in v.255

  ViewVC Help
Powered by ViewVC 1.1.26