5 |
|
|
6 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common/; |
7 |
|
|
8 |
use HyperEstraier; |
use Search::Estraier; |
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.09 |
21 |
|
|
22 |
=cut |
=cut |
23 |
|
|
24 |
our $VERSION = '0.01'; |
our $VERSION = '0.09'; |
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 |
|
|
77 |
|
|
78 |
sub new { |
sub new { |
79 |
my $class = shift; |
my $class = shift; |
80 |
my $self = {@_}; |
my $self = {@_}; |
81 |
bless($self, $class); |
bless($self, $class); |
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} = Search::Estraier::Node->new( debug => $self->{debug} ); |
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'; |
my $encoding = $self->{encoding} || 'ISO-8859-2'; |
119 |
$log->info("using encoding $encoding"); |
$log->info("using index $self->{url} with encoding $encoding"); |
120 |
|
|
121 |
$self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or |
$self->{iconv} = new Text::Iconv($encoding, 'UTF-8') or |
122 |
$log->logdie("can't create conversion from $encoding to UTF-8"); |
$log->logdie("can't create conversion from $encoding to UTF-8"); |
123 |
|
|
124 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
133 |
id => 42, |
id => 42, |
134 |
ds => $ds, |
ds => $ds, |
135 |
type => 'display', |
type => 'display', |
|
url_prefix => 'database name', |
|
136 |
text => 'optional text from which snippet is created', |
text => 'optional text from which snippet is created', |
137 |
); |
); |
138 |
|
|
139 |
This function will create entries in index using following URI format: |
This function will create entries in index using following URI format: |
140 |
|
|
141 |
C<file:///database%20name/000> |
C<file:///type/database%20name/000> |
142 |
|
|
143 |
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 |
144 |
attribute and corresponding hidden text (used for search). |
attribute and corresponding hidden text (used for search). |
160 |
} |
} |
161 |
|
|
162 |
my $type = $args->{'type'}; |
my $type = $args->{'type'}; |
163 |
my $mfn = $args->{'id'}; |
my $id = $args->{'id'}; |
164 |
|
|
165 |
my $uri = "file:///$type/$database/$mfn"; |
my $uri = "file:///$type/$database/$id"; |
166 |
$log->debug("creating $uri"); |
$log->debug("creating $uri"); |
167 |
|
|
168 |
my $doc = HyperEstraier::Document->new; |
my $doc = Search::Estraier::Document->new; |
169 |
$doc->add_attr('@uri', $self->{'iconv'}->convert($uri) ); |
$doc->add_attr('@uri', $self->{'iconv'}->convert($uri) ); |
170 |
|
|
171 |
$log->debug("ds = ", sub { Dumper($args->{'ds'}) } ); |
$log->debug("ds = ", sub { Dumper($args->{'ds'}) } ); |
172 |
|
|
173 |
# filter all tags which have type defined |
# filter all tags which have type defined |
174 |
my @tags = grep { |
my @tags = grep { |
175 |
defined( $args->{'ds'}->{$_}->{$type} ) |
ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} ) |
176 |
} keys %{ $args->{'ds'} }; |
} keys %{ $args->{'ds'} }; |
177 |
|
|
178 |
$log->debug("tags = ", join(",", @tags)); |
$log->debug("tags = ", join(",", @tags)); |
200 |
} |
} |
201 |
|
|
202 |
$log->debug("adding ", sub { $doc->dump_draft } ); |
$log->debug("adding ", sub { $doc->dump_draft } ); |
203 |
$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()); |
204 |
|
|
205 |
return 1; |
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 |
=head1 AUTHOR |
396 |
|
|
397 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |