/[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 89 by dpavlin, Tue Aug 31 09:04:15 2004 UTC
# Line 8  use XML::Simple; Line 8  use XML::Simple;
8  use Lingua::Spelling::Alternative;  use Lingua::Spelling::Alternative;
9  use Text::Iconv;  use Text::Iconv;
10  use Data::Pageset;  use Data::Pageset;
11    use FormatResult;
12    
13  # for pager  # for pager
14  my $pages_per_set = 20;  my $pages_per_set = 20;
# Line 16  Text::Iconv->raise_error(0);     # Conve Line 17  Text::Iconv->raise_error(0);     # Conve
17  my $config=XMLin(undef,  my $config=XMLin(undef,
18  #               keyattr => { label => "value" },  #               keyattr => { label => "value" },
19                  forcecontent => 0,                  forcecontent => 0,
20                    ForceArray => [ 'path' ],
21          );          );
22    
23  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});
24  sub x {  sub x {
25            return if (! defined $_[0]);
26          return $from_utf8->convert($_[0]);          return $from_utf8->convert($_[0]);
27  }  }
28    
# Line 39  my @spellings; Line 42  my @spellings;
42  # FIX: doesn't work very well  # FIX: doesn't work very well
43  if ($config->{findaffix}) {  if ($config->{findaffix}) {
44          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {
45                    next if (! -f $findaffix);
46                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
47                  $spelling_alt->load_findaffix($findaffix);                  $spelling_alt->load_findaffix($findaffix);
48                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 46  if ($config->{findaffix}) { Line 50  if ($config->{findaffix}) {
50  }  }
51  if ($config->{affix}) {  if ($config->{affix}) {
52          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {
53                    next if (! -f $affix);
54                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
55                  $spelling_alt->load_affix($affix);                  $spelling_alt->load_affix($affix);
56                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 61  foreach (@{$config->{labels}->{label}}) Line 66  foreach (@{$config->{labels}->{label}})
66          $labels{$_->{value}} = x($_->{content});          $labels{$_->{value}} = x($_->{content});
67  }  }
68    
69  my $path = param('path');       # limit to this path  my $path;
70    # limit to this path
71    $path .= '"'.join('*" or "',param('path')).'*"' if (param('path'));
72  my %path_label;  my %path_label;
73  my @path_name;  my @path_name;
74  foreach (@{$config->{paths}->{path}}) {  foreach (@{$config->{paths}->{path}}) {
   
 print STDERR "##: $_->{limit}",x($_->{content}),"\n";  
75          push @path_name,x($_->{limit});          push @path_name,x($_->{limit});
76          $path_label{$_->{limit}} = x($_->{content});          $path_label{$_->{limit}} = x($_->{content});
77  }  }
78    
79  my @properties = split(/\s+/,x($config->{properties}));  my @properties = split(/\s+/,x($config->{properties})) if ($config->{properties});
80    
81  if ($config->{charset}) {  if ($config->{charset}) {
82          print header(-charset=>x($config->{charset}));          print header(-charset=>x($config->{charset}));
# Line 89  print checkbox(-name=>'no_properties', - Line 94  print checkbox(-name=>'no_properties', -
94  if (@path_name) {  if (@path_name) {
95          print br,x($config->{text}->{limit});          print br,x($config->{text}->{limit});
96          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);
97    } elsif (param('path')) {
98            print hidden(-name=>'path',-values=>param('path'));
99  }  }
100  print end_form,hr;  print end_form,hr;
101    
# Line 97  if (param('search')) { Line 104  if (param('search')) {
104          my $s;          my $s;
105          # re-write query from +/- to and/and not          # re-write query from +/- to and/and not
106    
107            my @s_highlite;
108    
109          my $search = param('search');          my $search = param('search');
110          my $s_phrase = "";  
111            # strip spaces
112            $search =~ s/^\s+//;
113            $search =~ s/\s+$//;
114            # fixup search string
115            $search=~tr/¹ð¾èæ©Ð®ÈÆ/¹ð¾èæ©Ð®ÈÆ/;     # 1250 -> iso8859-2
116            $search=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;
117    
118            # extract phrases and put them first
119          while ($search =~ s/\s*("[^"]+")\s*/ /) {          while ($search =~ s/\s*("[^"]+")\s*/ /) {
120                  $s .= "$1 ";                  $s .= "$1 ";
121                    push @s_highlite, $1;
122          }          }
         $search =~ s/^\s+//;  
         $search =~ s/\s+$//;  
123    
124          my %words;          my %words;
125    
# Line 122  if (param('search')) { Line 138  if (param('search')) {
138                          } else {                          } else {
139                                  $s.="$2* ";                                  $s.="$2* ";
140                          }                          }
141                            push @s_highlite, $2 if ($1 ne "-");
142                  } else {                  } else {
143                          if (@spellings && !param('no_affix')) {                          if (@spellings && !param('no_affix')) {
144                                  my $w = $_; $w =~ s/[\*\s]+//g;                                  my $w = $_; $w =~ s/[\*\s]+//g;
                                 #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";  
145                                  my $or="";                                  my $or="";
146                                  foreach my $spelling_alt (@spellings) {                                  foreach my $spelling_alt (@spellings) {
147                                          $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";                                          $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";
# Line 134  if (param('search')) { Line 150  if (param('search')) {
150                          } else {                          } else {
151                                  $s.="$_* ";                                  $s.="$_* ";
152                          }                          }
153                            push @s_highlite, $_;
154                  }                  }
155          }          }
156    
157          # fixup search string          # fix multiple stars
         $s=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/;  # 1250 -> iso8859-2  
         $s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;  
158          $s=~s/\*\*+/*/g;          $s=~s/\*\*+/*/g;
159    
160          # limit to some path          # limit to some path
161          $s = "swishdocpath=(\"*$path*\") and $s" if ($path);          $s = "swishdocpath=($path) and $s" if ($path);
162    
163          my %params;     # optional parametars for swish          my %params;     # optional parametars for swish
164    
165          # default format for output          # default format for output
166          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";
167    
         # 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>  
         };  
   
168          if (@properties) {          if (@properties) {
169                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));
170                  $params{properties} = \@properties;                  $params{properties} = \@properties;
# Line 169  if (param('search')) { Line 172  if (param('search')) {
172                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));
173          }          }
174    
 #       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);  
 #       }  
   
175          my $swish = SWISH::API->new($config->{index});          my $swish = SWISH::API->new($config->{index});
   
176          $swish->AbortLastError if $swish->Error;          $swish->AbortLastError if $swish->Error;
   
177          my $results = $swish->Query($s);          my $results = $swish->Query($s);
   
178          my $hits = $results->Hits;          my $hits = $results->Hits;
179    
180    
   
181          # build pager          # build pager
182          my $current_page = param('page') || 1;          my $current_page = param('page') || 1;
183    
# Line 241  if (param('search')) { Line 200  if (param('search')) {
200                  printf (x($config->{text}->{hits}),$i,$results->Hits,$s);                  printf (x($config->{text}->{hits}),$i,$results->Hits,$s);
201          }          }
202    
203            my %path2title;
204            foreach my $p (@{$config->{path2title}->{path}}) {
205                    $path2title{$p->{dir}} = $p->{content};
206            }
207    
208            # output start of table
209            print qq{
210    <table border="0">
211            };
212            # html before and after each hit
213            my $tr_pre = qq{
214    <tr><td>
215            };
216            my $tr_post = qq{
217    </td></tr>
218            };
219    
220          for(my $i=$pager->first; $i<=$pager->last; $i++) {          for(my $i=$pager->first; $i<=$pager->last; $i++) {
221    
# Line 248  if (param('search')) { Line 223  if (param('search')) {
223                  last if (! $result);                  last if (! $result);
224    
225                  my @arr;                  my @arr;
226    
227                  foreach my $prop (@properties) {                  foreach my $prop (@properties) {
228                          if ($prop =~ m/swishdescription/) {                          if ($prop =~ m/swishdescription/) {
229                                  my $tmp = $result->Property($prop);                                  my $tmp = FormatResult::get_snippet(
230                                  $tmp =~ s/<[^>]+>//g;                                          e($result->Property($prop)),
231                                            @s_highlite,
232                                    );
233                                    
234                                  push @arr, $tmp;                                  push @arr, $tmp;
235                          } else {                          } else {
236                                  push @arr, $result->Property($prop);                                  push @arr, $result->Property($prop);
237                          }                          }
238                  }                  }
239    
240                    my $title = e($result->Property("swishtitle")) || 'untitled';
241                    my $rank = $result->Property("swishrank");
242                    my $host = $result->Property("swishdocpath");
243                    $host = "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath") if ($config->{url});
244    
245                    foreach my $p (keys %path2title) {
246                            if ($host =~ m/$p/i) {
247                                    $title =~ s/$path2title{$p}\s*[:-]+\s*//;
248                                    $title = $path2title{$p}." :: ".$title;
249                                    last;
250                            }
251                    }
252    
253                  print $tr_pre,$i,". ";                  print $tr_pre,$i,". ";
254                  if ($config->{url}) {                  # print collection name which is not link
255                          printf($hit_fmt, "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath"),                  if ($title =~ s/^(.+? :: )//) {
256                                  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);  
257                  }                  }
258    
259                    printf($hit_fmt, $host, $title || 'untitled', $rank, @arr);
260                  print $tr_post;                  print $tr_post;
261    
262          }          }
# Line 279  if (param('search')) { Line 266  if (param('search')) {
266    
267          my $nav_fmt=qq{ <a href="%s">%s</a> };          my $nav_fmt=qq{ <a href="%s">%s</a> };
268    
269            if ($pager->current_page() > $pager->first_page) {
270                    param('page', $pager->current_page - 1);
271                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');
272            }
273    
274          if ($pager->previous_set) {          if ($pager->previous_set) {
275                  param('page', $pager->previous_set);                  param('page', $pager->previous_set);
276                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
277          }          }
278    
279    
280          foreach my $p (@{$pager->pages_in_set()}) {          foreach my $p (@{$pager->pages_in_set()}) {
281                    next if ($p < 0);
282  #       for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) {  #       for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) {
283                  if($p == $pager->current_page()) {                  if($p == $pager->current_page()) {
284                          $nav_html .= "<b>$p</b> ";                          $nav_html .= "<b>$p</b> ";
# Line 297  if (param('search')) { Line 290  if (param('search')) {
290    
291          if ($pager->next_set) {          if ($pager->next_set) {
292                  param('page', $pager->next_set);                  param('page', $pager->next_set);
293                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
294            }
295    
296            if ($pager->current_page() < $pager->last_page) {
297                    param('page', $pager->current_page + 1);
298                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');
299          }          }
300    
301            if ($config->{text}->{pages}) {
302                    $nav_html = x($config->{text}->{pages})." ".$nav_html;
303            }
304    
305          # end html table          # end html table
306          print qq{          print qq{
307  <tr><td>  <tr><td>
308  Pages: $nav_html  $nav_html
309  </td></tr>  </td></tr>
310  </table>  </table>
311          };          };

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

  ViewVC Help
Powered by ViewVC 1.1.26