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 |
$c->stash->{template} = 'search.tt'; |
36 |
} |
37 |
|
38 |
=item suggest |
39 |
|
40 |
Returns results for URLs like C<search/suggest/FieldName> |
41 |
|
42 |
It will return field from URL (C<FieldName> in example>) and use |
43 |
same filed in paramerers (comming from form or URL) or if it doesn't |
44 |
exist field named C<all>. |
45 |
|
46 |
=cut |
47 |
|
48 |
sub suggest : Regex('^search/suggest/*([^/]*)') { |
49 |
my ( $self, $c ) = @_; |
50 |
|
51 |
my $what = $c->request->snippets->[0]; |
52 |
|
53 |
my $log = $c->log; |
54 |
|
55 |
my $webpac = $c->comp('Model::WebPAC'); |
56 |
|
57 |
my $q = $c->req->params->{$what} || |
58 |
$c->req->params->{all} || $c->res->output("no results"); |
59 |
|
60 |
|
61 |
$log->info("search for '$q' in $what\n"); |
62 |
|
63 |
my $max = $c->config->{'hits_for_suggest'}; |
64 |
if (! $max) { |
65 |
$log->info("hits_for_suggest isn't defined, defaulting to 10"); |
66 |
$c->config->{'hits_for_suggest'} = 10; |
67 |
$max = 10; |
68 |
} |
69 |
|
70 |
my @hits = $webpac->search( |
71 |
phrase => $q, |
72 |
max => $max, |
73 |
get_attr => [ $what ], |
74 |
); |
75 |
|
76 |
my $used; |
77 |
my @suggestions; |
78 |
|
79 |
foreach my $res (@hits) { |
80 |
my $v = $res->{ $what } || next; |
81 |
next if ($used->{ $v }++); |
82 |
push @suggestions, $v; |
83 |
} |
84 |
|
85 |
$log->debug( ($#suggestions + 1) . " unique hits returned"); |
86 |
|
87 |
$c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); |
88 |
} |
89 |
|
90 |
|
91 |
# specify all Hyper Estraier operators which should stop this module |
92 |
# from splitting search query and joining it with default operator |
93 |
my $hest_op_regex = qr/(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)/; |
94 |
|
95 |
=item results |
96 |
|
97 |
Method which uses C<Model::WebPAC> and returns results for search query |
98 |
|
99 |
=cut |
100 |
|
101 |
sub results : Local { |
102 |
my ( $self, $c ) = @_; |
103 |
|
104 |
my $webpac = $c->comp('Model::WebPAC'); |
105 |
my $params = $c->req->params; |
106 |
my $log = $c->log; |
107 |
|
108 |
$log->debug("results got params: " . Dumper( $params ) ); |
109 |
|
110 |
if (! $params->{_page} || $params->{_page} < 1) { |
111 |
$params->{_page} = 1; |
112 |
$log->warn("fixed _page parametar to 1"); |
113 |
} |
114 |
|
115 |
my @attr; |
116 |
my @words; |
117 |
# default operator to join fields/words |
118 |
my $operator = 'AND'; |
119 |
|
120 |
foreach my $f (keys %{ $params }) { |
121 |
|
122 |
next if ($f =~ m/^_/o); |
123 |
|
124 |
my $v = $params->{$f} || next; |
125 |
|
126 |
if (my $op = $params->{ '_' . $f}) { |
127 |
if ($v =~ $hest_op_regex) { |
128 |
# don't split words if there is Hyper Estraier |
129 |
# operator in them |
130 |
push @words, $v; |
131 |
} else { |
132 |
push @words, join(" $op ", split(/\s+/, $v) ); |
133 |
} |
134 |
} else { |
135 |
push @words, $v; |
136 |
} |
137 |
|
138 |
next if ($f eq 'all'); # don't add_attr for magic field all |
139 |
|
140 |
if ($v !~ /\s/) { |
141 |
push @attr, "$f ISTRINC $v"; |
142 |
} else { |
143 |
map { |
144 |
push @attr, "$f ISTRINC $_"; |
145 |
} grep { ! $hest_op_regex } split(/\s+/, $v); |
146 |
} |
147 |
} |
148 |
|
149 |
my $q = join(" $operator ", @words); |
150 |
|
151 |
my $template = $params->{'_template'} || $c->config->{webpac}->{template}; |
152 |
|
153 |
$log->die("can't find _template or default from configuration!") unless ($template); |
154 |
|
155 |
my $hits_on_page = $c->config->{'hyperestraier'}->{'hits_on_page'} || 10; |
156 |
|
157 |
$log->debug("using template $template to produce $hits_on_page results"); |
158 |
|
159 |
$c->stash->{html_results} = sub { |
160 |
my $res = $webpac->search( |
161 |
phrase => $q, |
162 |
template => $template, |
163 |
add_attr => \@attr, |
164 |
get_attr => [ '@uri' ], |
165 |
max => $hits_on_page, |
166 |
page => $params->{'_page'}, |
167 |
); |
168 |
# $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res )); |
169 |
return $res; |
170 |
}; |
171 |
|
172 |
$c->stash->{phrase} = $q; |
173 |
$c->stash->{attr} = \@attr; |
174 |
$c->stash->{page} = $params->{'_page'}; |
175 |
$c->stash->{hits_on_page} = $hits_on_page; |
176 |
|
177 |
$c->stash->{template} = 'results.tt'; |
178 |
} |
179 |
|
180 |
=item record |
181 |
|
182 |
forwarded to C</editor/record> |
183 |
|
184 |
=cut |
185 |
|
186 |
sub record : Local { |
187 |
my ( $self, $c ) = @_; |
188 |
|
189 |
$c->forward( '/editor/record' ); |
190 |
} |
191 |
|
192 |
=back |
193 |
|
194 |
=head1 AUTHOR |
195 |
|
196 |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
197 |
|
198 |
=head1 LICENSE |
199 |
|
200 |
This library is free software, you can redistribute it and/or modify |
201 |
it under the same terms as Perl itself. |
202 |
|
203 |
=cut |
204 |
|
205 |
1; |