/[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

Annotation of /trunk/MWS_swish.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (hide 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 dpavlin 12 #!/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 dpavlin 14 use Text::Iconv;
12     use Data::Dumper;
13 dpavlin 12
14 dpavlin 14 my $iso2utf = Text::Iconv->new('ISO-8859-2','UTF-8');
15     my $utf2iso = Text::Iconv->new('UTF-8','ISO-8859-2');
16    
17 dpavlin 12 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 dpavlin 21 croak "search_index needs query" if (! @_);
40 dpavlin 12
41     my $index = $self->open_index;
42    
43 dpavlin 21 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 dpavlin 12 }
54    
55 dpavlin 21 print STDERR "swish search: $sw\n";
56 dpavlin 18
57     # convert to UTF-8
58 dpavlin 21 $sw = $iso2utf->convert($sw) || $sw;
59     my $results = $index->Query($sw);
60 dpavlin 12
61 dpavlin 17 # store total number of hits
62     $self->{'total_hits'} = $results->Hits;
63    
64 dpavlin 14 my @res_ids;
65 dpavlin 12
66 dpavlin 18 my $count = 1;
67 dpavlin 17
68 dpavlin 14 while ( my $r = $results->NextResult ) {
69 dpavlin 13
70 dpavlin 14 sub p($$) {
71     my ($r,$prop) = @_;
72     $prop = $r->Property($prop);
73     $prop =~ s/##lf##/\n/gs;
74     return $utf2iso->convert($prop);
75     }
76 dpavlin 13
77 dpavlin 14 my $id = p($r,"swishdocpath");
78     push @res_ids, $id;
79    
80 dpavlin 13 foreach my $p (qw(from to cc bcc)) {
81 dpavlin 19 @{$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 dpavlin 13 }
87    
88     foreach my $p (qw(subject body date)) {
89 dpavlin 14 $self->{cache}->{$id}->{$p} = p($r,$p);
90 dpavlin 13 }
91    
92 dpavlin 20 $self->add_counter_calendar(p($r,'date_utime'));
93    
94 dpavlin 13 # this is redundant, but needed for templates later...
95     $self->{cache}->{$id}->{'id'} = $id;
96 dpavlin 17
97 dpavlin 18 last if (++$count > $self->{max_results});
98 dpavlin 12 }
99    
100 dpavlin 14 return @res_ids;
101 dpavlin 12 }
102    
103     sub add_index {
104     my $self = shift;
105 dpavlin 14
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 dpavlin 16 next if (! $data || $data eq '');
115 dpavlin 14 # save [cr/]lf before conversion to XML
116     $data =~ s/\n\r/##lf##/gs;
117     $data =~ s/\n/##lf##/gs;
118 dpavlin 16 $xml .= "<$tag><![CDATA[".$data."]]></$tag>\n";
119 dpavlin 14 }
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 dpavlin 12 }
129    
130     sub close_index {
131     my $self = shift;
132    
133     }
134    
135     1;

  ViewVC Help
Powered by ViewVC 1.1.26