/[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 458 by dpavlin, Wed May 10 14:08:15 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::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.11
21    
22  =cut  =cut
23    
24  our $VERSION = '0.02';  our $VERSION = '0.11';
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    
90          $log->debug("self: ", sub { Dumper($self) });          #$log->debug("self: ", sub { Dumper($self) });
91    
92          foreach my $p (qw/masterurl user passwd database/) {          foreach my $p (qw/masterurl user passwd database/) {
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};
         $url =~ s#//#/#g;  
99          $self->{url} = $url;          $self->{url} = $url;
100    
101          $log->info("opening Hyper Estraier index $self->{url}");          $self->{db} = Search::Estraier::Node->new(
102                    url => $url,
103          my @nodes = $self->est_master( action => 'nodelist' );                  user => $self->{user},
104                    passwd => $self->{passwd},
105          if (! grep(/$self->{database}/, @nodes)) {                  debug => $self->{debug},
106                  $log->info("creating index $url");                  create => 1,
107                  $self->est_master(                  label => "WebPAC $self->{database}",
108                          action => 'nodeadd',          );
109                          name => $self->{database},  
110                          label => "WebPAC $self->{database}",          $log->info("using index $self->{url} with encoding $self->{encoding}");
111                  ) || $log->logdie("can't create Hyper Estraier node $self->{database}");  
112            if ($self->{clean}) {
113                    $log->debug("clean $self->{database}");
114                    $self->master( action => 'nodeclr', name => $self->{database} );
115            } else {
116                    $log->debug("opening index $self->{url}");
117          }          }
118    
         $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 encoding $encoding");  
   
         $self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or  
                 $log->logdie("can't create conversion from $encoding to UTF-8");  
   
119          $self ? return $self : return undef;          $self ? return $self : return undef;
120  }  }
121    
# Line 125  Adds one entry to database. Line 128  Adds one entry to database.
128          id => 42,          id => 42,
129          ds => $ds,          ds => $ds,
130          type => 'display',          type => 'display',
         url_prefix => 'database name',  
131          text => 'optional text from which snippet is created',          text => 'optional text from which snippet is created',
132    );    );
133    
134  This function will create  entries in index using following URI format:  This function will create  entries in index using following URI format:
135    
136    C<file:///database%20name/000>    C<file:///type/database%20name/000>
137    
138  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
139  attribute and corresponding hidden text (used for search).  attribute and corresponding hidden text (used for search).
# Line 141  attribute and corresponding hidden text Line 143  attribute and corresponding hidden text
143  sub add {  sub add {
144          my $self = shift;          my $self = shift;
145    
146          my $args = {@_};          my $args = {@_};
147    
148          my $log = $self->_get_logger;          my $log = $self->_get_logger;
149    
# Line 153  sub add { Line 155  sub add {
155          }          }
156    
157          my $type = $args->{'type'};          my $type = $args->{'type'};
158          my $mfn = $args->{'id'};          my $id = $args->{'id'};
159    
160          my $uri = "file:///$type/$database/$mfn";          my $uri = "file:///$type/$database/$id";
161          $log->debug("creating $uri");          $log->debug("creating $uri");
162    
163          my $doc = HyperEstraier::Document->new;          my $doc = Search::Estraier::Document->new;
164          $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );          $doc->add_attr('@uri', $self->convert($uri) );
165    
166          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
167    
# Line 176  sub add { Line 178  sub add {
178    
179                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
180    
181                  $log->logconfess("no values for $tag/$type") unless ($vals);                  next if (! $vals);
182    
183                  $vals = $self->{'iconv'}->convert( $vals ) or                  $vals = $self->convert( $vals ) or
184                          $log->logdie("can't convert '$vals' to UTF-8");                          $log->logdie("can't convert '$vals' to UTF-8");
185    
186                  $doc->add_attr( $tag, $vals );                  $doc->add_attr( $tag, $vals );
# Line 187  sub add { Line 189  sub add {
189    
190          my $text = $args->{'text'};          my $text = $args->{'text'};
191          if ( $text ) {          if ( $text ) {
192                  $text = $self->{'iconv'}->convert( $text ) or                  $text = $self->convert( $text ) or
193                          $log->logdie("can't convert '$text' to UTF-8");                          $log->logdie("can't convert '$text' to UTF-8");
194                  $doc->add_text( $text );                  $doc->add_text( $text );
195          }          }
196    
197          $log->debug("adding ", sub { $doc->dump_draft } );          $log->debug("adding ", sub { $doc->dump_draft } );
198          $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());
199    
200          return 1;          return 1;
201  }  }
202    
203  =head2 est_master  =head2 add_link
204    
205  Issue administrative commands to C<estmaster> process and receive response    $est->add_link(
206  as array of lines          from => 'ps',
207            to => 'webpac2',
208    my $nodelist = $self->est_master( action => nodelist );          credit => 10000,
209      );
210    
211  =cut  =cut
212    
213  my $estmaster_actions = {  sub add_link {
         userdel => [ qw/name/ ],  
         nodelist => [],  
         nodeadd => [ qw/name label/ ],  
         nodedel => [ qw/name/ ],  
 };  
   
 sub est_master {  
214          my $self = shift;          my $self = shift;
215    
216          my $args = {@_};          my $args = {@_};
217          my $log = $self->_get_logger;          my $log = $self->_get_logger;
218    
219          $log->debug(Dumper($args));          my @labels = $self->master( action => 'nodelist' );
220    
221          my $action = $args->{action} || $log->logconfess("no action specified");          $log->debug("got labels: ", join("|", @labels));
222    
223          $log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action});          @labels = grep(/^$args->{to}\t/, @labels);
224            my $label = shift @labels;
225            (undef,$label) = split(/\t/, $label) if ($label);
226    
227          my $url = $self->{masterurl} . '/master?action=' . $action;          if (! $label) {
228                    $log->warn("can't find label for $args->{to}, skipping link creaton");
229          foreach my $arg (@{ $estmaster_actions->{$action} }) {                  return;
                 $log->logdie("missing parametar $arg for action $action") unless ($args->{$arg});  
                 $url .= '&' . $arg . '=' . uri_escape( $args->{$arg} );  
230          }          }
231    
232          $log->debug("calling $url");          $log->debug("using label $label for $args->{to}");
233    
234          my $tsv = get($url);          return $self->estcall(
235                    validate => 'node',
236                    action => '_set_link',
237                    rest_url => $self->{masterurl} . '/node/' . $args->{from} . '/_set_link' ,
238                    url => $self->{masterurl} . '/node/' . $args->{to},
239                    label => $label,
240                    credit => $args->{credit},
241            );
242    }
243    
         if (! $tsv) {  
                 $log->warn("unable to call $url");  
                 return;  
         }  
244    
245          return split(/\n/, $tsv);  =head2 master
246    
247    Issue administrative commands to C<estmaster> process. See documentation for
248    C<master> in L<Search::Estraier>::Node.
249    
250      $self->master(
251            action => 'nodeclr',
252            name => 'foobar',
253      );
254    
255    =cut
256    
257    sub master {
258            my $self = shift;
259            $self->{db}->master( @_ );
260    }
261    
262    
263    =head2 convert
264    
265     my $utf8_string = $self->convert('string in codepage');
266    
267    =cut
268    
269    sub convert {
270            my $self = shift;
271    
272            my $text = shift || return;
273            from_to($text, $self->{encoding}, 'UTF-8');
274            return $text;
275  }  }
276    
277  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26