4 |
use warnings; |
use warnings; |
5 |
use base 'Catalyst::Controller'; |
use base 'Catalyst::Controller'; |
6 |
|
|
|
use Data::Dumper; |
|
|
|
|
7 |
use lib '/data/webpac2/lib'; |
use lib '/data/webpac2/lib'; |
8 |
use WebPAC::Search::Estraier; |
use WebPAC::Search::Estraier 0.03; |
9 |
|
|
10 |
|
|
11 |
=head1 NAME |
=head1 NAME |
30 |
|
|
31 |
sub default : Private { |
sub default : Private { |
32 |
my ( $self, $c ) = @_; |
my ( $self, $c ) = @_; |
33 |
|
|
34 |
|
$c->log->dumper($c->req->params, 'params'); |
35 |
|
|
36 |
$c->stash->{template} = 'search.tt'; |
$c->stash->{template} = 'search.tt'; |
37 |
} |
} |
38 |
|
|
39 |
=item suggest |
=item suggest |
40 |
|
|
41 |
Returns results for URLs like C<search/suggest/FieldName> |
Returns results for REST URIs like: |
42 |
|
|
43 |
|
C<search/suggest?search=FieldName&show=TitleProper&FieldName=query%20string> |
44 |
|
|
45 |
It will return field from URL (C<FieldName> in example>) and use |
It will use C<search> field to filter results (and using additional |
46 |
same filed in paramerers (comming from form or URL) or if it doesn't |
C<FieldName> as value of search) and return C<TitleProper> field |
47 |
exist field named C<all>. |
for results. |
48 |
|
|
49 |
|
If C<search> field has magic value <all>, it will search over all data, not |
50 |
|
just one specified field: |
51 |
|
|
52 |
|
C<search/suggest?search=all&show=TitleProper&all=query%20string> |
53 |
|
|
54 |
=cut |
=cut |
55 |
|
|
56 |
sub suggest : Regex('^search/suggest/*([^/]*)') { |
sub suggest : Local { |
57 |
my ( $self, $c ) = @_; |
my ( $self, $c ) = @_; |
58 |
|
|
59 |
my $what = $c->request->snippets->[0]; |
my $search = $c->req->params->{search}; |
60 |
|
my $show = $c->req->params->{show}; |
61 |
|
|
62 |
my $log = $c->log; |
my $log = $c->log; |
63 |
|
|
64 |
my $webpac = $c->comp('Model::WebPAC'); |
my $webpac = $c->comp('Model::WebPAC'); |
65 |
|
#$c->log->dumper( $c->stash, 'stash' ); |
66 |
|
$webpac->setup_site( $c->stash->{site} ); |
67 |
|
|
68 |
my $q = $c->req->params->{$what} || |
my $q = $c->req->params->{ $search || 'all' } || $c->response->body("no results"); |
|
$c->req->params->{all} || $c->res->output("no results"); |
|
69 |
|
|
70 |
|
$log->info("search for '$q' in $search and display $show\n"); |
|
$log->info("search for '$q' in $what\n"); |
|
71 |
|
|
72 |
my $max = $c->config->{'hits_for_suggest'}; |
my $max = $c->config->{'hits_for_suggest'}; |
73 |
if (! $max) { |
if (! $max) { |
76 |
$max = 10; |
$max = 10; |
77 |
} |
} |
78 |
|
|
79 |
my @hits = $webpac->search( |
$c->forward('filter_database'); |
80 |
|
|
81 |
|
my $hits = $webpac->search( |
82 |
phrase => $q, |
phrase => $q, |
83 |
|
add_attr => $c->stash->{attr}, |
84 |
|
get_attr => [ $show ], |
85 |
max => $max, |
max => $max, |
|
get_attr => [ $what ], |
|
86 |
); |
); |
87 |
|
|
88 |
my $used; |
my $used; |
89 |
my @suggestions; |
my @suggestions; |
90 |
|
|
91 |
foreach my $res (@hits) { |
foreach my $res (@{$hits}) { |
92 |
my $v = $res->{ $what } || next; |
my $v = $res->{ $show } || next; |
93 |
next if ($used->{ $v }++); |
next if ($used->{ $v }++); |
94 |
push @suggestions, $v; |
push @suggestions, $v; |
95 |
} |
} |
96 |
|
|
97 |
$log->debug( ($#suggestions + 1) . " unique hits returned"); |
$log->debug( ($#suggestions + 1) . " unique hits returned"); |
98 |
|
|
99 |
$c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
$c->response->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
100 |
} |
} |
101 |
|
|
102 |
|
|
|
# specify all Hyper Estraier operators which should stop this module |
|
|
# from splitting search query and joining it with default operator |
|
|
my $hest_op_regex = qr/(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)/; |
|
|
|
|
103 |
=item results |
=item results |
104 |
|
|
105 |
Mothod which uses C<Model::WebPAC> and returns results for search query |
Returns results for search query |
106 |
|
|
107 |
=cut |
=cut |
108 |
|
|
109 |
sub results : Local { |
sub results : Local { |
110 |
my ( $self, $c ) = @_; |
my ( $self, $c ) = @_; |
111 |
|
|
|
my $webpac = $c->comp('Model::WebPAC'); |
|
112 |
my $params = $c->req->params; |
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 |
|
$webpac->setup_site( $c->stash->{site} ); |
138 |
my $log = $c->log; |
my $log = $c->log; |
139 |
|
|
140 |
$log->debug("results got params: " . Dumper( $params ) ); |
$log->dumper($params, 'params'); |
141 |
|
|
142 |
|
if (! $params->{_page} || $params->{_page} < 1) { |
143 |
|
$params->{_page} = 1; |
144 |
|
$log->warn("fixed _page parametar to 1"); |
145 |
|
} |
146 |
|
|
|
my @attr; |
|
147 |
my @words; |
my @words; |
148 |
# default operator to join fields/words |
# default operator to join fields/words |
149 |
my $operator = 'AND'; |
my $operator = 'AND'; |
155 |
my $v = $params->{$f} || next; |
my $v = $params->{$f} || next; |
156 |
|
|
157 |
if (my $op = $params->{ '_' . $f}) { |
if (my $op = $params->{ '_' . $f}) { |
158 |
if ($v =~ $hest_op_regex) { |
if ($v =~ /$hest_op_regex/) { |
159 |
# don't split words if there is Hyper Estraier |
# don't split words if there is Hyper Estraier |
160 |
# operator in them |
# operator in them |
161 |
push @words, $v; |
push @words, $v; |
169 |
next if ($f eq 'all'); # don't add_attr for magic field all |
next if ($f eq 'all'); # don't add_attr for magic field all |
170 |
|
|
171 |
if ($v !~ /\s/) { |
if ($v !~ /\s/) { |
172 |
push @attr, "$f ISTRINC $v"; |
push @{ $c->stash->{attr} }, "$f ISTRINC $v"; |
173 |
} else { |
} else { |
174 |
map { |
map { |
175 |
push @attr, "$f ISTRINC $_"; |
push @{ $c->stash->{attr} }, "$f ISTRINC $_"; |
176 |
} grep { ! $hest_op_regex } split(/\s+/, $v); |
} grep { ! /$hest_op_regex/ } split(/\s+/, $v); |
177 |
} |
} |
178 |
} |
} |
179 |
|
|
180 |
|
$c->forward('filter_database'); |
181 |
|
|
182 |
my $q = join(" $operator ", @words); |
my $q = join(" $operator ", @words); |
183 |
|
|
184 |
my $template = $params->{'_template'} || $c->config->{webpac}->{template}; |
my $template = $params->{'_template'} || $c->config->{webpac}->{template}; |
185 |
|
|
186 |
$log->die("can't find _template or default from configuration!") unless ($template); |
$log->die("can't find _template or default from configuration!") unless ($template); |
187 |
|
|
188 |
$log->debug("using template $template"); |
my $hits_on_page = $c->config->{'hyperestraier'}->{'hits_on_page'} || 10; |
189 |
|
|
190 |
|
$log->debug("using template $template to produce $hits_on_page results"); |
191 |
|
|
192 |
$c->stash->{html_results} = sub { |
$c->stash->{html_results} = $webpac->search( |
|
my $res = $webpac->search( |
|
193 |
phrase => $q, |
phrase => $q, |
194 |
template => $template, |
template => $template, |
195 |
add_attr => \@attr, |
add_attr => $c->{stash}->{attr}, |
196 |
get_attr => [ '@uri' ], |
get_attr => [ '@uri' ], |
197 |
max => $c->config->{'hits_on_page'}, |
max => $hits_on_page, |
198 |
); |
page => $params->{'_page'}, |
199 |
# $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res )); |
); |
200 |
return $res; |
|
201 |
}; |
$c->stash->{hints} = $webpac->hints; |
202 |
|
|
203 |
$c->stash->{phrase} = $q; |
$c->stash->{phrase} = $q; |
204 |
$c->stash->{attr} = \@attr; |
$c->stash->{page} = $params->{'_page'}; |
205 |
|
$c->stash->{hits_on_page} = $hits_on_page; |
206 |
|
|
207 |
$c->stash->{template} = 'results.tt'; |
$c->stash->{template} = 'results.tt'; |
208 |
|
|
209 |
} |
} |
210 |
|
|
211 |
=back |
=item filter_database |
212 |
|
|
213 |
|
Takes C<< $c->req->params >> and adds Hyper Estraier |
214 |
|
filters for checked databases to C<< $c->stash->{attr} >>. |
215 |
|
|
216 |
|
=cut |
217 |
|
|
218 |
|
sub filter_database : Private { |
219 |
|
my ( $self, $c ) = @_; |
220 |
|
|
221 |
|
my $params = $c->req->params; |
222 |
|
|
223 |
|
if ($params->{_database}) { |
224 |
|
my $type = $c->config->{hyperstraier}->{type} || 'search'; |
225 |
|
my $attr; |
226 |
|
if (ref($params->{_database}) eq 'ARRAY') { |
227 |
|
# FIXME do we need to add $ at end? |
228 |
|
$attr .= '(' . join("|",@{$params->{_database}}) . ')'; |
229 |
|
} else { |
230 |
|
$attr .= $params->{_database}; |
231 |
|
} |
232 |
|
push @{ $c->stash->{attr} }, '@uri STRRX /' . $type . '/' . $attr . '/'; |
233 |
|
$c->log->debug("filter_database: " . join(",", @{ $c->stash->{attr} }) ); |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
} |
238 |
|
|
239 |
|
=item record |
240 |
|
|
241 |
|
forwarded to C</editor/record> |
242 |
|
|
243 |
|
=cut |
244 |
|
|
245 |
|
sub record : Local { |
246 |
|
my ( $self, $c ) = @_; |
247 |
|
|
248 |
|
$c->detach( '/editor/record' ); |
249 |
|
} |
250 |
|
|
251 |
|
=back |
252 |
|
|
253 |
=head1 AUTHOR |
=head1 AUTHOR |
254 |
|
|