/[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 210 by dpavlin, Mon Dec 5 17:47:04 2005 UTC revision 345 by dpavlin, Sat Jan 7 02:21:18 2006 UTC
# Line 5  use strict; Line 5  use strict;
5    
6  use base qw/WebPAC::Common/;  use base qw/WebPAC::Common/;
7    
8  use HyperEstraier;  use Search::Estraier;
9  use Text::Iconv;  use Text::Iconv;
10  use Data::Dumper;  use Data::Dumper;
11  use LWP::Simple;  use LWP;
12  use URI::Escape;  use URI::Escape;
13    
14  =head1 NAME  =head1 NAME
# Line 17  WebPAC::Output::Estraier - Create Hyper Line 17  WebPAC::Output::Estraier - Create Hyper
17    
18  =head1 VERSION  =head1 VERSION
19    
20  Version 0.02  Version 0.09
21    
22  =cut  =cut
23    
24  our $VERSION = '0.02';  our $VERSION = '0.09';
25    
26  =head1 SYNOPSIS  =head1 SYNOPSIS
27    
# Line 40  Connect to Hyper Estraier index using HT Line 40  Connect to Hyper Estraier index using HT
40          passwd => 'admin',          passwd => 'admin',
41          database => 'demo',          database => 'demo',
42          encoding => 'iso-8859-2',          encoding => 'iso-8859-2',
43            clean => 1,
44   );   );
45    
46  Options are:  Options are:
# Line 76  Name of database will be used to form UR Line 77  Name of database will be used to form UR
77    
78  sub new {  sub new {
79          my $class = shift;          my $class = shift;
80          my $self = {@_};          my $self = {@_};
81          bless($self, $class);          bless($self, $class);
82    
83          my $log = $self->_get_logger;          my $log = $self->_get_logger;
84    
85          $log->debug("self: ", sub { Dumper($self) });          #$log->debug("self: ", sub { Dumper($self) });
86    
87          foreach my $p (qw/masterurl user passwd database/) {          foreach my $p (qw/masterurl user passwd database/) {
88                  $log->logdie("need $p") unless ($self->{$p});                  $log->logdie("need $p") unless ($self->{$p});
89          }          }
90    
91          my $url = $self->{masterurl} . '/node/' . $self->{database};          my $url = $self->{masterurl} . '/node/' . $self->{database};
         $url =~ s#//#/#g;  
92          $self->{url} = $url;          $self->{url} = $url;
93    
94          $log->info("opening Hyper Estraier index $self->{url}");          if ($self->{clean}) {
95                    $log->debug("nodedel $self->{database}");
96                    $self->master( action => 'nodedel', name => $self->{database} );
97            } else {
98                    $log->debug("opening index $self->{url}");
99            }
100    
101            my $nodes = $self->master( action => 'nodelist' );
102    
103          my @nodes = $self->est_master( action => 'nodelist' );          $log->debug("nodes found: $nodes");
104    
105          if (! grep(/$self->{database}/, @nodes)) {          if ($nodes !~ m/^$self->{database}\t/sm) {
106                  $log->info("creating index $url");                  $log->warn("creating index $url");
107                  $self->est_master(                  $self->master(
108                          action => 'nodeadd',                          action => 'nodeadd',
109                          name => $self->{database},                          name => $self->{database},
110                          label => "WebPAC $self->{database}",                          label => "WebPAC $self->{database}",
111                  ) || $log->logdie("can't create Hyper Estraier node $self->{database}");                  ) || $log->logdie("can't create Hyper Estraier node $self->{database}");
112          }          }
113    
114          $self->{'db'} = HyperEstraier::Node->new($self->{url});          $self->{db} = Search::Estraier::Node->new( debug => $self->{debug} );
115          $self->{'db'}->set_auth($self->{'user'}, $self->{passwd});          $self->{db}->set_url($self->{url});
116            $self->{db}->set_auth($self->{user}, $self->{passwd});
117    
118          my $encoding = $self->{'encoding'} || 'ISO-8859-2';          my $encoding = $self->{encoding} || 'ISO-8859-2';
119          $log->info("using encoding $encoding");          $log->info("using index $self->{url} with encoding $encoding");
120    
121          $self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or          $self->{iconv} = new Text::Iconv($encoding, 'UTF-8') or
122                  $log->logdie("can't create conversion from $encoding to UTF-8");                  $log->logdie("can't create conversion from $encoding to UTF-8");
123    
124          $self ? return $self : return undef;          $self ? return $self : return undef;
# Line 125  Adds one entry to database. Line 133  Adds one entry to database.
133          id => 42,          id => 42,
134          ds => $ds,          ds => $ds,
135          type => 'display',          type => 'display',
         url_prefix => 'database name',  
136          text => 'optional text from which snippet is created',          text => 'optional text from which snippet is created',
137    );    );
138    
139  This function will create  entries in index using following URI format:  This function will create  entries in index using following URI format:
140    
141    C<file:///database%20name/000>    C<file:///type/database%20name/000>
142    
143  Each tag in C<data_structure> with specified C<type> will create one  Each tag in C<data_structure> with specified C<type> will create one
144  attribute and corresponding hidden text (used for search).  attribute and corresponding hidden text (used for search).
# Line 153  sub add { Line 160  sub add {
160          }          }
161    
162          my $type = $args->{'type'};          my $type = $args->{'type'};
163          my $mfn = $args->{'id'};          my $id = $args->{'id'};
164    
165          my $uri = "file:///$type/$database/$mfn";          my $uri = "file:///$type/$database/$id";
166          $log->debug("creating $uri");          $log->debug("creating $uri");
167    
168          my $doc = HyperEstraier::Document->new;          my $doc = Search::Estraier::Document->new;
169          $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );          $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );
170    
171          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
# Line 193  sub add { Line 200  sub add {
200          }          }
201    
202          $log->debug("adding ", sub { $doc->dump_draft } );          $log->debug("adding ", sub { $doc->dump_draft } );
203          $self->{'db'}->put_doc($doc) || $log->logdie("can't add document $uri to index");          $self->{'db'}->put_doc($doc) || $log->warn("can't add document $uri with draft " . $doc->dump_draft . " to node " . $self->{url} . " status: " . $self->{db}->status());
204    
205          return 1;          return 1;
206  }  }
207    
208  =head2 est_master  #
209    # REST parametars validation data
210    #
211    
212    my $estraier_rest = {
213            master => {
214                    userdel => [ qw/name/ ],
215                    nodelist => [],
216                    nodeadd => [ qw/name label/ ],
217                    nodedel => [ qw/name/ ],
218            },
219            node => {
220                    _set_link => [ qw/url label credit/ ],
221            },
222    };
223    
224    =head2 master
225    
226  Issue administrative commands to C<estmaster> process and receive response  Issue administrative commands to C<estmaster> process and receive response
227  as array of lines  as array of lines
228    
229    my $nodelist = $self->est_master( action => nodelist );    my $nodelist = $est->master( action => 'nodelist' );
230    
231  =cut  =cut
232    
233  my $estmaster_actions = {  sub master {
234          userdel => [ qw/name/ ],          my $self = shift;
235          nodelist => [],  
236          nodeadd => [ qw/name label/ ],          my $args = {@_};
237          nodedel => [ qw/name/ ],          my $log = $self->_get_logger;
238  };  
239            my $action = $args->{action} || $log->logconfess("no action specified");
240    
241            $log->logdie("action '$action' isn't supported") unless ($estraier_rest->{master}->{$action});
242    
243            $log->debug("master action: $action");
244    
245            return $self->estcall(
246                    validate => 'master',
247                    rest_url => $self->{masterurl} . '/master?action=' . $action ,
248                    action => $action,
249                    %{ $args },
250            );
251    }
252    
253    =head2 add_link
254    
255      $est->add_link(
256            from => 'ps',
257            to => 'webpac2',
258            credit => 10000,
259      );
260    
261  sub est_master {  =cut
262    
263    sub add_link {
264          my $self = shift;          my $self = shift;
265    
266          my $args = {@_};          my $args = {@_};
267          my $log = $self->_get_logger;          my $log = $self->_get_logger;
268    
269          $log->debug(Dumper($args));          my @labels = $self->master( action => 'nodelist' );
270    
271          my $action = $args->{action} || $log->logconfess("no action specified");          $log->debug("got labels: ", join("|", @labels));
272    
273            @labels = grep(/^$args->{to}\t/, @labels);
274            my $label = shift @labels;
275            (undef,$label) = split(/\t/, $label) if ($label);
276    
277          $log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action});          if (! $label) {
278                    $log->warn("can't find label for $args->{to}, skipping link creaton");
279                    return;
280            }
281    
282            $log->debug("using label $label for $args->{to}");
283    
284            return $self->estcall(
285                    validate => 'node',
286                    action => '_set_link',
287                    rest_url => $self->{masterurl} . '/node/' . $args->{from} . '/_set_link' ,
288                    url => $self->{masterurl} . '/node/' . $args->{to},
289                    label => $label,
290                    credit => $args->{credit},
291            );
292    }
293    
294          my $url = $self->{masterurl} . '/master?action=' . $action;  =head2 estcall
295    
296          foreach my $arg (@{ $estmaster_actions->{$action} }) {  Workhourse which does actual calls to Hyper Estraier
297                  $log->logdie("missing parametar $arg for action $action") unless ($args->{$arg});  
298                  $url .= '&' . $arg . '=' . uri_escape( $args->{$arg} );    $self->estcall(
299            rest_url => '/master?action=' . $action,
300            validate => 'master',
301            # ...
302      );
303    
304    C<rest_url> is relative URL to C<estmaster> and C<validate> is entry into
305    internal hash which will check if all parametars are available before
306    calling function.
307    
308    =cut
309    
310    sub estcall {
311            my $self = shift;
312            my $args = {@_};
313            my $log = $self->_get_logger;
314    
315            $log->debug("estcall: ",Dumper($args));
316    
317            foreach my $p (qw/rest_url validate action/) {
318                    $log->die("ectcall needs $p parametar") unless ($args->{$p});
319          }          }
320    
321            my $url = $args->{rest_url};
322            my $del = '?';
323            $del = '&' if ($url =~ m#\?#);
324    
325            my $url_args;
326    
327            foreach my $arg (@{ $estraier_rest->{ $args->{validate} }->{ $args->{action} } }) {
328                    $log->logdie("missing parametar $arg for action $args->{action}") unless ($args->{$arg});
329                    $url_args .= $del . $arg . '=' . uri_escape( $args->{$arg} );
330                    $del = '&';
331            }
332    
333            $url .= $url_args if ($url_args);
334    
335          $log->debug("calling $url");          $log->debug("calling $url");
336    
337          my $tsv = get($url);          my $res = $self->est_ua()->get($url);
338    
339          if (! $tsv) {          if ($res->is_success) {
340                  $log->warn("unable to call $url");                  #$log->debug( $res->content );
341                    return split(/\n/, $res->content) if wantarray;
342                    return $res->content || 0E0;
343            } else {
344                    $log->warn("unable to call $url: " . $res->status_line);
345                  return;                  return;
346          }          }
347    
348          return split(/\n/, $tsv);  }
349    
350    =head2 est_ua
351    
352    This is helper function to create C<LWP::UserAgent> object with Super User
353    priviledges
354    
355      my $ua = $self->est_ua( user => 'admin', passwd => 'admin' );
356    
357    =cut
358    
359                                                
360    
361    sub est_ua {
362            my $self = shift;
363    
364            return $self->{_master_ua} if ($self->{_master_ua});
365    
366            {
367                    package AdminUserAgent;
368                    use base qw/LWP::UserAgent/;
369                    sub new {
370                            my $self = LWP::UserAgent::new(@_);
371                            $self->agent("webpac/$VERSION");
372                            $self;
373                    }
374                    sub get_basic_credentials {
375                            my($self, $realm, $uri) = @_;
376                            return ($self->{user}, $self->{passwd});
377                    }
378                    sub set_basic_credentials {
379                            my ($self, $user, $passwd) = @_;
380                            $self->{user} = $user;
381                            $self->{passwd} = $passwd;
382                    }
383            };
384    
385            $self->{_master_ua} = AdminUserAgent->new( ) || sub {
386                    my $log = $self->_get_logger;
387                    $log->logdie("can't create LWP::UserAgent: $!");
388            };
389    
390            $self->{_master_ua}->set_basic_credentials($self->{user}, $self->{passwd});
391    
392            return $self->{_master_ua};
393  }  }
394    
395  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.210  
changed lines
  Added in v.345

  ViewVC Help
Powered by ViewVC 1.1.26