1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
|
5 |
# |
6 |
# simple implementation to use SWISH-e with queryies like |
7 |
# Lucene (subject:something) |
8 |
# |
9 |
|
10 |
use SWISH::API; |
11 |
use Text::Iconv; |
12 |
use Data::Dumper; |
13 |
|
14 |
my $iso2utf = Text::Iconv->new('ISO-8859-2','UTF-8'); |
15 |
my $utf2iso = Text::Iconv->new('UTF-8','ISO-8859-2'); |
16 |
|
17 |
sub open_index { |
18 |
my $self = shift; |
19 |
|
20 |
my $swish = $self->{index}; |
21 |
|
22 |
if (! $swish) { |
23 |
|
24 |
my $index_file = $self->{index_file} || croak "open_index needs index filename"; |
25 |
$index_file .= "/swish-e"; |
26 |
print STDERR "opening index '$index_file'\n"; |
27 |
$swish = SWISH::API->new($index_file); |
28 |
$swish->AbortLastError if $swish->Error; |
29 |
|
30 |
$self->{index} = $swish; |
31 |
} |
32 |
|
33 |
return $swish; |
34 |
} |
35 |
|
36 |
sub search_index { |
37 |
my $self = shift; |
38 |
|
39 |
my $s = shift || croak "search_index needs query"; |
40 |
|
41 |
my $index = $self->open_index; |
42 |
|
43 |
if ($s =~ /:/) { |
44 |
my ($fld,$val) = split(/:/,$s,2); |
45 |
$s = "$fld=($val)"; |
46 |
} |
47 |
|
48 |
print STDERR "swish search: $s\n"; |
49 |
my $results = $index->Query($s); |
50 |
|
51 |
my @res_ids; |
52 |
|
53 |
while ( my $r = $results->NextResult ) { |
54 |
|
55 |
sub p($$) { |
56 |
my ($r,$prop) = @_; |
57 |
$prop = $r->Property($prop); |
58 |
$prop =~ s/##lf##/\n/gs; |
59 |
return $utf2iso->convert($prop); |
60 |
} |
61 |
|
62 |
my $id = p($r,"swishdocpath"); |
63 |
push @res_ids, $id; |
64 |
|
65 |
foreach my $p (qw(from to cc bcc)) { |
66 |
@{$self->{cache}->{$id}->{$p}} = split(/##/, p($r,$p.'_phrase')); |
67 |
} |
68 |
|
69 |
foreach my $p (qw(subject body date)) { |
70 |
$self->{cache}->{$id}->{$p} = p($r,$p); |
71 |
} |
72 |
|
73 |
# this is redundant, but needed for templates later... |
74 |
$self->{cache}->{$id}->{'id'} = $id; |
75 |
} |
76 |
|
77 |
return @res_ids; |
78 |
} |
79 |
|
80 |
sub add_index { |
81 |
my $self = shift; |
82 |
|
83 |
my $mbox_id = shift || croak "add_index needs mbox_id"; |
84 |
my $document = shift || croak "add_index needs document"; |
85 |
|
86 |
my ($mbox,$id) = split(/\s/,$mbox_id,2); |
87 |
|
88 |
my $xml = qq{<message>}; |
89 |
foreach my $tag (keys %$document) { |
90 |
my $data = $document->{$tag}; |
91 |
# save [cr/]lf before conversion to XML |
92 |
$data =~ s/\n\r/##lf##/gs; |
93 |
$data =~ s/\n/##lf##/gs; |
94 |
$xml .= "<$tag><![CDATA[".$data."]]></$tag>\n" if ($data && $data ne ''); |
95 |
} |
96 |
$xml .= qq{</message>}; |
97 |
|
98 |
$xml = $iso2utf->convert($xml); |
99 |
use bytes; # as opposed to chars |
100 |
print "Path-Name: $mbox $id\n"; |
101 |
print "Content-Length: ".(length($xml)+1)."\n"; |
102 |
print "Document-Type: XML\n\n$xml\n"; |
103 |
|
104 |
} |
105 |
|
106 |
sub close_index { |
107 |
my $self = shift; |
108 |
|
109 |
} |
110 |
|
111 |
1; |