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

Contents of /trunk/html/swish.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 73 - (show annotations)
Tue Apr 6 19:21:07 2004 UTC (20 years ago) by dpavlin
File size: 7885 byte(s)
print collection name before link - collection name
is part of document title up to first " :: " delimiter

1 #!/usr/bin/perl -w
2
3 use strict;
4 use CGI qw/:standard -no_xhtml/;
5 use CGI::Carp qw(fatalsToBrowser);
6 use SWISH::API;
7 use XML::Simple;
8 use Lingua::Spelling::Alternative;
9 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
16 my $config=XMLin(undef,
17 # keyattr => { label => "value" },
18 forcecontent => 0,
19 );
20
21 my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});
22 sub x {
23 return if (! defined $_[0]);
24 return $from_utf8->convert($_[0]);
25 }
26
27 # 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
39 my @spellings;
40 # FIX: doesn't work very well
41 if ($config->{findaffix}) {
42 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 }
48 if ($config->{affix}) {
49 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 }
55
56 my $hits=0;
57 my $max_hits=param('max_hits') || x($config->{max_hits});
58
59 my %labels;
60 foreach (@{$config->{labels}->{label}}) {
61 next if (! $_->{value}); # skip unlimited (0)
62 $labels{$_->{value}} = x($_->{content});
63 }
64
65 my $path = param('path'); # limit to this path
66 my %path_label;
67 my @path_name;
68 foreach (@{$config->{paths}->{path}}) {
69
70 print STDERR "##: $_->{limit}",x($_->{content}),"\n";
71 push @path_name,x($_->{limit});
72 $path_label{$_->{limit}} = x($_->{content});
73 }
74
75 my @properties = split(/\s+/,x($config->{properties})) if ($config->{properties});
76
77 if ($config->{charset}) {
78 print header(-charset=>x($config->{charset}));
79 } else {
80 print header;
81 }
82 print start_html(-title=>x($config->{title})),start_form;
83 print x($config->{text}->{search});
84 print popup_menu(-name=>'max_hits',-values=>[ sort keys %labels ],-labels=>\%labels,-default=>$max_hits);
85 print x($config->{text}->{documents});
86 print textfield('search');
87 print submit(-value=> x($config->{text}->{submit}));
88 print br,checkbox(-name=>'no_affix', -checked=>0, -label=>x($config->{text}->{no_spell})) if (@spellings);
89 print checkbox(-name=>'no_properties', -checked=>0, -label=>($config->{text}->{no_properties})) if (@properties);
90 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 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 my %words;
110
111 foreach (split(/\s+/,$search)) {
112 if (m/^([+-])(\S+)/) {
113 $s.= ($s) ? "and " : "";
114 $s.="not " if ($1 eq "-");
115 if (@spellings && !param('no_affix')) {
116 my $w = $2; $w =~ s/[\*\s]+//g;
117 $w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/;
118 my $or="";
119 foreach my $spelling_alt (@spellings) {
120 $s.="$or$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 ";
121 $or = "or ";
122 }
123 } else {
124 $s.="$2* ";
125 }
126 } else {
127 if (@spellings && !param('no_affix')) {
128 my $w = $_; $w =~ s/[\*\s]+//g;
129 #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";
130 my $or="";
131 foreach my $spelling_alt (@spellings) {
132 $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";
133 $or = "or ";
134 }
135 } 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 # limit to some path
147 $s = "swishdocpath=(\"*$path*\") and $s" if ($path);
148
149 my %params; # optional parametars for swish
150
151 # default format for output
152 my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";
153
154 # 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 if (@properties) {
167 $hit_fmt = x($config->{hit}) if (! param('no_properties'));
168 $params{properties} = \@properties;
169 } else {
170 $hit_fmt = x($config->{hit}) if (x($config->{hit}));
171 }
172
173 # 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 }
244
245
246 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 } else {
258 push @arr, $result->Property($prop);
259 }
260 }
261
262 my $title = e($result->Property("swishtitle")) || 'untitled';
263 my $rank = $result->Property("swishrank");
264 my $host = $result->Property("swishdocpath");
265 $host = "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath") if ($config->{url});
266 print $tr_pre,$i,". ";
267 # print collection name which is not link
268 if ($title =~ s/^(.+? :: )//) {
269 print $1;
270 }
271 printf($hit_fmt, $host, $title || 'untitled', $rank, @arr);
272 print $tr_post;
273
274 }
275
276 # pager navigation
277 my $nav_html;
278
279 my $nav_fmt=qq{ <a href="%s">%s</a> };
280
281 if ($pager->previous_set) {
282 param('page', $pager->previous_set);
283 $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&lt;&lt;');
284 }
285
286
287 foreach my $p (@{$pager->pages_in_set()}) {
288 next if ($p < 0);
289 # for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) {
290 if($p == $pager->current_page()) {
291 $nav_html .= "<b>$p</b> ";
292 } else {
293 param('page', $p);
294 $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),$p);
295 }
296 }
297
298 if ($pager->next_set) {
299 param('page', $pager->next_set);
300 $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'&gt;&gt;');
301 }
302
303 # end html table
304 print qq{
305 <tr><td>
306 Pages: $nav_html
307 </td></tr>
308 </table>
309 };
310
311
312
313 } else {
314 print p(x($config->{text}->{footer}));
315 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26