/[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 83 - (hide annotations)
Sun Aug 29 18:26:58 2004 UTC (19 years, 9 months ago) by dpavlin
File size: 8523 byte(s)
produce valid html, escape characters in snippet

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26