/[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 59 by dpavlin, Mon Jan 26 08:08:41 2004 UTC revision 82 by dpavlin, Sun Aug 29 18:17:15 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            my $max_desc = 16384;   # 16k to filter
16    
17            my $desc = shift || return '';
18            $desc = substr($desc,0,$max_desc) if (length($desc) > $max_desc);
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            my $i = 0;
25    
26            for my $q (@_) {
27                    if ($desc =~ m/(.*?)(\Q$q\E)(.*)/si) {
28                            my $bef = $1;
29                            my $qm = $2;
30                            my $af = $3;
31                            $bef = substr $bef, -$context_chars;
32                            $af = substr $af, 0, $context_chars;
33                            
34                            # no partial words...
35                            $af =~ s,^\S+\s+|\s+\S+$,,gs;
36                            $bef =~ s,^\S+\s+|\s+\S+$,,gs;
37    
38                            push(@snips, "$bef <span style=\"background:".$colors[$i]."; color:black;\">$qm</span> $af");
39                            $i++;
40                            $i = 0 if ($i > $#colors);
41                    }
42            }
43            my $ellip = ' ... ';
44            my $snippet = $ellip. join($ellip, @snips) . $ellip if (@snips);
45      
46            return $snippet;
47    }
48    
49  # for pager  # for pager
50  my $pages_per_set = 20;  my $pages_per_set = 20;
51    
# Line 16  Text::Iconv->raise_error(0);     # Conve Line 53  Text::Iconv->raise_error(0);     # Conve
53  my $config=XMLin(undef,  my $config=XMLin(undef,
54  #               keyattr => { label => "value" },  #               keyattr => { label => "value" },
55                  forcecontent => 0,                  forcecontent => 0,
56                    ForceArray => [ 'path' ],
57          );          );
58    
59  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});
60  sub x {  sub x {
61            return if (! defined $_[0]);
62          return $from_utf8->convert($_[0]);          return $from_utf8->convert($_[0]);
63  }  }
64    
# Line 39  my @spellings; Line 78  my @spellings;
78  # FIX: doesn't work very well  # FIX: doesn't work very well
79  if ($config->{findaffix}) {  if ($config->{findaffix}) {
80          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {
81                    next if (! -f $findaffix);
82                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
83                  $spelling_alt->load_findaffix($findaffix);                  $spelling_alt->load_findaffix($findaffix);
84                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 46  if ($config->{findaffix}) { Line 86  if ($config->{findaffix}) {
86  }  }
87  if ($config->{affix}) {  if ($config->{affix}) {
88          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {
89                    next if (! -f $affix);
90                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
91                  $spelling_alt->load_affix($affix);                  $spelling_alt->load_affix($affix);
92                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 61  foreach (@{$config->{labels}->{label}}) Line 102  foreach (@{$config->{labels}->{label}})
102          $labels{$_->{value}} = x($_->{content});          $labels{$_->{value}} = x($_->{content});
103  }  }
104    
105  my $path = param('path');       # limit to this path  my $path;
106    # limit to this path
107    $path .= '"'.join('*" or "',param('path')).'*"' if (param('path'));
108  my %path_label;  my %path_label;
109  my @path_name;  my @path_name;
110  foreach (@{$config->{paths}->{path}}) {  foreach (@{$config->{paths}->{path}}) {
# Line 71  print STDERR "##: $_->{limit}",x($_->{co Line 114  print STDERR "##: $_->{limit}",x($_->{co
114          $path_label{$_->{limit}} = x($_->{content});          $path_label{$_->{limit}} = x($_->{content});
115  }  }
116    
117  my @properties = split(/\s+/,x($config->{properties}));  my @properties = split(/\s+/,x($config->{properties})) if ($config->{properties});
118    
119  if ($config->{charset}) {  if ($config->{charset}) {
120          print header(-charset=>x($config->{charset}));          print header(-charset=>x($config->{charset}));
# Line 89  print checkbox(-name=>'no_properties', - Line 132  print checkbox(-name=>'no_properties', -
132  if (@path_name) {  if (@path_name) {
133          print br,x($config->{text}->{limit});          print br,x($config->{text}->{limit});
134          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);
135    } elsif (param('path')) {
136            print hidden(-name=>'path',-values=>param('path'));
137  }  }
138  print end_form,hr;  print end_form,hr;
139    
# Line 143  if (param('search')) { Line 188  if (param('search')) {
188          $s=~s/\*\*+/*/g;          $s=~s/\*\*+/*/g;
189    
190          # limit to some path          # limit to some path
191          $s = "swishdocpath=(\"*$path*\") and $s" if ($path);          $s = "swishdocpath=($path) and $s" if ($path);
192    
193          my %params;     # optional parametars for swish          my %params;     # optional parametars for swish
194    
# Line 169  if (param('search')) { Line 214  if (param('search')) {
214                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));
215          }          }
216    
 #       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);  
 #       }  
   
217          my $swish = SWISH::API->new($config->{index});          my $swish = SWISH::API->new($config->{index});
   
218          $swish->AbortLastError if $swish->Error;          $swish->AbortLastError if $swish->Error;
   
219          my $results = $swish->Query($s);          my $results = $swish->Query($s);
   
220          my $hits = $results->Hits;          my $hits = $results->Hits;
221    
222    
   
223          # build pager          # build pager
224          my $current_page = param('page') || 1;          my $current_page = param('page') || 1;
225    
# Line 241  if (param('search')) { Line 242  if (param('search')) {
242                  printf (x($config->{text}->{hits}),$i,$results->Hits,$s);                  printf (x($config->{text}->{hits}),$i,$results->Hits,$s);
243          }          }
244    
245            my %path2title;
246            use Data::Dumper;
247            foreach my $p (@{$config->{path2title}->{path}}) {
248                    $path2title{$p->{dir}} = $p->{content};
249            }
250    
251          for(my $i=$pager->first; $i<=$pager->last; $i++) {          for(my $i=$pager->first; $i<=$pager->last; $i++) {
252    
# Line 248  if (param('search')) { Line 254  if (param('search')) {
254                  last if (! $result);                  last if (! $result);
255    
256                  my @arr;                  my @arr;
257    
258                  foreach my $prop (@properties) {                  foreach my $prop (@properties) {
259                          if ($prop =~ m/swishdescription/) {                          if ($prop =~ m/swishdescription/) {
260                                  my $tmp = $result->Property($prop);                                  my $tmp = get_snippet(
261                                  $tmp =~ s/<[^>]+>//g;                                          $result->Property($prop),
262                                            split(/\s+/,$search)
263                                    );
264                                    
265                                  push @arr, $tmp;                                  push @arr, $tmp;
266                          } else {                          } else {
267                                  push @arr, $result->Property($prop);                                  push @arr, $result->Property($prop);
268                          }                          }
269                  }                  }
270    
271                    my $title = e($result->Property("swishtitle")) || 'untitled';
272                    my $rank = $result->Property("swishrank");
273                    my $host = $result->Property("swishdocpath");
274                    $host = "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath") if ($config->{url});
275    
276                    foreach my $p (keys %path2title) {
277                            if ($host =~ m/$p/i) {
278                                    $title =~ s/$path2title{$p}\s*[:-]+\s*//;
279                                    $title = $path2title{$p}." :: ".$title;
280                                    last;
281                            }
282                    }
283    
284                  print $tr_pre,$i,". ";                  print $tr_pre,$i,". ";
285                  if ($config->{url}) {                  # print collection name which is not link
286                          printf($hit_fmt, "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath"),                  if ($title =~ s/^(.+? :: )//) {
287                                  e($result->Property("swishtitle")) || 'untitled',                          print $1;
                                 $result->Property("swishrank"),  
                                 @arr);  
                 } else {  
                         printf($hit_fmt, $result->Property("swishdocpath"),  
                                 e($result->Property("swishtitle")) || 'untitled',  
                                 $result->Property("swishrank"),  
                                 @arr);  
288                  }                  }
289    
290                    printf($hit_fmt, $host, $title || 'untitled', $rank, @arr);
291                  print $tr_post;                  print $tr_post;
292    
293          }          }
# Line 279  if (param('search')) { Line 297  if (param('search')) {
297    
298          my $nav_fmt=qq{ <a href="%s">%s</a> };          my $nav_fmt=qq{ <a href="%s">%s</a> };
299    
300            if ($pager->current_page() > $pager->first_page) {
301                    param('page', $pager->current_page - 1);
302                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');
303            }
304    
305          if ($pager->previous_set) {          if ($pager->previous_set) {
306                  param('page', $pager->previous_set);                  param('page', $pager->previous_set);
307                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
308          }          }
309    
310    
311          foreach my $p (@{$pager->pages_in_set()}) {          foreach my $p (@{$pager->pages_in_set()}) {
312                    next if ($p < 0);
313  #       for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) {  #       for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) {
314                  if($p == $pager->current_page()) {                  if($p == $pager->current_page()) {
315                          $nav_html .= "<b>$p</b> ";                          $nav_html .= "<b>$p</b> ";
# Line 297  if (param('search')) { Line 321  if (param('search')) {
321    
322          if ($pager->next_set) {          if ($pager->next_set) {
323                  param('page', $pager->next_set);                  param('page', $pager->next_set);
324                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
325            }
326    
327            if ($pager->current_page() < $pager->last_page) {
328                    param('page', $pager->current_page + 1);
329                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');
330          }          }
331    
332            if ($config->{text}->{pages}) {
333                    $nav_html = x($config->{text}->{pages})." ".$nav_html;
334            }
335    
336          # end html table          # end html table
337          print qq{          print qq{
338  <tr><td>  <tr><td>
339  Pages: $nav_html  $nav_html
340  </td></tr>  </td></tr>
341  </table>  </table>
342          };          };

Legend:
Removed from v.59  
changed lines
  Added in v.82

  ViewVC Help
Powered by ViewVC 1.1.26