5 |
|
|
6 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common/; |
7 |
|
|
8 |
use HyperEstraier; |
use Search::Estraier 0.06; |
9 |
use Text::Iconv; |
use Encode qw/from_to/; |
10 |
use Data::Dumper; |
use Data::Dumper; |
11 |
use LWP::Simple; |
use LWP; |
12 |
use URI::Escape; |
use URI::Escape; |
13 |
|
use List::Util qw/first/; |
14 |
|
|
15 |
=head1 NAME |
=head1 NAME |
16 |
|
|
18 |
|
|
19 |
=head1 VERSION |
=head1 VERSION |
20 |
|
|
21 |
Version 0.02 |
Version 0.11 |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
our $VERSION = '0.02'; |
our $VERSION = '0.11'; |
26 |
|
|
27 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
28 |
|
|
40 |
user => 'admin', |
user => 'admin', |
41 |
passwd => 'admin', |
passwd => 'admin', |
42 |
database => 'demo', |
database => 'demo', |
43 |
|
label => 'node label', |
44 |
encoding => 'iso-8859-2', |
encoding => 'iso-8859-2', |
45 |
|
clean => 1, |
46 |
); |
); |
47 |
|
|
48 |
Options are: |
Options are: |
65 |
|
|
66 |
name of database from which data comes |
name of database from which data comes |
67 |
|
|
68 |
|
=item label |
69 |
|
|
70 |
|
label for node (optional) |
71 |
|
|
72 |
=item encoding |
=item encoding |
73 |
|
|
74 |
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> |
83 |
|
|
84 |
sub new { |
sub new { |
85 |
my $class = shift; |
my $class = shift; |
86 |
my $self = {@_}; |
my $self = {@_}; |
87 |
bless($self, $class); |
bless($self, $class); |
88 |
|
|
89 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
90 |
|
|
91 |
$log->debug("self: ", sub { Dumper($self) }); |
#$log->debug("self: ", sub { Dumper($self) }); |
92 |
|
|
93 |
foreach my $p (qw/masterurl user passwd database/) { |
foreach my $p (qw/masterurl user passwd database/) { |
94 |
$log->logdie("need $p") unless ($self->{$p}); |
$log->logdie("need $p") unless ($self->{$p}); |
95 |
} |
} |
96 |
|
|
97 |
|
$self->{encoding} ||= 'ISO-8859-2'; |
98 |
|
|
99 |
my $url = $self->{masterurl} . '/node/' . $self->{database}; |
my $url = $self->{masterurl} . '/node/' . $self->{database}; |
|
$url =~ s#//#/#g; |
|
100 |
$self->{url} = $url; |
$self->{url} = $url; |
101 |
|
|
102 |
$log->info("opening Hyper Estraier index $self->{url}"); |
$self->{db} = Search::Estraier::Node->new( |
103 |
|
url => $url, |
104 |
my @nodes = $self->est_master( action => 'nodelist' ); |
user => $self->{user}, |
105 |
|
passwd => $self->{passwd}, |
106 |
if (! grep(/$self->{database}/, @nodes)) { |
debug => $self->{debug}, |
107 |
$log->info("creating index $url"); |
create => 1, |
108 |
$self->est_master( |
label => "WebPAC $self->{database}", |
109 |
action => 'nodeadd', |
); |
110 |
name => $self->{database}, |
|
111 |
label => "WebPAC $self->{database}", |
$log->info("using ", $self->{clean} ? "new " : "", "index $self->{url} with encoding $self->{encoding}"); |
112 |
) || $log->logdie("can't create Hyper Estraier node $self->{database}"); |
|
113 |
|
if ($self->{clean}) { |
114 |
|
$log->debug("clean $self->{database}"); |
115 |
|
$self->master( action => 'nodeclr', name => $self->{database} ); |
116 |
|
} else { |
117 |
|
$log->debug("opening index $self->{url}"); |
118 |
} |
} |
119 |
|
|
|
$self->{'db'} = HyperEstraier::Node->new($self->{url}); |
|
|
$self->{'db'}->set_auth($self->{'user'}, $self->{passwd}); |
|
|
|
|
|
my $encoding = $self->{'encoding'} || 'ISO-8859-2'; |
|
|
$log->info("using encoding $encoding"); |
|
|
|
|
|
$self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or |
|
|
$log->logdie("can't create conversion from $encoding to UTF-8"); |
|
|
|
|
120 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
121 |
} |
} |
122 |
|
|
129 |
id => 42, |
id => 42, |
130 |
ds => $ds, |
ds => $ds, |
131 |
type => 'display', |
type => 'display', |
|
url_prefix => 'database name', |
|
132 |
text => 'optional text from which snippet is created', |
text => 'optional text from which snippet is created', |
133 |
); |
); |
134 |
|
|
135 |
This function will create entries in index using following URI format: |
This function will create entries in index using following URI format: |
136 |
|
|
137 |
C<file:///database%20name/000> |
C<file:///type/database%20name/000> |
138 |
|
|
139 |
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 |
140 |
attribute and corresponding hidden text (used for search). |
attribute and corresponding hidden text (used for search). |
144 |
sub add { |
sub add { |
145 |
my $self = shift; |
my $self = shift; |
146 |
|
|
147 |
my $args = {@_}; |
my $args = {@_}; |
148 |
|
|
149 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
150 |
|
|
156 |
} |
} |
157 |
|
|
158 |
my $type = $args->{'type'}; |
my $type = $args->{'type'}; |
159 |
my $mfn = $args->{'id'}; |
my $id = $args->{'id'}; |
160 |
|
|
161 |
my $uri = "file:///$type/$database/$mfn"; |
my $uri = "file:///$type/$database/$id"; |
162 |
$log->debug("creating $uri"); |
$log->debug("creating $uri"); |
163 |
|
|
164 |
my $doc = HyperEstraier::Document->new; |
my $doc = Search::Estraier::Document->new; |
165 |
$doc->add_attr('@uri', $self->{'iconv'}->convert($uri) ); |
$doc->add_attr('@uri', $self->convert($uri) ); |
166 |
|
|
167 |
$log->debug("ds = ", sub { Dumper($args->{'ds'}) } ); |
$log->debug("ds = ", sub { Dumper($args->{'ds'}) } ); |
168 |
|
|
179 |
|
|
180 |
my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} }); |
my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} }); |
181 |
|
|
182 |
$log->logconfess("no values for $tag/$type") unless ($vals); |
next if (! $vals); |
183 |
|
|
184 |
$vals = $self->{'iconv'}->convert( $vals ) or |
$vals = $self->convert( $vals ) or |
185 |
$log->logdie("can't convert '$vals' to UTF-8"); |
$log->logdie("can't convert '$vals' to UTF-8"); |
186 |
|
|
187 |
$doc->add_attr( $tag, $vals ); |
$doc->add_attr( $tag, $vals ); |
190 |
|
|
191 |
my $text = $args->{'text'}; |
my $text = $args->{'text'}; |
192 |
if ( $text ) { |
if ( $text ) { |
193 |
$text = $self->{'iconv'}->convert( $text ) or |
$text = $self->convert( $text ) or |
194 |
$log->logdie("can't convert '$text' to UTF-8"); |
$log->logdie("can't convert '$text' to UTF-8"); |
195 |
$doc->add_text( $text ); |
$doc->add_text( $text ); |
196 |
} |
} |
197 |
|
|
198 |
$log->debug("adding ", sub { $doc->dump_draft } ); |
$log->debug("adding ", sub { $doc->dump_draft } ); |
199 |
$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()); |
200 |
|
|
201 |
return 1; |
return 1; |
202 |
} |
} |
203 |
|
|
204 |
=head2 est_master |
=head2 add_link |
205 |
|
|
206 |
Issue administrative commands to C<estmaster> process and receive response |
$est->add_link( |
207 |
as array of lines |
from => 'ps', |
208 |
|
to => 'webpac2', |
209 |
my $nodelist = $self->est_master( action => nodelist ); |
credit => 10000, |
210 |
|
); |
211 |
|
|
212 |
=cut |
=cut |
213 |
|
|
214 |
my $estmaster_actions = { |
sub add_link { |
|
userdel => [ qw/name/ ], |
|
|
nodelist => [], |
|
|
nodeadd => [ qw/name label/ ], |
|
|
nodedel => [ qw/name/ ], |
|
|
}; |
|
|
|
|
|
sub est_master { |
|
215 |
my $self = shift; |
my $self = shift; |
216 |
|
|
217 |
my $args = {@_}; |
my $args = {@_}; |
218 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
219 |
|
|
220 |
$log->debug(Dumper($args)); |
my $node = first { $_->{name} eq $args->{to} } $self->master( action => 'nodelist' ); |
|
|
|
|
my $action = $args->{action} || $log->logconfess("no action specified"); |
|
221 |
|
|
222 |
$log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action}); |
if (! $node) { |
223 |
|
$log->warn("can't find node $args->{to}, skipping link creaton"); |
224 |
|
return; |
225 |
|
} |
226 |
|
|
227 |
my $url = $self->{masterurl} . '/master?action=' . $action; |
my $label = $node->{label}; |
228 |
|
|
229 |
foreach my $arg (@{ $estmaster_actions->{$action} }) { |
if (! $label) { |
230 |
$log->logdie("missing parametar $arg for action $action") unless ($args->{$arg}); |
$log->warn("can't find label for $args->{to}, skipping link creaton"); |
231 |
$url .= '&' . $arg . '=' . uri_escape( $args->{$arg} ); |
return; |
232 |
} |
} |
233 |
|
|
234 |
$log->debug("calling $url"); |
$log->debug("using label $label for $args->{to}"); |
235 |
|
|
236 |
|
return $self->{db}->set_link( |
237 |
|
$self->{masterurl} . '/node/' . $args->{to}, |
238 |
|
$label, |
239 |
|
$args->{credit}, |
240 |
|
); |
241 |
|
} |
242 |
|
|
|
my $tsv = get($url); |
|
243 |
|
|
244 |
if (! $tsv) { |
=head2 master |
245 |
$log->warn("unable to call $url"); |
|
246 |
return; |
Issue administrative commands to C<estmaster> process. See documentation for |
247 |
} |
C<master> in L<Search::Estraier>::Node. |
248 |
|
|
249 |
|
$self->master( |
250 |
|
action => 'nodeclr', |
251 |
|
name => 'foobar', |
252 |
|
); |
253 |
|
|
254 |
|
=cut |
255 |
|
|
256 |
|
sub master { |
257 |
|
my $self = shift; |
258 |
|
$self->{db}->master( @_ ); |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
|
=head2 convert |
263 |
|
|
264 |
|
my $utf8_string = $self->convert('string in codepage'); |
265 |
|
|
266 |
|
=cut |
267 |
|
|
268 |
|
sub convert { |
269 |
|
my $self = shift; |
270 |
|
|
271 |
return split(/\n/, $tsv); |
my $text = shift || return; |
272 |
|
from_to($text, $self->{encoding}, 'UTF-8'); |
273 |
|
return $text; |
274 |
} |
} |
275 |
|
|
276 |
=head1 AUTHOR |
=head1 AUTHOR |