/[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

Contents of /trunk/lib/WebPAC/Output/Estraier.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 343 - (show annotations)
Sat Jan 7 01:40:01 2006 UTC (18 years, 3 months ago) by dpavlin
File size: 8660 byte(s)
 r354@llin:  dpavlin | 2006-01-07 00:42:38 +0100
 update to use Search::Estraier [2.09]

1 package WebPAC::Output::Estraier;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common/;
7
8 use Search::Estraier;
9 use Text::Iconv;
10 use Data::Dumper;
11 use LWP;
12 use URI::Escape;
13
14 =head1 NAME
15
16 WebPAC::Output::Estraier - Create Hyper Estraier full text index
17
18 =head1 VERSION
19
20 Version 0.09
21
22 =cut
23
24 our $VERSION = '0.09';
25
26 =head1 SYNOPSIS
27
28 Create full text index using Hyper Estraier index from data with
29 type C<search>.
30
31 =head1 FUNCTIONS
32
33 =head2 new
34
35 Connect to Hyper Estraier index using HTTP
36
37 my $est = new WebPAC::Output::Estraier(
38 masterurl => 'http://localhost:1978/',
39 user => 'admin',
40 passwd => 'admin',
41 database => 'demo',
42 encoding => 'iso-8859-2',
43 clean => 1,
44 );
45
46 Options are:
47
48 =over 4
49
50 =item masterurl
51
52 URI to C<estmaster> node
53
54 =item user
55
56 C<estmaster> user with sufficient rights
57
58 =item passwd
59
60 password for user
61
62 =item database
63
64 name of database from which data comes
65
66 =item encoding
67
68 character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>
69 (and it probably is). This encoding will be converted to C<UTF-8> for
70 Hyper Estraier.
71
72 =back
73
74 Name of database will be used to form URI of documents in index.
75
76 =cut
77
78 sub new {
79 my $class = shift;
80 my $self = {@_};
81 bless($self, $class);
82
83 my $log = $self->_get_logger;
84
85 #$log->debug("self: ", sub { Dumper($self) });
86
87 foreach my $p (qw/masterurl user passwd database/) {
88 $log->logdie("need $p") unless ($self->{$p});
89 }
90
91 my $url = $self->{masterurl} . '/node/' . $self->{database};
92 $self->{url} = $url;
93
94 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 $log->debug("nodes found: $nodes");
104
105 if ($nodes !~ m/^$self->{database}\t/sm) {
106 $log->warn("creating index $url");
107 $self->master(
108 action => 'nodeadd',
109 name => $self->{database},
110 label => "WebPAC $self->{database}",
111 ) || $log->logdie("can't create Hyper Estraier node $self->{database}");
112 }
113
114 $self->{db} = Search::Estraier::Node->new( debug => 1 );
115 $self->{db}->set_url($self->{url});
116 $self->{db}->set_auth($self->{user}, $self->{passwd});
117
118 my $encoding = $self->{encoding} || 'ISO-8859-2';
119 $log->info("using index $self->{url} with encoding $encoding");
120
121 $self->{iconv} = new Text::Iconv($encoding, 'UTF-8') or
122 $log->logdie("can't create conversion from $encoding to UTF-8");
123
124 $self ? return $self : return undef;
125 }
126
127
128 =head2 add
129
130 Adds one entry to database.
131
132 $est->add(
133 id => 42,
134 ds => $ds,
135 type => 'display',
136 text => 'optional text from which snippet is created',
137 );
138
139 This function will create entries in index using following URI format:
140
141 C<file:///type/database%20name/000>
142
143 Each tag in C<data_structure> with specified C<type> will create one
144 attribute and corresponding hidden text (used for search).
145
146 =cut
147
148 sub add {
149 my $self = shift;
150
151 my $args = {@_};
152
153 my $log = $self->_get_logger;
154
155 my $database = $self->{'database'} || $log->logconfess('no database in $self');
156 $log->logconfess('need db in object') unless ($self->{'db'});
157
158 foreach my $p (qw/id ds type/) {
159 $log->logdie("need $p") unless ($args->{$p});
160 }
161
162 my $type = $args->{'type'};
163 my $id = $args->{'id'};
164
165 my $uri = "file:///$type/$database/$id";
166 $log->debug("creating $uri");
167
168 my $doc = Search::Estraier::Document->new;
169 $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );
170
171 $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
172
173 # filter all tags which have type defined
174 my @tags = grep {
175 ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} )
176 } keys %{ $args->{'ds'} };
177
178 $log->debug("tags = ", join(",", @tags));
179
180 return unless (@tags);
181
182 foreach my $tag (@tags) {
183
184 my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
185
186 $log->logconfess("no values for $tag/$type") unless ($vals);
187
188 $vals = $self->{'iconv'}->convert( $vals ) or
189 $log->logdie("can't convert '$vals' to UTF-8");
190
191 $doc->add_attr( $tag, $vals );
192 $doc->add_hidden_text( $vals );
193 }
194
195 my $text = $args->{'text'};
196 if ( $text ) {
197 $text = $self->{'iconv'}->convert( $text ) or
198 $log->logdie("can't convert '$text' to UTF-8");
199 $doc->add_text( $text );
200 }
201
202 $log->debug("adding ", sub { $doc->dump_draft } );
203 $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;
206 }
207
208 #
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
227 as array of lines
228
229 my $nodelist = $est->master( action => 'nodelist' );
230
231 =cut
232
233 sub master {
234 my $self = shift;
235
236 my $args = {@_};
237 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 =cut
262
263 sub add_link {
264 my $self = shift;
265
266 my $args = {@_};
267 my $log = $self->_get_logger;
268
269 my @labels = $self->master( action => 'nodelist' );
270
271 $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 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 =head2 estcall
295
296 Workhourse which does actual calls to Hyper Estraier
297
298 $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");
336
337 my $res = $self->est_ua()->get($url);
338
339 if ($res->is_success) {
340 #$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;
346 }
347
348 }
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
396
397 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
398
399 =head1 COPYRIGHT & LICENSE
400
401 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
402
403 This program is free software; you can redistribute it and/or modify it
404 under the same terms as Perl itself.
405
406 =cut
407
408 1; # End of WebPAC::Output::Estraier

  ViewVC Help
Powered by ViewVC 1.1.26