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 |
# store total number of hits |
52 |
$self->{'total_hits'} = $results->Hits; |
53 |
|
54 |
my @res_ids; |
55 |
|
56 |
my $count = 0; |
57 |
|
58 |
while ( my $r = $results->NextResult ) { |
59 |
|
60 |
sub p($$) { |
61 |
my ($r,$prop) = @_; |
62 |
$prop = $r->Property($prop); |
63 |
$prop =~ s/##lf##/\n/gs; |
64 |
return $utf2iso->convert($prop); |
65 |
} |
66 |
|
67 |
my $id = p($r,"swishdocpath"); |
68 |
push @res_ids, $id; |
69 |
|
70 |
foreach my $p (qw(from to cc bcc)) { |
71 |
@{$self->{cache}->{$id}->{$p}} = split(/##/, p($r,$p.'_phrase')); |
72 |
} |
73 |
|
74 |
foreach my $p (qw(subject body date)) { |
75 |
$self->{cache}->{$id}->{$p} = p($r,$p); |
76 |
} |
77 |
|
78 |
# this is redundant, but needed for templates later... |
79 |
$self->{cache}->{$id}->{'id'} = $id; |
80 |
|
81 |
last if ($count++ > $self->{max_results}); |
82 |
} |
83 |
|
84 |
return @res_ids; |
85 |
} |
86 |
|
87 |
sub add_index { |
88 |
my $self = shift; |
89 |
|
90 |
my $mbox_id = shift || croak "add_index needs mbox_id"; |
91 |
my $document = shift || croak "add_index needs document"; |
92 |
|
93 |
my ($mbox,$id) = split(/\s/,$mbox_id,2); |
94 |
|
95 |
my $xml = qq{<message>}; |
96 |
foreach my $tag (keys %$document) { |
97 |
my $data = $document->{$tag}; |
98 |
next if (! $data || $data eq ''); |
99 |
# save [cr/]lf before conversion to XML |
100 |
$data =~ s/\n\r/##lf##/gs; |
101 |
$data =~ s/\n/##lf##/gs; |
102 |
$xml .= "<$tag><![CDATA[".$data."]]></$tag>\n"; |
103 |
} |
104 |
$xml .= qq{</message>}; |
105 |
|
106 |
$xml = $iso2utf->convert($xml); |
107 |
use bytes; # as opposed to chars |
108 |
print "Path-Name: $mbox $id\n"; |
109 |
print "Content-Length: ".(length($xml)+1)."\n"; |
110 |
print "Document-Type: XML\n\n$xml\n"; |
111 |
|
112 |
} |
113 |
|
114 |
sub close_index { |
115 |
my $self = shift; |
116 |
|
117 |
} |
118 |
|
119 |
1; |