line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Application::Search; |
2
|
6
|
|
|
6
|
|
343179
|
use base 'CGI::Application'; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
7456
|
|
3
|
6
|
|
|
6
|
|
56903
|
use strict; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
210
|
|
4
|
6
|
|
|
6
|
|
31
|
use warnings; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
165
|
|
5
|
6
|
|
|
6
|
|
33
|
use Carp; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
449
|
|
6
|
6
|
|
|
6
|
|
8367
|
use CGI::Application::Plugin::AnyTemplate; |
|
6
|
|
|
|
|
44168
|
|
|
6
|
|
|
|
|
415
|
|
7
|
6
|
|
|
6
|
|
6771
|
use Data::Page; |
|
6
|
|
|
|
|
398172
|
|
|
6
|
|
|
|
|
98
|
|
8
|
6
|
|
|
6
|
|
6692
|
use File::Spec::Functions qw(catfile splitpath catdir); |
|
6
|
|
|
|
|
6340
|
|
|
6
|
|
|
|
|
628
|
|
9
|
6
|
|
|
6
|
|
6302
|
use Number::Format qw(format_bytes format_number); |
|
6
|
|
|
|
|
119683
|
|
|
6
|
|
|
|
|
1011
|
|
10
|
6
|
|
|
6
|
|
5996
|
use HTML::FillInForm; |
|
6
|
|
|
|
|
21657
|
|
|
6
|
|
|
|
|
1029
|
|
11
|
6
|
|
|
6
|
|
6424
|
use Time::HiRes; |
|
6
|
|
|
|
|
12050
|
|
|
6
|
|
|
|
|
32
|
|
12
|
6
|
|
|
6
|
|
9623
|
use Time::Piece; |
|
6
|
|
|
|
|
101339
|
|
|
6
|
|
|
|
|
34
|
|
13
|
6
|
|
|
6
|
|
547
|
use POSIX qw(ceil); |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
61
|
|
14
|
6
|
|
|
6
|
|
10197
|
use Text::Context; |
|
6
|
|
|
|
|
27706
|
|
|
6
|
|
|
|
|
83
|
|
15
|
6
|
|
|
6
|
|
26226
|
use Unicode::Normalize; |
|
6
|
|
|
|
|
17684
|
|
|
6
|
|
|
|
|
602
|
|
16
|
6
|
|
|
6
|
|
6722
|
use Encode qw(decode_utf8 encode_utf8); |
|
6
|
|
|
|
|
96238
|
|
|
6
|
|
|
|
|
763
|
|
17
|
6
|
|
|
6
|
|
6094
|
use File::Slurp qw(read_file); |
|
6
|
|
|
|
|
46934
|
|
|
6
|
|
|
|
|
1487
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '1.14'; |
20
|
|
|
|
|
|
|
our ( |
21
|
|
|
|
|
|
|
$DEBUG, # a debug flag |
22
|
|
|
|
|
|
|
@SUGGEST_CACHE, # cached suggestions |
23
|
|
|
|
|
|
|
$SUGGEST_CACHE_TIME # time of the last cache |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
$SUGGEST_CACHE_TIME = 0; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# some thing that will never appear in any doc that let's us return everything |
28
|
|
|
|
|
|
|
our $BLANK_SEARCH = 'not asdfqweasdfasdflqkqoiqnwlekjiquwoienlkqw'; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# load SWISH::API and complain if not available. This is done here |
31
|
|
|
|
|
|
|
# and not in Makefile.PL because SWISH::API is not on CPAN. It's part |
32
|
|
|
|
|
|
|
# of the Swish-e distribution. |
33
|
|
|
|
|
|
|
BEGIN { |
34
|
6
|
|
|
6
|
|
409
|
eval "use SWISH::API"; |
|
6
|
|
|
6
|
|
2578
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
35
|
6
|
50
|
|
|
|
212103
|
croak(<
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Unable to load SWISH::API. This module is included in the Swish-e |
38
|
|
|
|
|
|
|
distribution, inside the perl/ directory. Please see the |
39
|
|
|
|
|
|
|
CGI::Application::Search documentation for more details. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Error returned was: $@ |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
END |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
__PACKAGE__->add_callback( |
47
|
|
|
|
|
|
|
init => sub { |
48
|
|
|
|
|
|
|
my $self = shift; |
49
|
|
|
|
|
|
|
return unless $self->param('UTF8'); |
50
|
|
|
|
|
|
|
my $q = $self->query; |
51
|
|
|
|
|
|
|
$q->charset('UTF-8'); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# mark the strings as UTF8 so other stuff doesn't have to worry about it |
54
|
|
|
|
|
|
|
my @names = $q->param(); |
55
|
|
|
|
|
|
|
foreach my $name (@names) { |
56
|
|
|
|
|
|
|
my @values = $q->param($name); |
57
|
|
|
|
|
|
|
foreach my $i (0 .. $#values) { |
58
|
|
|
|
|
|
|
# don't do filehandles |
59
|
|
|
|
|
|
|
next if lc(ref $values[$i]) eq 'fh'; |
60
|
|
|
|
|
|
|
$values[$i] = Unicode::Normalize::NFC(decode_utf8($values[$i])); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$q->param($name => @values); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 NAME |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
CGI::Application::Search - Base class for CGI::App Swish-e site engines |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
package My::Search; |
75
|
|
|
|
|
|
|
use base 'CGI::Application::Search'; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub cgiapp_init { |
78
|
|
|
|
|
|
|
my $self = shift; |
79
|
|
|
|
|
|
|
$self->param( |
80
|
|
|
|
|
|
|
'SWISHE_INDEX' => 'my-swishe.index', |
81
|
|
|
|
|
|
|
'TEMPLATE' => 'search_results.tmpl', |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub cgiapp_prerun { |
86
|
|
|
|
|
|
|
my $self = shift; |
87
|
|
|
|
|
|
|
my $query = $self->query; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# let the user turn context highlighting off |
90
|
|
|
|
|
|
|
$self->param('HIGHLIGHT' => 0) if $query->param('highlight_off'); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# let the user change which property is used in the sort |
93
|
|
|
|
|
|
|
my $sort_by = $query->param('sort_by'); |
94
|
|
|
|
|
|
|
$self->param('SORT_BY' => $sort_by) if $sort_by; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 DESCRIPTION |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
A L based control module that uses Swish-e API in |
102
|
|
|
|
|
|
|
perl (L) to to perform searches on a swish-e index |
103
|
|
|
|
|
|
|
of documents. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 Features |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=over |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item * Sub-Classable. Unlike the Perl examples that come with swish-e, |
110
|
|
|
|
|
|
|
this is not a script, and can be customized without modifiying the |
111
|
|
|
|
|
|
|
original so that several sites may share the same underlying code. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item * Uses L to allow flexibility |
114
|
|
|
|
|
|
|
in template engine choice (L, L |
115
|
|
|
|
|
|
|
or L). |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item * Built-in templates to use out of box or as examples for your |
118
|
|
|
|
|
|
|
own templates |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item * HiLighted search results |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item * HiLighted pages linked from search results |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item * AJAX results sent to page without need of a page reload |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item * AJAX powered 'auto-suggest' to give the user list of choices |
127
|
|
|
|
|
|
|
available for search |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=back |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 TUTORIAL |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
If this is your first time using Swish-e (or you think you need |
134
|
|
|
|
|
|
|
a refresher) or if you want step-by-step instructions on how to |
135
|
|
|
|
|
|
|
use the AJAX capabilities of this module, then please see the |
136
|
|
|
|
|
|
|
L<"Tutorial"|CGI::Application::Search::Tutorial>. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 RUN_MODES |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
The start_mode is B. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 show_search() |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
This method will load the template pointed to by the B param |
145
|
|
|
|
|
|
|
(falling back to a default internal template if none is specified) and |
146
|
|
|
|
|
|
|
display it to the user. It will 'associate' this template with $self |
147
|
|
|
|
|
|
|
so that any parameters in $self->param() are also accessible to the |
148
|
|
|
|
|
|
|
template. It will also use L to fill in the search form |
149
|
|
|
|
|
|
|
with the previously selected parameters (unless it's a 'non-page-refresh' |
150
|
|
|
|
|
|
|
AJAX search). |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=cut |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub show_search { |
155
|
|
|
|
|
|
|
my $self = shift; |
156
|
|
|
|
|
|
|
my $q = $self->query(); |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my $tmpl_file; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# if we have a user specified template |
161
|
|
|
|
|
|
|
if ($self->param('TEMPLATE')) { |
162
|
|
|
|
|
|
|
$tmpl_file = $self->param('TEMPLATE'); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
} else { |
165
|
|
|
|
|
|
|
# what type of template do we want? |
166
|
|
|
|
|
|
|
my $ext = $self->param('TEMPLATE_TYPE') eq 'TemplateToolkit' ? '.tt' : '.tmpl'; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# is it an AJAX template? |
169
|
|
|
|
|
|
|
$tmpl_file = ($self->param('AJAX') ? 'ajax_' : '') . "search_results$ext"; |
170
|
|
|
|
|
|
|
$tmpl_file = catfile($self->param('DEFAULT_TEMPLATE_PATH'), $tmpl_file); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
my $tmpl = $self->template->load($tmpl_file); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# give it all the stuff in $self |
175
|
|
|
|
|
|
|
$tmpl->param(self => $self); |
176
|
|
|
|
|
|
|
foreach my $param qw( |
177
|
|
|
|
|
|
|
searched elapsed_time keywords hits first_page |
178
|
|
|
|
|
|
|
last_page prev_page next_page pages start_num |
179
|
|
|
|
|
|
|
stop_num total_entries |
180
|
|
|
|
|
|
|
) { |
181
|
|
|
|
|
|
|
$tmpl->param($param => $self->param($param)) |
182
|
|
|
|
|
|
|
if ($self->param($param)); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# add this url to the template too |
186
|
|
|
|
|
|
|
$tmpl->param(url => $q->url(-absolute => 1, path_info => 1)); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# add the possible ajax flag |
189
|
|
|
|
|
|
|
$tmpl->param(ajax => $q->param('ajax')); |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
my $output = $tmpl->output(); |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
# don't use FiF if we are using AJAX |
194
|
|
|
|
|
|
|
unless ($self->param('AJAX') && $q->param('ajax')) { |
195
|
|
|
|
|
|
|
my $filler = HTML::FillInForm->new(); |
196
|
|
|
|
|
|
|
$output = $filler->fill( |
197
|
|
|
|
|
|
|
scalarref => ref($output) ? $output : \$output, |
198
|
|
|
|
|
|
|
fobject => $q, |
199
|
|
|
|
|
|
|
($self->param('FIF_DEFAULTS') ? %{$self->param('FIF_DEFAULTS')} : ()) |
200
|
|
|
|
|
|
|
); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
return $output; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head2 perform_search() |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
This is where the meat of the searching is performed. We create a |
208
|
|
|
|
|
|
|
L object on the B and create the query for the |
209
|
|
|
|
|
|
|
search based on the value of the I parameter in CGI and any |
210
|
|
|
|
|
|
|
other B. The search is executed and if B |
211
|
|
|
|
|
|
|
is true we will use L to highlight it and then format |
212
|
|
|
|
|
|
|
the results, only showing B number of elements A paging list |
213
|
|
|
|
|
|
|
is also shown for navigating through the results. Then we will return |
214
|
|
|
|
|
|
|
to the B method for displaying everything. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
sub _apply_range_values { |
219
|
|
|
|
|
|
|
my ($self, $search) = @_; |
220
|
|
|
|
|
|
|
my $q = $self->query; |
221
|
|
|
|
|
|
|
if ($self->param('EXTRA_RANGE_PROPERTIES')) { |
222
|
|
|
|
|
|
|
foreach my $prop (@{$self->param('EXTRA_RANGE_PROPERTIES')}) { |
223
|
|
|
|
|
|
|
my ($start, $stop) = ($q->param("${prop}_start"), $q->param("${prop}_stop")); |
224
|
|
|
|
|
|
|
if (defined $start and defined $stop) { |
225
|
|
|
|
|
|
|
$search->SetSearchLimit($prop, $start, $stop); |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
sub perform_search { |
232
|
|
|
|
|
|
|
my $self = shift; |
233
|
|
|
|
|
|
|
my $q = $self->query; |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
# if we have any keywords |
236
|
|
|
|
|
|
|
my $keywords = $q->param('keywords'); |
237
|
|
|
|
|
|
|
if (defined $keywords && !$self->param('results')) { |
238
|
|
|
|
|
|
|
my $index = $self->param('SWISHE_INDEX'); |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
# make sure the index exists and is readable |
241
|
|
|
|
|
|
|
croak "Index file $index does not exist!" |
242
|
|
|
|
|
|
|
unless (-e $index); |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
$self->param('searched' => 1); |
245
|
|
|
|
|
|
|
my $start_time = Time::HiRes::time(); |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
# create my swish-e object |
248
|
|
|
|
|
|
|
my $swish = SWISH::API->new($index); |
249
|
|
|
|
|
|
|
croak "Problem reading $index : " . $swish->ErrorString |
250
|
|
|
|
|
|
|
if ($swish->Error); |
251
|
|
|
|
|
|
|
my $search = $swish->new_search_object(); |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
# add any range values |
254
|
|
|
|
|
|
|
$self->_apply_range_values($search); |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# set which property to sort by if given |
257
|
|
|
|
|
|
|
my $sort_by = $self->param('SORT_BY'); |
258
|
|
|
|
|
|
|
$search->set_sort($sort_by) if $sort_by; |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
my $search_query = $self->generate_search_query($keywords); |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
# if we got one |
263
|
|
|
|
|
|
|
if (defined $search_query) { |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
my $results = $search->execute($search_query); |
266
|
|
|
|
|
|
|
if ($swish->Error) { |
267
|
|
|
|
|
|
|
carp "Unable to create query: " . $swish->ErrorString |
268
|
|
|
|
|
|
|
if ($DEBUG); |
269
|
|
|
|
|
|
|
return $self->show_search(); |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
$self->param('elapsed_time' => format_number(Time::HiRes::time - $start_time, 3, 1)); |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
# create my pager and then go to the start page |
275
|
|
|
|
|
|
|
$self->_get_paging_vars($results); |
276
|
|
|
|
|
|
|
my @words = $self->_get_search_terms($swish, $search, $results, $keywords); |
277
|
|
|
|
|
|
|
$self->param( |
278
|
|
|
|
|
|
|
'hits' => $self->_process_results($swish, $search, $results, $search_query)); |
279
|
|
|
|
|
|
|
} else { |
280
|
|
|
|
|
|
|
return $self->show_search(); |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
# if there are any extra properties used in the search, make them available to |
285
|
|
|
|
|
|
|
# the templates with the value in the query object |
286
|
|
|
|
|
|
|
my @extra_props; |
287
|
|
|
|
|
|
|
push(@extra_props, @{$self->param('EXTRA_PROPERTIES')}) if $self->param('EXTRA_PROPERTIES'); |
288
|
|
|
|
|
|
|
push(@extra_props, @{$self->param('EXTRA_RANGE_PROPERTIES')}) |
289
|
|
|
|
|
|
|
if $self->param('EXTRA_RANGE_PROPERTIES'); |
290
|
|
|
|
|
|
|
foreach my $prop (@extra_props) { |
291
|
|
|
|
|
|
|
$self->param($prop => $q->param($prop)); |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
$self->param('keywords' => $keywords); |
294
|
|
|
|
|
|
|
return $self->show_search(); |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=head2 highlight_remote_page |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
This run mode will fetch a remote page (with either a relative, or |
300
|
|
|
|
|
|
|
absolute URL using the C Query param) and highlight the keywords |
301
|
|
|
|
|
|
|
used in the search on that page (the C Query param) using the |
302
|
|
|
|
|
|
|
B, B or B options. This |
303
|
|
|
|
|
|
|
run mode is best used in the links of the search results listing. |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
about us |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=cut |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
sub highlight_remote_page { |
310
|
|
|
|
|
|
|
my $self = shift; |
311
|
|
|
|
|
|
|
my $q = $self->query(); |
312
|
|
|
|
|
|
|
my $url = $q->param('url'); |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
# if it's relative, get the hostname and make it absolute |
315
|
|
|
|
|
|
|
if ($url !~ /^https?:\/\//) { |
316
|
|
|
|
|
|
|
$url = $q->url(-base => 1) . $url; |
317
|
|
|
|
|
|
|
} |
318
|
|
|
|
|
|
|
return $self->_hilight_page($url); |
319
|
|
|
|
|
|
|
} |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
sub _hilight_page { |
322
|
|
|
|
|
|
|
my ($self, $page) = @_; |
323
|
|
|
|
|
|
|
my $content; |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
# Search::Tools::HiLiter doesn't like blank searches so handle those on our own |
326
|
|
|
|
|
|
|
if( -e $page ) { |
327
|
|
|
|
|
|
|
$content = decode_utf8(read_file($page)); |
328
|
|
|
|
|
|
|
} else { |
329
|
|
|
|
|
|
|
require HTTP::Request; |
330
|
|
|
|
|
|
|
require LWP::UserAgent; |
331
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
332
|
|
|
|
|
|
|
my $request = HTTP::Request->new( GET => $page); |
333
|
|
|
|
|
|
|
my $response = $ua->request($request); |
334
|
|
|
|
|
|
|
if ( $response->is_error ) { |
335
|
|
|
|
|
|
|
warn "Error: Couldn't get '$page': response code " . $response->code . "\n"; |
336
|
|
|
|
|
|
|
return; |
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
$content = $response->content; |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
my $search_query = $self->query->param('keywords'); |
343
|
|
|
|
|
|
|
if( $search_query && $search_query ne $BLANK_SEARCH ) { |
344
|
|
|
|
|
|
|
eval { require Search::Tools::HiLiter }; |
345
|
|
|
|
|
|
|
if ($@) { |
346
|
|
|
|
|
|
|
warn "Could not load Search::Tools::HiLiter so no hilighting will be done: $@"; |
347
|
|
|
|
|
|
|
} else { |
348
|
|
|
|
|
|
|
$content = Search::Tools::HiLiter->new( |
349
|
|
|
|
|
|
|
tag => $self->param('HIGHLIGHT_TAG'), |
350
|
|
|
|
|
|
|
class => $self->param('HIGHLIGHT_CLASS'), |
351
|
|
|
|
|
|
|
colors => $self->param('HIGHLIGHT_COLORS'), |
352
|
|
|
|
|
|
|
query => $search_query, |
353
|
|
|
|
|
|
|
)->hilite($content); |
354
|
|
|
|
|
|
|
} |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
return $content; |
357
|
|
|
|
|
|
|
} |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=head2 highlight_local_page |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
This run mode will fetch a local page (only allowing relative files based |
362
|
|
|
|
|
|
|
in the B config var and the path using the C Query |
363
|
|
|
|
|
|
|
param) and highlight the keywords used in the search on that page (the |
364
|
|
|
|
|
|
|
C Query param) using the B, B |
365
|
|
|
|
|
|
|
or B options. This run mode is best used in the links |
366
|
|
|
|
|
|
|
of the search results listing. |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
about us |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
=cut |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
sub highlight_local_page { |
373
|
|
|
|
|
|
|
my $self = shift; |
374
|
|
|
|
|
|
|
my $q = $self->query(); |
375
|
|
|
|
|
|
|
my $doc_root = $self->param('DOCUMENT_ROOT'); |
376
|
|
|
|
|
|
|
my $path = $q->param('path'); |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
if (!$doc_root) { |
379
|
|
|
|
|
|
|
croak "You must define your DOCUMENT_ROOT to use this run mode!"; |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
# make sure $path doesn't have any '/..' tricks in it |
383
|
|
|
|
|
|
|
$path =~ s/\/\.\.//g; |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
my $file = catfile($doc_root, $path); |
386
|
|
|
|
|
|
|
return $self->_hilight_page($file); |
387
|
|
|
|
|
|
|
} |
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
=head2 suggestions |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
This run mode will return an AJAX listing of words that should be |
392
|
|
|
|
|
|
|
suggested to the user for the words that they have typed so far. It |
393
|
|
|
|
|
|
|
uses the C method to actually choose which words to |
394
|
|
|
|
|
|
|
send back. |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
=cut |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
sub suggestions { |
399
|
|
|
|
|
|
|
my $self = shift; |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
if ($self->param('AUTO_SUGGEST')) { |
402
|
|
|
|
|
|
|
return $self->_auto_complete_results( |
403
|
|
|
|
|
|
|
$self->suggested_words($self->query->param('keywords'))); |
404
|
|
|
|
|
|
|
} else { |
405
|
|
|
|
|
|
|
carp "Trying to use auto-suggest feature without AUTO_SUGGEST turned on!"; |
406
|
|
|
|
|
|
|
return ''; |
407
|
|
|
|
|
|
|
} |
408
|
|
|
|
|
|
|
} |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
sub _auto_complete_results { |
411
|
|
|
|
|
|
|
my ($self, $values) = @_; |
412
|
|
|
|
|
|
|
my $html = ' |
413
|
|
|
|
|
|
|
foreach (@$values) { |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
# straight from the CGI.pm bible. |
416
|
|
|
|
|
|
|
s/&/&/g; |
417
|
|
|
|
|
|
|
s/\"/"/g; #" |
418
|
|
|
|
|
|
|
s/>/>/g; |
419
|
|
|
|
|
|
|
s/</g; |
420
|
|
|
|
|
|
|
s/'/'/g; #' |
421
|
|
|
|
|
|
|
$html .= "$_"; |
422
|
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
return $html . ''; |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
=head1 OTHER METHODS |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
Most of the time you will not need to call the methods that are |
429
|
|
|
|
|
|
|
implemented in this module. But in some cases customizing the templates |
430
|
|
|
|
|
|
|
is not enought. If so, it might be prudent to override or extend these |
431
|
|
|
|
|
|
|
methods in your derived class. |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
=head2 new() |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
We simply override and extend the L new() to setup |
436
|
|
|
|
|
|
|
our defaults. |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
=cut |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
sub new { |
441
|
|
|
|
|
|
|
my ($class, %args) = @_; |
442
|
|
|
|
|
|
|
# setup my defaults |
443
|
|
|
|
|
|
|
$args{PARAMS} = { |
444
|
|
|
|
|
|
|
SWISHE_INDEX => catfile('data', 'swish-e.index'), |
445
|
|
|
|
|
|
|
PER_PAGE => 10, |
446
|
|
|
|
|
|
|
HIGHLIGHT => 1, |
447
|
|
|
|
|
|
|
HIGHLIGHT_TAG => q(strong), |
448
|
|
|
|
|
|
|
HIGHLIGHT_CLASS => 'hilite', |
449
|
|
|
|
|
|
|
HIGHLIGHT_COLORS => [], |
450
|
|
|
|
|
|
|
DESCRIPTION_LENGTH => 250, |
451
|
|
|
|
|
|
|
TEMPLATE_TYPE => 'HTMLTemplate', |
452
|
|
|
|
|
|
|
TEMPLATE_CONFIG => undef, |
453
|
|
|
|
|
|
|
UTF8 => 1, |
454
|
|
|
|
|
|
|
$args{PARAMS} ? %{$args{PARAMS}} : (), |
455
|
|
|
|
|
|
|
}; |
456
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
my $self = $class->SUPER::new(%args); |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
# setup the template configs |
460
|
|
|
|
|
|
|
my $path = catdir((splitpath($INC{'CGI/Application/Search.pm'}))[1], 'Search', 'templates',); |
461
|
|
|
|
|
|
|
$self->param('DEFAULT_TEMPLATE_PATH' => $path); |
462
|
|
|
|
|
|
|
my %tmpl_config = ( |
463
|
|
|
|
|
|
|
default_type => $self->param('TEMPLATE_TYPE'), |
464
|
|
|
|
|
|
|
auto_add_template_extension => 0, |
465
|
|
|
|
|
|
|
include_paths => [$self->tmpl_path, $path], |
466
|
|
|
|
|
|
|
HTMLTemplate => { |
467
|
|
|
|
|
|
|
global_vars => 1, |
468
|
|
|
|
|
|
|
loop_context_vars => 1, |
469
|
|
|
|
|
|
|
die_on_bad_params => 0, |
470
|
|
|
|
|
|
|
associate => $self, |
471
|
|
|
|
|
|
|
}, |
472
|
|
|
|
|
|
|
HTMLTemplateExpr => { |
473
|
|
|
|
|
|
|
global_vars => 1, |
474
|
|
|
|
|
|
|
loop_context_vars => 1, |
475
|
|
|
|
|
|
|
die_on_bad_params => 0, |
476
|
|
|
|
|
|
|
associate => $self, |
477
|
|
|
|
|
|
|
}, |
478
|
|
|
|
|
|
|
TemplateToolkit => { |
479
|
|
|
|
|
|
|
ABSOLUTE => 1, |
480
|
|
|
|
|
|
|
DEBUG_PROVIDER => 1, |
481
|
|
|
|
|
|
|
DEBUG => 1, |
482
|
|
|
|
|
|
|
}, |
483
|
|
|
|
|
|
|
); |
484
|
|
|
|
|
|
|
|
485
|
|
|
|
|
|
|
# add any overriding TEMPLATE_CONFIG options |
486
|
|
|
|
|
|
|
if ($self->param('TEMPLATE_CONFIG')) { |
487
|
|
|
|
|
|
|
$tmpl_config{$self->param('TEMPLATE_TYPE')} = |
488
|
|
|
|
|
|
|
{%{$tmpl_config{$self->param('TEMPLATE_TYPE')}}, %{$self->param('TEMPLATE_CONFIG')},}; |
489
|
|
|
|
|
|
|
} |
490
|
|
|
|
|
|
|
$self->template->config(%tmpl_config); |
491
|
|
|
|
|
|
|
return $self; |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
=head2 setup() |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
Here's were we setup our run modes. If you override this method, make |
497
|
|
|
|
|
|
|
sure you also call it in your base class |
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
sub setup { |
500
|
|
|
|
|
|
|
my $self = shift; |
501
|
|
|
|
|
|
|
# do your thing |
502
|
|
|
|
|
|
|
... |
503
|
|
|
|
|
|
|
$self->SUPER::setup(); |
504
|
|
|
|
|
|
|
} |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
=cut |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
sub setup { |
509
|
|
|
|
|
|
|
my $self = shift; |
510
|
|
|
|
|
|
|
$self->start_mode('show_search'); |
511
|
|
|
|
|
|
|
$self->run_modes( |
512
|
|
|
|
|
|
|
[ |
513
|
|
|
|
|
|
|
qw( |
514
|
|
|
|
|
|
|
show_search |
515
|
|
|
|
|
|
|
perform_search |
516
|
|
|
|
|
|
|
highlight_remote_page |
517
|
|
|
|
|
|
|
highlight_local_page |
518
|
|
|
|
|
|
|
suggestions |
519
|
|
|
|
|
|
|
) |
520
|
|
|
|
|
|
|
] |
521
|
|
|
|
|
|
|
); |
522
|
|
|
|
|
|
|
} |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
=head2 generate_search_query($keywords) |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
This method is used to generate the query for swish-e from the |
527
|
|
|
|
|
|
|
C<$keywords> (by default the 'keywords' CGI parameter), as well as any |
528
|
|
|
|
|
|
|
B that are present. |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
If you wish to generate your own search query then you should override |
531
|
|
|
|
|
|
|
this method. This is common if you need to have access/authorization |
532
|
|
|
|
|
|
|
control that will need to be taken into account for your search. (eg, |
533
|
|
|
|
|
|
|
anything under /protected can't be seen by someone not logged in). |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
Please see the swish-e documentation on the exact syntax for the query. |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
=cut |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
sub generate_search_query { |
540
|
|
|
|
|
|
|
my $self = shift; |
541
|
|
|
|
|
|
|
my $keywords = shift; |
542
|
|
|
|
|
|
|
my $q = $self->query; |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
# create a new swish-e search object |
545
|
|
|
|
|
|
|
my $search = $keywords || ''; |
546
|
|
|
|
|
|
|
$search =~ s/=/\=/g; # escape '=' just in case |
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
# add any EXTRA_PROPERTIES to the search |
549
|
|
|
|
|
|
|
if ($self->param('EXTRA_PROPERTIES')) { |
550
|
|
|
|
|
|
|
foreach my $prop (@{$self->param('EXTRA_PROPERTIES')}) { |
551
|
|
|
|
|
|
|
my $value = $q->param($prop); |
552
|
|
|
|
|
|
|
$search .= ' and ' if $search; |
553
|
|
|
|
|
|
|
$search .= "$prop=($value)" if defined $value && length $value; |
554
|
|
|
|
|
|
|
} |
555
|
|
|
|
|
|
|
} |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
return $search || $BLANK_SEARCH; |
558
|
|
|
|
|
|
|
} |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
=head2 suggested_words($word) |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
This object method is used by the B flag to return the words |
563
|
|
|
|
|
|
|
that should be suggested to the user after they have typed a C<$word>. |
564
|
|
|
|
|
|
|
It returns an array reference of those words. |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
By default it will just look for words in the B |
567
|
|
|
|
|
|
|
that begin with C<$word>. If you need more performance or flexibility |
568
|
|
|
|
|
|
|
(eg, storing your words in a database and querying for them) you are |
569
|
|
|
|
|
|
|
encouraged to override this method. |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
=cut |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
sub suggested_words { |
574
|
|
|
|
|
|
|
my ($self, $phrase) = @_; |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
# just get the last word in this phrase |
577
|
|
|
|
|
|
|
my @phrase_words = split(/\s+/, $phrase); |
578
|
|
|
|
|
|
|
my $word = pop(@phrase_words); |
579
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
my $want_to_cache = $self->param('AUTO_SUGGEST_CACHE'); |
581
|
|
|
|
|
|
|
my $file = $self->param('AUTO_SUGGEST_FILE'); |
582
|
|
|
|
|
|
|
my @suggestions; |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
if (!$file) { |
585
|
|
|
|
|
|
|
warn "AUTO_SUGGEST_FILE was not specified!"; |
586
|
|
|
|
|
|
|
return []; |
587
|
|
|
|
|
|
|
} elsif (!-r $file) { |
588
|
|
|
|
|
|
|
warn "AUTO_SUGGEST_FILE $file is not readable!"; |
589
|
|
|
|
|
|
|
return []; |
590
|
|
|
|
|
|
|
} |
591
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
# if we are going to use the cache (meaning we want to use |
593
|
|
|
|
|
|
|
# it and there's up-to-date data in there) |
594
|
|
|
|
|
|
|
my $file_mod_time = (stat($file))[9]; |
595
|
|
|
|
|
|
|
if ( $want_to_cache |
596
|
|
|
|
|
|
|
and @SUGGEST_CACHE |
597
|
|
|
|
|
|
|
and $SUGGEST_CACHE_TIME >= $file_mod_time) |
598
|
|
|
|
|
|
|
{ |
599
|
|
|
|
|
|
|
foreach my $cached (@SUGGEST_CACHE) { |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
# if it starts with this $word |
602
|
|
|
|
|
|
|
if (index($cached, lc $word) == 0) { |
603
|
|
|
|
|
|
|
push(@suggestions, $cached); |
604
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
# else if this is the first mis-match |
606
|
|
|
|
|
|
|
} elsif (@suggestions) { |
607
|
|
|
|
|
|
|
last; |
608
|
|
|
|
|
|
|
} |
609
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
# if we have a limit and we've reached it |
611
|
|
|
|
|
|
|
# don't do any more |
612
|
|
|
|
|
|
|
if ( $self->param('AUTO_SUGGEST_LIMIT') |
613
|
|
|
|
|
|
|
and @suggestions >= $self->param('AUTO_SUGGEST_LIMIT')) |
614
|
|
|
|
|
|
|
{ |
615
|
|
|
|
|
|
|
last; |
616
|
|
|
|
|
|
|
} |
617
|
|
|
|
|
|
|
} |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
# else we don't have anything cached, so just load from the file |
620
|
|
|
|
|
|
|
} else { |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
# reset it if we want to cache |
623
|
|
|
|
|
|
|
if ($want_to_cache) { |
624
|
|
|
|
|
|
|
@SUGGEST_CACHE = (); |
625
|
|
|
|
|
|
|
$SUGGEST_CACHE_TIME = time(); |
626
|
|
|
|
|
|
|
} |
627
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
# read each line from the AUTO_SUGGEST_FILE |
629
|
|
|
|
|
|
|
my $IN; |
630
|
|
|
|
|
|
|
open($IN, '<', $file) |
631
|
|
|
|
|
|
|
or die "Could not open $file for reading! $!"; |
632
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
# now look at each line |
634
|
|
|
|
|
|
|
LINE: while (my $line = <$IN>) { |
635
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
# if we want to cache the words in this file |
637
|
|
|
|
|
|
|
if ($want_to_cache) { |
638
|
|
|
|
|
|
|
chomp($line); |
639
|
|
|
|
|
|
|
push(@SUGGEST_CACHE, $line); |
640
|
|
|
|
|
|
|
} |
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
# if it starts with this $word |
643
|
|
|
|
|
|
|
if (index($line, lc $word) == 0) { |
644
|
|
|
|
|
|
|
chomp($line) unless ($want_to_cache); |
645
|
|
|
|
|
|
|
push(@suggestions, $line); |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
# else if we aren't caching, and this is the first mis-match |
648
|
|
|
|
|
|
|
# then we want to finish and close the file |
649
|
|
|
|
|
|
|
} elsif (@suggestions && !$want_to_cache) { |
650
|
|
|
|
|
|
|
last LINE; |
651
|
|
|
|
|
|
|
} |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
# if we have a limit and we've reached it |
654
|
|
|
|
|
|
|
# don't do any more |
655
|
|
|
|
|
|
|
if ( $self->param('AUTO_SUGGEST_LIMIT') |
656
|
|
|
|
|
|
|
and @suggestions >= $self->param('AUTO_SUGGEST_LIMIT')) |
657
|
|
|
|
|
|
|
{ |
658
|
|
|
|
|
|
|
last; |
659
|
|
|
|
|
|
|
} |
660
|
|
|
|
|
|
|
} |
661
|
|
|
|
|
|
|
close($IN) |
662
|
|
|
|
|
|
|
or die "Could not close $file! $!"; |
663
|
|
|
|
|
|
|
} |
664
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
# if we have something in the phrase that's not |
666
|
|
|
|
|
|
|
# in the word, add the phrase before the suggestion |
667
|
|
|
|
|
|
|
if (@phrase_words) { |
668
|
|
|
|
|
|
|
my $prefix = join(' ', @phrase_words); |
669
|
|
|
|
|
|
|
@suggestions = map { "$prefix $_" } @suggestions; |
670
|
|
|
|
|
|
|
} |
671
|
|
|
|
|
|
|
return \@suggestions; |
672
|
|
|
|
|
|
|
} |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
=head1 CONFIGURATION |
675
|
|
|
|
|
|
|
|
676
|
|
|
|
|
|
|
There are several configuration parameters that you can set at any time |
677
|
|
|
|
|
|
|
(using C<< param() >> in your cgiapp_init, or PARAMS hash in new()) |
678
|
|
|
|
|
|
|
before the run mode is called that will affect the search and display |
679
|
|
|
|
|
|
|
of the results. They are: |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
=head2 SWISHE_INDEX |
682
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
This is the swishe index used for the searches. The default is |
684
|
|
|
|
|
|
|
'data/swish-e.index'. You will probably set this every time. |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
=head2 AJAX |
687
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
This is a boolean indicating whether or not a non-page-refresh AJAX |
689
|
|
|
|
|
|
|
search will be permitted. |
690
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
Please see the L<"Tutorial"|CGI::Application::Search::Tutorial> for more |
692
|
|
|
|
|
|
|
information on how to use the AJAX capabilities of this module. |
693
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
=head2 TEMPLATE |
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
The name of the search interface template. Default templates are included |
697
|
|
|
|
|
|
|
with this distribution and will be used if you don't specify one. Which |
698
|
|
|
|
|
|
|
default template is used depends on which B you are using |
699
|
|
|
|
|
|
|
(I or I) and whether or not the B |
700
|
|
|
|
|
|
|
flag is true. |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
These sample templates are installed with the module, or you can view them |
703
|
|
|
|
|
|
|
by looking under the F directory of the source distribution |
704
|
|
|
|
|
|
|
(I<.tar.gz>). |
705
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
Please see L for more information on which variables |
707
|
|
|
|
|
|
|
are passed into your template. |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
=head2 TEMPLATE_TYPE |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
This module uses L to allow |
712
|
|
|
|
|
|
|
flexibility in choosing which templating system to use for your |
713
|
|
|
|
|
|
|
search. This works especially well when you are trying to integrate the |
714
|
|
|
|
|
|
|
Search into an existing app with an existing templating structure. |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
This value is passed to the C<< $self->template->config() >> method as |
717
|
|
|
|
|
|
|
the C<< default_type >>. By default it is 'HTMLTemplate'. Please see |
718
|
|
|
|
|
|
|
L for more options. |
719
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
If you want more control of configuration for the template the it would |
721
|
|
|
|
|
|
|
probably best be done by subclassing CGI::Application::Search and passing |
722
|
|
|
|
|
|
|
your desired params to C<< $self->template->config >>. |
723
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
=head2 PER_PAGE |
725
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
How many search result items to display per page. The default is 10. |
727
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
=head2 HIGHLIGHT |
729
|
|
|
|
|
|
|
|
730
|
|
|
|
|
|
|
Boolean indicating whether or not we should highlight the description |
731
|
|
|
|
|
|
|
given to the templates. The default is true. |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
=head2 HIGHLIGHT_TAG |
734
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
The HTML tag used to surround the highlighted context. The default is C<< |
736
|
|
|
|
|
|
|
strong >>. |
737
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
=head2 HIGHLIGHT_CLASS |
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
The class attribute of the B HTML tag. This is useful |
741
|
|
|
|
|
|
|
when you want to dictacte the style through a CSS style sheet. If given, |
742
|
|
|
|
|
|
|
this value will override that of B. It has no value |
743
|
|
|
|
|
|
|
by default. |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
=head2 HIGHLIGHT_COLORS |
746
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
This is an array ref of acceptable HTML colors. If provided, it will |
748
|
|
|
|
|
|
|
highlight each matching word/phrase in an alternating style. For instance, |
749
|
|
|
|
|
|
|
if given 2 colors, every other highlighted phrase would be a different |
750
|
|
|
|
|
|
|
color. By default it is an empty array. |
751
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
=head2 EXTRA_PROPERTIES |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
This is an array ref of extra properties used in the search. By default, |
755
|
|
|
|
|
|
|
the module will only use the value of the 'keywords' parameter coming |
756
|
|
|
|
|
|
|
in the CGI query. If anything is provided as an extra property then it |
757
|
|
|
|
|
|
|
will be added to the query used in the search. |
758
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
An example: You have some of you pages designated into categories. You |
760
|
|
|
|
|
|
|
want the user to have the option of narrowing his results by category. You |
761
|
|
|
|
|
|
|
add the word 'category' to the 'EXTRA_PROPERTIES' list and then you |
762
|
|
|
|
|
|
|
add a 'category' form element that the user has the option of giving |
763
|
|
|
|
|
|
|
a value to your search form. If the user gives that element a value, |
764
|
|
|
|
|
|
|
then it will be seen and applied to the search. This will also only |
765
|
|
|
|
|
|
|
work if you have the 'category' element defined for your documents (see |
766
|
|
|
|
|
|
|
I and 'MetaNames' in the swish-e.org SWISH-CONF |
767
|
|
|
|
|
|
|
documentation). |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
By default, this list is empty. |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
=head2 EXTRA_RANGE_PROPERTIES |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
This is almost exactly like the C above except that |
774
|
|
|
|
|
|
|
instead of searching for the given properties as simple strings, we |
775
|
|
|
|
|
|
|
will use a range. Since ranges need to values, if you're searching |
776
|
|
|
|
|
|
|
for the C property, then you need to have a C and |
777
|
|
|
|
|
|
|
a C value coming from the query. So if C is in your |
778
|
|
|
|
|
|
|
C and you have a CGI query string like this: |
779
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
?foo_start=123&foo_end=234 |
781
|
|
|
|
|
|
|
|
782
|
|
|
|
|
|
|
Then we will a generate a Swish-E query that looks something like this: |
783
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
-L foo 123 234 |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
=head2 DESCRIPTION_LENGTH |
787
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
This is the maximum length for the context (in chars) that is displayed |
789
|
|
|
|
|
|
|
for each search result. The default is 250 characters. |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
=head2 DESCRIPTION_CONTEXT |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
This is the number of words on either side of the searched for words and |
794
|
|
|
|
|
|
|
phrases (keywords) that will be displayed as part of the description. If |
795
|
|
|
|
|
|
|
this is 0, then the entire description will be displayed. The default |
796
|
|
|
|
|
|
|
is 0. |
797
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
B: This directive will cause Search to use L, which |
799
|
|
|
|
|
|
|
can be slow and CPU intensive at times. These computations may prove to |
800
|
|
|
|
|
|
|
be too much for some servers (eg, a shared hosting environment). |
801
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
=head2 AUTO_SUGGEST |
803
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
If true, then this will allow the broswer to give suggestions to the user |
805
|
|
|
|
|
|
|
as they type. To use this, you must either use the B |
806
|
|
|
|
|
|
|
configuration option, or override the C method. |
807
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
You template must also have the appropriate JavaScript code. Please see |
809
|
|
|
|
|
|
|
the L<"Tutorial"|CGI::Application::Search::Tutorial> for more details. |
810
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
=head2 AUTO_SUGGEST_FILE |
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
The name of the file where the suggested words are stored. These words |
814
|
|
|
|
|
|
|
should be in alphabetical order with one word per line. |
815
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
=head2 AUTO_SUGGEST_CACHE |
817
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
A boolean indicating whether or not the results of the |
819
|
|
|
|
|
|
|
B should be cached in memory or not. This will save |
820
|
|
|
|
|
|
|
repeated file accesses when used in a persistant environment. |
821
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
=head2 AUTO_SUGGEST_LIMIT |
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
An integer count of the most suggestions to show the user at a time. This |
825
|
|
|
|
|
|
|
is useful when you don't want to overwhelm the end user and take over |
826
|
|
|
|
|
|
|
their screen with all of your helpful suggestions. |
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
=head2 DOCUMENT_ROOT |
829
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
This is the root directory to use when looking for files when using the |
831
|
|
|
|
|
|
|
C run mode. |
832
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
=head2 SORT_BY |
834
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
This is a string used by Swish-e to sort the results. The string is a |
836
|
|
|
|
|
|
|
space separated list of valid document properties. Each property may |
837
|
|
|
|
|
|
|
contain a qualifier (either C or C) that sets the direction |
838
|
|
|
|
|
|
|
of the sort. Leave it alone and Swish-e will sort by C in |
839
|
|
|
|
|
|
|
descending order. But say you wanted to reverse that for some reason. You |
840
|
|
|
|
|
|
|
could specify a C of |
841
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
swishrank asc |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
=head2 FIF_DEFAULTS |
845
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
A hash reference of extra parameters that are passed to L |
847
|
|
|
|
|
|
|
when it does it's business with the forms. This is useful to telling |
848
|
|
|
|
|
|
|
L to skip certain forms or form elements. |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
=cut |
851
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
#-------------------------PRIVATE METHODS----------------------- |
853
|
|
|
|
|
|
|
sub _process_results { |
854
|
|
|
|
|
|
|
my ($self, $swish, $search, $results, $search_query) = @_; |
855
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
# now let's go through the results and build our loop |
857
|
|
|
|
|
|
|
my @result_loop = (); |
858
|
|
|
|
|
|
|
my $count = 0; |
859
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
# while we still have more results |
861
|
|
|
|
|
|
|
while (my $current = $results->NextResult) { |
862
|
|
|
|
|
|
|
my %tmp = ( |
863
|
|
|
|
|
|
|
reccount => $current->Property('swishreccount'), |
864
|
|
|
|
|
|
|
rank => $current->Property('swishrank'), |
865
|
|
|
|
|
|
|
title => $current->Property('swishtitle'), |
866
|
|
|
|
|
|
|
path => $current->Property('swishdocpath'), |
867
|
|
|
|
|
|
|
size => format_bytes($current->Property('swishdocsize')), |
868
|
|
|
|
|
|
|
description => $current->Property('swishdescription') || '', |
869
|
|
|
|
|
|
|
last_modified => |
870
|
|
|
|
|
|
|
localtime($current->Property('swishlastmodified'))->strftime('%B %d, %Y'), |
871
|
|
|
|
|
|
|
); |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
# now add any EXTRA_PROPERTIES that we want to show |
874
|
|
|
|
|
|
|
if ($self->param('EXTRA_PROPERTIES')) { |
875
|
|
|
|
|
|
|
$tmp{$_} = eval { $current->Property($_) } |
876
|
|
|
|
|
|
|
foreach (@{$self->param('EXTRA_PROPERTIES')}); |
877
|
|
|
|
|
|
|
} |
878
|
|
|
|
|
|
|
if ($self->param('EXTRA_RANGE_PROPERTIES')) { |
879
|
|
|
|
|
|
|
$tmp{$_} = eval { $current->Property($_) } |
880
|
|
|
|
|
|
|
foreach (@{$self->param('EXTRA_RANGE_PROPERTIES')}); |
881
|
|
|
|
|
|
|
} |
882
|
|
|
|
|
|
|
|
883
|
|
|
|
|
|
|
my $description = $tmp{description}; |
884
|
|
|
|
|
|
|
if ($description) { |
885
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
# if we want to zero in on the context |
887
|
|
|
|
|
|
|
if ($self->param('DESCRIPTION_CONTEXT')) { |
888
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
# get the keywords from the swish search |
890
|
|
|
|
|
|
|
my @keywords = (); |
891
|
|
|
|
|
|
|
foreach my $kw ($results->ParsedWords($self->param('SWISHE_INDEX'))) { |
892
|
|
|
|
|
|
|
|
893
|
|
|
|
|
|
|
# remove boolean operators 'and', 'or' and 'not' |
894
|
|
|
|
|
|
|
my $lc_kw = lc($kw); |
895
|
|
|
|
|
|
|
if ($lc_kw ne 'and' && $lc_kw ne 'or' && $lc_kw ne 'not') { |
896
|
|
|
|
|
|
|
push(@keywords, $kw); |
897
|
|
|
|
|
|
|
} |
898
|
|
|
|
|
|
|
} |
899
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
# now get the context |
901
|
|
|
|
|
|
|
my $context = Text::Context->new($description, @keywords,); |
902
|
|
|
|
|
|
|
$description = $context->as_text(); |
903
|
|
|
|
|
|
|
} |
904
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
# if we want to highlight the description |
906
|
|
|
|
|
|
|
if ($self->param('HIGHLIGHT') && $search_query && $search_query ne $BLANK_SEARCH) { |
907
|
|
|
|
|
|
|
eval { require Search::Tools::HiLiter }; |
908
|
|
|
|
|
|
|
if ($@) { |
909
|
|
|
|
|
|
|
warn "Could not load Search::Tools::HiLiter so no hilighting will be done: $@"; |
910
|
|
|
|
|
|
|
} else { |
911
|
|
|
|
|
|
|
my $hl = Search::Tools::HiLiter->new( |
912
|
|
|
|
|
|
|
tag => $self->param('HIGHLIGHT_TAG'), |
913
|
|
|
|
|
|
|
class => $self->param('HIGHLIGHT_CLASS'), |
914
|
|
|
|
|
|
|
colors => $self->param('HIGHLIGHT_COLORS'), |
915
|
|
|
|
|
|
|
query => $search_query, |
916
|
|
|
|
|
|
|
); |
917
|
|
|
|
|
|
|
$description = $hl->plain($description); |
918
|
|
|
|
|
|
|
} |
919
|
|
|
|
|
|
|
} |
920
|
|
|
|
|
|
|
|
921
|
|
|
|
|
|
|
# now make sure it's the appropriate length |
922
|
|
|
|
|
|
|
$tmp{description} = substr($description, 0, $self->param('DESCRIPTION_LENGTH')); |
923
|
|
|
|
|
|
|
} |
924
|
|
|
|
|
|
|
push(@result_loop, \%tmp); |
925
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
# only go as far as the number per page |
927
|
|
|
|
|
|
|
++$count; |
928
|
|
|
|
|
|
|
last if ($count == $self->param('PER_PAGE')); |
929
|
|
|
|
|
|
|
} |
930
|
|
|
|
|
|
|
return \@result_loop; |
931
|
|
|
|
|
|
|
} |
932
|
|
|
|
|
|
|
|
933
|
|
|
|
|
|
|
sub _get_search_terms { |
934
|
|
|
|
|
|
|
my ($self, $swish, $search, $results, $keywords) = @_; |
935
|
|
|
|
|
|
|
my @phrases = (); |
936
|
|
|
|
|
|
|
my %terms = (); |
937
|
|
|
|
|
|
|
|
938
|
|
|
|
|
|
|
while ($keywords =~ /\G\s*"([^"]+)"/g) { |
939
|
|
|
|
|
|
|
push(@phrases, $1); |
940
|
|
|
|
|
|
|
} |
941
|
|
|
|
|
|
|
|
942
|
|
|
|
|
|
|
$keywords =~ s/"[^"]+?"//g; |
943
|
|
|
|
|
|
|
|
944
|
|
|
|
|
|
|
# remove stop words from highlighting |
945
|
|
|
|
|
|
|
# for some reason swish-e doesn't remove boolean operators as stop words... which |
946
|
|
|
|
|
|
|
# is probably good so that they actually get used in the searches, but still... |
947
|
|
|
|
|
|
|
my %stop_words = (); |
948
|
|
|
|
|
|
|
foreach my $word ($results->RemovedStopwords($self->param('SWISHE_INDEX')), 'and', 'or', 'not') |
949
|
|
|
|
|
|
|
{ |
950
|
|
|
|
|
|
|
$stop_words{$word} = 1; |
951
|
|
|
|
|
|
|
} |
952
|
|
|
|
|
|
|
$stop_words{$_} = 1 foreach qw(and or not); |
953
|
|
|
|
|
|
|
|
954
|
|
|
|
|
|
|
for my $word (split(/\s+/, $keywords)) { |
955
|
|
|
|
|
|
|
if ($word) { |
956
|
|
|
|
|
|
|
next if $stop_words{$word}; |
957
|
|
|
|
|
|
|
$terms{$word} = 1; |
958
|
|
|
|
|
|
|
} |
959
|
|
|
|
|
|
|
} |
960
|
|
|
|
|
|
|
|
961
|
|
|
|
|
|
|
# now look at the stems of these words |
962
|
|
|
|
|
|
|
$terms{$swish->fuzzify($swish->index_names, $_)->WordList} = 1 foreach (keys %terms); |
963
|
|
|
|
|
|
|
return keys %terms, @phrases; |
964
|
|
|
|
|
|
|
} |
965
|
|
|
|
|
|
|
|
966
|
|
|
|
|
|
|
# create a loop of pages with the first page, at most five pages before |
967
|
|
|
|
|
|
|
# the current page, the current page, at most five pages after the current page |
968
|
|
|
|
|
|
|
# and then the last page |
969
|
|
|
|
|
|
|
sub _get_paging_vars { |
970
|
|
|
|
|
|
|
my ($self, $results) = @_; |
971
|
|
|
|
|
|
|
my @pages = (); |
972
|
|
|
|
|
|
|
|
973
|
|
|
|
|
|
|
# create my pager from the 'page' parameter in CGI or just use the first page |
974
|
|
|
|
|
|
|
my $page_num = $self->query->param('page') || 1; |
975
|
|
|
|
|
|
|
my $pager = Data::Page->new($results->Hits, $self->param('PER_PAGE'), $page_num); |
976
|
|
|
|
|
|
|
|
977
|
|
|
|
|
|
|
# go to the result that we want to look at first |
978
|
|
|
|
|
|
|
$results->SeekResult($pager->first - 1); |
979
|
|
|
|
|
|
|
|
980
|
|
|
|
|
|
|
# now let's create the paging summary vars |
981
|
|
|
|
|
|
|
$self->param('total_entries' => $pager->total_entries); |
982
|
|
|
|
|
|
|
$self->param('start_num' => $pager->first); |
983
|
|
|
|
|
|
|
$self->param('stop_num' => $pager->last); |
984
|
|
|
|
|
|
|
$self->param('next_page' => $pager->next_page); |
985
|
|
|
|
|
|
|
$self->param('prev_page' => $pager->previous_page); |
986
|
|
|
|
|
|
|
$self->param('first_page' => $pager->first_page eq $page_num); |
987
|
|
|
|
|
|
|
$self->param('last_page' => $pager->last_page eq $page_num); |
988
|
|
|
|
|
|
|
|
989
|
|
|
|
|
|
|
foreach (($page_num - 5) .. ($page_num + 5)) { |
990
|
|
|
|
|
|
|
# if we are in a real range |
991
|
|
|
|
|
|
|
if (($_ > 0) && ($_ <= ceil($pager->total_entries / $self->param('PER_PAGE')))) { |
992
|
|
|
|
|
|
|
my %hash = (page_num => $_, current => $_ eq $page_num); |
993
|
|
|
|
|
|
|
push(@pages, \%hash); |
994
|
|
|
|
|
|
|
} |
995
|
|
|
|
|
|
|
} |
996
|
|
|
|
|
|
|
$self->param(pages => \@pages) if ($#pages); |
997
|
|
|
|
|
|
|
} |
998
|
|
|
|
|
|
|
|
999
|
|
|
|
|
|
|
1; |
1000
|
|
|
|
|
|
|
|
1001
|
|
|
|
|
|
|
__END__ |