/[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 319 by dpavlin, Sat Dec 24 11:23:17 2005 UTC revision 459 by dpavlin, Wed May 10 14:48:25 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 0.06;
9  use Text::Iconv;  use Encode qw/from_to/;
10  use Data::Dumper;  use Data::Dumper;
11  use LWP;  use LWP;
12  use URI::Escape;  use URI::Escape;
13    use List::Util qw/first/;
14    
15  =head1 NAME  =head1 NAME
16    
# Line 17  WebPAC::Output::Estraier - Create Hyper Line 18  WebPAC::Output::Estraier - Create Hyper
18    
19  =head1 VERSION  =head1 VERSION
20    
21  Version 0.08  Version 0.11
22    
23  =cut  =cut
24    
25  our $VERSION = '0.08';  our $VERSION = '0.11';
26    
27  =head1 SYNOPSIS  =head1 SYNOPSIS
28    
# Line 39  Connect to Hyper Estraier index using HT Line 40  Connect to Hyper Estraier index using HT
40          user => 'admin',          user => 'admin',
41          passwd => 'admin',          passwd => 'admin',
42          database => 'demo',          database => 'demo',
43            label => 'node label',
44          encoding => 'iso-8859-2',          encoding => 'iso-8859-2',
45          clean => 1,          clean => 1,
46   );   );
# Line 63  password for user Line 65  password for user
65    
66  name of database from which data comes  name of database from which data comes
67    
68    =item label
69    
70    label for node (optional)
71    
72  =item encoding  =item encoding
73    
74  character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>  character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>
# Line 77  Name of database will be used to form UR Line 83  Name of database will be used to form UR
83    
84  sub new {  sub new {
85          my $class = shift;          my $class = shift;
86          my $self = {@_};          my $self = {@_};
87          bless($self, $class);          bless($self, $class);
88    
89          my $log = $self->_get_logger;          my $log = $self->_get_logger;
90    
# Line 88  sub new { Line 94  sub new {
94                  $log->logdie("need $p") unless ($self->{$p});                  $log->logdie("need $p") unless ($self->{$p});
95          }          }
96    
97            $self->{encoding} ||= 'ISO-8859-2';
98    
99          my $url = $self->{masterurl} . '/node/' . $self->{database};          my $url = $self->{masterurl} . '/node/' . $self->{database};
100          $self->{url} = $url;          $self->{url} = $url;
101    
102            $self->{db} = Search::Estraier::Node->new(
103                    url => $url,
104                    user => $self->{user},
105                    passwd => $self->{passwd},
106                    debug => $self->{debug},
107                    create => 1,
108                    label => "WebPAC $self->{database}",
109            );
110    
111            $log->info("using index $self->{url} with encoding $self->{encoding}");
112    
113          if ($self->{clean}) {          if ($self->{clean}) {
114                  $log->debug("nodedel $self->{database}");                  $log->debug("clean $self->{database}");
115                  $self->master( action => 'nodedel', name => $self->{database} );                  $self->master( action => 'nodeclr', name => $self->{database} );
116          } else {          } else {
117                  $log->debug("opening index $self->{url}");                  $log->debug("opening index $self->{url}");
118          }          }
119    
         my $nodes = $self->master( action => 'nodelist' );  
   
         $log->debug("nodes found: $nodes");  
   
         if ($nodes !~ m/^$self->{database}\t/sm) {  
                 $log->warn("creating index $url");  
                 $self->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 index $self->{url} with encoding $encoding");  
   
         $self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or  
                 $log->logdie("can't create conversion from $encoding to UTF-8");  
   
120          $self ? return $self : return undef;          $self ? return $self : return undef;
121  }  }
122    
# Line 147  attribute and corresponding hidden text Line 144  attribute and corresponding hidden text
144  sub add {  sub add {
145          my $self = shift;          my $self = shift;
146    
147          my $args = {@_};          my $args = {@_};
148    
149          my $log = $self->_get_logger;          my $log = $self->_get_logger;
150    
# Line 164  sub add { Line 161  sub add {
161          my $uri = "file:///$type/$database/$id";          my $uri = "file:///$type/$database/$id";
162          $log->debug("creating $uri");          $log->debug("creating $uri");
163    
164          my $doc = HyperEstraier::Document->new;          my $doc = Search::Estraier::Document->new;
165          $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );          $doc->add_attr('@uri', $self->convert($uri) );
166    
167          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
168    
# Line 182  sub add { Line 179  sub add {
179    
180                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
181    
182                  $log->logconfess("no values for $tag/$type") unless ($vals);                  next if (! $vals);
183    
184                  $vals = $self->{'iconv'}->convert( $vals ) or                  $vals = $self->convert( $vals ) or
185                          $log->logdie("can't convert '$vals' to UTF-8");                          $log->logdie("can't convert '$vals' to UTF-8");
186    
187                  $doc->add_attr( $tag, $vals );                  $doc->add_attr( $tag, $vals );
# Line 193  sub add { Line 190  sub add {
190    
191          my $text = $args->{'text'};          my $text = $args->{'text'};
192          if ( $text ) {          if ( $text ) {
193                  $text = $self->{'iconv'}->convert( $text ) or                  $text = $self->convert( $text ) or
194                          $log->logdie("can't convert '$text' to UTF-8");                          $log->logdie("can't convert '$text' to UTF-8");
195                  $doc->add_text( $text );                  $doc->add_text( $text );
196          }          }
# Line 204  sub add { Line 201  sub add {
201          return 1;          return 1;
202  }  }
203    
 #  
 # 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<estmaster> process and receive response  
 as array of lines  
   
   my $nodelist = $est->master( action => 'nodelist' );  
   
 =cut  
   
 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,  
                 %{ $args },  
         );  
 }  
   
204  =head2 add_link  =head2 add_link
205    
206    $est->add_link(    $est->add_link(
# Line 265  sub add_link { Line 217  sub add_link {
217          my $args = {@_};          my $args = {@_};
218          my $log = $self->_get_logger;          my $log = $self->_get_logger;
219    
220          my @labels = $self->master( action => 'nodelist' );          my $node = first { $_->{name} eq $args->{to} } $self->master( action => 'nodelist' );
221    
222          $log->debug("got labels: ", join("|", @labels));          if (! $node) {
223                    $log->warn("can't find node $args->{to}, skipping link creaton");
224                    return;
225            }
226    
227          @labels = grep(/^$args->{to}\t/, @labels);          my $label = $node->{label};
         my $label = shift @labels;  
         (undef,$label) = split(/\t/, $label) if ($label);  
228    
229          if (! $label) {          if (! $label) {
230                  $log->warn("can't find label for $args->{to}, skipping link creaton");                  $log->warn("can't find label for $args->{to}, skipping link creaton");
# Line 280  sub add_link { Line 233  sub add_link {
233    
234          $log->debug("using label $label for $args->{to}");          $log->debug("using label $label for $args->{to}");
235    
236          return $self->estcall(          return $self->{db}->set_link(
237                  validate => 'node',                  $self->{masterurl} . '/node/' . $args->{to},
238                  action => '_set_link',                  $label,
239                  rest_url => $self->{masterurl} . '/node/' . $args->{from} . '/_set_link' ,                  $args->{credit},
                 url => $self->{masterurl} . '/node/' . $args->{to},  
                 label => $label,  
                 credit => $args->{credit},  
240          );          );
241  }  }
242    
 =head2 estcall  
243    
244  Workhourse which does actual calls to Hyper Estraier  =head2 master
245    
246    $self->estcall(  Issue administrative commands to C<estmaster> process. See documentation for
247          rest_url => '/master?action=' . $action,  C<master> in L<Search::Estraier>::Node.
         validate => 'master',  
         # ...  
   );  
248    
249  C<rest_url> is relative URL to C<estmaster> and C<validate> is entry into    $self->master(
250  internal hash which will check if all parametars are available before          action => 'nodeclr',
251  calling function.          name => 'foobar',
252      );
253    
254  =cut  =cut
255    
256  sub estcall {  sub master {
257          my $self = shift;          my $self = shift;
258          my $args = {@_};          $self->{db}->master( @_ );
         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 = '&';  
         }  
   
         $url .= $url_args if ($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 || 0E0;  
         } else {  
                 $log->warn("unable to call $url: " . $res->status_line);  
                 return;  
         }  
   
259  }  }
260    
 =head2 est_ua  
261    
262  This is helper function to create C<LWP::UserAgent> object with Super User  =head2 convert
 priviledges  
263    
264    my $ua = $self->est_ua( user => 'admin', passwd => 'admin' );   my $utf8_string = $self->convert('string in codepage');
265    
266  =cut  =cut
267    
268                                                sub convert {
   
 sub est_ua {  
269          my $self = shift;          my $self = shift;
270    
271          return $self->{_master_ua} if ($self->{_master_ua});          my $text = shift || return;
272            from_to($text, $self->{encoding}, 'UTF-8');
273          {          return $text;
                 package AdminUserAgent;  
                 use base qw/LWP::UserAgent/;  
                 sub new {  
                         my $self = LWP::UserAgent::new(@_);  
                         $self->agent("webpac/$VERSION");  
                         $self;  
                 }  
                 sub get_basic_credentials {  
                         my($self, $realm, $uri) = @_;  
                         return ($self->{user}, $self->{passwd});  
                 }  
                 sub set_basic_credentials {  
                         my ($self, $user, $passwd) = @_;  
                         $self->{user} = $user;  
                         $self->{passwd} = $passwd;  
                 }  
         };  
   
         $self->{_master_ua} = AdminUserAgent->new( ) || sub {  
                 my $log = $self->_get_logger;  
                 $log->logdie("can't create LWP::UserAgent: $!");  
         };  
   
         $self->{_master_ua}->set_basic_credentials($self->{user}, $self->{passwd});  
   
         return $self->{_master_ua};  
274  }  }
275    
276  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.319  
changed lines
  Added in v.459

  ViewVC Help
Powered by ViewVC 1.1.26