r1383@llin: dpavlin | 2007-10-30 23:07:09 +0100
[webpac2] / lib / WebPAC / Output / KinoSearch.pm
1 package WebPAC::Output::KinoSearch;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
7 __PACKAGE__->mk_accessors(qw(
8         path
9         database
10         encoding
11         clean
12
13         index
14 ));
15
16 use KinoSearch::Simple;
17 use File::Path;
18 use Encode qw/from_to/;
19 use Data::Dump qw/dump/;
20 use Storable;
21
22 =head1 NAME
23
24 WebPAC::Output::KinoSearch - Create KinoSearch full text index
25
26 =head1 VERSION
27
28 Version 0.05
29
30 =cut
31
32 our $VERSION = '0.05';
33
34 =head1 SYNOPSIS
35
36 Create full text index using KinoSearch index from data with
37 type C<search>.
38
39 =head1 FUNCTIONS
40
41 =head2 new
42
43 Open KinoSearch index
44
45  my $out = new WebPAC::Output::KinoSearch({
46         path => '/path/to/invindex',
47         database => 'demo',
48         encoding => 'iso-8859-2',
49         clean => 1,
50  });
51
52 Options are:
53
54 =over 4
55
56 =item path
57
58 path to KinoSearch index to use
59
60 =item database
61
62 name of database from which data comes
63
64 =item encoding
65
66 character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>
67 (and it probably is). This encoding will be converted to C<UTF-8> for
68 index.
69
70 =back
71
72 =head2 init
73
74   $out->init;
75
76 =cut
77
78 sub init {
79         my $self = shift;
80
81         my $log = $self->_get_logger;
82
83         #$log->debug("self: ", sub { dump($self) });
84
85         foreach my $p (qw/path database/) {
86                 $log->logdie("need $p") unless ($self->$p);
87         }
88
89 #       $log->logdie("fields is not ARRAY") unless (ref($self->{fields}) eq 'ARRAY');
90
91         $self->encoding( 'ISO-8859-2' ) unless $self->encoding;
92
93         if ( ! -e $self->path ) {
94                 mkpath $self->path || $log->logdie("can't create ", $self->path,": $!");
95                 $log->info("created ", $self->path);
96         } elsif ( $self->clean ) {
97                 $log->info("removing existing ", $self->path);
98                 rmtree $self->path || $log->logdie("can't remove ", $self->path,": $!");
99                 mkpath $self->path || $log->logdie("can't create ", $self->path,": $!");
100         }
101
102         my $path = $self->path . '/' . $self->database;
103
104         $log->info("using index $path with encoding ", $self->encoding);
105
106         my $index = KinoSearch::Simple->new(
107                 path => $path,
108                 language => 'en',
109         );
110
111         $log->logdie("can't open $path: $!") unless $index;
112
113         $self->index( $index );
114
115 }
116
117
118 =head2 add
119
120 Adds one entry
121
122   $out->add( 42, $ds );
123
124 =cut
125
126 sub add {
127         my $self = shift;
128
129         my ( $id, $ds ) = @_;
130
131         my $log = $self->_get_logger;
132         $log->logdie("need id") unless defined $id;
133         $log->logdie("need ds") unless $ds;
134
135         $log->debug("id: $id ds = ",dump($ds));
136
137         my $hash = $self->ds_to_hash( $ds, 'search' ) || return;
138
139         $hash->{database} ||= $self->database;
140         $hash->{id} ||= $id;
141
142         $log->debug("add( $id, ", sub { dump($ds) }," ) => ", sub { dump( $hash ) });
143
144         $self->index->add_doc( $hash );
145
146         return 1;
147 }
148
149 =head2 finish
150
151 Close index
152
153  $out->finish;
154
155 =cut
156
157 sub finish {
158         my $self = shift;
159
160         my $log = $self->_get_logger();
161
162         $log->info("dummy finish");
163
164 }
165
166 =head1 AUTHOR
167
168 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
169
170 =head1 COPYRIGHT & LICENSE
171
172 Copyright 2005-2007 Dobrica Pavlinusic, All Rights Reserved.
173
174 This program is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
176
177 =cut
178
179 1; # End of WebPAC::Output::Estraier