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; |
12 |
|
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.01 |
Version 0.12 |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
our $VERSION = '0.01'; |
our $VERSION = '0.12'; |
26 |
|
|
27 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
28 |
|
|
36 |
Connect to Hyper Estraier index using HTTP |
Connect to Hyper Estraier index using HTTP |
37 |
|
|
38 |
my $est = new WebPAC::Output::Estraier( |
my $est = new WebPAC::Output::Estraier( |
39 |
url => 'http://localhost:1978/node/webpac2', |
masterurl => 'http://localhost:1978/', |
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: |
49 |
|
|
50 |
=over 4 |
=over 4 |
51 |
|
|
52 |
=item url |
=item masterurl |
53 |
|
|
54 |
URI to C<estmaster> node |
URI to C<estmaster> node |
55 |
|
|
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 |
foreach my $p (qw/url user passwd/) { |
#$log->debug("self: ", sub { Dumper($self) }); |
92 |
|
|
93 |
|
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 |
$log->info("opening Hyper Estraier index $self->{'url'}"); |
$self->{encoding} ||= 'ISO-8859-2'; |
98 |
|
|
99 |
$self->{'db'} = HyperEstraier::Node->new($self->{'url'}); |
my $url = $self->{masterurl} . '/node/' . $self->{database}; |
100 |
$self->{'db'}->set_auth($self->{'user'}, $self->{'passwd'}); |
$self->{url} = $url; |
101 |
|
|
102 |
my $encoding = $self->{'encoding'} || 'ISO-8859-2'; |
$self->{label} ||= "WebPAC $self->{database}"; |
|
$log->info("using encoding $encoding"); |
|
103 |
|
|
104 |
$self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or |
$self->{db} = Search::Estraier::Node->new( |
105 |
$log->die("can't create conversion from $encoding to UTF-8"); |
url => $url, |
106 |
|
user => $self->{user}, |
107 |
|
passwd => $self->{passwd}, |
108 |
|
debug => $self->{debug}, |
109 |
|
create => 1, |
110 |
|
label => $self->convert( $self->{label} ), |
111 |
|
); |
112 |
|
|
113 |
|
$log->info("using ", $self->{clean} ? "new " : "", "index $self->{url} '$self->{label}' with encoding $self->{encoding}"); |
114 |
|
|
115 |
|
if ($self->{clean}) { |
116 |
|
$log->debug("clean $self->{database}"); |
117 |
|
$self->master( action => 'nodeclr', name => $self->{database} ); |
118 |
|
} else { |
119 |
|
$log->debug("opening index $self->{url}"); |
120 |
|
} |
121 |
|
|
122 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
123 |
} |
} |
131 |
id => 42, |
id => 42, |
132 |
ds => $ds, |
ds => $ds, |
133 |
type => 'display', |
type => 'display', |
|
url_prefix => 'database name', |
|
134 |
text => 'optional text from which snippet is created', |
text => 'optional text from which snippet is created', |
135 |
); |
); |
136 |
|
|
137 |
This function will create entries in index using following URI format: |
This function will create entries in index using following URI format: |
138 |
|
|
139 |
C<file:///database%20name/000> |
C<file:///type/database%20name/000> |
140 |
|
|
141 |
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 |
142 |
attribute and corresponding hidden text (used for search). |
attribute and corresponding hidden text (used for search). |
146 |
sub add { |
sub add { |
147 |
my $self = shift; |
my $self = shift; |
148 |
|
|
149 |
my $args = {@_}; |
my $args = {@_}; |
150 |
|
|
151 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
152 |
|
|
158 |
} |
} |
159 |
|
|
160 |
my $type = $args->{'type'}; |
my $type = $args->{'type'}; |
161 |
my $mfn = $args->{'id'}; |
my $id = $args->{'id'}; |
162 |
|
|
163 |
my $uri = "file:///$type/$database/$mfn"; |
my $uri = "file:///$type/$database/$id"; |
164 |
$log->debug("creating $uri"); |
$log->debug("creating $uri"); |
165 |
|
|
166 |
my $doc = HyperEstraier::Document->new; |
my $doc = Search::Estraier::Document->new; |
167 |
$doc->add_attr('@uri', $self->{'iconv'}->convert($uri) ); |
$doc->add_attr('@uri', $self->convert($uri) ); |
168 |
|
|
169 |
$log->debug("ds = ", sub { Dumper($args->{'ds'}) } ); |
$log->debug("ds = ", sub { Dumper($args->{'ds'}) } ); |
170 |
|
|
171 |
# filter all tags which have type defined |
# filter all tags which have type defined |
172 |
my @tags = grep { |
my @tags = grep { |
173 |
defined( $args->{'ds'}->{$_}->{$type} ) |
ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} ) |
174 |
} keys %{ $args->{'ds'} }; |
} keys %{ $args->{'ds'} }; |
175 |
|
|
176 |
$log->debug("tags = ", join(",", @tags)); |
$log->debug("tags = ", join(",", @tags)); |
181 |
|
|
182 |
my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} }); |
my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} }); |
183 |
|
|
184 |
$log->logconfess("no values for $tag/$type") unless ($vals); |
next if (! $vals); |
185 |
|
|
186 |
$vals = $self->{'iconv'}->convert( $vals ) or |
$vals = $self->convert( $vals ) or |
187 |
$log->die("can't convert '$vals' to UTF-8"); |
$log->logdie("can't convert '$vals' to UTF-8"); |
188 |
|
|
189 |
$doc->add_attr( $tag, $vals ); |
$doc->add_attr( $tag, $vals ); |
190 |
$doc->add_hidden_text( $vals ); |
$doc->add_hidden_text( $vals ); |
192 |
|
|
193 |
my $text = $args->{'text'}; |
my $text = $args->{'text'}; |
194 |
if ( $text ) { |
if ( $text ) { |
195 |
$text = $self->{'iconv'}->convert( $text ) or |
$text = $self->convert( $text ) or |
196 |
$log->die("can't convert '$text' to UTF-8"); |
$log->logdie("can't convert '$text' to UTF-8"); |
197 |
$doc->add_text( $text ); |
$doc->add_text( $text ); |
198 |
} |
} |
199 |
|
|
200 |
$log->debug("adding ", sub { $doc->dump_draft } ); |
$log->debug("adding ", sub { $doc->dump_draft } ); |
201 |
$self->{'db'}->put_doc($doc) || $log->die("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()); |
202 |
|
|
203 |
return 1; |
return 1; |
204 |
} |
} |
205 |
|
|
206 |
|
=head2 add_link |
207 |
|
|
208 |
|
$est->add_link( |
209 |
|
from => 'ps', |
210 |
|
to => 'webpac2', |
211 |
|
credit => 10000, |
212 |
|
); |
213 |
|
|
214 |
|
=cut |
215 |
|
|
216 |
|
sub add_link { |
217 |
|
my $self = shift; |
218 |
|
|
219 |
|
my $args = {@_}; |
220 |
|
my $log = $self->_get_logger; |
221 |
|
|
222 |
|
foreach my $p (qw/from to credit/) { |
223 |
|
$log->logdie("need $p") unless ($args->{$p}); |
224 |
|
} |
225 |
|
|
226 |
|
my $node = first { $_->{name} eq $args->{to} } $self->master( action => 'nodelist' ); |
227 |
|
|
228 |
|
if (! $node) { |
229 |
|
$log->warn("can't find node $args->{to}, skipping link creaton"); |
230 |
|
return; |
231 |
|
} |
232 |
|
|
233 |
|
my $label = $node->{label}; |
234 |
|
|
235 |
|
if (! $label) { |
236 |
|
$log->warn("can't find label for $args->{to}, skipping link creaton"); |
237 |
|
return; |
238 |
|
} |
239 |
|
|
240 |
|
$log->debug("using label $label for $args->{to}"); |
241 |
|
|
242 |
|
return $self->{db}->set_link( |
243 |
|
$self->{masterurl} . '/node/' . $args->{to}, |
244 |
|
$label, |
245 |
|
$args->{credit}, |
246 |
|
); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
=head2 master |
251 |
|
|
252 |
|
Issue administrative commands to C<estmaster> process. See documentation for |
253 |
|
C<master> in L<Search::Estraier>::Node. |
254 |
|
|
255 |
|
$self->master( |
256 |
|
action => 'nodeclr', |
257 |
|
name => 'foobar', |
258 |
|
); |
259 |
|
|
260 |
|
=cut |
261 |
|
|
262 |
|
sub master { |
263 |
|
my $self = shift; |
264 |
|
$self->{db}->master( @_ ); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
=head2 convert |
269 |
|
|
270 |
|
my $utf8_string = $self->convert('string in codepage'); |
271 |
|
|
272 |
|
=cut |
273 |
|
|
274 |
|
sub convert { |
275 |
|
my $self = shift; |
276 |
|
|
277 |
|
my $text = shift || return; |
278 |
|
from_to($text, $self->{encoding}, 'UTF-8'); |
279 |
|
return $text; |
280 |
|
} |
281 |
|
|
282 |
=head1 AUTHOR |
=head1 AUTHOR |
283 |
|
|
284 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |