/[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

Annotation of /trunk/html/swish.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (hide annotations)
Fri Feb 6 13:27:51 2004 UTC (20 years, 2 months ago) by dpavlin
File size: 7857 byte(s)
small improvements

1 dpavlin 8 #!/usr/bin/perl -w
2    
3     use strict;
4     use CGI qw/:standard -no_xhtml/;
5     use CGI::Carp qw(fatalsToBrowser);
6 dpavlin 59 use SWISH::API;
7 dpavlin 8 use XML::Simple;
8     use Lingua::Spelling::Alternative;
9     use Text::Iconv;
10 dpavlin 59 use Data::Pageset;
11 dpavlin 8
12 dpavlin 59 # for pager
13     my $pages_per_set = 20;
14    
15 dpavlin 8 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
16     my $config=XMLin(undef,
17     # keyattr => { label => "value" },
18     forcecontent => 0,
19     );
20    
21 dpavlin 18 my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});
22     sub x {
23 dpavlin 62 return if (! defined $_[0]);
24 dpavlin 18 return $from_utf8->convert($_[0]);
25     }
26    
27 dpavlin 29 # Escape <, >, & and ", and to produce valid XML
28     my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
29     my $escape_re = join '|' => keys %escape;
30     sub e {
31     my $out;
32     foreach my $v (@_) {
33     $v =~ s/($escape_re)/$escape{$1}/g;
34     $out .= $v;
35     }
36     return $out;
37     }
38 dpavlin 8
39 dpavlin 39 my @spellings;
40 dpavlin 8 # FIX: doesn't work very well
41 dpavlin 16 if ($config->{findaffix}) {
42 dpavlin 41 foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {
43     my $spelling_alt = new Lingua::Spelling::Alternative;
44     $spelling_alt->load_findaffix($findaffix);
45     push @spellings,$spelling_alt;
46     }
47 dpavlin 8 }
48 dpavlin 16 if ($config->{affix}) {
49 dpavlin 41 foreach my $affix (split(/[, ]+/,x($config->{affix}))) {
50     my $spelling_alt = new Lingua::Spelling::Alternative;
51     $spelling_alt->load_affix($affix);
52     push @spellings,$spelling_alt;
53     }
54 dpavlin 16 }
55 dpavlin 8
56     my $hits=0;
57 dpavlin 59 my $max_hits=param('max_hits') || x($config->{max_hits});
58 dpavlin 8
59     my %labels;
60     foreach (@{$config->{labels}->{label}}) {
61 dpavlin 59 next if (! $_->{value}); # skip unlimited (0)
62 dpavlin 18 $labels{$_->{value}} = x($_->{content});
63 dpavlin 8 }
64    
65 dpavlin 23 my $path = param('path'); # limit to this path
66     my %path_label;
67     my @path_name;
68     foreach (@{$config->{paths}->{path}}) {
69 dpavlin 39
70     print STDERR "##: $_->{limit}",x($_->{content}),"\n";
71 dpavlin 23 push @path_name,x($_->{limit});
72     $path_label{$_->{limit}} = x($_->{content});
73     }
74    
75 dpavlin 62 my @properties = split(/\s+/,x($config->{properties})) if ($config->{properties});
76 dpavlin 32
77 dpavlin 16 if ($config->{charset}) {
78 dpavlin 18 print header(-charset=>x($config->{charset}));
79 dpavlin 16 } else {
80     print header;
81     }
82 dpavlin 18 print start_html(-title=>x($config->{title})),start_form;
83     print x($config->{text}->{search});
84 dpavlin 16 print popup_menu(-name=>'max_hits',-values=>[ sort keys %labels ],-labels=>\%labels,-default=>$max_hits);
85 dpavlin 18 print x($config->{text}->{documents});
86 dpavlin 16 print textfield('search');
87 dpavlin 18 print submit(-value=> x($config->{text}->{submit}));
88 dpavlin 39 print br,checkbox(-name=>'no_affix', -checked=>0, -label=>x($config->{text}->{no_spell})) if (@spellings);
89 dpavlin 32 print checkbox(-name=>'no_properties', -checked=>0, -label=>($config->{text}->{no_properties})) if (@properties);
90 dpavlin 23 if (@path_name) {
91     print br,x($config->{text}->{limit});
92     print popup_menu(-name=>'path',-values=>\@path_name,-labels=>\%path_label,-default=>$path);
93     }
94 dpavlin 8 print end_form,hr;
95    
96     if (param('search')) {
97    
98     my $s;
99     # re-write query from +/- to and/and not
100    
101     my $search = param('search');
102     my $s_phrase = "";
103     while ($search =~ s/\s*("[^"]+")\s*/ /) {
104     $s .= "$1 ";
105     }
106     $search =~ s/^\s+//;
107     $search =~ s/\s+$//;
108    
109 dpavlin 41 my %words;
110    
111 dpavlin 8 foreach (split(/\s+/,$search)) {
112     if (m/^([+-])(\S+)/) {
113     $s.= ($s) ? "and " : "";
114     $s.="not " if ($1 eq "-");
115 dpavlin 39 if (@spellings && !param('no_affix')) {
116 dpavlin 8 my $w = $2; $w =~ s/[\*\s]+//g;
117     $w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/;
118 dpavlin 39 my $or="";
119     foreach my $spelling_alt (@spellings) {
120     $s.="$or$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 ";
121     $or = "or ";
122     }
123 dpavlin 8 } else {
124     $s.="$2* ";
125     }
126     } else {
127 dpavlin 39 if (@spellings && !param('no_affix')) {
128 dpavlin 8 my $w = $_; $w =~ s/[\*\s]+//g;
129     #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";
130 dpavlin 39 my $or="";
131     foreach my $spelling_alt (@spellings) {
132     $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";
133     $or = "or ";
134     }
135 dpavlin 8 } else {
136     $s.="$_* ";
137     }
138     }
139     }
140    
141     # fixup search string
142     $s=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/; # 1250 -> iso8859-2
143     $s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;
144     $s=~s/\*\*+/*/g;
145    
146 dpavlin 23 # limit to some path
147 dpavlin 39 $s = "swishdocpath=(\"*$path*\") and $s" if ($path);
148 dpavlin 23
149 dpavlin 22 my %params; # optional parametars for swish
150    
151 dpavlin 32 # default format for output
152     my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";
153 dpavlin 21
154 dpavlin 59 # output start of table
155     print qq{
156     <table border="0">
157     };
158     # html before and after each hit
159     my $tr_pre = qq{
160     <tr><td>
161     };
162     my $tr_post = qq{
163     </td></tr>
164     };
165    
166 dpavlin 32 if (@properties) {
167     $hit_fmt = x($config->{hit}) if (! param('no_properties'));
168 dpavlin 39 $params{properties} = \@properties;
169 dpavlin 32 } else {
170     $hit_fmt = x($config->{hit}) if (x($config->{hit}));
171     }
172    
173 dpavlin 59 # my $sh = SWISH->connect('Fork',
174     # prog => x($config->{prog}),
175     # indexes => x($config->{index}),
176     # results => sub {
177     # my ($sh,$hit) = @_;
178     #
179     # if ($config->{url}) {
180     # printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties));
181     # } else {
182     # printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) );
183     #
184     # }
185     #
186     ## print $_[1]->as_string,"<br>\n";
187     ## my @fields = $hit->field_names;
188     ## print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields;
189     # },
190     # maxhits => param('max_hits') || $max_hits,
191     # \%params,
192     # );
193     #
194     # die $SWISH::errstr unless $sh;
195     #
196     # $hits = $sh->query($s);
197     #
198     # if ($hits && $hits > 0) {
199     # print p,hr;
200     # printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s);
201     # } else {
202     # print p;
203     # printf (x($config->{text}->{no_hits}),$s,$sh->errstr);
204     # }
205     # if ($hits && $hits > 0) {
206     # print p,hr;
207     # printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s);
208     # } else {
209     # print p;
210     # printf (x($config->{text}->{no_hits}),$s,$sh->errstr);
211     # }
212    
213     my $swish = SWISH::API->new($config->{index});
214    
215     $swish->AbortLastError if $swish->Error;
216    
217     my $results = $swish->Query($s);
218    
219     my $hits = $results->Hits;
220    
221    
222    
223     # build pager
224     my $current_page = param('page') || 1;
225    
226     my $pager = Data::Pageset->new({
227     'total_entries' => $hits,
228     'entries_per_page' => $max_hits,
229     'current_page' => $current_page,
230     'pages_per_set' => $pages_per_set,
231     });
232    
233     $results->SeekResult( $pager->first - 1 );
234    
235     # get number of entries on this page
236     my $i = $pager->entries_on_this_page;
237    
238     # print number of hits or error message
239     if ( !$hits ) {
240     printf (x($config->{text}->{no_hits}),$s,$swish->ErrorString);
241     } else {
242     printf (x($config->{text}->{hits}),$i,$results->Hits,$s);
243 dpavlin 57 }
244    
245 dpavlin 8
246 dpavlin 59 for(my $i=$pager->first; $i<=$pager->last; $i++) {
247    
248     my $result = $results->NextResult;
249     last if (! $result);
250    
251     my @arr;
252     foreach my $prop (@properties) {
253     if ($prop =~ m/swishdescription/) {
254     my $tmp = $result->Property($prop);
255     $tmp =~ s/<[^>]+>//g;
256     push @arr, $tmp;
257 dpavlin 18 } else {
258 dpavlin 59 push @arr, $result->Property($prop);
259 dpavlin 18 }
260 dpavlin 59 }
261 dpavlin 18
262 dpavlin 59 print $tr_pre,$i,". ";
263     if ($config->{url}) {
264     printf($hit_fmt, "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath"),
265     e($result->Property("swishtitle")) || 'untitled',
266     $result->Property("swishrank"),
267     @arr);
268     } else {
269     printf($hit_fmt, $result->Property("swishdocpath"),
270     e($result->Property("swishtitle")) || 'untitled',
271     $result->Property("swishrank"),
272     @arr);
273     }
274     print $tr_post;
275 dpavlin 8
276 dpavlin 59 }
277 dpavlin 8
278 dpavlin 59 # pager navigation
279     my $nav_html;
280 dpavlin 8
281 dpavlin 59 my $nav_fmt=qq{ <a href="%s">%s</a> };
282 dpavlin 8
283 dpavlin 59 if ($pager->previous_set) {
284     param('page', $pager->previous_set);
285     $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');
286 dpavlin 8 }
287 dpavlin 59
288    
289     foreach my $p (@{$pager->pages_in_set()}) {
290 dpavlin 60 next if ($p < 0);
291 dpavlin 59 # for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) {
292     if($p == $pager->current_page()) {
293     $nav_html .= "<b>$p</b> ";
294     } else {
295     param('page', $p);
296     $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),$p);
297     }
298     }
299    
300     if ($pager->next_set) {
301     param('page', $pager->next_set);
302     $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');
303     }
304    
305     # end html table
306     print qq{
307     <tr><td>
308     Pages: $nav_html
309     </td></tr>
310     </table>
311     };
312    
313    
314    
315 dpavlin 8 } else {
316 dpavlin 18 print p(x($config->{text}->{footer}));
317 dpavlin 8 }

Properties

Name Value
cvs2svn:cvs-rev 1.16
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26