28 |
my $ON_PAGE =$cfg_global->val('webpac', 'on_page') || 10; |
my $ON_PAGE =$cfg_global->val('webpac', 'on_page') || 10; |
29 |
my $MIN_WILDCARD =$cfg_global->val('webpac', 'min_wildcard') || 1; |
my $MIN_WILDCARD =$cfg_global->val('webpac', 'min_wildcard') || 1; |
30 |
my $TEMPLATE =$cfg_global->val('webpac', 'template'); |
my $TEMPLATE =$cfg_global->val('webpac', 'template'); |
31 |
|
my $UNAC_FILTER =$cfg_global->val('global', 'unac_filter'); |
32 |
|
my $BASE_PATH =$cfg_global->val('webpac', 'base_path'); |
33 |
|
|
34 |
|
if ($UNAC_FILTER) { |
35 |
|
require $UNAC_FILTER; |
36 |
|
} |
37 |
|
|
38 |
Text::Iconv->raise_error(0); # Conversion errors raise exceptions |
Text::Iconv->raise_error(0); # Conversion errors raise exceptions |
39 |
|
|
40 |
my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET); |
my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET); |
41 |
|
|
42 |
|
# use path from cgi script to support templates in subdirs |
43 |
|
sub url_ex { |
44 |
|
my $q = shift || die "suff2file needs CGI object!"; |
45 |
|
my $tpl = shift || die "url_ex needs template name!"; |
46 |
|
return suff2file($BASE_PATH, $q->url(-path => 1),$TEMPLATE_PATH,$tpl); |
47 |
|
} |
48 |
|
|
49 |
|
sub suff2file($$$$) { |
50 |
|
my ($base_path, $p, $path, $tpl) = @_; |
51 |
|
|
52 |
|
return $tpl if (! $base_path); |
53 |
|
|
54 |
|
# strip everything to and including base path, leaving only |
55 |
|
# additional (virtual) path |
56 |
|
if ($p =~ s,^.*?$base_path,,) { |
57 |
|
$p =~ s,/*,,g; |
58 |
|
my ($name,$ext) = split(/\./,$tpl); |
59 |
|
$p = $name . $p . "." . $ext; |
60 |
|
} else { |
61 |
|
# if unable reset it! |
62 |
|
$p = $tpl; |
63 |
|
} |
64 |
|
|
65 |
|
if ( -e "$path/$p") { |
66 |
|
return $p; |
67 |
|
} else { |
68 |
|
return $tpl; |
69 |
|
} |
70 |
|
|
71 |
|
} |
72 |
|
|
73 |
sub setup { |
sub setup { |
74 |
my $self = shift; |
my $self = shift; |
86 |
} |
} |
87 |
|
|
88 |
sub in_template { |
sub in_template { |
89 |
my $html = shift || "This page is left unintentionally blank"; |
my $q = shift || die "need CGI object!"; |
90 |
|
my $html = shift || die "This page is left unintentionally blank"; |
91 |
return $html if (! defined($TEMPLATE)); |
return $html if (! defined($TEMPLATE)); |
92 |
if (open(T, $TEMPLATE)) { |
|
93 |
|
my ($dir,$tpl); |
94 |
|
if ($TEMPLATE =~ m,^(.*?/*)([^/]+)$,) { |
95 |
|
($dir,$tpl) = ($1,$2); |
96 |
|
} else { |
97 |
|
die "can't parse TEMPLATE path"; |
98 |
|
} |
99 |
|
|
100 |
|
my $master_tpl = suff2file($BASE_PATH, $q->url(-path => 1),$dir,$tpl); |
101 |
|
if (open(T, $master_tpl)) { |
102 |
my $template_html = join("\n",<T>); |
my $template_html = join("\n",<T>); |
103 |
close(T); |
close(T); |
104 |
$template_html =~ s/##webpac##/$html/gsi; |
$template_html =~ s/##webpac##/$html/gsi; |
105 |
return $template_html; |
return $template_html; |
106 |
} else { |
} else { |
107 |
return "Can't read template '$TEMPLATE'"; |
return "Can't read template '$master_tpl'"; |
108 |
} |
} |
109 |
} |
} |
110 |
|
|
114 |
# Get the CGI.pm query object |
# Get the CGI.pm query object |
115 |
my $q = $self->query(); |
my $q = $self->query(); |
116 |
|
|
117 |
my $tmpl = $self->load_tmpl('search.html'); |
my $tmpl = $self->load_tmpl(url_ex($q,'search.html')); |
118 |
my $html = $tmpl->output; |
my $html = $tmpl->output; |
119 |
|
|
120 |
my $fif = new HTML::FillInForm; |
my $fif = new HTML::FillInForm; |
121 |
|
|
122 |
return in_template($fif->fill(scalarref => \$html, fobject => $q, |
return in_template($q,$fif->fill(scalarref => \$html, fobject => $q, |
123 |
target => 'search')); |
target => 'search')); |
124 |
} |
} |
125 |
|
|
150 |
|
|
151 |
push @persist_vars, "f$i"; |
push @persist_vars, "f$i"; |
152 |
push @persist_vars, "v$i"; |
push @persist_vars, "v$i"; |
153 |
|
push @persist_vars, "e$i" if ($q->param("e$i")); |
154 |
|
|
155 |
push @url_params,"f$i=".$q->url_param("f$i"); |
push @url_params,"f$i=".$q->url_param("f$i"); |
156 |
push @url_params,"v$i=".$q->url_param("v$i"); |
foreach my $v ($q->url_param("v$i")) { |
157 |
|
push @url_params,"v$i=$v"; |
158 |
|
} |
159 |
|
push @url_params,"e$i=".$q->url_param("e$i"); |
160 |
|
|
161 |
my $wc="*"; # swish wildcard |
my $wc="*"; # swish wildcard |
162 |
$wc="" if ($i eq ""); # don't apply wildcard on field 0 |
$wc="" if ($i eq ""); # don't apply wildcard on field 0 |
164 |
# re-write query from +/- to and/and not |
# re-write query from +/- to and/and not |
165 |
my @param_vals = $q->param("v$i"); |
my @param_vals = $q->param("v$i"); |
166 |
my @swish_q; |
my @swish_q; |
167 |
|
my ($pre,$post,$exact) = ('','',''); |
168 |
while (my $search = shift @param_vals) { |
while (my $search = shift @param_vals) { |
169 |
my $s; |
my $s; |
170 |
# remove accents |
# remove accents |
175 |
$search =~ s/^\s+//; |
$search =~ s/^\s+//; |
176 |
$search =~ s/\s+$//; |
$search =~ s/\s+$//; |
177 |
|
|
178 |
|
# filed e[nr] is exact match bitmask |
179 |
|
# 1 = beginning, 2=end, 3=both |
180 |
|
$pre = '"xxbxx ' if ($q->param("e$i") & 1); |
181 |
|
$post = ' xxexx"' if ($q->param("e$i") & 2); |
182 |
|
# add qotes on other side |
183 |
|
if ($q->param("e$i")) { |
184 |
|
$pre = '"' if (! $pre); |
185 |
|
$post = '"' if (! $post); |
186 |
|
# what about wildcards? |
187 |
|
$wc = ''; |
188 |
|
$wc = '*' if ($q->param("e$i") & 4); |
189 |
|
$exact = '_exact'; |
190 |
|
} |
191 |
|
|
192 |
foreach (split(/\s+/,$search)) { |
foreach (split(/\s+/,$search)) { |
193 |
if (m/^([+-])(\S+)/) { |
if (m/^([+-])(\S+)/) { |
194 |
$s.= ($s) ? "and " : ""; |
$s.= ($s) ? "and " : ""; |
195 |
$s.="not " if ($1 eq "-"); |
$s.="not " if ($1 eq "-"); |
196 |
$s.="$2$wc "; |
$s.=$2.$wc." "; |
197 |
} elsif (m/^\s*(and|or|not)\s*$/i) { |
} elsif (m/^\s*(and|or|not)\s*$/i) { |
198 |
$s.="$_ "; |
$s.=$_." "; |
199 |
# don't add * to words with less than x chars |
# don't add * to words with less than x chars |
200 |
} elsif (length($_) <= $MIN_WILDCARD) { |
} elsif (length($_) <= $MIN_WILDCARD) { |
201 |
$s.="$_ "; |
$s.=$_." "; |
202 |
} else { |
} else { |
203 |
$s.="$_$wc "; |
$s.=$_.$wc." "; |
204 |
} |
} |
205 |
} |
} |
206 |
$s =~ s/\*+/*/g; |
$s =~ s/\*+/*/g; |
207 |
|
$s = $pre.$s.$post if ($q->param("e$i")); |
208 |
push @swish_q,$s; |
push @swish_q,$s; |
209 |
} |
} |
210 |
# FIXME default operator for multi-value fields is or. There is |
# FIXME default operator for multi-value fields is or. There is |
211 |
# no way to change it, except here for now. Is there need? |
# no way to change it, except here for now. Is there need? |
212 |
push @s_arr, $q->param("f$i")."_swish=(".join(" or ",@swish_q).")"; |
push @s_arr, $q->param("f$i")."_swish".$exact."=(".join(" or ",@swish_q).")"; |
213 |
} |
} |
214 |
|
|
215 |
my $tmpl = $self->load_tmpl('results.html', global_vars => 1); |
my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1); |
216 |
|
|
217 |
sub esc_html { |
sub esc_html { |
218 |
my $html = shift; |
my $html = shift; |
300 |
|
|
301 |
my $html = $pager->output; |
my $html = $pager->output; |
302 |
|
|
303 |
return in_template($html); |
return in_template($q,$html); |
304 |
} |
} |
305 |
|
|
306 |
sub show_index { |
sub show_index { |
323 |
|
|
324 |
my $total = $index->count($field,$limit); |
my $total = $index->count($field,$limit); |
325 |
if (! $total) { |
if (! $total) { |
326 |
my $tmpl = $self->load_tmpl('no_index.html'); |
my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html')); |
327 |
$tmpl->param('field',$field); |
$tmpl->param('field',$field); |
328 |
$html = $tmpl->output; |
$html = $tmpl->output; |
329 |
return $html; |
return $html; |
330 |
} |
} |
331 |
|
|
332 |
my $tmpl = $self->load_tmpl('index_res.html', global_vars => 1); |
my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1); |
333 |
$tmpl->param('field',$field); |
$tmpl->param('field',$field); |
334 |
$tmpl->param('limit',$limit); |
$tmpl->param('limit',$limit); |
335 |
$tmpl->param('total',$total); |
$tmpl->param('total',$total); |
358 |
template => $tmpl, |
template => $tmpl, |
359 |
); |
); |
360 |
|
|
361 |
return in_template($pager->output); |
return in_template($q,$pager->output); |
362 |
} |
} |
363 |
|
|
364 |
1; |
1; |