/[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 57 by dpavlin, Sun Jan 25 16:49:50 2004 UTC revision 80 by dpavlin, Sat May 22 18:33:33 2004 UTC
# Line 3  Line 3 
3  use strict;  use strict;
4  use CGI qw/:standard -no_xhtml/;  use CGI qw/:standard -no_xhtml/;
5  use CGI::Carp qw(fatalsToBrowser);  use CGI::Carp qw(fatalsToBrowser);
6  use SWISH;  use SWISH::API;
7  use XML::Simple;  use XML::Simple;
8  use Lingua::Spelling::Alternative;  use Lingua::Spelling::Alternative;
9  use Text::Iconv;  use Text::Iconv;
10    use Data::Pageset;
11    
12    # for pager
13    my $pages_per_set = 20;
14    
15  Text::Iconv->raise_error(0);     # Conversion errors raise exceptions  Text::Iconv->raise_error(0);     # Conversion errors raise exceptions
16  my $config=XMLin(undef,  my $config=XMLin(undef,
17  #               keyattr => { label => "value" },  #               keyattr => { label => "value" },
18                  forcecontent => 0,                  forcecontent => 0,
19                    ForceArray => [ 'path' ],
20          );          );
21    
22  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});  my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});
23  sub x {  sub x {
24            return if (! defined $_[0]);
25          return $from_utf8->convert($_[0]);          return $from_utf8->convert($_[0]);
26  }  }
27    
# Line 35  my @spellings; Line 41  my @spellings;
41  # FIX: doesn't work very well  # FIX: doesn't work very well
42  if ($config->{findaffix}) {  if ($config->{findaffix}) {
43          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {
44                    next if (! -f $findaffix);
45                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
46                  $spelling_alt->load_findaffix($findaffix);                  $spelling_alt->load_findaffix($findaffix);
47                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 42  if ($config->{findaffix}) { Line 49  if ($config->{findaffix}) {
49  }  }
50  if ($config->{affix}) {  if ($config->{affix}) {
51          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {
52                    next if (! -f $affix);
53                  my $spelling_alt = new Lingua::Spelling::Alternative;                  my $spelling_alt = new Lingua::Spelling::Alternative;
54                  $spelling_alt->load_affix($affix);                  $spelling_alt->load_affix($affix);
55                  push @spellings,$spelling_alt;                  push @spellings,$spelling_alt;
# Line 49  if ($config->{affix}) { Line 57  if ($config->{affix}) {
57  }  }
58    
59  my $hits=0;  my $hits=0;
60  my $max_hits=x($config->{max_hits});  my $max_hits=param('max_hits') || x($config->{max_hits});
61    
62  my %labels;  my %labels;
63  foreach (@{$config->{labels}->{label}}) {  foreach (@{$config->{labels}->{label}}) {
64            next if (! $_->{value});        # skip unlimited (0)
65          $labels{$_->{value}} = x($_->{content});          $labels{$_->{value}} = x($_->{content});
66  }  }
67    
# Line 66  print STDERR "##: $_->{limit}",x($_->{co Line 75  print STDERR "##: $_->{limit}",x($_->{co
75          $path_label{$_->{limit}} = x($_->{content});          $path_label{$_->{limit}} = x($_->{content});
76  }  }
77    
78  my @properties = split(/\s+/,x($config->{properties}));  my @properties = split(/\s+/,x($config->{properties})) if ($config->{properties});
79    
80  if ($config->{charset}) {  if ($config->{charset}) {
81          print header(-charset=>x($config->{charset}));          print header(-charset=>x($config->{charset}));
# Line 145  if (param('search')) { Line 154  if (param('search')) {
154          # default format for output          # default format for output
155          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";
156    
157            # output start of table
158            print qq{
159    <table border="0">
160            };
161            # html before and after each hit
162            my $tr_pre = qq{
163    <tr><td>
164            };
165            my $tr_post = qq{
166    </td></tr>
167            };
168    
169          if (@properties) {          if (@properties) {
170                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));
171                  $params{properties} = \@properties;                  $params{properties} = \@properties;
# Line 152  if (param('search')) { Line 173  if (param('search')) {
173                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));
174          }          }
175    
176          sub kill_html {          my $swish = SWISH::API->new($config->{index});
177                  my @out;          $swish->AbortLastError if $swish->Error;
178                  foreach (@_) {          my $results = $swish->Query($s);
179                          s/<[^>]+>//g;          my $hits = $results->Hits;
180                          push @out,$_;  
181                  }  
182                  return @out;          # build pager
183            my $current_page = param('page') || 1;
184    
185            my $pager = Data::Pageset->new({
186                    'total_entries' => $hits,
187                    'entries_per_page' => $max_hits,
188                    'current_page' => $current_page,
189                    'pages_per_set' => $pages_per_set,
190            });
191    
192            $results->SeekResult( $pager->first - 1 );
193    
194            # get number of entries on this page
195            my $i = $pager->entries_on_this_page;
196    
197            # print number of hits or error message
198            if ( !$hits ) {
199                    printf (x($config->{text}->{no_hits}),$s,$swish->ErrorString);
200            } else {
201                    printf (x($config->{text}->{hits}),$i,$results->Hits,$s);
202          }          }
203    
204          my $sh = SWISH->connect('Fork',          my %path2title;
205                  prog     => x($config->{prog}),          use Data::Dumper;
206                  indexes  => x($config->{index}),          foreach my $p (@{$config->{path2title}->{path}}) {
207                  results  => sub {                  $path2title{$p->{dir}} = $p->{content};
208                          my ($sh,$hit) = @_;          }
209    
210            for(my $i=$pager->first; $i<=$pager->last; $i++) {
211    
212                          if ($config->{url}) {                  my $result = $results->NextResult;
213                                  printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties));                  last if (! $result);
214    
215                    my @arr;
216                    foreach my $prop (@properties) {
217                            if ($prop =~ m/swishdescription/) {
218                                    my $tmp = $result->Property($prop);
219                                    $tmp =~ s/<[^>]+>//g;
220                                    push @arr, $tmp;
221                          } else {                          } else {
222                                  printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) );                                  push @arr, $result->Property($prop);
223                            }
224                    }
225    
226                    my $title = e($result->Property("swishtitle")) || 'untitled';
227                    my $rank = $result->Property("swishrank");
228                    my $host = $result->Property("swishdocpath");
229                    $host = "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath") if ($config->{url});
230    
231                    foreach my $p (keys %path2title) {
232                            if ($host =~ m/$p/i) {
233                                    $title =~ s/$path2title{$p}\s*[:-]+\s*//;
234                                    $title = $path2title{$p}." :: ".$title;
235                                    last;
236                          }                          }
237                    }
238    
239  #                       print $_[1]->as_string,"<br>\n";                  print $tr_pre,$i,". ";
240  #                       my @fields = $hit->field_names;                  # print collection name which is not link
241  #                       print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields;                  if ($title =~ s/^(.+? :: )//) {
242                  },                          print $1;
243                  maxhits => param('max_hits') || $max_hits,                  }
                 \%params,  
         );  
244    
245          die $SWISH::errstr unless $sh;                  printf($hit_fmt, $host, $title || 'untitled', $rank, @arr);
246                    print $tr_post;
247    
248            }
249    
250          $hits = $sh->query($s);          # pager navigation
251            my $nav_html;
252    
253          if ($hits && $hits > 0) {          my $nav_fmt=qq{ <a href="%s">%s</a> };
254                  print p,hr;  
255                  printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s);          if ($pager->current_page() > $pager->first_page) {
256          } else {                  param('page', $pager->current_page - 1);
257                  print p;                  $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');
258                  printf (x($config->{text}->{no_hits}),$s,$sh->errstr);          }
259    
260            if ($pager->previous_set) {
261                    param('page', $pager->previous_set);
262                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
263            }
264    
265    
266            foreach my $p (@{$pager->pages_in_set()}) {
267                    next if ($p < 0);
268    #       for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) {
269                    if($p == $pager->current_page()) {
270                            $nav_html .= "<b>$p</b> ";
271                    } else {
272                            param('page', $p);
273                            $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),$p);
274                    }
275            }
276    
277            if ($pager->next_set) {
278                    param('page', $pager->next_set);
279                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..');
280          }          }
281    
282            if ($pager->current_page() < $pager->last_page) {
283                    param('page', $pager->current_page + 1);
284                    $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');
285            }
286    
287            if ($config->{text}->{pages}) {
288                    $nav_html = x($config->{text}->{pages})." ".$nav_html;
289            }
290    
291            # end html table
292            print qq{
293    <tr><td>
294    $nav_html
295    </td></tr>
296    </table>
297            };
298    
299    
300    
301  } else {  } else {
302          print p(x($config->{text}->{footer}));          print p(x($config->{text}->{footer}));
303  }  }

Legend:
Removed from v.57  
changed lines
  Added in v.80

  ViewVC Help
Powered by ViewVC 1.1.26