r1802@llin: dpavlin | 2009-04-23 15:20:49 +0200
[webpac2] / lib / WebPAC / Output / SWISH.pm
index 69c1fc7..60ce87f 100644 (file)
@@ -3,12 +3,21 @@ package WebPAC::Output::SWISH;
 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
@@ -26,9 +35,9 @@ type C<search>.
 
 =head2 new
 
- my $out = new WebPAC::Output::SWISH(
+ my $out = new WebPAC::Output::SWISH({
        database => 'demo',
- );
});
 
 Options are:
 
@@ -46,16 +55,14 @@ Name of database will be used to form URI of documents in index.
 
 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";
 
@@ -80,7 +87,7 @@ UndefinedXMLAttributes auto
 IndexFile $dir/$database
 
 # Croatian ISO-8859-2 characters to unaccented equivalents
-TranslateCharacters ¹©ðÐèÈæƾ® ssddcccczz
+#TranslateCharacters ¹©ðÐèÈæƾ® ssddcccczz
 
 
 # disable output
@@ -91,10 +98,12 @@ DEFAULT_SWISH_CONF
 
        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} = {};
 
@@ -105,27 +114,9 @@ DEFAULT_SWISH_CONF
 
   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
 
@@ -133,29 +124,21 @@ my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
 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));
 
@@ -165,7 +148,10 @@ sub add {
 
        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;
 
@@ -179,10 +165,10 @@ sub add {
 
        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;
 }
@@ -201,6 +187,8 @@ sub finish {
        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