/[mws]/trunk/MWS_swish.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/MWS_swish.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (show annotations)
Sat May 8 00:54:16 2004 UTC (20 years ago) by dpavlin
File size: 2682 byte(s)
major improvements:
- decoding of Base-64 haders
- search now accepts array of limits and operators
- date searches work as expected (result of above)
- autoformat now reformats ALL parafraphs (oh, bug)
- search is preseved when browsing through calendar

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 croak "search_index needs query" if (! @_);
40
41 my $index = $self->open_index;
42
43 my $sw;
44
45 foreach my $s (@_) {
46
47 if ($s =~ /^\s*(\w+):(.+)\s*$/) {
48 $sw .= "$1=($2)";
49 } else {
50 # and/or/not operators
51 $sw .= " $s ";
52 }
53 }
54
55 print STDERR "swish search: $sw\n";
56
57 # convert to UTF-8
58 $sw = $iso2utf->convert($sw) || $sw;
59 my $results = $index->Query($sw);
60
61 # store total number of hits
62 $self->{'total_hits'} = $results->Hits;
63
64 my @res_ids;
65
66 my $count = 1;
67
68 while ( my $r = $results->NextResult ) {
69
70 sub p($$) {
71 my ($r,$prop) = @_;
72 $prop = $r->Property($prop);
73 $prop =~ s/##lf##/\n/gs;
74 return $utf2iso->convert($prop);
75 }
76
77 my $id = p($r,"swishdocpath");
78 push @res_ids, $id;
79
80 foreach my $p (qw(from to cc bcc)) {
81 @{$self->{cache}->{$id}->{$p}} = ();
82 foreach my $v (split(/##/, p($r,$p.'_phrase'))) {
83 push @{$self->{cache}->{$id}->{$p}}, $v;
84 $self->add_counter($p,$v);
85 }
86 }
87
88 foreach my $p (qw(subject body date)) {
89 $self->{cache}->{$id}->{$p} = p($r,$p);
90 }
91
92 $self->add_counter_calendar(p($r,'date_utime'));
93
94 # this is redundant, but needed for templates later...
95 $self->{cache}->{$id}->{'id'} = $id;
96
97 last if (++$count > $self->{max_results});
98 }
99
100 return @res_ids;
101 }
102
103 sub add_index {
104 my $self = shift;
105
106 my $mbox_id = shift || croak "add_index needs mbox_id";
107 my $document = shift || croak "add_index needs document";
108
109 my ($mbox,$id) = split(/\s/,$mbox_id,2);
110
111 my $xml = qq{<message>};
112 foreach my $tag (keys %$document) {
113 my $data = $document->{$tag};
114 next if (! $data || $data eq '');
115 # save [cr/]lf before conversion to XML
116 $data =~ s/\n\r/##lf##/gs;
117 $data =~ s/\n/##lf##/gs;
118 $xml .= "<$tag><![CDATA[".$data."]]></$tag>\n";
119 }
120 $xml .= qq{</message>};
121
122 $xml = $iso2utf->convert($xml);
123 use bytes; # as opposed to chars
124 print "Path-Name: $mbox $id\n";
125 print "Content-Length: ".(length($xml)+1)."\n";
126 print "Document-Type: XML\n\n$xml\n";
127
128 }
129
130 sub close_index {
131 my $self = shift;
132
133 }
134
135 1;

  ViewVC Help
Powered by ViewVC 1.1.26