r8988@llin: dpavlin | 2005-11-20 20:46:12 +0100
[webpac2] / lib / WebPAC / Output / Estraier.pm
1 package WebPAC::Output::Estraier;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common/;
7
8 use HyperEstraier;
9 use Text::Iconv;
10 use Data::Dumper;
11
12 =head1 NAME
13
14 WebPAC::Output::Estraier - Create Hyper Estraier full text index
15
16 =head1 VERSION
17
18 Version 0.01
19
20 =cut
21
22 our $VERSION = '0.01';
23
24 =head1 SYNOPSIS
25
26 Create full text index using Hyper Estraier index from data with
27 type C<search>.
28
29 =head1 FUNCTIONS
30
31 =head2 new
32
33 Connect to Hyper Estraier index using HTTP
34
35  my $est = new WebPAC::Output::Estraier(
36         url => 'http://localhost:1978/node/webpac2',
37         user => 'admin',
38         passwd => 'admin',
39         database => 'demo',
40         encoding => 'iso-8859-2',
41  );
42
43 Options are:
44
45 =over 4
46
47 =item url
48
49 URI to C<estmaster> node
50
51 =item user
52
53 C<estmaster> user with sufficient rights
54
55 =item passwd
56
57 password for user
58
59 =item database
60
61 name of database from which data comes
62
63 =item encoding
64
65 character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>
66 (and it probably is). This encoding will be converted to C<UTF-8> for
67 Hyper Estraier.
68
69 =back
70
71 Name of database will be used to form URI of documents in index.
72
73 =cut
74
75 sub new {
76         my $class = shift;
77         my $self = {@_};
78         bless($self, $class);
79
80         my $log = $self->_get_logger;
81
82         foreach my $p (qw/url user passwd/) {
83                 $log->logdie("need $p") unless ($self->{$p});
84         }
85
86         $log->info("opening Hyper Estraier index $self->{'url'}");
87
88         $self->{'db'} = HyperEstraier::Node->new($self->{'url'});
89         $self->{'db'}->set_auth($self->{'user'}, $self->{'passwd'});
90
91         my $encoding = $self->{'encoding'} || 'ISO-8859-2';
92         $log->info("using encoding $encoding");
93
94         my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');
95
96         $self ? return $self : return undef;
97 }
98
99 =head2 add
100
101 Adds one entry to database.
102
103   $est->add(
104         id => 42,
105         ds => $ds,
106         type => 'display',
107         url_prefix => 'database name',
108         text => 'optional text from which snippet is created',
109   );
110
111 This function will create  entries in index using following URI format:
112
113   C<file:///database%20name/000>
114
115 Each tag in C<data_structure> with specified C<type> will create one
116 attribute and corresponding hidden text (used for search).
117
118 =cut
119
120 sub add {
121         my $self = shift;
122
123         my $args = {@_};
124
125         my $log = $self->_get_logger;
126
127         my $database = $self->{'database'} || $log->logconfess('no database in $self');
128         $log->logconfess('need db in object') unless ($self->{'db'});
129
130         foreach my $p (qw/id ds type/) {
131                 $log->logdie("need $p") unless ($args->{$p});
132         }
133
134         my $type = $args->{'type'};
135         my $mfn = $args->{'id'};
136
137         my $uri = "file:///$type/$database/$mfn";
138         $log->debug("creating $uri");
139
140         my $doc = HyperEstraier::Document->new;
141         $doc->add_attr('@uri', $uri);
142
143         $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
144
145         # filter all tags which have type defined
146         my @tags = grep {
147                 defined( $args->{'ds'}->{$_}->{$type} )
148         } keys %{ $args->{'ds'} };
149
150         $log->debug("tags = ", join(",", @tags));
151
152         return unless (@tags);
153
154         foreach my $tag (@tags) {
155
156                 my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
157
158                 $log->logconfess("no values for $tag/$type") unless ($vals);
159
160                 $doc->add_attr($tag, $vals);
161                 $doc->add_hidden_text($vals);
162         }
163
164         my $text = $args->{'text'};
165         $doc->add_text( $text ) if ( $text );
166
167         $log->debug("adding ", sub { $doc->dump_draft } );
168         $self->{'db'}->put_doc($doc) || $log->die("can't add document $uri to index");
169
170         return 1;
171 }
172
173 =head1 AUTHOR
174
175 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
176
177 =head1 COPYRIGHT & LICENSE
178
179 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
180
181 This program is free software; you can redistribute it and/or modify it
182 under the same terms as Perl itself.
183
184 =cut
185
186 1; # End of WebPAC::Output::Estraier