4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.03'; |
our $VERSION = '0.06'; |
8 |
|
|
9 |
use Carp; |
use Carp; |
10 |
use File::Temp qw/ tempdir /; |
use File::Temp qw/ tempdir /; |
31 |
|
|
32 |
=head1 METHODS |
=head1 METHODS |
33 |
|
|
34 |
=head2 open |
=head2 new |
35 |
|
|
36 |
Create new indexing object. |
Create new indexing object. |
37 |
|
|
38 |
my $i = SWISH::PlusPlus->open( |
my $i = SWISH::PlusPlus->new( |
39 |
index_dir => '/path/to/index', |
index_dir => '/path/to/index', |
40 |
index => 'index++', |
index => 'index++', |
41 |
search => 'search++', |
search => 'search++', |
44 |
use_stopwords => 1, |
use_stopwords => 1, |
45 |
); |
); |
46 |
|
|
47 |
Options to open are following: |
Options to new are following: |
48 |
|
|
49 |
=over 5 |
=over 5 |
50 |
|
|
81 |
|
|
82 |
=cut |
=cut |
83 |
|
|
84 |
sub open { |
sub new { |
85 |
my $class = shift; |
my $class = shift; |
86 |
my $self = {@_}; |
my $self = {@_}; |
87 |
bless($self, $class); |
bless($self, $class); |
90 |
croak "need $_" unless $self->{$_}; |
croak "need $_" unless $self->{$_}; |
91 |
} |
} |
92 |
|
|
93 |
if (! -e $self->{'index_dir'}) { |
my $index_dir = $self->{'index_dir'}; |
94 |
mkdir $self->{'index_dir'} || confess "can't create index ",$self->{'index'},": $!"; |
|
95 |
|
if ($index_dir !~ m#^/#) { |
96 |
|
chomp(my $cwd = `pwd`); |
97 |
|
$index_dir = "$cwd/$index_dir"; |
98 |
|
print STDERR "## full path to index_dir: $index_dir\n" if ($self->{'debug'}); |
99 |
|
$self->{'index_dir'} = $index_dir; |
100 |
|
} |
101 |
|
|
102 |
|
if (! -e $index_dir) { |
103 |
|
mkdir $index_dir || confess "can't create index ",$self->{'index'},": $!"; |
104 |
} |
} |
105 |
|
|
106 |
# default executables |
# default executables |
107 |
$self->{'index'} ||= 'index'; |
$self->{'index'} ||= 'index'; |
108 |
$self->{'search'} ||= 'search'; |
$self->{'search'} ||= 'search'; |
109 |
|
|
110 |
print STDERR "## open index_dir: ",$self->{'index_dir'}," index: ",$self->{'index'}, " search: ",$self->{'search'},"\n" if ($self->{'debug'}); |
print STDERR "## new index_dir: ",$index_dir," index: ",$self->{'index'}, " search: ",$self->{'search'},"\n" if ($self->{'debug'}); |
111 |
|
|
112 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
113 |
} |
} |
115 |
|
|
116 |
=head2 check_bin |
=head2 check_bin |
117 |
|
|
118 |
Check if swish++ binaries specified in L<open> are available and verify |
Check if swish++ binaries specified in L<new> are available and verify |
119 |
version signature. |
version signature. |
120 |
|
|
121 |
if ($i->check_bin) { |
if ($i->check_bin) { |
227 |
my $open_cmd = $self->{'search'}." -i ".$self->{'index_dir'}.'/index "'.$query.'" |'; |
my $open_cmd = $self->{'search'}." -i ".$self->{'index_dir'}.'/index "'.$query.'" |'; |
228 |
print STDERR "## search $open_cmd\n" if ($self->{'debug'}); |
print STDERR "## search $open_cmd\n" if ($self->{'debug'}); |
229 |
|
|
230 |
CORE::open(SEARCH, $open_cmd) || confess "can't start $open_cmd: $!"; |
open(SEARCH, $open_cmd) || confess "can't start $open_cmd: $!"; |
231 |
while(<SEARCH>) { |
while(<SEARCH>) { |
232 |
next if (/^#/); |
next if (/^#/); |
233 |
chomp; |
chomp; |
248 |
return @results; |
return @results; |
249 |
} |
} |
250 |
|
|
251 |
|
=head2 finish_update |
252 |
|
|
253 |
|
This method will close index. |
254 |
|
|
255 |
|
$i->finish_update; |
256 |
|
|
257 |
|
It will be called on DESTROY when $i goes out of scope. |
258 |
|
|
259 |
|
=cut |
260 |
|
|
261 |
|
sub finish_update { |
262 |
|
my $self = shift; |
263 |
|
|
264 |
|
$self->_close_index; |
265 |
|
} |
266 |
|
|
267 |
|
sub DESTROY { |
268 |
|
my $self = shift; |
269 |
|
$self->finish_update; |
270 |
|
} |
271 |
|
|
272 |
=head1 PRIVATE METHODS |
=head1 PRIVATE METHODS |
273 |
|
|
274 |
Private methods implement internals for creating temporary file needed for |
Private methods implement internals for creating temporary file needed for |
293 |
|
|
294 |
chdir $self->{'tmp_dir'} || confess "can't chdir to ".$self->{'tmp_dir'}.": $!"; |
chdir $self->{'tmp_dir'} || confess "can't chdir to ".$self->{'tmp_dir'}.": $!"; |
295 |
|
|
296 |
my $opt = "-v 4"; |
my $opt = "-v " . ($self->{'debug'} || '0'); |
297 |
|
|
298 |
unless ($self->{'use_stopwrods'}) { |
unless ($self->{'use_stopwrods'}) { |
299 |
CORE::open(STOP, '>', "_stopwords_") || carp "can't create empty stopword file, skipping\n"; |
open(STOP, '>', "_stopwords_") || carp "can't create empty stopword file, skipping\n"; |
300 |
print STOP " "; |
print STOP " "; |
301 |
close(STOP); |
close(STOP); |
302 |
$opt .= " -s _stopwords_"; |
$opt .= " -s _stopwords_"; |
304 |
|
|
305 |
my $open_cmd = '| '.$self->{'index'}.' '.$opt.' -e "html:*" -i '.$self->{'index_dir'}.'/index -'; |
my $open_cmd = '| '.$self->{'index'}.' '.$opt.' -e "html:*" -i '.$self->{'index_dir'}.'/index -'; |
306 |
|
|
307 |
|
print STDERR "## open index $open_cmd\n" if ($self->{'index'}); |
308 |
|
|
309 |
CORE::open($self->{'index_fh'}, $open_cmd) || confess "can't start index with $open_cmd: $!"; |
open($self->{'index_fh'}, $open_cmd) || confess "can't start index with $open_cmd: $!"; |
310 |
|
|
311 |
|
|
312 |
return $self->{'index_fh'}; |
return $self->{'index_fh'}; |
340 |
|
|
341 |
my $path = $self->{'tmp_dir'} || confess "no tmp_dir?"; |
my $path = $self->{'tmp_dir'} || confess "no tmp_dir?"; |
342 |
|
|
343 |
CORE::open(TMP, '>', $arg->{'path'}) || die "can't create temp file ".$arg->{'path'}.": $!"; |
open(TMP, '>', $arg->{'path'}) || die "can't create temp file ".$arg->{'path'}.": $!"; |
344 |
|
|
345 |
print TMP '<html><head>'; |
print TMP '<html><head>'; |
346 |
|
|
347 |
$arg->{'body'} ||= ''; |
$arg->{'body'} ||= ''; |
348 |
|
|
349 |
if ($arg->{'meta'}) { |
if ($arg->{'meta'}) { |
350 |
confess "not yet implemented"; |
foreach my $name (keys %{$arg->{'meta'}}) { |
351 |
|
my $content = $arg->{'meta'}->{$name}; |
352 |
|
print TMP qq{<meta name="$name" content="$content">}; |
353 |
|
$arg->{'body'} .= " $content" if ($self->{'meta_in_body'}); |
354 |
|
} |
355 |
} |
} |
356 |
|
|
357 |
if (defined($arg->{'title'})) { |
if (defined($arg->{'title'})) { |
401 |
Debian version of swish++ is often old (version 5 at moment of this writing |
Debian version of swish++ is often old (version 5 at moment of this writing |
402 |
while version 6 is available in source code), so this module by default |
while version 6 is available in source code), so this module by default |
403 |
uses executable names B<index> and B<search> for self-compiled version |
uses executable names B<index> and B<search> for self-compiled version |
404 |
instead of one from Debian package. See L<open> how to specify Debian |
instead of one from Debian package. See L<new> how to specify Debian |
405 |
default binaries B<index++> and B<search++>. |
default binaries B<index++> and B<search++>. |
406 |
|
|
407 |
=head2 SWISH++ |
=head2 SWISH++ |
425 |
pages. To see my very relaxed sample configuration take a look at C<swish++> |
pages. To see my very relaxed sample configuration take a look at C<swish++> |
426 |
directory included in distribution. |
directory included in distribution. |
427 |
|
|
428 |
|
=head2 SWISH++ config |
429 |
|
|
430 |
|
C<config.h> located in C<swish++> directory of this distribution is relaxed |
431 |
|
SWISH++ configuration that will index all words passed to it. This |
432 |
|
configuration is needed for B<date test> because default configuration |
433 |
|
doesn't recognize 2004-12-05 as date. Have in mind that your index size |
434 |
|
might explode. |
435 |
|
|
436 |
=head1 SEE ALSO |
=head1 SEE ALSO |
437 |
|
|
438 |
C<swish++> web site L<http://homepage.mac.com/pauljlucas/software/swish/> |
C<swish++> web site L<http://homepage.mac.com/pauljlucas/software/swish/> |