use warnings;
use strict;
-use base qw/WebPAC::Common/;
+use lib 'lib';
+
+use base qw/WebPAC::Common Class::Accessor/;
+__PACKAGE__->mk_accessors(qw(
+ database
+ type
+
+ index_path
+));
use File::Path qw/make_path/;
use Data::Dump qw/dump/;
use YAML;
+
=head1 NAME
WebPAC::Output::SWISH - Create swish-e full text index
=head2 new
- my $out = new WebPAC::Output::SWISH(
+ my $out = new WebPAC::Output::SWISH({
database => 'demo',
- );
+ });
Options are:
our $dir = 'var/swish';
-sub new {
- my $class = shift;
- my $self = {@_};
- bless($self, $class);
+sub init {
+ my $self = shift;
my $log = $self->_get_logger;
- #$log->debug("self: ", sub { dump($self) });
+ my $database = $self->database || $log->logdie("need database");
- my $database = $self->{database} || $log->logdie("need database");
+ make_path $dir if ! -e $dir;
my $path = "$dir/$database.conf";
IndexFile $dir/$database
# Croatian ISO-8859-2 characters to unaccented equivalents
-TranslateCharacters ¹©ðÐèÈæƾ® ssddcccczz
+#TranslateCharacters ¹©ðÐèÈæƾ® ssddcccczz
# disable output
close($conf) || die "can't write config $path: $!";
- $self->{_swish_index_path} = "$dir/$database";
+ $self->index_path( "$dir/$database" );
my $swish = "swish-e -S prog -c $path";
- open( $self->{_swish_fh}, '-|', $swish ) || die "can't open pipe to $swish: $!";
+ open( $self->{_swish_fh}, '|-', $swish ) || die "can't open pipe to $swish: $!";
+
+ $log->info( "created $path ", -s $path, " bytes for ", $self->index_path );
$self->{stats} = {};
my $path = $out->index_path;
-=cut
-
-sub index_path { $_[0]->{_swish_index_path} };
-
=head2 add
-Adds one entry to database.
-
- $out->add(
- id => 42,
- ds => $ds,
- type => 'display',
- text => 'optional text from which snippet is created',
- );
-
-This function will create entries in index using following URI format:
-
- C<file:///type/database%20name/000>
-
-Each tag in C<data_structure> with specified C<type> will create one
-attribute and corresponding hidden text (used for search).
+ $out->add( 42, $ds );
=cut
my $escape_re = join '|' => keys %escape;
sub add {
- my $self = shift;
-
- my $args = {@_};
+ my ($self,$id,$ds) = @_;
my $log = $self->_get_logger;
+ $log->debug("id: $id ds = ",sub { dump($ds) });
- my $database = $self->{'database'} || $log->logconfess('no database in $self');
- $log->logconfess('need db in object') unless ($self->{'db'});
+ my $database = $self->database || $log->logconfess('no database in $self');
- foreach my $p (qw/id ds type/) {
- $log->logdie("need $p") unless ($args->{$p});
- }
-
- my $type = $args->{'type'};
- my $id = $args->{'id'};
-
- my $uri = "$database/$id";
+ my $uri = $self->database . "/$id";
$log->debug("creating $uri");
# filter all tags which have type defined
+ my $type = $self->type || 'search';
my @tags = grep {
- ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} )
- } keys %{ $args->{'ds'} };
+ ref($ds->{$_}) eq 'HASH' && defined( $ds->{$_}->{$type} )
+ } keys %{ $ds };
$log->debug("tags = ", join(",", @tags));
foreach my $tag (@tags) {
- my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
+ my $r = ref $ds->{$tag}->{$type};
+ die "tag $tag type $type not ARRAY but '$r' = ",dump( $ds->{$tag}->{$type} ) unless $r eq 'ARRAY';
+
+ my $vals = join(" ", @{ $ds->{$tag}->{$type} });
next if ! $vals;
my $len = length($xml);
- my $fh = $self->{_swish_fh};
+ my $fh = $self->{_swish_fh} || die "_swish_fh missing";
- print $fh "Path-Name: $uri\nContent-Length: $len\nDocument-Type: XML\n\n$xml";
-# || die "can't add $uri: $@\n$xml\n";
+ print $fh "Path-Name: $uri\nContent-Length: $len\nDocument-Type: XML\n\n$xml" or
+ die "can't add $uri: $@\n$xml";
return 1;
}
YAML::DumpFile( $path, $self->{stats} );
$log->info("created $path ", -s $path, " bytes");
$log->debug( dump( $self->{stats} ) );
+
+ close( $self->{_swish_fh} ) || die "can't close index ", $self->index_path, ": $!";
}
=head1 AUTHOR
use Test::More tests => 35;
BEGIN {
-use_ok( 'WebPAC::Test' );
-use_ok( 'WebPAC::Output::SWISH' );
-use_ok( 'SWISH::API' );
+ use lib 'lib';
+ use_ok( 'WebPAC::Test' );
+ use_ok( 'WebPAC::Output::SWISH' );
+ use_ok( 'SWISH::API' );
}
my $path = "$abs_path/kino/";
-ok(my $out = new WebPAC::Output::SWISH(
+ok(my $out = new WebPAC::Output::SWISH({
database => 'test',
%LOG
-), "new");
+}), "new");
+
+ok( $out->init, 'init' );
my $ds = {
'Source' => {
'search' => [ 'tko zna' ]
},
'ID' => {
- 'search' => 'id',
+ 'search' => [ 'id' ],
},
'Array' => {
'search' => [ qw/a1 a2 s3 a4 a5/ ],
},
};
-throws_ok { $out->add( ) } qr/need id/, 'add without params';
-throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds';
-
ok( $out->add( 42, $ds ), 'add 42' );
my @strange = ( qw/èajðinica odma¹æivanje ¾abokreèina ¹uma/ );
my $swish = SWISH::API->new( $out->index_path );
$swish->abort_last_error if $swish->Error;
- my $results = $swish->search( $query_string );
+ my $results = $swish->query( $query_string );
my $total_hits = $results->hits;