--- trunk/MWS_swish.pm 2004/05/06 16:53:40 13 +++ trunk/MWS_swish.pm 2004/05/06 19:46:58 14 @@ -8,6 +8,11 @@ # use SWISH::API; +use Text::Iconv; +use Data::Dumper; + +my $iso2utf = Text::Iconv->new('ISO-8859-2','UTF-8'); +my $utf2iso = Text::Iconv->new('UTF-8','ISO-8859-2'); sub open_index { my $self = shift; @@ -43,34 +48,59 @@ print STDERR "swish search: $s\n"; my $results = $index->Query($s); - my @r; + my @res_ids; + + while ( my $r = $results->NextResult ) { - while ( my $result = $results->NextResult ) { - my $id = $result->Property( "swishdocpath" ); - push @r, $id; + sub p($$) { + my ($r,$prop) = @_; + $prop = $r->Property($prop); + $prop =~ s/##lf##/\n/gs; + return $utf2iso->convert($prop); + } + my $id = p($r,"swishdocpath"); + push @res_ids, $id; foreach my $p (qw(from to cc bcc)) { - @{$self->{cache}->{$id}->{$p}} = split(/##/, $result->Property($p.'_phrase')); + @{$self->{cache}->{$id}->{$p}} = split(/##/, p($r,$p.'_phrase')); } foreach my $p (qw(subject body date)) { - $self->{cache}->{$id}->{$p} = $result->Property($p); + $self->{cache}->{$id}->{$p} = p($r,$p); } - $self->{cache}->{$id}->{'body'} =~ s/##lf##/\n/gs; - # this is redundant, but needed for templates later... $self->{cache}->{$id}->{'id'} = $id; } - return @r; + return @res_ids; } sub add_index { my $self = shift; - - croak("add_index is not implemented for swish!"); + + my $mbox_id = shift || croak "add_index needs mbox_id"; + my $document = shift || croak "add_index needs document"; + + my ($mbox,$id) = split(/\s/,$mbox_id,2); + + my $xml = qq{}; + foreach my $tag (keys %$document) { + my $data = $document->{$tag}; + # save [cr/]lf before conversion to XML + $data =~ s/\n\r/##lf##/gs; + $data =~ s/\n/##lf##/gs; + $xml .= "<$tag>\n" if ($data && $data ne ''); + } + $xml .= qq{}; + + $xml = $iso2utf->convert($xml); + use bytes; # as opposed to chars + print "Path-Name: $mbox $id\n"; + print "Content-Length: ".(length($xml)+1)."\n"; + print "Document-Type: XML\n\n$xml\n"; + } sub close_index {