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