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