/[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 14 - (hide annotations)
Thu May 6 19:46:58 2004 UTC (20 years ago) by dpavlin
File size: 2253 byte(s)
Make mbox2swish universal for all indexer (moving index-specific
thinks in own .pm) -- after MWS_plucene.pm update it should work with it.
Use Text::AutoFormat to re-format messages wider than wrap_margin
(from global.conf). Hard-coded ISO-8859-2 encoding now works correctly,
Subject searches now remove common subject prefixes, template updates.

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     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 dpavlin 14 my @res_ids;
52 dpavlin 12
53 dpavlin 14 while ( my $r = $results->NextResult ) {
54 dpavlin 13
55 dpavlin 14 sub p($$) {
56     my ($r,$prop) = @_;
57     $prop = $r->Property($prop);
58     $prop =~ s/##lf##/\n/gs;
59     return $utf2iso->convert($prop);
60     }
61 dpavlin 13
62 dpavlin 14 my $id = p($r,"swishdocpath");
63     push @res_ids, $id;
64    
65 dpavlin 13 foreach my $p (qw(from to cc bcc)) {
66 dpavlin 14 @{$self->{cache}->{$id}->{$p}} = split(/##/, p($r,$p.'_phrase'));
67 dpavlin 13 }
68    
69     foreach my $p (qw(subject body date)) {
70 dpavlin 14 $self->{cache}->{$id}->{$p} = p($r,$p);
71 dpavlin 13 }
72    
73     # this is redundant, but needed for templates later...
74     $self->{cache}->{$id}->{'id'} = $id;
75 dpavlin 12 }
76    
77 dpavlin 14 return @res_ids;
78 dpavlin 12 }
79    
80     sub add_index {
81     my $self = shift;
82 dpavlin 14
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 dpavlin 12 }
105    
106     sub close_index {
107     my $self = shift;
108    
109     }
110    
111     1;

  ViewVC Help
Powered by ViewVC 1.1.26