/[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 527 by dpavlin, Mon May 22 19:34:28 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.12
22    
23  =cut  =cut
24    
25  our $VERSION = '0.08';  our $VERSION = '0.12';
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          if ($self->{clean}) {          $self->{label} ||= "WebPAC $self->{database}";
                 $log->debug("nodedel $self->{database}");  
                 $self->master( action => 'nodedel', name => $self->{database} );  
         } else {  
                 $log->debug("opening index $self->{url}");  
         }  
103    
104          my $nodes = $self->master( action => 'nodelist' );          $self->{db} = Search::Estraier::Node->new(
105                    url => $url,
106                    user => $self->{user},
107                    passwd => $self->{passwd},
108                    debug => $self->{debug},
109                    create => 1,
110                    label => $self->convert( $self->{label} ),
111            );
112    
113          $log->debug("nodes found: $nodes");          $log->info("using ", $self->{clean} ? "new " : "", "index $self->{url} '$self->{label}' with encoding $self->{encoding}");
114    
115          if ($nodes !~ m/^$self->{database}\t/sm) {          if ($self->{clean}) {
116                  $log->warn("creating index $url");                  $log->debug("clean $self->{database}");
117                  $self->master(                  $self->master( action => 'nodeclr', name => $self->{database} );
118                          action => 'nodeadd',          } else {
119                          name => $self->{database},                  $log->debug("opening index $self->{url}");
                         label => "WebPAC $self->{database}",  
                 ) || $log->logdie("can't create Hyper Estraier node $self->{database}");  
120          }          }
121    
         $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");  
   
122          $self ? return $self : return undef;          $self ? return $self : return undef;
123  }  }
124    
# Line 147  attribute and corresponding hidden text Line 146  attribute and corresponding hidden text
146  sub add {  sub add {
147          my $self = shift;          my $self = shift;
148    
149          my $args = {@_};          my $args = {@_};
150    
151          my $log = $self->_get_logger;          my $log = $self->_get_logger;
152    
# Line 164  sub add { Line 163  sub add {
163          my $uri = "file:///$type/$database/$id";          my $uri = "file:///$type/$database/$id";
164          $log->debug("creating $uri");          $log->debug("creating $uri");
165    
166          my $doc = HyperEstraier::Document->new;          my $doc = Search::Estraier::Document->new;
167          $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );          $doc->add_attr('@uri', $self->convert($uri) );
168    
169          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
170    
# Line 182  sub add { Line 181  sub add {
181    
182                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
183    
184                  $log->logconfess("no values for $tag/$type") unless ($vals);                  next if (! $vals);
185    
186                  $vals = $self->{'iconv'}->convert( $vals ) or                  $vals = $self->convert( $vals ) or
187                          $log->logdie("can't convert '$vals' to UTF-8");                          $log->logdie("can't convert '$vals' to UTF-8");
188    
189                  $doc->add_attr( $tag, $vals );                  $doc->add_attr( $tag, $vals );
# Line 193  sub add { Line 192  sub add {
192    
193          my $text = $args->{'text'};          my $text = $args->{'text'};
194          if ( $text ) {          if ( $text ) {
195                  $text = $self->{'iconv'}->convert( $text ) or                  $text = $self->convert( $text ) or
196                          $log->logdie("can't convert '$text' to UTF-8");                          $log->logdie("can't convert '$text' to UTF-8");
197                  $doc->add_text( $text );                  $doc->add_text( $text );
198          }          }
# Line 204  sub add { Line 203  sub add {
203          return 1;          return 1;
204  }  }
205    
 #  
 # 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 },  
         );  
 }  
   
206  =head2 add_link  =head2 add_link
207    
208    $est->add_link(    $est->add_link(
# Line 265  sub add_link { Line 219  sub add_link {
219          my $args = {@_};          my $args = {@_};
220          my $log = $self->_get_logger;          my $log = $self->_get_logger;
221    
222          my @labels = $self->master( action => 'nodelist' );          foreach my $p (qw/from to credit/) {
223                    $log->logdie("need $p") unless ($args->{$p});
224            }
225    
226            my $node = first { $_->{name} eq $args->{to} } $self->master( action => 'nodelist' );
227    
228          $log->debug("got labels: ", join("|", @labels));          if (! $node) {
229                    $log->warn("can't find node $args->{to}, skipping link creaton");
230                    return;
231            }
232    
233          @labels = grep(/^$args->{to}\t/, @labels);          my $label = $node->{label};
         my $label = shift @labels;  
         (undef,$label) = split(/\t/, $label) if ($label);  
234    
235          if (! $label) {          if (! $label) {
236                  $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 239  sub add_link {
239    
240          $log->debug("using label $label for $args->{to}");          $log->debug("using label $label for $args->{to}");
241    
242          return $self->estcall(          return $self->{db}->set_link(
243                  validate => 'node',                  $self->{masterurl} . '/node/' . $args->{to},
244                  action => '_set_link',                  $label,
245                  rest_url => $self->{masterurl} . '/node/' . $args->{from} . '/_set_link' ,                  $args->{credit},
                 url => $self->{masterurl} . '/node/' . $args->{to},  
                 label => $label,  
                 credit => $args->{credit},  
246          );          );
247  }  }
248    
 =head2 estcall  
249    
250  Workhourse which does actual calls to Hyper Estraier  =head2 master
251    
252    $self->estcall(  Issue administrative commands to C<estmaster> process. See documentation for
253          rest_url => '/master?action=' . $action,  C<master> in L<Search::Estraier>::Node.
         validate => 'master',  
         # ...  
   );  
254    
255  C<rest_url> is relative URL to C<estmaster> and C<validate> is entry into    $self->master(
256  internal hash which will check if all parametars are available before          action => 'nodeclr',
257  calling function.          name => 'foobar',
258      );
259    
260  =cut  =cut
261    
262  sub estcall {  sub master {
263          my $self = shift;          my $self = shift;
264          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;  
         }  
   
265  }  }
266    
 =head2 est_ua  
267    
268  This is helper function to create C<LWP::UserAgent> object with Super User  =head2 convert
 priviledges  
269    
270    my $ua = $self->est_ua( user => 'admin', passwd => 'admin' );   my $utf8_string = $self->convert('string in codepage');
271    
272  =cut  =cut
273    
274                                                sub convert {
   
 sub est_ua {  
275          my $self = shift;          my $self = shift;
276    
277          return $self->{_master_ua} if ($self->{_master_ua});          my $text = shift || return;
278            from_to($text, $self->{encoding}, 'UTF-8');
279          {          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};  
280  }  }
281    
282  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26