--- trunk/html/swish.cgi 2003/03/21 21:10:51 23 +++ trunk/html/swish.cgi 2004/01/25 16:49:50 57 @@ -19,18 +19,33 @@ return $from_utf8->convert($_[0]); } -use Data::Dumper; -#print Dumper($config); +# Escape <, >, & and ", and to produce valid XML +my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); +my $escape_re = join '|' => keys %escape; +sub e { + my $out; + foreach my $v (@_) { + $v =~ s/($escape_re)/$escape{$1}/g; + $out .= $v; + } + return $out; +} -my $spelling_alt; +my @spellings; # FIX: doesn't work very well if ($config->{findaffix}) { - $spelling_alt = new Lingua::Spelling::Alternative; - $spelling_alt->load_findaffix(x($config->{findaffix})); + foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) { + my $spelling_alt = new Lingua::Spelling::Alternative; + $spelling_alt->load_findaffix($findaffix); + push @spellings,$spelling_alt; + } } if ($config->{affix}) { - $spelling_alt = new Lingua::Spelling::Alternative; - $spelling_alt->load_affix(x($config->{affix})); + foreach my $affix (split(/[, ]+/,x($config->{affix}))) { + my $spelling_alt = new Lingua::Spelling::Alternative; + $spelling_alt->load_affix($affix); + push @spellings,$spelling_alt; + } } my $hits=0; @@ -45,10 +60,14 @@ my %path_label; my @path_name; foreach (@{$config->{paths}->{path}}) { + +print STDERR "##: $_->{limit}",x($_->{content}),"\n"; push @path_name,x($_->{limit}); $path_label{$_->{limit}} = x($_->{content}); } +my @properties = split(/\s+/,x($config->{properties})); + if ($config->{charset}) { print header(-charset=>x($config->{charset})); } else { @@ -60,7 +79,8 @@ print x($config->{text}->{documents}); print textfield('search'); print submit(-value=> x($config->{text}->{submit})); -print 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); +print checkbox(-name=>'no_properties', -checked=>0, -label=>($config->{text}->{no_properties})) if (@properties); if (@path_name) { print br,x($config->{text}->{limit}); print popup_menu(-name=>'path',-values=>\@path_name,-labels=>\%path_label,-default=>$path); @@ -80,22 +100,32 @@ $search =~ s/^\s+//; $search =~ s/\s+$//; + my %words; + foreach (split(/\s+/,$search)) { if (m/^([+-])(\S+)/) { $s.= ($s) ? "and " : ""; $s.="not " if ($1 eq "-"); - if ($spelling_alt && !param('no_affix')) { + if (@spellings && !param('no_affix')) { my $w = $2; $w =~ s/[\*\s]+//g; $w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/; - $s.="$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 "; + my $or=""; + foreach my $spelling_alt (@spellings) { + $s.="$or$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 "; + $or = "or "; + } } else { $s.="$2* "; } } else { - if ($spelling_alt && !param('no_affix')) { + if (@spellings && !param('no_affix')) { my $w = $_; $w =~ s/[\*\s]+//g; #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) "; - $s.="(".join("* or ",$spelling_alt->alternatives($w))."*) "; + my $or=""; + foreach my $spelling_alt (@spellings) { + $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) "; + $or = "or "; + } } else { $s.="$_* "; } @@ -108,12 +138,28 @@ $s=~s/\*\*+/*/g; # limit to some path - $s = "swishdocpath=($path) and $s" if ($path); + $s = "swishdocpath=(\"*$path*\") and $s" if ($path); my %params; # optional parametars for swish - my @properties = split(/\s+/,x($config->{properties})); - $params{properties} = \@properties if (@properties); + # default format for output + my $hit_fmt = "%s [%s]
\n"; + + if (@properties) { + $hit_fmt = x($config->{hit}) if (! param('no_properties')); + $params{properties} = \@properties; + } else { + $hit_fmt = x($config->{hit}) if (x($config->{hit})); + } + + sub kill_html { + my @out; + foreach (@_) { + s/<[^>]+>//g; + push @out,$_; + } + return @out; + } my $sh = SWISH->connect('Fork', prog => x($config->{prog}), @@ -121,13 +167,10 @@ results => sub { my ($sh,$hit) = @_; - my $hit_fmt = x($config->{hit}) || - "%s [%s]
\n"; - if ($config->{url}) { - printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,$hit->swishtitle || 'untitled',$hit->swishrank); + printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties)); } else { - printf ($hit_fmt ,$hit->swishdocpath,$hit->swishtitle || 'untitled',$hit->swishrank, map($hit->$_, @properties) ); + printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) ); } @@ -144,7 +187,7 @@ $hits = $sh->query($s); - if ($hits > 0) { + if ($hits && $hits > 0) { print p,hr; printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); } else {