/[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 255 by dpavlin, Fri Dec 16 01:04:20 2005 UTC revision 456 by dpavlin, Wed May 10 13:56:21 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 Encode qw/from_to/;
10  use Data::Dumper;  use Data::Dumper;
11  use LWP;  use LWP;
12  use URI::Escape;  use URI::Escape;
# Line 17  WebPAC::Output::Estraier - Create Hyper Line 17  WebPAC::Output::Estraier - Create Hyper
17    
18  =head1 VERSION  =head1 VERSION
19    
20  Version 0.05  Version 0.10
21    
22  =cut  =cut
23    
24  our $VERSION = '0.05';  our $VERSION = '0.10';
25    
26  =head1 SYNOPSIS  =head1 SYNOPSIS
27    
# Line 39  Connect to Hyper Estraier index using HT Line 39  Connect to Hyper Estraier index using HT
39          user => 'admin',          user => 'admin',
40          passwd => 'admin',          passwd => 'admin',
41          database => 'demo',          database => 'demo',
42            label => 'node label',
43          encoding => 'iso-8859-2',          encoding => 'iso-8859-2',
44            clean => 1,
45   );   );
46    
47  Options are:  Options are:
# Line 62  password for user Line 64  password for user
64    
65  name of database from which data comes  name of database from which data comes
66    
67    =item label
68    
69    label for node (optional)
70    
71  =item encoding  =item encoding
72    
73  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 76  Name of database will be used to form UR Line 82  Name of database will be used to form UR
82    
83  sub new {  sub new {
84          my $class = shift;          my $class = shift;
85          my $self = {@_};          my $self = {@_};
86          bless($self, $class);          bless($self, $class);
87    
88          my $log = $self->_get_logger;          my $log = $self->_get_logger;
89    
# Line 87  sub new { Line 93  sub new {
93                  $log->logdie("need $p") unless ($self->{$p});                  $log->logdie("need $p") unless ($self->{$p});
94          }          }
95    
96            $self->{encoding} ||= 'ISO-8859-2';
97    
98          my $url = $self->{masterurl} . '/node/' . $self->{database};          my $url = $self->{masterurl} . '/node/' . $self->{database};
99          $self->{url} = $url;          $self->{url} = $url;
100    
101          $log->info("opening Hyper Estraier index $self->{url}");          if ($self->{clean}) {
102                    $log->debug("nodedel $self->{database}");
103                    $self->master( action => 'nodedel', name => $self->{database} );
104            } else {
105                    $log->debug("opening index $self->{url}");
106            }
107    
108          my $nodes = $self->master( action => 'nodelist' );          my $nodes = $self->master( action => 'nodelist' );
109    
110          $log->debug("nodes found: $nodes");          $log->debug("nodes found: $nodes");
111    
112          if ($nodes !~ m/^$self->{database}\t/sm) {          if ($nodes !~ m/^$self->{database}\t/sm) {
113                  $log->info("creating index $url");                  my $label = $self->{label} || 'WebPAC ' . $self->{database};
114                    $log->warn("creating index $url ($label)");
115                  $self->master(                  $self->master(
116                          action => 'nodeadd',                          action => 'nodeadd',
117                          name => $self->{database},                          name => $self->{database},
118                          label => "WebPAC $self->{database}",                          label => $self->convert( $label ),
119                  ) || $log->logdie("can't create Hyper Estraier node $self->{database}");                  ) || $log->logdie("can't create Hyper Estraier node $self->{database}");
120          }          }
121    
122          $self->{'db'} = HyperEstraier::Node->new($self->{url});          $self->{db} = Search::Estraier::Node->new( debug => $self->{debug} );
123          $self->{'db'}->set_auth($self->{'user'}, $self->{passwd});          $self->{db}->set_url($self->{url});
124            $self->{db}->set_auth($self->{user}, $self->{passwd});
125    
126          my $encoding = $self->{'encoding'} || 'ISO-8859-2';          $log->info("using index $self->{url} with encoding $self->{encoding}");
         $log->info("using encoding $encoding");  
   
         $self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or  
                 $log->logdie("can't create conversion from $encoding to UTF-8");  
127    
128          $self ? return $self : return undef;          $self ? return $self : return undef;
129  }  }
# Line 141  attribute and corresponding hidden text Line 152  attribute and corresponding hidden text
152  sub add {  sub add {
153          my $self = shift;          my $self = shift;
154    
155          my $args = {@_};          my $args = {@_};
156    
157          my $log = $self->_get_logger;          my $log = $self->_get_logger;
158    
# Line 158  sub add { Line 169  sub add {
169          my $uri = "file:///$type/$database/$id";          my $uri = "file:///$type/$database/$id";
170          $log->debug("creating $uri");          $log->debug("creating $uri");
171    
172          my $doc = HyperEstraier::Document->new;          my $doc = Search::Estraier::Document->new;
173          $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );          $doc->add_attr('@uri', $self->convert($uri) );
174    
175          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
176    
# Line 176  sub add { Line 187  sub add {
187    
188                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
189    
190                  $log->logconfess("no values for $tag/$type") unless ($vals);                  next if (! $vals);
191    
192                  $vals = $self->{'iconv'}->convert( $vals ) or                  $vals = $self->convert( $vals ) or
193                          $log->logdie("can't convert '$vals' to UTF-8");                          $log->logdie("can't convert '$vals' to UTF-8");
194    
195                  $doc->add_attr( $tag, $vals );                  $doc->add_attr( $tag, $vals );
# Line 187  sub add { Line 198  sub add {
198    
199          my $text = $args->{'text'};          my $text = $args->{'text'};
200          if ( $text ) {          if ( $text ) {
201                  $text = $self->{'iconv'}->convert( $text ) or                  $text = $self->convert( $text ) or
202                          $log->logdie("can't convert '$text' to UTF-8");                          $log->logdie("can't convert '$text' to UTF-8");
203                  $doc->add_text( $text );                  $doc->add_text( $text );
204          }          }
205    
206          $log->debug("adding ", sub { $doc->dump_draft } );          $log->debug("adding ", sub { $doc->dump_draft } );
207          $self->{'db'}->put_doc($doc) || $log->logdie("can't add document $uri to node " . $self->{url} . " status: " . $self->{db}->status());          $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());
208    
209          return 1;          return 1;
210  }  }
# Line 239  sub master { Line 250  sub master {
250                  validate => 'master',                  validate => 'master',
251                  rest_url => $self->{masterurl} . '/master?action=' . $action ,                  rest_url => $self->{masterurl} . '/master?action=' . $action ,
252                  action => $action,                  action => $action,
253                    %{ $args },
254          );          );
255  }  }
256    
# Line 262  sub add_link { Line 274  sub add_link {
274    
275          $log->debug("got labels: ", join("|", @labels));          $log->debug("got labels: ", join("|", @labels));
276    
277          @labels = grep(/^$args->{to}/, @labels);          @labels = grep(/^$args->{to}\t/, @labels);
278            my $label = shift @labels;
279          my (undef,$label) = split(/\t/, shift @labels);          (undef,$label) = split(/\t/, $label) if ($label);
280    
281          if (! $label) {          if (! $label) {
282                  $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 322  sub estcall { Line 334  sub estcall {
334                  $del = '&';                  $del = '&';
335          }          }
336    
337          $url_args =~ s#^\&#?# if ($url =~ m#\?#);          $url .= $url_args if ($url_args);
         $url .= $url_args;  
338    
339          $log->debug("calling $url");          $log->debug("calling $url");
340    
# Line 349  priviledges Line 360  priviledges
360    
361  =cut  =cut
362    
363                                                
364    
365  sub est_ua {  sub est_ua {
366          my $self = shift;          my $self = shift;
367    
368          return $self->{_master_ua} if ($self->{_master_ua});          return $self->{_master_ua} if ($self->{_master_ua});
369    
370          $self->{_master_ua} = LWP::UserAgent->new( ) || sub {          {
371                    package AdminUserAgent;
372                    use base qw/LWP::UserAgent/;
373                    sub new {
374                            my $self = LWP::UserAgent::new(@_);
375                            $self->agent("webpac/$VERSION");
376                            $self;
377                    }
378                    sub get_basic_credentials {
379                            my($self, $realm, $uri) = @_;
380                            return ($self->{user}, $self->{passwd});
381                    }
382                    sub set_basic_credentials {
383                            my ($self, $user, $passwd) = @_;
384                            $self->{user} = $user;
385                            $self->{passwd} = $passwd;
386                    }
387            };
388    
389            $self->{_master_ua} = AdminUserAgent->new( ) || sub {
390                  my $log = $self->_get_logger;                  my $log = $self->_get_logger;
391                  $log->logdie("can't create LWP::UserAgent: $!");                  $log->logdie("can't create LWP::UserAgent: $!");
392          };          };
393    
394          $self->{_master_ua}->credentials('localhost:1978','Super User', $self->{user} => $self->{passwd});          $self->{_master_ua}->set_basic_credentials($self->{user}, $self->{passwd});
395    
396          return $self->{_master_ua};          return $self->{_master_ua};
397  }  }
398    
399    =head2 convert
400    
401     my $utf8_string = $self->convert('string in codepage');
402    
403    =cut
404    
405    sub convert {
406            my $self = shift;
407    
408            my $text = shift || return;
409            from_to($text, $self->{encoding}, 'UTF-8');
410            return $text;
411    }
412    
413  =head1 AUTHOR  =head1 AUTHOR
414    
415  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

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

  ViewVC Help
Powered by ViewVC 1.1.26