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