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