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

Legend:
Removed from v.13  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26