/[webpac2]/trunk/lib/WebPAC/Output/EstraierNative.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/WebPAC/Output/EstraierNative.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 674 - (hide annotations)
Tue Sep 12 14:47:25 2006 UTC (17 years, 8 months ago) by dpavlin
File size: 6198 byte(s)
 r947@llin:  dpavlin | 2006-09-12 16:45:55 +0200
 fixed indexing with EstraierNative (it will be used for every database, not
 just first one), and create separate indexes for each database (we'll merge them at end)

1 dpavlin 627 package WebPAC::Output::EstraierNative;
2    
3     use warnings;
4     use strict;
5    
6     use base qw/WebPAC::Common/;
7    
8     use Estraier;
9     use Encode qw/from_to/;
10     use Data::Dumper;
11     use LWP;
12     use URI::Escape;
13     use List::Util qw/first/;
14 dpavlin 673 use File::Path;
15 dpavlin 627
16     $Estraier::DEBUG = 1;
17    
18    
19     =head1 NAME
20    
21     WebPAC::Output::EstraierNative - Create Hyper Estraier full text index using native bindings
22    
23     =head1 VERSION
24    
25 dpavlin 673 Version 0.2
26 dpavlin 627
27     =cut
28    
29 dpavlin 673 our $VERSION = '0.2';
30 dpavlin 627
31     =head1 SYNOPSIS
32    
33     Create full text index using Hyper Estraier index from data with
34     type C<search>.
35    
36     =head1 FUNCTIONS
37    
38     =head2 new
39    
40     Connect to Hyper Estraier index using HTTP
41    
42     my $est = new WebPAC::Output::Estraier(
43     path => 'casket/',
44     database => 'demo',
45     label => 'node label',
46     encoding => 'iso-8859-2',
47     clean => 1,
48     );
49    
50     Options are:
51    
52     =over 4
53    
54     =item path
55    
56     full or relative path to Hyper Estraier database
57    
58     =item database
59    
60     name of database from which data comes
61    
62     =item label
63    
64     label for node (optional)
65    
66     =item encoding
67    
68     character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>
69     (and it probably is). This encoding will be converted to C<UTF-8> for
70     Hyper Estraier.
71    
72     =back
73    
74     Name of database will be used to form URI of documents in index.
75    
76     =cut
77    
78     sub new {
79     my $class = shift;
80     my $self = {@_};
81     bless($self, $class);
82    
83     my $log = $self->_get_logger;
84    
85     #$log->debug("self: ", sub { Dumper($self) });
86    
87     foreach my $p (qw/path database/) {
88     $log->logdie("need $p") unless ($self->{$p});
89     }
90    
91     $self->{encoding} ||= 'ISO-8859-2';
92    
93     $self->{label} ||= "WebPAC $self->{database}";
94    
95     my $path = 'casket';
96 dpavlin 673 $path =~ s!/+$!!;
97 dpavlin 627
98 dpavlin 674 $path .= '/' . $self->{database};
99 dpavlin 673
100 dpavlin 674 $self->{path} = $path;
101    
102 dpavlin 673 $path .= '.tmp';
103     if (-e $path) {
104     rmtree($path) || $log->logdie("can't remove old temporary directory $path: $!");
105     }
106     mkpath($path) || $log->logdie("can't create new temporary directory $path: $!");
107    
108 dpavlin 627 my $db = new Database();
109     unless($db->open($path, Database::DBWRITER | Database::DBCREAT)) {
110     $log->logdie("can't open $path: ", $db->err_msg($db->error()) );
111     }
112    
113     $self->{db} = $db;
114    
115     $log->info("using ", $self->{clean} ? "new " : "", "index $self->{path} '$self->{label}' with encoding $self->{encoding}");
116    
117     $self ? return $self : return undef;
118     }
119    
120    
121     =head2 add
122    
123     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     C<file:///type/database%20name/000>
135    
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     =cut
140    
141     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     my $id = $args->{'id'};
157    
158     my $uri = "file:///$type/$database/$id";
159     $log->debug("creating $uri");
160    
161     my $doc = new Document();
162     $doc->add_attr('@uri', $self->convert($uri) );
163    
164     # store type and database name
165     $doc->add_attr('_database', $database );
166     $doc->add_attr('_type', $type );
167    
168     $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
169    
170     # filter all tags which have type defined
171     my @tags = grep {
172     ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} )
173     } keys %{ $args->{'ds'} };
174    
175     $log->debug("tags = ", join(",", @tags));
176    
177     return unless (@tags);
178    
179     foreach my $tag (@tags) {
180    
181 dpavlin 630 $log->debug("$tag :: $type == ",Dumper( $args->{'ds'}->{$tag}->{$type} ) );
182    
183 dpavlin 627 my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
184    
185     next if (! $vals);
186    
187     $vals = join(" ") if (ref($vals) eq 'ARRAY');
188    
189     $vals = $self->convert( $vals ) or
190     $log->logdie("can't convert '$vals' to UTF-8");
191    
192     $doc->add_attr( $tag, $vals );
193     $doc->add_hidden_text( $vals );
194     }
195    
196     my $text = $args->{'text'};
197     if ( $text ) {
198     $text = $self->convert( $text ) or
199     $log->logdie("can't convert '$text' to UTF-8");
200     $doc->add_text( $text );
201     }
202    
203     $log->debug("adding ", sub { $doc->dump_draft } );
204     $self->{'db'}->put_doc($doc, Database::PDCLEAN) || $log->warn("can't add document $uri with draft " . $doc->dump_draft . " to node " . $self->{path} . " status: " . $self->{db}->status());
205    
206     return 1;
207     }
208    
209     =head2 add_link
210    
211     $est->add_link(
212     from => 'ps',
213     to => 'webpac2',
214     credit => 10000,
215     );
216    
217     =cut
218    
219     sub add_link {
220     my $self = shift;
221    
222     my $args = {@_};
223     my $log = $self->_get_logger;
224    
225     $log->warn("add_link is not implemented");
226     return;
227    
228     foreach my $p (qw/from to credit/) {
229     $log->logdie("need $p") unless ($args->{$p});
230     }
231    
232     my $node = first { $_->{name} eq $args->{to} } $self->master( action => 'nodelist' );
233    
234     if (! $node) {
235     $log->warn("can't find node $args->{to}, skipping link creaton");
236     return;
237     }
238    
239     my $label = $node->{label};
240    
241     if (! $label) {
242     $log->warn("can't find label for $args->{to}, skipping link creaton");
243     return;
244     }
245    
246     $log->debug("using label $label for $args->{to}");
247    
248     return $self->{db}->set_link(
249     $self->{masterurl} . '/node/' . $args->{to},
250     $label,
251     $args->{credit},
252     );
253     }
254    
255    
256 dpavlin 673 =head2 finish
257    
258     Close index and rename of to final path
259    
260     $est->finish;
261    
262     =cut
263    
264     sub finish {
265     my $self = shift;
266    
267     my $log = $self->_get_logger;
268     $log->info("closing Hyper Estraier index make it current...");
269    
270     $self->{db}->close || $log->logdie("can't close index");
271    
272 dpavlin 674 my $path = $self->{path} || $log->logdie("no path?");
273 dpavlin 673
274     if (-e $path) {
275     $log->warn("removing old $path");
276     rmtree($path) || $log->logdie("can't remove old temporary directory $path: $!");
277     }
278    
279     rename $path . '.tmp', $path || $log->logdie("can't rename ${path}.tmp -> $path: $!");
280    
281     }
282    
283    
284 dpavlin 627 =head2 convert
285    
286     my $utf8_string = $self->convert('string in codepage');
287    
288     =cut
289    
290     sub convert {
291     my $self = shift;
292    
293     my $text = shift || return;
294     from_to($text, $self->{encoding}, 'UTF-8');
295     return $text;
296     }
297    
298     =head1 AUTHOR
299    
300     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
301    
302     =head1 COPYRIGHT & LICENSE
303    
304     Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
305    
306     This program is free software; you can redistribute it and/or modify it
307     under the same terms as Perl itself.
308    
309     =cut
310    
311     1; # End of WebPAC::Output::Estraier

  ViewVC Help
Powered by ViewVC 1.1.26