/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 155 by dpavlin, Sat Nov 26 01:54:42 2005 UTC revision 400 by dpavlin, Sun Feb 19 13:14:26 2006 UTC
# Line 4  use strict; Line 4  use strict;
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
# Line 32  Catalyst Controller for search fields Hy Line 30  Catalyst Controller for search fields Hy
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 $what\n");          $log->info("search for '$q' in $search and display $show\n");
71    
72          my $max = $c->config->{'hits_for_suggest'};          my $max = $c->config->{'hits_for_suggest'};
73          if (! $max) {          if (! $max) {
# Line 67  sub suggest : Regex('^search/suggest/*([ Line 76  sub suggest : Regex('^search/suggest/*([
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';
# Line 119  sub results : Local { Line 155  sub results : Local {
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;
# Line 133  sub results : Local { Line 169  sub results : Local {
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} = sub {
193                  my $res = $webpac->search(                  my $res = $webpac->search(
194                          phrase => $q,                          phrase => $q,
195                          template => $template,                          template => $template,
196                          add_attr => \@attr,                          add_attr => $c->{stash}->{attr},
197                          get_attr => [ '@uri' ],                          get_attr => [ '@uri' ],
198                          max => $c->config->{'hits_on_page'},                          max => $hits_on_page,
199                            page => $params->{'_page'},
200                  );                  );
201          #       $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res ));  #               $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res ));
202                  return $res;                  return $res;
203          };          };
204    
205          $c->stash->{phrase} = $q;          $c->stash->{phrase} = $q;
206          $c->stash->{attr} = \@attr;          $c->stash->{page} = $params->{'_page'};
207            $c->stash->{hits_on_page} = $hits_on_page;
208    
209          $c->stash->{template} = 'results.tt';          $c->stash->{template} = 'results.tt';
210    
211  }  }
212    
213  =back  =item filter_database
214    
215    Takes C<< $c->req->params >> and adds Hyper Estraier
216    filters for checked databases to C<< $c->stash->{attr} >>.
217    
218    =cut
219    
220    sub filter_database : Private {
221            my ( $self, $c ) = @_;
222    
223            my $params = $c->req->params;
224    
225            if ($params->{_database}) {
226                    my $type = $c->config->{hyperstraier}->{type} || 'search';
227                    my $attr;
228                    if (ref($params->{_database}) eq 'ARRAY') {
229                            # FIXME do we need to add $ at end?
230                            $attr .= '(' . join("|",@{$params->{_database}}) . ')';
231                    } else {
232                            $attr .= $params->{_database};
233                    }
234                    push @{ $c->stash->{attr} }, '@uri STRRX /' . $type . '/' . $attr . '/';
235                    $c->log->debug("filter_database: " . join(",", @{ $c->stash->{attr} }) );
236            }
237    
238    
239    }
240    
241    =item record
242    
243    forwarded to C</editor/record>
244    
245    =cut
246    
247    sub record : Local {
248            my ( $self, $c ) = @_;
249    
250            $c->forward( '/editor/record' );
251    }
252    
253    =back
254    
255  =head1 AUTHOR  =head1 AUTHOR
256    

Legend:
Removed from v.155  
changed lines
  Added in v.400

  ViewVC Help
Powered by ViewVC 1.1.26