5 |
|
|
6 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common/; |
7 |
|
|
8 |
use Search::Estraier; |
use Search::Estraier 0.06; |
9 |
use Text::Iconv; |
use Encode qw/from_to/; |
10 |
use Data::Dumper; |
use Data::Dump qw/dump/; |
11 |
use LWP; |
use LWP; |
12 |
use URI::Escape; |
use URI::Escape; |
13 |
|
use List::Util qw/first/; |
14 |
|
use YAML; |
15 |
|
|
16 |
=head1 NAME |
=head1 NAME |
17 |
|
|
19 |
|
|
20 |
=head1 VERSION |
=head1 VERSION |
21 |
|
|
22 |
Version 0.09 |
Version 0.12 |
23 |
|
|
24 |
=cut |
=cut |
25 |
|
|
26 |
our $VERSION = '0.09'; |
our $VERSION = '0.12'; |
27 |
|
|
28 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
29 |
|
|
41 |
user => 'admin', |
user => 'admin', |
42 |
passwd => 'admin', |
passwd => 'admin', |
43 |
database => 'demo', |
database => 'demo', |
44 |
|
label => 'node label', |
45 |
encoding => 'iso-8859-2', |
encoding => 'iso-8859-2', |
46 |
clean => 1, |
clean => 1, |
47 |
); |
); |
66 |
|
|
67 |
name of database from which data comes |
name of database from which data comes |
68 |
|
|
69 |
|
=item label |
70 |
|
|
71 |
|
label for node (optional) |
72 |
|
|
73 |
=item encoding |
=item encoding |
74 |
|
|
75 |
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> |
89 |
|
|
90 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
91 |
|
|
92 |
#$log->debug("self: ", sub { Dumper($self) }); |
#$log->debug("self: ", sub { dump($self) }); |
93 |
|
|
94 |
foreach my $p (qw/masterurl user passwd database/) { |
foreach my $p (qw/masterurl user passwd database/) { |
95 |
$log->logdie("need $p") unless ($self->{$p}); |
$log->logdie("need $p") unless ($self->{$p}); |
96 |
} |
} |
97 |
|
|
98 |
|
$self->{encoding} ||= 'ISO-8859-2'; |
99 |
|
|
100 |
my $url = $self->{masterurl} . '/node/' . $self->{database}; |
my $url = $self->{masterurl} . '/node/' . $self->{database}; |
101 |
$self->{url} = $url; |
$self->{url} = $url; |
102 |
|
|
103 |
if ($self->{clean}) { |
$self->{label} ||= "WebPAC $self->{database}"; |
|
$log->debug("nodedel $self->{database}"); |
|
|
$self->master( action => 'nodedel', name => $self->{database} ); |
|
|
} else { |
|
|
$log->debug("opening index $self->{url}"); |
|
|
} |
|
104 |
|
|
105 |
my $nodes = $self->master( action => 'nodelist' ); |
$self->{db} = Search::Estraier::Node->new( |
106 |
|
url => $url, |
107 |
|
user => $self->{user}, |
108 |
|
passwd => $self->{passwd}, |
109 |
|
debug => $self->{debug}, |
110 |
|
create => 1, |
111 |
|
label => $self->convert( $self->{label} ), |
112 |
|
); |
113 |
|
|
114 |
$log->debug("nodes found: $nodes"); |
$log->info("using ", $self->{clean} ? "new " : "", "index $self->{url} '$self->{label}' with encoding $self->{encoding}"); |
115 |
|
|
116 |
if ($nodes !~ m/^$self->{database}\t/sm) { |
if ($self->{clean}) { |
117 |
$log->warn("creating index $url"); |
$log->debug("clean $self->{database}"); |
118 |
$self->master( |
$self->master( action => 'nodeclr', name => $self->{database} ); |
119 |
action => 'nodeadd', |
} else { |
120 |
name => $self->{database}, |
$log->debug("opening index $self->{url}"); |
|
label => "WebPAC $self->{database}", |
|
|
) || $log->logdie("can't create Hyper Estraier node $self->{database}"); |
|
121 |
} |
} |
122 |
|
|
123 |
$self->{db} = Search::Estraier::Node->new( debug => 1 ); |
$self->{stats} = {}; |
|
$self->{db}->set_url($self->{url}); |
|
|
$self->{db}->set_auth($self->{user}, $self->{passwd}); |
|
|
|
|
|
my $encoding = $self->{encoding} || 'ISO-8859-2'; |
|
|
$log->info("using index $self->{url} with encoding $encoding"); |
|
|
|
|
|
$self->{iconv} = new Text::Iconv($encoding, 'UTF-8') or |
|
|
$log->logdie("can't create conversion from $encoding to UTF-8"); |
|
124 |
|
|
125 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
126 |
} |
} |
149 |
sub add { |
sub add { |
150 |
my $self = shift; |
my $self = shift; |
151 |
|
|
152 |
my $args = {@_}; |
my $args = {@_}; |
153 |
|
|
154 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
155 |
|
|
167 |
$log->debug("creating $uri"); |
$log->debug("creating $uri"); |
168 |
|
|
169 |
my $doc = Search::Estraier::Document->new; |
my $doc = Search::Estraier::Document->new; |
170 |
$doc->add_attr('@uri', $self->{'iconv'}->convert($uri) ); |
$doc->add_attr('@uri', $self->convert($uri) ); |
171 |
|
|
172 |
$log->debug("ds = ", sub { Dumper($args->{'ds'}) } ); |
$log->debug("ds = ", sub { dump($args->{'ds'}) } ); |
173 |
|
|
174 |
# filter all tags which have type defined |
# filter all tags which have type defined |
175 |
my @tags = grep { |
my @tags = grep { |
184 |
|
|
185 |
my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} }); |
my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} }); |
186 |
|
|
187 |
$log->logconfess("no values for $tag/$type") unless ($vals); |
next if (! $vals); |
188 |
|
|
189 |
$vals = $self->{'iconv'}->convert( $vals ) or |
$vals = $self->convert( $vals ) or |
190 |
$log->logdie("can't convert '$vals' to UTF-8"); |
$log->logdie("can't convert '$vals' to UTF-8"); |
191 |
|
|
192 |
$doc->add_attr( $tag, $vals ); |
$doc->add_attr( $tag, $vals ); |
193 |
$doc->add_hidden_text( $vals ); |
$doc->add_hidden_text( $vals ); |
194 |
|
|
195 |
|
$self->{stats}->{attr}->{$tag}++; |
196 |
} |
} |
197 |
|
|
198 |
my $text = $args->{'text'}; |
my $text = $args->{'text'}; |
199 |
if ( $text ) { |
if ( $text ) { |
200 |
$text = $self->{'iconv'}->convert( $text ) or |
$text = $self->convert( $text ) or |
201 |
$log->logdie("can't convert '$text' to UTF-8"); |
$log->logdie("can't convert '$text' to UTF-8"); |
202 |
$doc->add_text( $text ); |
$doc->add_text( $text ); |
203 |
} |
} |
208 |
return 1; |
return 1; |
209 |
} |
} |
210 |
|
|
|
# |
|
|
# REST parametars validation data |
|
|
# |
|
|
|
|
|
my $estraier_rest = { |
|
|
master => { |
|
|
userdel => [ qw/name/ ], |
|
|
nodelist => [], |
|
|
nodeadd => [ qw/name label/ ], |
|
|
nodedel => [ qw/name/ ], |
|
|
}, |
|
|
node => { |
|
|
_set_link => [ qw/url label credit/ ], |
|
|
}, |
|
|
}; |
|
|
|
|
|
=head2 master |
|
|
|
|
|
Issue administrative commands to C<estmaster> process and receive response |
|
|
as array of lines |
|
|
|
|
|
my $nodelist = $est->master( action => 'nodelist' ); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub master { |
|
|
my $self = shift; |
|
|
|
|
|
my $args = {@_}; |
|
|
my $log = $self->_get_logger; |
|
|
|
|
|
my $action = $args->{action} || $log->logconfess("no action specified"); |
|
|
|
|
|
$log->logdie("action '$action' isn't supported") unless ($estraier_rest->{master}->{$action}); |
|
|
|
|
|
$log->debug("master action: $action"); |
|
|
|
|
|
return $self->estcall( |
|
|
validate => 'master', |
|
|
rest_url => $self->{masterurl} . '/master?action=' . $action , |
|
|
action => $action, |
|
|
%{ $args }, |
|
|
); |
|
|
} |
|
|
|
|
211 |
=head2 add_link |
=head2 add_link |
212 |
|
|
213 |
$est->add_link( |
$est->add_link( |
224 |
my $args = {@_}; |
my $args = {@_}; |
225 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
226 |
|
|
227 |
my @labels = $self->master( action => 'nodelist' ); |
foreach my $p (qw/from to credit/) { |
228 |
|
$log->logdie("need $p") unless ($args->{$p}); |
229 |
|
} |
230 |
|
|
231 |
$log->debug("got labels: ", join("|", @labels)); |
my $node = first { $_->{name} eq $args->{to} } $self->master( action => 'nodelist' ); |
232 |
|
|
233 |
@labels = grep(/^$args->{to}\t/, @labels); |
if (! $node) { |
234 |
my $label = shift @labels; |
$log->warn("can't find node $args->{to}, skipping link creaton"); |
235 |
(undef,$label) = split(/\t/, $label) if ($label); |
return; |
236 |
|
} |
237 |
|
|
238 |
|
my $label = $node->{label}; |
239 |
|
|
240 |
if (! $label) { |
if (! $label) { |
241 |
$log->warn("can't find label for $args->{to}, skipping link creaton"); |
$log->warn("can't find label for $args->{to}, skipping link creaton"); |
244 |
|
|
245 |
$log->debug("using label $label for $args->{to}"); |
$log->debug("using label $label for $args->{to}"); |
246 |
|
|
247 |
return $self->estcall( |
return $self->{db}->set_link( |
248 |
validate => 'node', |
$self->{masterurl} . '/node/' . $args->{to}, |
249 |
action => '_set_link', |
$label, |
250 |
rest_url => $self->{masterurl} . '/node/' . $args->{from} . '/_set_link' , |
$args->{credit}, |
|
url => $self->{masterurl} . '/node/' . $args->{to}, |
|
|
label => $label, |
|
|
credit => $args->{credit}, |
|
251 |
); |
); |
252 |
} |
} |
253 |
|
|
|
=head2 estcall |
|
254 |
|
|
255 |
Workhourse which does actual calls to Hyper Estraier |
=head2 master |
256 |
|
|
257 |
|
Issue administrative commands to C<estmaster> process. See documentation for |
258 |
|
C<master> in L<Search::Estraier>::Node. |
259 |
|
|
260 |
$self->estcall( |
$self->master( |
261 |
rest_url => '/master?action=' . $action, |
action => 'nodeclr', |
262 |
validate => 'master', |
name => 'foobar', |
|
# ... |
|
263 |
); |
); |
264 |
|
|
|
C<rest_url> is relative URL to C<estmaster> and C<validate> is entry into |
|
|
internal hash which will check if all parametars are available before |
|
|
calling function. |
|
|
|
|
265 |
=cut |
=cut |
266 |
|
|
267 |
sub estcall { |
sub master { |
268 |
my $self = shift; |
my $self = shift; |
269 |
my $args = {@_}; |
$self->{db}->master( @_ ); |
270 |
my $log = $self->_get_logger; |
} |
|
|
|
|
$log->debug("estcall: ",Dumper($args)); |
|
|
|
|
|
foreach my $p (qw/rest_url validate action/) { |
|
|
$log->die("ectcall needs $p parametar") unless ($args->{$p}); |
|
|
} |
|
271 |
|
|
|
my $url = $args->{rest_url}; |
|
|
my $del = '?'; |
|
|
$del = '&' if ($url =~ m#\?#); |
|
|
|
|
|
my $url_args; |
|
|
|
|
|
foreach my $arg (@{ $estraier_rest->{ $args->{validate} }->{ $args->{action} } }) { |
|
|
$log->logdie("missing parametar $arg for action $args->{action}") unless ($args->{$arg}); |
|
|
$url_args .= $del . $arg . '=' . uri_escape( $args->{$arg} ); |
|
|
$del = '&'; |
|
|
} |
|
272 |
|
|
273 |
$url .= $url_args if ($url_args); |
=head2 convert |
274 |
|
|
275 |
$log->debug("calling $url"); |
my $utf8_string = $self->convert('string in codepage'); |
276 |
|
|
277 |
my $res = $self->est_ua()->get($url); |
=cut |
278 |
|
|
279 |
if ($res->is_success) { |
sub convert { |
280 |
#$log->debug( $res->content ); |
my $self = shift; |
|
return split(/\n/, $res->content) if wantarray; |
|
|
return $res->content || 0E0; |
|
|
} else { |
|
|
$log->warn("unable to call $url: " . $res->status_line); |
|
|
return; |
|
|
} |
|
281 |
|
|
282 |
|
my $text = shift || return; |
283 |
|
from_to($text, $self->{encoding}, 'UTF-8'); |
284 |
|
return $text; |
285 |
} |
} |
286 |
|
|
287 |
=head2 est_ua |
=head2 finish |
288 |
|
|
289 |
This is helper function to create C<LWP::UserAgent> object with Super User |
Dump attributes used on disk |
|
priviledges |
|
|
|
|
|
my $ua = $self->est_ua( user => 'admin', passwd => 'admin' ); |
|
290 |
|
|
291 |
=cut |
=cut |
292 |
|
|
293 |
|
sub finish { |
|
|
|
|
sub est_ua { |
|
294 |
my $self = shift; |
my $self = shift; |
295 |
|
my $log = $self->_get_logger(); |
296 |
|
|
297 |
return $self->{_master_ua} if ($self->{_master_ua}); |
my $path = 'var/estraier/' . $self->{database} . '.yaml'; |
298 |
|
YAML::DumpFile( $path, $self->{stats} ); |
299 |
{ |
$log->info("created $path ", -s $path, " bytes"); |
300 |
package AdminUserAgent; |
$log->debug( dump( $self->{stats} ) ); |
|
use base qw/LWP::UserAgent/; |
|
|
sub new { |
|
|
my $self = LWP::UserAgent::new(@_); |
|
|
$self->agent("webpac/$VERSION"); |
|
|
$self; |
|
|
} |
|
|
sub get_basic_credentials { |
|
|
my($self, $realm, $uri) = @_; |
|
|
return ($self->{user}, $self->{passwd}); |
|
|
} |
|
|
sub set_basic_credentials { |
|
|
my ($self, $user, $passwd) = @_; |
|
|
$self->{user} = $user; |
|
|
$self->{passwd} = $passwd; |
|
|
} |
|
|
}; |
|
|
|
|
|
$self->{_master_ua} = AdminUserAgent->new( ) || sub { |
|
|
my $log = $self->_get_logger; |
|
|
$log->logdie("can't create LWP::UserAgent: $!"); |
|
|
}; |
|
|
|
|
|
$self->{_master_ua}->set_basic_credentials($self->{user}, $self->{passwd}); |
|
|
|
|
|
return $self->{_master_ua}; |
|
301 |
} |
} |
302 |
|
|
303 |
=head1 AUTHOR |
=head1 AUTHOR |