8 |
use HyperEstraier; |
use HyperEstraier; |
9 |
use Text::Iconv; |
use Text::Iconv; |
10 |
use Data::Dumper; |
use Data::Dumper; |
11 |
|
use LWP; |
12 |
|
use URI::Escape; |
13 |
|
|
14 |
=head1 NAME |
=head1 NAME |
15 |
|
|
17 |
|
|
18 |
=head1 VERSION |
=head1 VERSION |
19 |
|
|
20 |
Version 0.01 |
Version 0.08 |
21 |
|
|
22 |
=cut |
=cut |
23 |
|
|
24 |
our $VERSION = '0.01'; |
our $VERSION = '0.08'; |
25 |
|
|
26 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
27 |
|
|
35 |
Connect to Hyper Estraier index using HTTP |
Connect to Hyper Estraier index using HTTP |
36 |
|
|
37 |
my $est = new WebPAC::Output::Estraier( |
my $est = new WebPAC::Output::Estraier( |
38 |
url => 'http://localhost:1978/node/webpac2', |
masterurl => 'http://localhost:1978/', |
39 |
user => 'admin', |
user => 'admin', |
40 |
passwd => 'admin', |
passwd => 'admin', |
41 |
database => 'demo', |
database => 'demo', |
42 |
encoding => 'iso-8859-2', |
encoding => 'iso-8859-2', |
43 |
|
clean => 1, |
44 |
); |
); |
45 |
|
|
46 |
Options are: |
Options are: |
47 |
|
|
48 |
=over 4 |
=over 4 |
49 |
|
|
50 |
=item url |
=item masterurl |
51 |
|
|
52 |
URI to C<estmaster> node |
URI to C<estmaster> node |
53 |
|
|
82 |
|
|
83 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
84 |
|
|
85 |
foreach my $p (qw/url user passwd/) { |
#$log->debug("self: ", sub { Dumper($self) }); |
86 |
|
|
87 |
|
foreach my $p (qw/masterurl user passwd database/) { |
88 |
$log->logdie("need $p") unless ($self->{$p}); |
$log->logdie("need $p") unless ($self->{$p}); |
89 |
} |
} |
90 |
|
|
91 |
$log->info("opening Hyper Estraier index $self->{'url'}"); |
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 |
$self->{'db'} = HyperEstraier::Node->new($self->{'url'}); |
if ($nodes !~ m/^$self->{database}\t/sm) { |
106 |
$self->{'db'}->set_auth($self->{'user'}, $self->{'passwd'}); |
$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'} = HyperEstraier::Node->new($self->{url}); |
115 |
|
$self->{'db'}->set_auth($self->{'user'}, $self->{passwd}); |
116 |
|
|
117 |
my $encoding = $self->{'encoding'} || 'ISO-8859-2'; |
my $encoding = $self->{'encoding'} || 'ISO-8859-2'; |
118 |
$log->info("using encoding $encoding"); |
$log->info("using index $self->{url} with encoding $encoding"); |
119 |
|
|
120 |
$self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or |
$self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or |
121 |
$log->die("can't create conversion from $encoding to UTF-8"); |
$log->logdie("can't create conversion from $encoding to UTF-8"); |
122 |
|
|
123 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
124 |
} |
} |
132 |
id => 42, |
id => 42, |
133 |
ds => $ds, |
ds => $ds, |
134 |
type => 'display', |
type => 'display', |
|
url_prefix => 'database name', |
|
135 |
text => 'optional text from which snippet is created', |
text => 'optional text from which snippet is created', |
136 |
); |
); |
137 |
|
|
138 |
This function will create entries in index using following URI format: |
This function will create entries in index using following URI format: |
139 |
|
|
140 |
C<file:///database%20name/000> |
C<file:///type/database%20name/000> |
141 |
|
|
142 |
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 |
143 |
attribute and corresponding hidden text (used for search). |
attribute and corresponding hidden text (used for search). |
159 |
} |
} |
160 |
|
|
161 |
my $type = $args->{'type'}; |
my $type = $args->{'type'}; |
162 |
my $mfn = $args->{'id'}; |
my $id = $args->{'id'}; |
163 |
|
|
164 |
my $uri = "file:///$type/$database/$mfn"; |
my $uri = "file:///$type/$database/$id"; |
165 |
$log->debug("creating $uri"); |
$log->debug("creating $uri"); |
166 |
|
|
167 |
my $doc = HyperEstraier::Document->new; |
my $doc = HyperEstraier::Document->new; |
171 |
|
|
172 |
# filter all tags which have type defined |
# filter all tags which have type defined |
173 |
my @tags = grep { |
my @tags = grep { |
174 |
defined( $args->{'ds'}->{$_}->{$type} ) |
ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} ) |
175 |
} keys %{ $args->{'ds'} }; |
} keys %{ $args->{'ds'} }; |
176 |
|
|
177 |
$log->debug("tags = ", join(",", @tags)); |
$log->debug("tags = ", join(",", @tags)); |
185 |
$log->logconfess("no values for $tag/$type") unless ($vals); |
$log->logconfess("no values for $tag/$type") unless ($vals); |
186 |
|
|
187 |
$vals = $self->{'iconv'}->convert( $vals ) or |
$vals = $self->{'iconv'}->convert( $vals ) or |
188 |
$log->die("can't convert '$vals' to UTF-8"); |
$log->logdie("can't convert '$vals' to UTF-8"); |
189 |
|
|
190 |
$doc->add_attr( $tag, $vals ); |
$doc->add_attr( $tag, $vals ); |
191 |
$doc->add_hidden_text( $vals ); |
$doc->add_hidden_text( $vals ); |
194 |
my $text = $args->{'text'}; |
my $text = $args->{'text'}; |
195 |
if ( $text ) { |
if ( $text ) { |
196 |
$text = $self->{'iconv'}->convert( $text ) or |
$text = $self->{'iconv'}->convert( $text ) or |
197 |
$log->die("can't convert '$text' to UTF-8"); |
$log->logdie("can't convert '$text' to UTF-8"); |
198 |
$doc->add_text( $text ); |
$doc->add_text( $text ); |
199 |
} |
} |
200 |
|
|
201 |
$log->debug("adding ", sub { $doc->dump_draft } ); |
$log->debug("adding ", sub { $doc->dump_draft } ); |
202 |
$self->{'db'}->put_doc($doc) || $log->die("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()); |
203 |
|
|
204 |
return 1; |
return 1; |
205 |
} |
} |
206 |
|
|
207 |
|
# |
208 |
|
# REST parametars validation data |
209 |
|
# |
210 |
|
|
211 |
|
my $estraier_rest = { |
212 |
|
master => { |
213 |
|
userdel => [ qw/name/ ], |
214 |
|
nodelist => [], |
215 |
|
nodeadd => [ qw/name label/ ], |
216 |
|
nodedel => [ qw/name/ ], |
217 |
|
}, |
218 |
|
node => { |
219 |
|
_set_link => [ qw/url label credit/ ], |
220 |
|
}, |
221 |
|
}; |
222 |
|
|
223 |
|
=head2 master |
224 |
|
|
225 |
|
Issue administrative commands to C<estmaster> process and receive response |
226 |
|
as array of lines |
227 |
|
|
228 |
|
my $nodelist = $est->master( action => 'nodelist' ); |
229 |
|
|
230 |
|
=cut |
231 |
|
|
232 |
|
sub master { |
233 |
|
my $self = shift; |
234 |
|
|
235 |
|
my $args = {@_}; |
236 |
|
my $log = $self->_get_logger; |
237 |
|
|
238 |
|
my $action = $args->{action} || $log->logconfess("no action specified"); |
239 |
|
|
240 |
|
$log->logdie("action '$action' isn't supported") unless ($estraier_rest->{master}->{$action}); |
241 |
|
|
242 |
|
$log->debug("master action: $action"); |
243 |
|
|
244 |
|
return $self->estcall( |
245 |
|
validate => 'master', |
246 |
|
rest_url => $self->{masterurl} . '/master?action=' . $action , |
247 |
|
action => $action, |
248 |
|
%{ $args }, |
249 |
|
); |
250 |
|
} |
251 |
|
|
252 |
|
=head2 add_link |
253 |
|
|
254 |
|
$est->add_link( |
255 |
|
from => 'ps', |
256 |
|
to => 'webpac2', |
257 |
|
credit => 10000, |
258 |
|
); |
259 |
|
|
260 |
|
=cut |
261 |
|
|
262 |
|
sub add_link { |
263 |
|
my $self = shift; |
264 |
|
|
265 |
|
my $args = {@_}; |
266 |
|
my $log = $self->_get_logger; |
267 |
|
|
268 |
|
my @labels = $self->master( action => 'nodelist' ); |
269 |
|
|
270 |
|
$log->debug("got labels: ", join("|", @labels)); |
271 |
|
|
272 |
|
@labels = grep(/^$args->{to}\t/, @labels); |
273 |
|
my $label = shift @labels; |
274 |
|
(undef,$label) = split(/\t/, $label) if ($label); |
275 |
|
|
276 |
|
if (! $label) { |
277 |
|
$log->warn("can't find label for $args->{to}, skipping link creaton"); |
278 |
|
return; |
279 |
|
} |
280 |
|
|
281 |
|
$log->debug("using label $label for $args->{to}"); |
282 |
|
|
283 |
|
return $self->estcall( |
284 |
|
validate => 'node', |
285 |
|
action => '_set_link', |
286 |
|
rest_url => $self->{masterurl} . '/node/' . $args->{from} . '/_set_link' , |
287 |
|
url => $self->{masterurl} . '/node/' . $args->{to}, |
288 |
|
label => $label, |
289 |
|
credit => $args->{credit}, |
290 |
|
); |
291 |
|
} |
292 |
|
|
293 |
|
=head2 estcall |
294 |
|
|
295 |
|
Workhourse which does actual calls to Hyper Estraier |
296 |
|
|
297 |
|
$self->estcall( |
298 |
|
rest_url => '/master?action=' . $action, |
299 |
|
validate => 'master', |
300 |
|
# ... |
301 |
|
); |
302 |
|
|
303 |
|
C<rest_url> is relative URL to C<estmaster> and C<validate> is entry into |
304 |
|
internal hash which will check if all parametars are available before |
305 |
|
calling function. |
306 |
|
|
307 |
|
=cut |
308 |
|
|
309 |
|
sub estcall { |
310 |
|
my $self = shift; |
311 |
|
my $args = {@_}; |
312 |
|
my $log = $self->_get_logger; |
313 |
|
|
314 |
|
$log->debug("estcall: ",Dumper($args)); |
315 |
|
|
316 |
|
foreach my $p (qw/rest_url validate action/) { |
317 |
|
$log->die("ectcall needs $p parametar") unless ($args->{$p}); |
318 |
|
} |
319 |
|
|
320 |
|
my $url = $args->{rest_url}; |
321 |
|
my $del = '?'; |
322 |
|
$del = '&' if ($url =~ m#\?#); |
323 |
|
|
324 |
|
my $url_args; |
325 |
|
|
326 |
|
foreach my $arg (@{ $estraier_rest->{ $args->{validate} }->{ $args->{action} } }) { |
327 |
|
$log->logdie("missing parametar $arg for action $args->{action}") unless ($args->{$arg}); |
328 |
|
$url_args .= $del . $arg . '=' . uri_escape( $args->{$arg} ); |
329 |
|
$del = '&'; |
330 |
|
} |
331 |
|
|
332 |
|
$url .= $url_args if ($url_args); |
333 |
|
|
334 |
|
$log->debug("calling $url"); |
335 |
|
|
336 |
|
my $res = $self->est_ua()->get($url); |
337 |
|
|
338 |
|
if ($res->is_success) { |
339 |
|
#$log->debug( $res->content ); |
340 |
|
return split(/\n/, $res->content) if wantarray; |
341 |
|
return $res->content || 0E0; |
342 |
|
} else { |
343 |
|
$log->warn("unable to call $url: " . $res->status_line); |
344 |
|
return; |
345 |
|
} |
346 |
|
|
347 |
|
} |
348 |
|
|
349 |
|
=head2 est_ua |
350 |
|
|
351 |
|
This is helper function to create C<LWP::UserAgent> object with Super User |
352 |
|
priviledges |
353 |
|
|
354 |
|
my $ua = $self->est_ua( user => 'admin', passwd => 'admin' ); |
355 |
|
|
356 |
|
=cut |
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
sub est_ua { |
361 |
|
my $self = shift; |
362 |
|
|
363 |
|
return $self->{_master_ua} if ($self->{_master_ua}); |
364 |
|
|
365 |
|
{ |
366 |
|
package AdminUserAgent; |
367 |
|
use base qw/LWP::UserAgent/; |
368 |
|
sub new { |
369 |
|
my $self = LWP::UserAgent::new(@_); |
370 |
|
$self->agent("webpac/$VERSION"); |
371 |
|
$self; |
372 |
|
} |
373 |
|
sub get_basic_credentials { |
374 |
|
my($self, $realm, $uri) = @_; |
375 |
|
return ($self->{user}, $self->{passwd}); |
376 |
|
} |
377 |
|
sub set_basic_credentials { |
378 |
|
my ($self, $user, $passwd) = @_; |
379 |
|
$self->{user} = $user; |
380 |
|
$self->{passwd} = $passwd; |
381 |
|
} |
382 |
|
}; |
383 |
|
|
384 |
|
$self->{_master_ua} = AdminUserAgent->new( ) || sub { |
385 |
|
my $log = $self->_get_logger; |
386 |
|
$log->logdie("can't create LWP::UserAgent: $!"); |
387 |
|
}; |
388 |
|
|
389 |
|
$self->{_master_ua}->set_basic_credentials($self->{user}, $self->{passwd}); |
390 |
|
|
391 |
|
return $self->{_master_ua}; |
392 |
|
} |
393 |
|
|
394 |
=head1 AUTHOR |
=head1 AUTHOR |
395 |
|
|
396 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |