r1380@llin: dpavlin | 2007-10-30 22:57:36 +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.04
29
30 =cut
31
32 our $VERSION = '0.04';
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         }
97
98         my $path = $self->path . '/' . $self->database;
99
100         $log->info("using index $path with encoding ", $self->encoding);
101
102         my $index = KinoSearch::Simple->new(
103                 path => $path,
104                 language => 'en',
105         );
106
107         $log->logdie("can't open $path: $!") unless $index;
108
109         $self->index( $index );
110
111 }
112
113
114 =head2 add
115
116 Adds one entry
117
118   $out->add( 42, $ds );
119
120 =cut
121
122 sub add {
123         my $self = shift;
124
125         my ( $id, $ds ) = @_;
126
127         my $log = $self->_get_logger;
128         $log->logdie("need id") unless defined $id;
129         $log->logdie("need ds") unless $ds;
130
131         $log->debug("id: $id ds = ",dump($ds));
132
133         my $hash = $self->ds_to_hash( $ds, 'search' ) || return;
134
135         warn "add( $id, ",dump($ds)," ) => ", dump( $hash );
136
137         $self->index->add_doc( $hash );
138
139         return 1;
140 }
141
142 =head2 finish
143
144 Close index
145
146  $out->finish;
147
148 =cut
149
150 sub finish {
151         my $self = shift;
152
153         my $log = $self->_get_logger();
154
155         $log->info("dummy finish");
156
157 }
158
159 =head1 AUTHOR
160
161 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
162
163 =head1 COPYRIGHT & LICENSE
164
165 Copyright 2005-2007 Dobrica Pavlinusic, All Rights Reserved.
166
167 This program is free software; you can redistribute it and/or modify it
168 under the same terms as Perl itself.
169
170 =cut
171
172 1; # End of WebPAC::Output::Estraier