/[webpac2]/Webpacus/lib/Webpacus/Controller/Search.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /Webpacus/lib/Webpacus/Controller/Search.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 399 - (show annotations)
Sun Feb 19 12:37:27 2006 UTC (18 years, 2 months ago) by dpavlin
File size: 5805 byte(s)
 r461@llin:  dpavlin | 2006-02-19 13:46:03 +0100
 each site now uses default index which is named same as site, added site_url to view,
 refactor site handling

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 $c->log->debug( "stash: ", Dumper($c->stash) );
68 $webpac->setup_site( $c->stash->{site} );
69
70 my $q = $c->req->params->{ $search || 'all' } || $c->response->body("no results");
71
72 $log->info("search for '$q' in $search and display $show\n");
73
74 my $max = $c->config->{'hits_for_suggest'};
75 if (! $max) {
76 $log->info("hits_for_suggest isn't defined, defaulting to 10");
77 $c->config->{'hits_for_suggest'} = 10;
78 $max = 10;
79 }
80
81 $c->forward('filter_database');
82
83 my $hits = $webpac->search(
84 phrase => $q,
85 add_attr => $c->stash->{attr},
86 get_attr => [ $show ],
87 max => $max,
88 );
89
90 my $used;
91 my @suggestions;
92
93 foreach my $res (@{$hits}) {
94 my $v = $res->{ $show } || next;
95 next if ($used->{ $v }++);
96 push @suggestions, $v;
97 }
98
99 $log->debug( ($#suggestions + 1) . " unique hits returned");
100
101 $c->response->body( $c->prototype->auto_complete_result( \@suggestions ) );
102 }
103
104
105 =item results
106
107 Returns results for search query
108
109 =cut
110
111 sub results : Local {
112 my ( $self, $c ) = @_;
113
114 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;
141
142 $log->debug("results got params: " . Dumper( $params ) );
143
144 if (! $params->{_page} || $params->{_page} < 1) {
145 $params->{_page} = 1;
146 $log->warn("fixed _page parametar to 1");
147 }
148
149 my @words;
150 # default operator to join fields/words
151 my $operator = 'AND';
152
153 foreach my $f (keys %{ $params }) {
154
155 next if ($f =~ m/^_/o);
156
157 my $v = $params->{$f} || next;
158
159 if (my $op = $params->{ '_' . $f}) {
160 if ($v =~ /$hest_op_regex/) {
161 # don't split words if there is Hyper Estraier
162 # operator in them
163 push @words, $v;
164 } else {
165 push @words, join(" $op ", split(/\s+/, $v) );
166 }
167 } else {
168 push @words, $v;
169 }
170
171 next if ($f eq 'all'); # don't add_attr for magic field all
172
173 if ($v !~ /\s/) {
174 push @{ $c->stash->{attr} }, "$f ISTRINC $v";
175 } else {
176 map {
177 push @{ $c->stash->{attr} }, "$f ISTRINC $_";
178 } grep { ! /$hest_op_regex/ } split(/\s+/, $v);
179 }
180 }
181
182 $c->forward('filter_database');
183
184 my $q = join(" $operator ", @words);
185
186 my $template = $params->{'_template'} || $c->config->{webpac}->{template};
187
188 $log->die("can't find _template or default from configuration!") unless ($template);
189
190 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 {
195 my $res = $webpac->search(
196 phrase => $q,
197 template => $template,
198 add_attr => $c->{stash}->{attr},
199 get_attr => [ '@uri' ],
200 max => $hits_on_page,
201 page => $params->{'_page'},
202 );
203 # $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res ));
204 return $res;
205 };
206
207 $c->stash->{phrase} = $q;
208 $c->stash->{page} = $params->{'_page'};
209 $c->stash->{hits_on_page} = $hits_on_page;
210
211 $c->stash->{template} = 'results.tt';
212
213 }
214
215 =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
258
259 Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>
260
261 =head1 LICENSE
262
263 This library is free software, you can redistribute it and/or modify
264 it under the same terms as Perl itself.
265
266 =cut
267
268 1;

  ViewVC Help
Powered by ViewVC 1.1.26