/[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 87 by dpavlin, Mon Aug 30 16:59:17 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    sub get_snippet {
13    
14            # maximum length of context in characters
15            my $cc = 50;
16    
17            my $desc = shift || return '';
18            $desc = e($desc);
19    
20            # sort words from longer to shorter (for hilighting later)
21            my @words = sort { length($b) <=> length($a) } @_;
22    
23            # colors to highlite
24            my @colors = qw{#ffff66 #a0ffff #99ff99 #ff9999 #ff66ff};
25    
26            # construct regex
27            my $re = qq/^(.*?\\b)(/ . join('|', @words) . qq/)/;
28    
29            my $ellip = ' ... ';
30            my $snippet = '';
31    
32    #print "<ul>";
33    
34            while ($desc =~ s/$re//si) {
35                    my ($foo, $match) = ($1,$2);
36    
37    #print "<br>desc: <small>$desc</small>\n";
38    #print "<br>foo: <small>$foo<b>$match</b></small>\n";
39    
40                    if (length($foo) < $cc * 2) {
41                            $snippet .= $foo . $match;
42                    } else {
43    
44                            if ($foo =~ m/^(.{0,$cc})(\s.*?\s|\s|)?(.{0,$cc})$/) {
45    
46    #       print "<li><small>$snippet</small><br>
47    #       ",length($1),": <i>$1</i><br>
48    #       ",length($2),": <span style=\"color:grey\">$2</span><br>
49    #       ",length($3),": <i>$3</i><br>
50    #       <b>$match</b>\n";
51    
52                                    if ($snippet) {
53                                            $snippet .= $1 . $ellip . $3 . $match;
54                                    } else {
55                                            $snippet = $ellip . $3 . $match ;
56                                    }
57    
58                            } else {
59    #                               print "<li> <big>SKIP</big> $foo\n";
60                                    print STDERR "this shouldn't happen!\n";
61                            }
62    
63                    }
64    
65            }
66    #print "</ul>";
67    
68            # color offset
69            my $i = 0;
70    
71            foreach my $w (@words) {
72                    $snippet =~ s,(\b\Q$w\E),<span style="background: $colors[$i]; color:black;">$1</span>,gsi;
73                    $i++;
74                    $i = 0 if ($i > $#colors);
75            }
76    
77            $snippet .= $ellip if ($snippet);
78    
79            return $snippet;
80    }
81    
82  # for pager  # for pager
83  my $pages_per_set = 20;  my $pages_per_set = 20;
84    
# Line 16  Text::Iconv->raise_error(0);     # Conve Line 86  Text::Iconv->raise_error(0);     # Conve
86  my $config=XMLin(undef,  my $config=XMLin(undef,
87  #               keyattr => { label => "value" },  #               keyattr => { label => "value" },
88                  forcecontent => 0,                  forcecontent => 0,
89                    ForceArray => [ 'path' ],
90          );          );
91    
92  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});
# Line 40  my @spellings; Line 111  my @spellings;
111  # FIX: doesn't work very well  # FIX: doesn't work very well
112  if ($config->{findaffix}) {  if ($config->{findaffix}) {
113          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {
114                    next if (! -f $findaffix);
115                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
116                  $spelling_alt->load_findaffix($findaffix);                  $spelling_alt->load_findaffix($findaffix);
117                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 47  if ($config->{findaffix}) { Line 119  if ($config->{findaffix}) {
119  }  }
120  if ($config->{affix}) {  if ($config->{affix}) {
121          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {
122                    next if (! -f $affix);
123                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
124                  $spelling_alt->load_affix($affix);                  $spelling_alt->load_affix($affix);
125                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 62  foreach (@{$config->{labels}->{label}}) Line 135  foreach (@{$config->{labels}->{label}})
135          $labels{$_->{value}} = x($_->{content});          $labels{$_->{value}} = x($_->{content});
136  }  }
137    
138  my $path = param('path');       # limit to this path  my $path;
139    # limit to this path
140    $path .= '"'.join('*" or "',param('path')).'*"' if (param('path'));
141  my %path_label;  my %path_label;
142  my @path_name;  my @path_name;
143  foreach (@{$config->{paths}->{path}}) {  foreach (@{$config->{paths}->{path}}) {
   
 print STDERR "##: $_->{limit}",x($_->{content}),"\n";  
144          push @path_name,x($_->{limit});          push @path_name,x($_->{limit});
145          $path_label{$_->{limit}} = x($_->{content});          $path_label{$_->{limit}} = x($_->{content});
146  }  }
# Line 90  print checkbox(-name=>'no_properties', - Line 163  print checkbox(-name=>'no_properties', -
163  if (@path_name) {  if (@path_name) {
164          print br,x($config->{text}->{limit});          print br,x($config->{text}->{limit});
165          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);
166    } elsif (param('path')) {
167            print hidden(-name=>'path',-values=>param('path'));
168  }  }
169  print end_form,hr;  print end_form,hr;
170    
# Line 98  if (param('search')) { Line 173  if (param('search')) {
173          my $s;          my $s;
174          # re-write query from +/- to and/and not          # re-write query from +/- to and/and not
175    
176            my @s_elem;
177    
178          my $search = param('search');          my $search = param('search');
179          my $s_phrase = "";  
180            # strip spaces
181            $search =~ s/^\s+//;
182            $search =~ s/\s+$//;
183            # fixup search string
184            $search=~tr/¹ð¾èæ©Ð®ÈÆ/¹ð¾èæ©Ð®ÈÆ/;     # 1250 -> iso8859-2
185            $search=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;
186    
187            # extract phrases and put them first
188          while ($search =~ s/\s*("[^"]+")\s*/ /) {          while ($search =~ s/\s*("[^"]+")\s*/ /) {
189                  $s .= "$1 ";                  $s .= "$1 ";
190                    push @s_elem, $1;
191          }          }
         $search =~ s/^\s+//;  
         $search =~ s/\s+$//;  
192    
193          my %words;          my %words;
194    
# Line 123  if (param('search')) { Line 207  if (param('search')) {
207                          } else {                          } else {
208                                  $s.="$2* ";                                  $s.="$2* ";
209                          }                          }
210                            push @s_elem, $2;
211                  } else {                  } else {
212                          if (@spellings && !param('no_affix')) {                          if (@spellings && !param('no_affix')) {
213                                  my $w = $_; $w =~ s/[\*\s]+//g;                                  my $w = $_; $w =~ s/[\*\s]+//g;
                                 #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";  
214                                  my $or="";                                  my $or="";
215                                  foreach my $spelling_alt (@spellings) {                                  foreach my $spelling_alt (@spellings) {
216                                          $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";                                          $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";
# Line 135  if (param('search')) { Line 219  if (param('search')) {
219                          } else {                          } else {
220                                  $s.="$_* ";                                  $s.="$_* ";
221                          }                          }
222                            push @s_elem, $_;
223                  }                  }
224          }          }
225    
226          # fixup search string          # fix multiple stars
         $s=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/;  # 1250 -> iso8859-2  
         $s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;  
227          $s=~s/\*\*+/*/g;          $s=~s/\*\*+/*/g;
228    
229          # limit to some path          # limit to some path
230          $s = "swishdocpath=(\"*$path*\") and $s" if ($path);          $s = "swishdocpath=($path) and $s" if ($path);
231    
232          my %params;     # optional parametars for swish          my %params;     # optional parametars for swish
233    
234          # default format for output          # default format for output
235          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";
236    
         # 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>  
         };  
   
237          if (@properties) {          if (@properties) {
238                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));
239                  $params{properties} = \@properties;                  $params{properties} = \@properties;
# Line 170  if (param('search')) { Line 241  if (param('search')) {
241                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));
242          }          }
243    
 #       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);  
 #       }  
   
244          my $swish = SWISH::API->new($config->{index});          my $swish = SWISH::API->new($config->{index});
   
245          $swish->AbortLastError if $swish->Error;          $swish->AbortLastError if $swish->Error;
   
246          my $results = $swish->Query($s);          my $results = $swish->Query($s);
   
247          my $hits = $results->Hits;          my $hits = $results->Hits;
248    
249    
   
250          # build pager          # build pager
251          my $current_page = param('page') || 1;          my $current_page = param('page') || 1;
252    
# Line 242  if (param('search')) { Line 269  if (param('search')) {
269                  printf (x($config->{text}->{hits}),$i,$results->Hits,$s);                  printf (x($config->{text}->{hits}),$i,$results->Hits,$s);
270          }          }
271    
272            my %path2title;
273            foreach my $p (@{$config->{path2title}->{path}}) {
274                    $path2title{$p->{dir}} = $p->{content};
275            }
276    
277            # output start of table
278            print qq{
279    <table border="0">
280            };
281            # html before and after each hit
282            my $tr_pre = qq{
283    <tr><td>
284            };
285            my $tr_post = qq{
286    </td></tr>
287            };
288    
289          for(my $i=$pager->first; $i<=$pager->last; $i++) {          for(my $i=$pager->first; $i<=$pager->last; $i++) {
290    
# Line 249  if (param('search')) { Line 292  if (param('search')) {
292                  last if (! $result);                  last if (! $result);
293    
294                  my @arr;                  my @arr;
295    
296                  foreach my $prop (@properties) {                  foreach my $prop (@properties) {
297                          if ($prop =~ m/swishdescription/) {                          if ($prop =~ m/swishdescription/) {
298                                  my $tmp = $result->Property($prop);                                  my $tmp = get_snippet(
299                                  $tmp =~ s/<[^>]+>//g;                                          $result->Property($prop),
300                                            @s_elem,
301                                    );
302                                    
303                                  push @arr, $tmp;                                  push @arr, $tmp;
304                          } else {                          } else {
305                                  push @arr, $result->Property($prop);                                  push @arr, $result->Property($prop);
# Line 263  if (param('search')) { Line 310  if (param('search')) {
310                  my $rank = $result->Property("swishrank");                  my $rank = $result->Property("swishrank");
311                  my $host = $result->Property("swishdocpath");                  my $host = $result->Property("swishdocpath");
312                  $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});
313    
314                    foreach my $p (keys %path2title) {
315                            if ($host =~ m/$p/i) {
316                                    $title =~ s/$path2title{$p}\s*[:-]+\s*//;
317                                    $title = $path2title{$p}." :: ".$title;
318                                    last;
319                            }
320                    }
321    
322                  print $tr_pre,$i,". ";                  print $tr_pre,$i,". ";
323                  # print collection name which is not link                  # print collection name which is not link
324                  if ($title =~ s/^(.+? :: )//) {                  if ($title =~ s/^(.+? :: )//) {
325                          print $1;                          print $1;
326                  }                  }
327    
328                  printf($hit_fmt, $host, $title || 'untitled', $rank, @arr);                  printf($hit_fmt, $host, $title || 'untitled', $rank, @arr);
329                  print $tr_post;                  print $tr_post;
330    
# Line 278  if (param('search')) { Line 335  if (param('search')) {
335    
336          my $nav_fmt=qq{ <a href="%s">%s</a> };          my $nav_fmt=qq{ <a href="%s">%s</a> };
337    
338            if ($pager->current_page() > $pager->first_page) {
339                    param('page', $pager->current_page - 1);
340                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');
341            }
342    
343          if ($pager->previous_set) {          if ($pager->previous_set) {
344                  param('page', $pager->previous_set);                  param('page', $pager->previous_set);
345                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
346          }          }
347    
348    
# Line 297  if (param('search')) { Line 359  if (param('search')) {
359    
360          if ($pager->next_set) {          if ($pager->next_set) {
361                  param('page', $pager->next_set);                  param('page', $pager->next_set);
362                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
363            }
364    
365            if ($pager->current_page() < $pager->last_page) {
366                    param('page', $pager->current_page + 1);
367                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');
368          }          }
369    
370            if ($config->{text}->{pages}) {
371                    $nav_html = x($config->{text}->{pages})." ".$nav_html;
372            }
373    
374          # end html table          # end html table
375          print qq{          print qq{
376  <tr><td>  <tr><td>
377  Pages: $nav_html  $nav_html
378  </td></tr>  </td></tr>
379  </table>  </table>
380          };          };

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

  ViewVC Help
Powered by ViewVC 1.1.26