/[swish]/trunk/html/swish.cgi
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/html/swish.cgi

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

revision 73 by dpavlin, Tue Apr 6 19:21:07 2004 UTC revision 86 by dpavlin, Mon Aug 30 11:16:39 2004 UTC
# Line 9  use Lingua::Spelling::Alternative; Line 9  use Lingua::Spelling::Alternative;
9  use Text::Iconv;  use Text::Iconv;
10  use Data::Pageset;  use Data::Pageset;
11    
12    
13    sub get_snippet {
14            my $context_chars = 100;
15    
16            my $desc = shift || return '';
17            $desc = e($desc);
18    
19            # test if $desc contains any of our query words
20            my @snips;
21    
22            my @colors = qw{#ffff66 #a0ffff #99ff99 #ff9999 #ff66ff};
23    
24            # construct regex
25            my $re = qq/^.*?(.{$context_chars}?)(\Q/ . join("|",@_) .
26                    qq/\E)(.{$context_chars})/;
27    
28            while ($desc =~ s/$re//si) {
29                    my ($bef,$qm,$af) = ($1, $2, $3);
30                            
31                    # no partial words...
32                    $bef =~ s,^\S+\s+|\s+\S+$,,gs;
33                    $af =~ s,^\S+\s+|\s+\S+$,,gs;
34    
35                    push @snips, "$bef $qm $af";
36            }
37    
38            my $ellip = ' ... ';
39            my $snippet;
40            
41            if (@snips) {
42                    $snippet = $ellip. join($ellip, @snips) . $ellip;
43            } else {
44                    return '';
45            }
46    
47            # color offset
48            my $i = 0;
49    
50            foreach my $w (@_) {
51                    $snippet =~ s,(\Q$w\E),<span style="background: $colors[$i]; color:black;">$1</span>,gsi;
52                    $i++;
53                    $i = 0 if ($i > $#colors);
54            }
55    
56            return $snippet;
57    }
58    
59  # for pager  # for pager
60  my $pages_per_set = 20;  my $pages_per_set = 20;
61    
# Line 16  Text::Iconv->raise_error(0);     # Conve Line 63  Text::Iconv->raise_error(0);     # Conve
63  my $config=XMLin(undef,  my $config=XMLin(undef,
64  #               keyattr => { label => "value" },  #               keyattr => { label => "value" },
65                  forcecontent => 0,                  forcecontent => 0,
66                    ForceArray => [ 'path' ],
67          );          );
68    
69  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});
# Line 40  my @spellings; Line 88  my @spellings;
88  # FIX: doesn't work very well  # FIX: doesn't work very well
89  if ($config->{findaffix}) {  if ($config->{findaffix}) {
90          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {
91                    next if (! -f $findaffix);
92                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
93                  $spelling_alt->load_findaffix($findaffix);                  $spelling_alt->load_findaffix($findaffix);
94                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 47  if ($config->{findaffix}) { Line 96  if ($config->{findaffix}) {
96  }  }
97  if ($config->{affix}) {  if ($config->{affix}) {
98          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {
99                    next if (! -f $affix);
100                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
101                  $spelling_alt->load_affix($affix);                  $spelling_alt->load_affix($affix);
102                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 62  foreach (@{$config->{labels}->{label}}) Line 112  foreach (@{$config->{labels}->{label}})
112          $labels{$_->{value}} = x($_->{content});          $labels{$_->{value}} = x($_->{content});
113  }  }
114    
115  my $path = param('path');       # limit to this path  my $path;
116    # limit to this path
117    $path .= '"'.join('*" or "',param('path')).'*"' if (param('path'));
118  my %path_label;  my %path_label;
119  my @path_name;  my @path_name;
120  foreach (@{$config->{paths}->{path}}) {  foreach (@{$config->{paths}->{path}}) {
   
 print STDERR "##: $_->{limit}",x($_->{content}),"\n";  
121          push @path_name,x($_->{limit});          push @path_name,x($_->{limit});
122          $path_label{$_->{limit}} = x($_->{content});          $path_label{$_->{limit}} = x($_->{content});
123  }  }
# Line 90  print checkbox(-name=>'no_properties', - Line 140  print checkbox(-name=>'no_properties', -
140  if (@path_name) {  if (@path_name) {
141          print br,x($config->{text}->{limit});          print br,x($config->{text}->{limit});
142          print popup_menu(-name=>'path',-values=>\@path_name,-labels=>\%path_label,-default=>$path);          print popup_menu(-name=>'path',-values=>\@path_name,-labels=>\%path_label,-default=>$path);
143    } elsif (param('path')) {
144            print hidden(-name=>'path',-values=>param('path'));
145  }  }
146  print end_form,hr;  print end_form,hr;
147    
# Line 98  if (param('search')) { Line 150  if (param('search')) {
150          my $s;          my $s;
151          # re-write query from +/- to and/and not          # re-write query from +/- to and/and not
152    
153            my @s_elem;
154    
155          my $search = param('search');          my $search = param('search');
156          my $s_phrase = "";  
157            # strip spaces
158            $search =~ s/^\s+//;
159            $search =~ s/\s+$//;
160            # fixup search string
161            $search=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/;     # 1250 -> iso8859-2
162            $search=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;
163    
164            # extract phrases and put them first
165          while ($search =~ s/\s*("[^"]+")\s*/ /) {          while ($search =~ s/\s*("[^"]+")\s*/ /) {
166                  $s .= "$1 ";                  $s .= "$1 ";
167                    push @s_elem, $1;
168          }          }
         $search =~ s/^\s+//;  
         $search =~ s/\s+$//;  
169    
170          my %words;          my %words;
171    
# Line 123  if (param('search')) { Line 184  if (param('search')) {
184                          } else {                          } else {
185                                  $s.="$2* ";                                  $s.="$2* ";
186                          }                          }
187                            push @s_elem, $2;
188                  } else {                  } else {
189                          if (@spellings && !param('no_affix')) {                          if (@spellings && !param('no_affix')) {
190                                  my $w = $_; $w =~ s/[\*\s]+//g;                                  my $w = $_; $w =~ s/[\*\s]+//g;
                                 #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";  
191                                  my $or="";                                  my $or="";
192                                  foreach my $spelling_alt (@spellings) {                                  foreach my $spelling_alt (@spellings) {
193                                          $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";                                          $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";
# Line 135  if (param('search')) { Line 196  if (param('search')) {
196                          } else {                          } else {
197                                  $s.="$_* ";                                  $s.="$_* ";
198                          }                          }
199                            push @s_elem, $_;
200                  }                  }
201          }          }
202    
203          # fixup search string          # fix multiple stars
         $s=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/;  # 1250 -> iso8859-2  
         $s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;  
204          $s=~s/\*\*+/*/g;          $s=~s/\*\*+/*/g;
205    
206          # limit to some path          # limit to some path
207          $s = "swishdocpath=(\"*$path*\") and $s" if ($path);          $s = "swishdocpath=($path) and $s" if ($path);
208    
209          my %params;     # optional parametars for swish          my %params;     # optional parametars for swish
210    
211          # default format for output          # default format for output
212          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";
213    
         # output start of table  
         print qq{  
 <table border="0">  
         };  
         # html before and after each hit  
         my $tr_pre = qq{  
 <tr><td>  
         };  
         my $tr_post = qq{  
 </td></tr>  
         };  
   
214          if (@properties) {          if (@properties) {
215                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));
216                  $params{properties} = \@properties;                  $params{properties} = \@properties;
# Line 170  if (param('search')) { Line 218  if (param('search')) {
218                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));
219          }          }
220    
 #       my $sh = SWISH->connect('Fork',  
 #               prog     => x($config->{prog}),  
 #               indexes  => x($config->{index}),  
 #               results  => sub {  
 #                       my ($sh,$hit) = @_;  
 #  
 #                       if ($config->{url}) {  
 #                               printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties));  
 #                       } else {  
 #                               printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) );  
 #  
 #                       }  
 #  
 ##                      print $_[1]->as_string,"<br>\n";  
 ##                      my @fields = $hit->field_names;  
 ##                      print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields;  
 #               },  
 #               maxhits => param('max_hits') || $max_hits,  
 #               \%params,  
 #       );  
 #  
 #       die $SWISH::errstr unless $sh;  
 #  
 #       $hits = $sh->query($s);  
 #  
 #       if ($hits && $hits > 0) {  
 #               print p,hr;  
 #               printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s);  
 #       } else {  
 #               print p;  
 #               printf (x($config->{text}->{no_hits}),$s,$sh->errstr);  
 #       }  
 #       if ($hits && $hits > 0) {  
 #               print p,hr;  
 #               printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s);  
 #       } else {  
 #               print p;  
 #               printf (x($config->{text}->{no_hits}),$s,$sh->errstr);  
 #       }  
   
221          my $swish = SWISH::API->new($config->{index});          my $swish = SWISH::API->new($config->{index});
   
222          $swish->AbortLastError if $swish->Error;          $swish->AbortLastError if $swish->Error;
   
223          my $results = $swish->Query($s);          my $results = $swish->Query($s);
   
224          my $hits = $results->Hits;          my $hits = $results->Hits;
225    
226    
   
227          # build pager          # build pager
228          my $current_page = param('page') || 1;          my $current_page = param('page') || 1;
229    
# Line 242  if (param('search')) { Line 246  if (param('search')) {
246                  printf (x($config->{text}->{hits}),$i,$results->Hits,$s);                  printf (x($config->{text}->{hits}),$i,$results->Hits,$s);
247          }          }
248    
249            my %path2title;
250            foreach my $p (@{$config->{path2title}->{path}}) {
251                    $path2title{$p->{dir}} = $p->{content};
252            }
253    
254            # output start of table
255            print qq{
256    <table border="0">
257            };
258            # html before and after each hit
259            my $tr_pre = qq{
260    <tr><td>
261            };
262            my $tr_post = qq{
263    </td></tr>
264            };
265    
266          for(my $i=$pager->first; $i<=$pager->last; $i++) {          for(my $i=$pager->first; $i<=$pager->last; $i++) {
267    
# Line 249  if (param('search')) { Line 269  if (param('search')) {
269                  last if (! $result);                  last if (! $result);
270    
271                  my @arr;                  my @arr;
272    
273                  foreach my $prop (@properties) {                  foreach my $prop (@properties) {
274                          if ($prop =~ m/swishdescription/) {                          if ($prop =~ m/swishdescription/) {
275                                  my $tmp = $result->Property($prop);                                  my $tmp = get_snippet(
276                                  $tmp =~ s/<[^>]+>//g;                                          $result->Property($prop),
277                                            @s_elem,
278                                    );
279                                    
280                                  push @arr, $tmp;                                  push @arr, $tmp;
281                          } else {                          } else {
282                                  push @arr, $result->Property($prop);                                  push @arr, $result->Property($prop);
# Line 263  if (param('search')) { Line 287  if (param('search')) {
287                  my $rank = $result->Property("swishrank");                  my $rank = $result->Property("swishrank");
288                  my $host = $result->Property("swishdocpath");                  my $host = $result->Property("swishdocpath");
289                  $host = "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath") if ($config->{url});                  $host = "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath") if ($config->{url});
290    
291                    foreach my $p (keys %path2title) {
292                            if ($host =~ m/$p/i) {
293                                    $title =~ s/$path2title{$p}\s*[:-]+\s*//;
294                                    $title = $path2title{$p}." :: ".$title;
295                                    last;
296                            }
297                    }
298    
299                  print $tr_pre,$i,". ";                  print $tr_pre,$i,". ";
300                  # print collection name which is not link                  # print collection name which is not link
301                  if ($title =~ s/^(.+? :: )//) {                  if ($title =~ s/^(.+? :: )//) {
302                          print $1;                          print $1;
303                  }                  }
304    
305                  printf($hit_fmt, $host, $title || 'untitled', $rank, @arr);                  printf($hit_fmt, $host, $title || 'untitled', $rank, @arr);
306                  print $tr_post;                  print $tr_post;
307    
# Line 278  if (param('search')) { Line 312  if (param('search')) {
312    
313          my $nav_fmt=qq{ <a href="%s">%s</a> };          my $nav_fmt=qq{ <a href="%s">%s</a> };
314    
315            if ($pager->current_page() > $pager->first_page) {
316                    param('page', $pager->current_page - 1);
317                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');
318            }
319    
320          if ($pager->previous_set) {          if ($pager->previous_set) {
321                  param('page', $pager->previous_set);                  param('page', $pager->previous_set);
322                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
323          }          }
324    
325    
# Line 297  if (param('search')) { Line 336  if (param('search')) {
336    
337          if ($pager->next_set) {          if ($pager->next_set) {
338                  param('page', $pager->next_set);                  param('page', $pager->next_set);
339                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
340            }
341    
342            if ($pager->current_page() < $pager->last_page) {
343                    param('page', $pager->current_page + 1);
344                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');
345          }          }
346    
347            if ($config->{text}->{pages}) {
348                    $nav_html = x($config->{text}->{pages})." ".$nav_html;
349            }
350    
351          # end html table          # end html table
352          print qq{          print qq{
353  <tr><td>  <tr><td>
354  Pages: $nav_html  $nav_html
355  </td></tr>  </td></tr>
356  </table>  </table>
357          };          };

Legend:
Removed from v.73  
changed lines
  Added in v.86

  ViewVC Help
Powered by ViewVC 1.1.26