1 |
dpavlin |
1 |
package WebPAC::Output::Estraier; |
2 |
|
|
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
dpavlin |
74 |
use base qw/WebPAC::Common/; |
7 |
|
|
|
8 |
|
|
use HyperEstraier; |
9 |
|
|
use Text::Iconv; |
10 |
|
|
use Data::Dumper; |
11 |
dpavlin |
211 |
use LWP; |
12 |
dpavlin |
210 |
use URI::Escape; |
13 |
dpavlin |
74 |
|
14 |
dpavlin |
1 |
=head1 NAME |
15 |
|
|
|
16 |
dpavlin |
74 |
WebPAC::Output::Estraier - Create Hyper Estraier full text index |
17 |
dpavlin |
1 |
|
18 |
|
|
=head1 VERSION |
19 |
|
|
|
20 |
dpavlin |
245 |
Version 0.04 |
21 |
dpavlin |
1 |
|
22 |
|
|
=cut |
23 |
|
|
|
24 |
dpavlin |
245 |
our $VERSION = '0.04'; |
25 |
dpavlin |
1 |
|
26 |
|
|
=head1 SYNOPSIS |
27 |
|
|
|
28 |
dpavlin |
74 |
Create full text index using Hyper Estraier index from data with |
29 |
|
|
type C<search>. |
30 |
dpavlin |
1 |
|
31 |
dpavlin |
74 |
=head1 FUNCTIONS |
32 |
dpavlin |
1 |
|
33 |
dpavlin |
74 |
=head2 new |
34 |
dpavlin |
1 |
|
35 |
dpavlin |
74 |
Connect to Hyper Estraier index using HTTP |
36 |
dpavlin |
1 |
|
37 |
dpavlin |
74 |
my $est = new WebPAC::Output::Estraier( |
38 |
dpavlin |
210 |
masterurl => 'http://localhost:1978/', |
39 |
dpavlin |
74 |
user => 'admin', |
40 |
|
|
passwd => 'admin', |
41 |
|
|
database => 'demo', |
42 |
|
|
encoding => 'iso-8859-2', |
43 |
|
|
); |
44 |
dpavlin |
1 |
|
45 |
dpavlin |
74 |
Options are: |
46 |
dpavlin |
1 |
|
47 |
dpavlin |
74 |
=over 4 |
48 |
dpavlin |
1 |
|
49 |
dpavlin |
210 |
=item masterurl |
50 |
dpavlin |
1 |
|
51 |
dpavlin |
74 |
URI to C<estmaster> node |
52 |
|
|
|
53 |
|
|
=item user |
54 |
|
|
|
55 |
|
|
C<estmaster> user with sufficient rights |
56 |
|
|
|
57 |
|
|
=item passwd |
58 |
|
|
|
59 |
|
|
password for user |
60 |
|
|
|
61 |
|
|
=item database |
62 |
|
|
|
63 |
|
|
name of database from which data comes |
64 |
|
|
|
65 |
|
|
=item encoding |
66 |
|
|
|
67 |
|
|
character encoding of C<data_structure> if it's differenet than C<ISO-8859-2> |
68 |
|
|
(and it probably is). This encoding will be converted to C<UTF-8> for |
69 |
|
|
Hyper Estraier. |
70 |
|
|
|
71 |
|
|
=back |
72 |
|
|
|
73 |
|
|
Name of database will be used to form URI of documents in index. |
74 |
|
|
|
75 |
dpavlin |
1 |
=cut |
76 |
|
|
|
77 |
dpavlin |
74 |
sub new { |
78 |
|
|
my $class = shift; |
79 |
|
|
my $self = {@_}; |
80 |
|
|
bless($self, $class); |
81 |
|
|
|
82 |
|
|
my $log = $self->_get_logger; |
83 |
|
|
|
84 |
dpavlin |
211 |
#$log->debug("self: ", sub { Dumper($self) }); |
85 |
dpavlin |
210 |
|
86 |
|
|
foreach my $p (qw/masterurl user passwd database/) { |
87 |
dpavlin |
74 |
$log->logdie("need $p") unless ($self->{$p}); |
88 |
|
|
} |
89 |
|
|
|
90 |
dpavlin |
210 |
my $url = $self->{masterurl} . '/node/' . $self->{database}; |
91 |
|
|
$self->{url} = $url; |
92 |
dpavlin |
74 |
|
93 |
dpavlin |
210 |
$log->info("opening Hyper Estraier index $self->{url}"); |
94 |
dpavlin |
74 |
|
95 |
dpavlin |
211 |
my $nodes = $self->est_master( action => 'nodelist' ); |
96 |
dpavlin |
210 |
|
97 |
dpavlin |
211 |
$log->debug("nodes found: $nodes"); |
98 |
|
|
|
99 |
|
|
if ($nodes !~ m/^$self->{database}\t/sm) { |
100 |
dpavlin |
210 |
$log->info("creating index $url"); |
101 |
|
|
$self->est_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}); |
109 |
|
|
$self->{'db'}->set_auth($self->{'user'}, $self->{passwd}); |
110 |
|
|
|
111 |
dpavlin |
74 |
my $encoding = $self->{'encoding'} || 'ISO-8859-2'; |
112 |
|
|
$log->info("using encoding $encoding"); |
113 |
|
|
|
114 |
dpavlin |
75 |
$self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or |
115 |
dpavlin |
85 |
$log->logdie("can't create conversion from $encoding to UTF-8"); |
116 |
dpavlin |
74 |
|
117 |
|
|
$self ? return $self : return undef; |
118 |
dpavlin |
1 |
} |
119 |
|
|
|
120 |
dpavlin |
75 |
|
121 |
dpavlin |
74 |
=head2 add |
122 |
dpavlin |
1 |
|
123 |
dpavlin |
74 |
Adds one entry to database. |
124 |
|
|
|
125 |
|
|
$est->add( |
126 |
|
|
id => 42, |
127 |
|
|
ds => $ds, |
128 |
|
|
type => 'display', |
129 |
|
|
text => 'optional text from which snippet is created', |
130 |
|
|
); |
131 |
|
|
|
132 |
|
|
This function will create entries in index using following URI format: |
133 |
|
|
|
134 |
dpavlin |
212 |
C<file:///type/database%20name/000> |
135 |
dpavlin |
74 |
|
136 |
|
|
Each tag in C<data_structure> with specified C<type> will create one |
137 |
|
|
attribute and corresponding hidden text (used for search). |
138 |
|
|
|
139 |
dpavlin |
1 |
=cut |
140 |
|
|
|
141 |
dpavlin |
74 |
sub add { |
142 |
|
|
my $self = shift; |
143 |
|
|
|
144 |
|
|
my $args = {@_}; |
145 |
|
|
|
146 |
|
|
my $log = $self->_get_logger; |
147 |
|
|
|
148 |
|
|
my $database = $self->{'database'} || $log->logconfess('no database in $self'); |
149 |
|
|
$log->logconfess('need db in object') unless ($self->{'db'}); |
150 |
|
|
|
151 |
|
|
foreach my $p (qw/id ds type/) { |
152 |
|
|
$log->logdie("need $p") unless ($args->{$p}); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
my $type = $args->{'type'}; |
156 |
dpavlin |
213 |
my $id = $args->{'id'}; |
157 |
dpavlin |
74 |
|
158 |
dpavlin |
213 |
my $uri = "file:///$type/$database/$id"; |
159 |
dpavlin |
74 |
$log->debug("creating $uri"); |
160 |
|
|
|
161 |
|
|
my $doc = HyperEstraier::Document->new; |
162 |
dpavlin |
75 |
$doc->add_attr('@uri', $self->{'iconv'}->convert($uri) ); |
163 |
dpavlin |
74 |
|
164 |
|
|
$log->debug("ds = ", sub { Dumper($args->{'ds'}) } ); |
165 |
|
|
|
166 |
|
|
# filter all tags which have type defined |
167 |
|
|
my @tags = grep { |
168 |
dpavlin |
113 |
ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} ) |
169 |
dpavlin |
74 |
} keys %{ $args->{'ds'} }; |
170 |
|
|
|
171 |
|
|
$log->debug("tags = ", join(",", @tags)); |
172 |
|
|
|
173 |
|
|
return unless (@tags); |
174 |
|
|
|
175 |
|
|
foreach my $tag (@tags) { |
176 |
|
|
|
177 |
|
|
my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} }); |
178 |
|
|
|
179 |
|
|
$log->logconfess("no values for $tag/$type") unless ($vals); |
180 |
|
|
|
181 |
dpavlin |
75 |
$vals = $self->{'iconv'}->convert( $vals ) or |
182 |
dpavlin |
85 |
$log->logdie("can't convert '$vals' to UTF-8"); |
183 |
dpavlin |
75 |
|
184 |
|
|
$doc->add_attr( $tag, $vals ); |
185 |
|
|
$doc->add_hidden_text( $vals ); |
186 |
dpavlin |
74 |
} |
187 |
|
|
|
188 |
|
|
my $text = $args->{'text'}; |
189 |
dpavlin |
75 |
if ( $text ) { |
190 |
|
|
$text = $self->{'iconv'}->convert( $text ) or |
191 |
dpavlin |
85 |
$log->logdie("can't convert '$text' to UTF-8"); |
192 |
dpavlin |
75 |
$doc->add_text( $text ); |
193 |
|
|
} |
194 |
dpavlin |
74 |
|
195 |
|
|
$log->debug("adding ", sub { $doc->dump_draft } ); |
196 |
dpavlin |
212 |
$self->{'db'}->put_doc($doc) || $log->logdie("can't add document $uri to node " . $self->{url} . " status: " . $self->{db}->status()); |
197 |
dpavlin |
74 |
|
198 |
|
|
return 1; |
199 |
dpavlin |
1 |
} |
200 |
|
|
|
201 |
dpavlin |
210 |
=head2 est_master |
202 |
|
|
|
203 |
|
|
Issue administrative commands to C<estmaster> process and receive response |
204 |
|
|
as array of lines |
205 |
|
|
|
206 |
|
|
my $nodelist = $self->est_master( action => nodelist ); |
207 |
|
|
|
208 |
|
|
=cut |
209 |
|
|
|
210 |
|
|
my $estmaster_actions = { |
211 |
|
|
userdel => [ qw/name/ ], |
212 |
|
|
nodelist => [], |
213 |
|
|
nodeadd => [ qw/name label/ ], |
214 |
|
|
nodedel => [ qw/name/ ], |
215 |
|
|
}; |
216 |
|
|
|
217 |
dpavlin |
246 |
=head2 est_ua |
218 |
|
|
|
219 |
|
|
Make C<LWP::UserAgent> object with Super User priviledges |
220 |
|
|
|
221 |
|
|
my $ua = $self->est_ua( user => 'admin', passwd => 'admin' ); |
222 |
|
|
|
223 |
|
|
=cut |
224 |
|
|
|
225 |
dpavlin |
238 |
sub est_ua { |
226 |
|
|
my $self = shift; |
227 |
|
|
|
228 |
|
|
return $self->{_master_ua} if ($self->{_master_ua}); |
229 |
|
|
|
230 |
|
|
$self->{_master_ua} = LWP::UserAgent->new( ) || sub { |
231 |
|
|
my $log = $self->_get_logger; |
232 |
|
|
$log->logdie("can't create LWP::UserAgent: $!"); |
233 |
|
|
}; |
234 |
|
|
|
235 |
|
|
$self->{_master_ua}->credentials('localhost:1978','Super User', $self->{user} => $self->{passwd}); |
236 |
|
|
|
237 |
|
|
return $self->{_master_ua}; |
238 |
|
|
} |
239 |
|
|
|
240 |
dpavlin |
210 |
sub est_master { |
241 |
|
|
my $self = shift; |
242 |
|
|
my $args = {@_}; |
243 |
|
|
my $log = $self->_get_logger; |
244 |
|
|
|
245 |
|
|
$log->debug(Dumper($args)); |
246 |
|
|
|
247 |
|
|
my $action = $args->{action} || $log->logconfess("no action specified"); |
248 |
|
|
|
249 |
|
|
$log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action}); |
250 |
|
|
|
251 |
|
|
my $url = $self->{masterurl} . '/master?action=' . $action; |
252 |
|
|
|
253 |
|
|
foreach my $arg (@{ $estmaster_actions->{$action} }) { |
254 |
|
|
$log->logdie("missing parametar $arg for action $action") unless ($args->{$arg}); |
255 |
|
|
$url .= '&' . $arg . '=' . uri_escape( $args->{$arg} ); |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
$log->debug("calling $url"); |
259 |
|
|
|
260 |
dpavlin |
238 |
my $res = $self->est_ua()->get($url); |
261 |
dpavlin |
210 |
|
262 |
dpavlin |
211 |
if ($res->is_success) { |
263 |
|
|
#$log->debug( $res->content ); |
264 |
|
|
return split(/\n/, $res->content) if wantarray; |
265 |
|
|
return $res->content; |
266 |
|
|
} else { |
267 |
|
|
$log->warn("unable to call $url: " . $res->status_line); |
268 |
dpavlin |
210 |
return; |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
} |
272 |
|
|
|
273 |
dpavlin |
1 |
=head1 AUTHOR |
274 |
|
|
|
275 |
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
276 |
|
|
|
277 |
|
|
=head1 COPYRIGHT & LICENSE |
278 |
|
|
|
279 |
|
|
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
280 |
|
|
|
281 |
|
|
This program is free software; you can redistribute it and/or modify it |
282 |
|
|
under the same terms as Perl itself. |
283 |
|
|
|
284 |
|
|
=cut |
285 |
|
|
|
286 |
|
|
1; # End of WebPAC::Output::Estraier |