/[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 32 by dpavlin, Wed Apr 30 12:40:09 2003 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 31  sub e { Line 37  sub e {
37          return $out;          return $out;
38  }  }
39    
40  my $spelling_alt;  my @spellings;
41  # FIX: doesn't work very well  # FIX: doesn't work very well
42  if ($config->{findaffix}) {  if ($config->{findaffix}) {
43          $spelling_alt = new Lingua::Spelling::Alternative;          foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {
44          $spelling_alt->load_findaffix(x($config->{findaffix}));                  next if (! -f $findaffix);
45                    my $spelling_alt = new Lingua::Spelling::Alternative;
46                    $spelling_alt->load_findaffix($findaffix);
47                    push @spellings,$spelling_alt;
48            }
49  }  }
50  if ($config->{affix}) {  if ($config->{affix}) {
51          $spelling_alt = new Lingua::Spelling::Alternative;          foreach my $affix (split(/[, ]+/,x($config->{affix}))) {
52          $spelling_alt->load_affix(x($config->{affix}));                  next if (! -f $affix);
53                    my $spelling_alt = new Lingua::Spelling::Alternative;
54                    $spelling_alt->load_affix($affix);
55                    push @spellings,$spelling_alt;
56            }
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 54  my $path = param('path');      # limit to thi Line 69  my $path = param('path');      # limit to thi
69  my %path_label;  my %path_label;
70  my @path_name;  my @path_name;
71  foreach (@{$config->{paths}->{path}}) {  foreach (@{$config->{paths}->{path}}) {
72    
73    print STDERR "##: $_->{limit}",x($_->{content}),"\n";
74          push @path_name,x($_->{limit});          push @path_name,x($_->{limit});
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 71  print popup_menu(-name=>'max_hits',-valu Line 88  print popup_menu(-name=>'max_hits',-valu
88  print x($config->{text}->{documents});  print x($config->{text}->{documents});
89  print textfield('search');  print textfield('search');
90  print submit(-value=> x($config->{text}->{submit}));  print submit(-value=> x($config->{text}->{submit}));
91  print br,checkbox(-name=>'no_affix', -checked=>0, -label=>x($config->{text}->{no_spell})) if ($spelling_alt);  print br,checkbox(-name=>'no_affix', -checked=>0, -label=>x($config->{text}->{no_spell})) if (@spellings);
92  print checkbox(-name=>'no_properties', -checked=>0, -label=>($config->{text}->{no_properties})) if (@properties);  print checkbox(-name=>'no_properties', -checked=>0, -label=>($config->{text}->{no_properties})) if (@properties);
93  if (@path_name) {  if (@path_name) {
94          print br,x($config->{text}->{limit});          print br,x($config->{text}->{limit});
# Line 92  if (param('search')) { Line 109  if (param('search')) {
109          $search =~ s/^\s+//;          $search =~ s/^\s+//;
110          $search =~ s/\s+$//;          $search =~ s/\s+$//;
111    
112            my %words;
113    
114          foreach (split(/\s+/,$search)) {          foreach (split(/\s+/,$search)) {
115                  if (m/^([+-])(\S+)/) {                  if (m/^([+-])(\S+)/) {
116                          $s.= ($s) ? "and " : "";                          $s.= ($s) ? "and " : "";
117                          $s.="not " if ($1 eq "-");                          $s.="not " if ($1 eq "-");
118                          if ($spelling_alt && !param('no_affix')) {                          if (@spellings && !param('no_affix')) {
119                                  my $w = $2; $w =~ s/[\*\s]+//g;                                  my $w = $2; $w =~ s/[\*\s]+//g;
120                                  $w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/;                                  $w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/;
121                                  $s.="$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 ";                                  my $or="";
122                                    foreach my $spelling_alt (@spellings) {
123                                            $s.="$or$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 ";
124                                            $or = "or ";
125                                    }
126                          } else {                          } else {
127                                  $s.="$2* ";                                  $s.="$2* ";
128                          }                          }
129                  } else {                  } else {
130                          if ($spelling_alt && !param('no_affix')) {                          if (@spellings && !param('no_affix')) {
131                                  my $w = $_; $w =~ s/[\*\s]+//g;                                  my $w = $_; $w =~ s/[\*\s]+//g;
132                                  #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";                                  #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";
133                                  $s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";                                  my $or="";
134                                    foreach my $spelling_alt (@spellings) {
135                                            $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";
136                                            $or = "or ";
137                                    }
138                          } else {                          } else {
139                                  $s.="$_* ";                                  $s.="$_* ";
140                          }                          }
# Line 120  if (param('search')) { Line 147  if (param('search')) {
147          $s=~s/\*\*+/*/g;          $s=~s/\*\*+/*/g;
148    
149          # limit to some path          # limit to some path
150          $s = "swishdocpath=(\"$path\") and $s" if ($path);          $s = "swishdocpath=(\"*$path*\") and $s" if ($path);
151    
152          my %params;     # optional parametars for swish          my %params;     # optional parametars for swish
153    
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 if (@properties);                  $params{properties} = \@properties;
172          } else {          } else {
173                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));                  $hit_fmt = x($config->{hit}) if (x($config->{hit}));
174          }          }
175    
176          my $sh = SWISH->connect('Fork',          my $swish = SWISH::API->new($config->{index});
177                  prog     => x($config->{prog}),          $swish->AbortLastError if $swish->Error;
178                  indexes  => x($config->{index}),          my $results = $swish->Query($s);
179                  results  => sub {          my $hits = $results->Hits;
180                          my ($sh,$hit) = @_;  
181    
182            # 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 %path2title;
205            use Data::Dumper;
206            foreach my $p (@{$config->{path2title}->{path}}) {
207                    $path2title{$p->{dir}} = $p->{content};
208            }
209    
210                          if ($config->{url}) {          for(my $i=$pager->first; $i<=$pager->last; $i++) {
211                                  printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties));  
212                    my $result = $results->NextResult;
213                    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 > 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.32  
changed lines
  Added in v.80

  ViewVC Help
Powered by ViewVC 1.1.26