1 package WebPAC::Output::Estraier;
6 use base qw/WebPAC::Common/;
16 WebPAC::Output::Estraier - Create Hyper Estraier full text index
24 our $VERSION = '0.03';
28 Create full text index using Hyper Estraier index from data with
35 Connect to Hyper Estraier index using HTTP
37 my $est = new WebPAC::Output::Estraier(
38 masterurl => 'http://localhost:1978/',
42 encoding => 'iso-8859-2',
51 URI to C<estmaster> node
55 C<estmaster> user with sufficient rights
63 name of database from which data comes
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
73 Name of database will be used to form URI of documents in index.
82 my $log = $self->_get_logger;
84 #$log->debug("self: ", sub { Dumper($self) });
86 foreach my $p (qw/masterurl user passwd database/) {
87 $log->logdie("need $p") unless ($self->{$p});
90 my $url = $self->{masterurl} . '/node/' . $self->{database};
94 $log->info("opening Hyper Estraier index $self->{url}");
96 my $nodes = $self->est_master( action => 'nodelist' );
98 $log->debug("nodes found: $nodes");
100 if ($nodes !~ m/^$self->{database}\t/sm) {
101 $log->info("creating index $url");
104 name => $self->{database},
105 label => "WebPAC $self->{database}",
106 ) || $log->logdie("can't create Hyper Estraier node $self->{database}");
109 $self->{'db'} = HyperEstraier::Node->new($self->{url});
110 $self->{'db'}->set_auth($self->{'user'}, $self->{passwd});
112 my $encoding = $self->{'encoding'} || 'ISO-8859-2';
113 $log->info("using encoding $encoding");
115 $self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or
116 $log->logdie("can't create conversion from $encoding to UTF-8");
118 $self ? return $self : return undef;
124 Adds one entry to database.
130 url_prefix => 'database name',
131 text => 'optional text from which snippet is created',
134 This function will create entries in index using following URI format:
136 C<file:///database%20name/000>
138 Each tag in C<data_structure> with specified C<type> will create one
139 attribute and corresponding hidden text (used for search).
148 my $log = $self->_get_logger;
150 my $database = $self->{'database'} || $log->logconfess('no database in $self');
151 $log->logconfess('need db in object') unless ($self->{'db'});
153 foreach my $p (qw/id ds type/) {
154 $log->logdie("need $p") unless ($args->{$p});
157 my $type = $args->{'type'};
158 my $mfn = $args->{'id'};
160 my $uri = "file:///$type/$database/$mfn";
161 $log->debug("creating $uri");
163 my $doc = HyperEstraier::Document->new;
164 $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );
166 $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
168 # filter all tags which have type defined
170 ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} )
171 } keys %{ $args->{'ds'} };
173 $log->debug("tags = ", join(",", @tags));
175 return unless (@tags);
177 foreach my $tag (@tags) {
179 my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
181 $log->logconfess("no values for $tag/$type") unless ($vals);
183 $vals = $self->{'iconv'}->convert( $vals ) or
184 $log->logdie("can't convert '$vals' to UTF-8");
186 $doc->add_attr( $tag, $vals );
187 $doc->add_hidden_text( $vals );
190 my $text = $args->{'text'};
192 $text = $self->{'iconv'}->convert( $text ) or
193 $log->logdie("can't convert '$text' to UTF-8");
194 $doc->add_text( $text );
197 $log->debug("adding ", sub { $doc->dump_draft } );
198 $self->{'db'}->put_doc($doc) || $log->logdie("can't add document $uri to index");
205 Issue administrative commands to C<estmaster> process and receive response
208 my $nodelist = $self->est_master( action => nodelist );
212 my $estmaster_actions = {
213 userdel => [ qw/name/ ],
215 nodeadd => [ qw/name label/ ],
216 nodedel => [ qw/name/ ],
222 my $log = $self->_get_logger;
224 $log->debug(Dumper($args));
226 my $action = $args->{action} || $log->logconfess("no action specified");
228 $log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action});
230 my $url = $self->{masterurl} . '/master?action=' . $action;
232 foreach my $arg (@{ $estmaster_actions->{$action} }) {
233 $log->logdie("missing parametar $arg for action $action") unless ($args->{$arg});
234 $url .= '&' . $arg . '=' . uri_escape( $args->{$arg} );
237 $log->debug("calling $url");
239 if (! $self->{_master_ua}) {
240 $self->{_master_ua} = LWP::UserAgent->new( ) || $log->logdie("can't create LWP::UserAgent: $!");
241 $self->{_master_ua}->credentials('localhost:1978','Super User', $self->{user} => $self->{passwd});
244 my $res = $self->{_master_ua}->get($url);
246 if ($res->is_success) {
247 #$log->debug( $res->content );
248 return split(/\n/, $res->content) if wantarray;
249 return $res->content;
251 $log->warn("unable to call $url: " . $res->status_line);
252 #$log->debug(Dumper($res, $self->{'_master_ua'}));
260 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
262 =head1 COPYRIGHT & LICENSE
264 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
266 This program is free software; you can redistribute it and/or modify it
267 under the same terms as Perl itself.
271 1; # End of WebPAC::Output::Estraier