/[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 41 - (hide annotations)
Sun Jun 1 12:13:36 2003 UTC (20 years, 11 months ago) by dpavlin
File size: 5020 byte(s)
- support for more than one affix or findaffix file at same time

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     use SWISH;
7     use XML::Simple;
8     use Lingua::Spelling::Alternative;
9     use Text::Iconv;
10    
11     Text::Iconv->raise_error(0); # Conversion errors raise exceptions
12     my $config=XMLin(undef,
13     # keyattr => { label => "value" },
14     forcecontent => 0,
15     );
16    
17 dpavlin 18 my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});
18     sub x {
19     return $from_utf8->convert($_[0]);
20     }
21    
22 dpavlin 29 # Escape <, >, & and ", and to produce valid XML
23     my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
24     my $escape_re = join '|' => keys %escape;
25     sub e {
26     my $out;
27     foreach my $v (@_) {
28     $v =~ s/($escape_re)/$escape{$1}/g;
29     $out .= $v;
30     }
31     return $out;
32     }
33 dpavlin 8
34 dpavlin 39 my @spellings;
35 dpavlin 8 # FIX: doesn't work very well
36 dpavlin 16 if ($config->{findaffix}) {
37 dpavlin 41 foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) {
38     my $spelling_alt = new Lingua::Spelling::Alternative;
39     $spelling_alt->load_findaffix($findaffix);
40     push @spellings,$spelling_alt;
41     }
42 dpavlin 8 }
43 dpavlin 16 if ($config->{affix}) {
44 dpavlin 41 foreach my $affix (split(/[, ]+/,x($config->{affix}))) {
45     my $spelling_alt = new Lingua::Spelling::Alternative;
46     $spelling_alt->load_affix($affix);
47     push @spellings,$spelling_alt;
48     }
49 dpavlin 16 }
50 dpavlin 8
51     my $hits=0;
52 dpavlin 18 my $max_hits=x($config->{max_hits});
53 dpavlin 8
54     my %labels;
55     foreach (@{$config->{labels}->{label}}) {
56 dpavlin 18 $labels{$_->{value}} = x($_->{content});
57 dpavlin 8 }
58    
59 dpavlin 23 my $path = param('path'); # limit to this path
60     my %path_label;
61     my @path_name;
62     foreach (@{$config->{paths}->{path}}) {
63 dpavlin 39
64     print STDERR "##: $_->{limit}",x($_->{content}),"\n";
65 dpavlin 23 push @path_name,x($_->{limit});
66     $path_label{$_->{limit}} = x($_->{content});
67     }
68    
69 dpavlin 32 my @properties = split(/\s+/,x($config->{properties}));
70    
71 dpavlin 16 if ($config->{charset}) {
72 dpavlin 18 print header(-charset=>x($config->{charset}));
73 dpavlin 16 } else {
74     print header;
75     }
76 dpavlin 18 print start_html(-title=>x($config->{title})),start_form;
77     print x($config->{text}->{search});
78 dpavlin 16 print popup_menu(-name=>'max_hits',-values=>[ sort keys %labels ],-labels=>\%labels,-default=>$max_hits);
79 dpavlin 18 print x($config->{text}->{documents});
80 dpavlin 16 print textfield('search');
81 dpavlin 18 print submit(-value=> x($config->{text}->{submit}));
82 dpavlin 39 print br,checkbox(-name=>'no_affix', -checked=>0, -label=>x($config->{text}->{no_spell})) if (@spellings);
83 dpavlin 32 print checkbox(-name=>'no_properties', -checked=>0, -label=>($config->{text}->{no_properties})) if (@properties);
84 dpavlin 23 if (@path_name) {
85     print br,x($config->{text}->{limit});
86     print popup_menu(-name=>'path',-values=>\@path_name,-labels=>\%path_label,-default=>$path);
87     }
88 dpavlin 8 print end_form,hr;
89    
90     if (param('search')) {
91    
92     my $s;
93     # re-write query from +/- to and/and not
94    
95     my $search = param('search');
96     my $s_phrase = "";
97     while ($search =~ s/\s*("[^"]+")\s*/ /) {
98     $s .= "$1 ";
99     }
100     $search =~ s/^\s+//;
101     $search =~ s/\s+$//;
102    
103 dpavlin 41 my %words;
104    
105 dpavlin 8 foreach (split(/\s+/,$search)) {
106     if (m/^([+-])(\S+)/) {
107     $s.= ($s) ? "and " : "";
108     $s.="not " if ($1 eq "-");
109 dpavlin 39 if (@spellings && !param('no_affix')) {
110 dpavlin 8 my $w = $2; $w =~ s/[\*\s]+//g;
111     $w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/;
112 dpavlin 39 my $or="";
113     foreach my $spelling_alt (@spellings) {
114     $s.="$or$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 ";
115     $or = "or ";
116     }
117 dpavlin 8 } else {
118     $s.="$2* ";
119     }
120     } else {
121 dpavlin 39 if (@spellings && !param('no_affix')) {
122 dpavlin 8 my $w = $_; $w =~ s/[\*\s]+//g;
123     #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";
124 dpavlin 39 my $or="";
125     foreach my $spelling_alt (@spellings) {
126     $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";
127     $or = "or ";
128     }
129 dpavlin 8 } else {
130     $s.="$_* ";
131     }
132     }
133     }
134    
135     # fixup search string
136     $s=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/; # 1250 -> iso8859-2
137     $s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;
138     $s=~s/\*\*+/*/g;
139    
140 dpavlin 23 # limit to some path
141 dpavlin 39 $s = "swishdocpath=(\"*$path*\") and $s" if ($path);
142 dpavlin 23
143 dpavlin 22 my %params; # optional parametars for swish
144    
145 dpavlin 32 # default format for output
146     my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";
147 dpavlin 21
148 dpavlin 32 if (@properties) {
149     $hit_fmt = x($config->{hit}) if (! param('no_properties'));
150 dpavlin 39 $params{properties} = \@properties;
151 dpavlin 32 } else {
152     $hit_fmt = x($config->{hit}) if (x($config->{hit}));
153     }
154    
155 dpavlin 8 my $sh = SWISH->connect('Fork',
156 dpavlin 18 prog => x($config->{prog}),
157     indexes => x($config->{index}),
158 dpavlin 8 results => sub {
159     my ($sh,$hit) = @_;
160    
161 dpavlin 18 if ($config->{url}) {
162 dpavlin 32 printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties));
163 dpavlin 18 } else {
164 dpavlin 29 printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) );
165 dpavlin 8
166 dpavlin 18 }
167    
168 dpavlin 8 # print $_[1]->as_string,"<br>\n";
169     # my @fields = $hit->field_names;
170     # print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields;
171     },
172     maxhits => param('max_hits') || $max_hits,
173 dpavlin 22 \%params,
174 dpavlin 8 );
175    
176     die $SWISH::errstr unless $sh;
177    
178    
179     $hits = $sh->query($s);
180    
181 dpavlin 35 if ($hits && $hits > 0) {
182 dpavlin 16 print p,hr;
183 dpavlin 18 printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s);
184 dpavlin 8 } else {
185 dpavlin 16 print p;
186 dpavlin 18 printf (x($config->{text}->{no_hits}),$s,$sh->errstr);
187 dpavlin 8 }
188     } else {
189 dpavlin 18 print p(x($config->{text}->{footer}));
190 dpavlin 8 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26