/[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 270 - (show annotations)
Sat Dec 17 00:37:12 2005 UTC (18 years, 4 months ago) by dpavlin
File size: 4774 byte(s)
 r11747@llin:  dpavlin | 2005-12-17 05:37:34 +0100
 make browsers without JavaScript supported, at least for search [0.20]

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

  ViewVC Help
Powered by ViewVC 1.1.26