1 |
package Webpacus::Controller::Search; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use base 'Catalyst::Controller'; |
6 |
|
7 |
use Data::Dumper; |
8 |
|
9 |
use lib '/data/webpac2/lib'; |
10 |
use WebPAC::Search::Estraier 0.03; |
11 |
|
12 |
|
13 |
=head1 NAME |
14 |
|
15 |
Webpacus::Controller::Search - Search WebPAC data |
16 |
|
17 |
=head1 SYNOPSIS |
18 |
|
19 |
See L<WebPAC>, L<Webpacus> |
20 |
|
21 |
=head1 DESCRIPTION |
22 |
|
23 |
Catalyst Controller for search fields Hyper Estraier |
24 |
|
25 |
=head1 METHODS |
26 |
|
27 |
=over 4 |
28 |
|
29 |
=item default |
30 |
|
31 |
=cut |
32 |
|
33 |
sub default : Private { |
34 |
my ( $self, $c ) = @_; |
35 |
|
36 |
$c->log->debug("default search got param: ".Dumper($c->req->params)); |
37 |
|
38 |
$c->stash->{template} = 'search.tt'; |
39 |
} |
40 |
|
41 |
=item suggest |
42 |
|
43 |
Returns results for REST URIs like: |
44 |
|
45 |
C<search/suggest?search=FieldName&show=TitleProper&FieldName=query%20string> |
46 |
|
47 |
It will use C<search> field to filter results (and using additional |
48 |
C<FieldName> as value of search) and return C<TitleProper> field |
49 |
for results. |
50 |
|
51 |
If C<search> field has magic value <all>, it will search over all data, not |
52 |
just one specified field: |
53 |
|
54 |
C<search/suggest?search=all&show=TitleProper&all=query%20string> |
55 |
|
56 |
=cut |
57 |
|
58 |
sub suggest : Local { |
59 |
my ( $self, $c ) = @_; |
60 |
|
61 |
my $search = $c->req->params->{search}; |
62 |
my $show = $c->req->params->{show}; |
63 |
|
64 |
my $log = $c->log; |
65 |
|
66 |
my $webpac = $c->comp('Model::WebPAC'); |
67 |
|
68 |
my $q = $c->req->params->{ $search || 'all' } || $c->response->body("no results"); |
69 |
|
70 |
$log->info("search for '$q' in $search and display $show\n"); |
71 |
|
72 |
my $max = $c->config->{'hits_for_suggest'}; |
73 |
if (! $max) { |
74 |
$log->info("hits_for_suggest isn't defined, defaulting to 10"); |
75 |
$c->config->{'hits_for_suggest'} = 10; |
76 |
$max = 10; |
77 |
} |
78 |
|
79 |
$c->forward('filter_database'); |
80 |
|
81 |
my $hits = $webpac->search( |
82 |
phrase => $q, |
83 |
add_attr => $c->stash->{attr}, |
84 |
get_attr => [ $show ], |
85 |
max => $max, |
86 |
); |
87 |
|
88 |
my $used; |
89 |
my @suggestions; |
90 |
|
91 |
foreach my $res (@{$hits}) { |
92 |
my $v = $res->{ $show } || next; |
93 |
next if ($used->{ $v }++); |
94 |
push @suggestions, $v; |
95 |
} |
96 |
|
97 |
$log->debug( ($#suggestions + 1) . " unique hits returned"); |
98 |
|
99 |
$c->response->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
100 |
} |
101 |
|
102 |
|
103 |
=item results |
104 |
|
105 |
Returns results for search query |
106 |
|
107 |
=cut |
108 |
|
109 |
sub results : Local { |
110 |
my ( $self, $c ) = @_; |
111 |
|
112 |
my $params = $c->req->params; |
113 |
|
114 |
# do full-page refresh for clients without JavaScript |
115 |
$c->stash->{results} = $c->subreq('/search/results/ajax', {}, $params); |
116 |
$c->stash->{template} = 'search.tt'; |
117 |
} |
118 |
|
119 |
|
120 |
=item results_ajax |
121 |
|
122 |
Private method which uses C<Model::WebPAC> and returns results for search |
123 |
query It generatets just I<inner> HTML for results div, so it has C<_ajax> |
124 |
in name. |
125 |
|
126 |
=cut |
127 |
|
128 |
# specify all Hyper Estraier operators which should stop this module |
129 |
# from splitting search query and joining it with default operator |
130 |
my $hest_op_regex = '(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)'; |
131 |
|
132 |
sub results_ajax : Path( 'results/ajax' ) { |
133 |
my ( $self, $c ) = @_; |
134 |
|
135 |
my $params = $c->req->params; |
136 |
my $webpac = $c->comp('Model::WebPAC'); |
137 |
my $log = $c->log; |
138 |
|
139 |
$log->debug("results got params: " . Dumper( $params ) ); |
140 |
|
141 |
if (! $params->{_page} || $params->{_page} < 1) { |
142 |
$params->{_page} = 1; |
143 |
$log->warn("fixed _page parametar to 1"); |
144 |
} |
145 |
|
146 |
my @words; |
147 |
# default operator to join fields/words |
148 |
my $operator = 'AND'; |
149 |
|
150 |
foreach my $f (keys %{ $params }) { |
151 |
|
152 |
next if ($f =~ m/^_/o); |
153 |
|
154 |
my $v = $params->{$f} || next; |
155 |
|
156 |
if (my $op = $params->{ '_' . $f}) { |
157 |
if ($v =~ /$hest_op_regex/) { |
158 |
# don't split words if there is Hyper Estraier |
159 |
# operator in them |
160 |
push @words, $v; |
161 |
} else { |
162 |
push @words, join(" $op ", split(/\s+/, $v) ); |
163 |
} |
164 |
} else { |
165 |
push @words, $v; |
166 |
} |
167 |
|
168 |
next if ($f eq 'all'); # don't add_attr for magic field all |
169 |
|
170 |
if ($v !~ /\s/) { |
171 |
push @{ $c->stash->{attr} }, "$f ISTRINC $v"; |
172 |
} else { |
173 |
map { |
174 |
push @{ $c->stash->{attr} }, "$f ISTRINC $_"; |
175 |
} grep { ! /$hest_op_regex/ } split(/\s+/, $v); |
176 |
} |
177 |
} |
178 |
|
179 |
$c->forward('filter_database'); |
180 |
|
181 |
my $q = join(" $operator ", @words); |
182 |
|
183 |
my $template = $params->{'_template'} || $c->config->{webpac}->{template}; |
184 |
|
185 |
$log->die("can't find _template or default from configuration!") unless ($template); |
186 |
|
187 |
my $hits_on_page = $c->config->{'hyperestraier'}->{'hits_on_page'} || 10; |
188 |
|
189 |
$log->debug("using template $template to produce $hits_on_page results"); |
190 |
|
191 |
$c->stash->{html_results} = sub { |
192 |
my $res = $webpac->search( |
193 |
phrase => $q, |
194 |
template => $template, |
195 |
add_attr => $c->{stash}->{attr}, |
196 |
get_attr => [ '@uri' ], |
197 |
max => $hits_on_page, |
198 |
page => $params->{'_page'}, |
199 |
); |
200 |
# $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res )); |
201 |
return $res; |
202 |
}; |
203 |
|
204 |
$c->stash->{phrase} = $q; |
205 |
$c->stash->{page} = $params->{'_page'}; |
206 |
$c->stash->{hits_on_page} = $hits_on_page; |
207 |
|
208 |
$c->stash->{template} = 'results.tt'; |
209 |
|
210 |
} |
211 |
|
212 |
=item filter_database |
213 |
|
214 |
Takes C<< $c->req->params >> and adds Hyper Estraier |
215 |
filters for checked databases to C<< $c->stash->{attr} >>. |
216 |
|
217 |
=cut |
218 |
|
219 |
sub filter_database : Private { |
220 |
my ( $self, $c ) = @_; |
221 |
|
222 |
my $params = $c->req->params; |
223 |
|
224 |
if ($params->{_database}) { |
225 |
my $type = $c->config->{hyperstraier}->{type} || 'search'; |
226 |
my $attr; |
227 |
if (ref($params->{_database}) eq 'ARRAY') { |
228 |
# FIXME do we need to add $ at end? |
229 |
$attr .= '(' . join("|",@{$params->{_database}}) . ')'; |
230 |
} else { |
231 |
$attr .= $params->{_database}; |
232 |
} |
233 |
push @{ $c->stash->{attr} }, '@uri STRRX /' . $type . '/' . $attr . '/'; |
234 |
$c->log->debug("filter_database: " . join(",", @{ $c->stash->{attr} }) ); |
235 |
} |
236 |
|
237 |
|
238 |
} |
239 |
|
240 |
=item record |
241 |
|
242 |
forwarded to C</editor/record> |
243 |
|
244 |
=cut |
245 |
|
246 |
sub record : Local { |
247 |
my ( $self, $c ) = @_; |
248 |
|
249 |
$c->forward( '/editor/record' ); |
250 |
} |
251 |
|
252 |
=back |
253 |
|
254 |
=head1 AUTHOR |
255 |
|
256 |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
257 |
|
258 |
=head1 LICENSE |
259 |
|
260 |
This library is free software, you can redistribute it and/or modify |
261 |
it under the same terms as Perl itself. |
262 |
|
263 |
=cut |
264 |
|
265 |
1; |