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

Diff of /trunk/MWS_swish.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 12 by dpavlin, Thu May 6 12:40:11 2004 UTC revision 21 by dpavlin, Sat May 8 00:54:16 2004 UTC
# Line 8  use strict; Line 8  use strict;
8  #  #
9    
10  use SWISH::API;  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 {  sub open_index {
18          my $self = shift;          my $self = shift;
# Line 31  sub open_index { Line 36  sub open_index {
36  sub search_index {  sub search_index {
37          my $self = shift;          my $self = shift;
38    
39          my $s = shift || croak "search_index needs query";          croak "search_index needs query" if (! @_);
40    
41          my $index = $self->open_index;          my $index = $self->open_index;
42    
43          if ($s =~ /:/) {          my $sw;
44                  my ($fld,$val) = split(/:/,$s,2);  
45                  $s = "$fld=($val)";          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: $s\n";          print STDERR "swish search: $sw\n";
56          my $results = $index->Query($s);  
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          my @r;                  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          while ( my $result = $results->NextResult ) {                  foreach my $p (qw(subject body date)) {
89                  push @r, $result->Property( "swishdocpath" );                          $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 @r;          return @res_ids;
101  }  }
102    
103  sub add_index {  sub add_index {
104          my $self = shift;          my $self = shift;
105            
106          croak("add_index is not implemented for swish!");          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 {  sub close_index {

Legend:
Removed from v.12  
changed lines
  Added in v.21

  ViewVC Help
Powered by ViewVC 1.1.26