/[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 200 by dpavlin, Wed Nov 30 23:21:30 2005 UTC revision 382 by dpavlin, Sun Jan 22 02:52:24 2006 UTC
# Line 32  Catalyst Controller for search fields Hy Line 32  Catalyst Controller for search fields Hy
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 return field from URL (C<FieldName> in example>) and use  It will use C<search> field to filter results (and using additional
48  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
49  exist field named C<all>.  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  =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    
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  Method 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          my $log = $c->log;          my $log = $c->log;
138    
139          $log->debug("results got params: " . Dumper( $params ) );          $log->debug("results got params: " . Dumper( $params ) );
# Line 112  sub results : Local { Line 143  sub results : Local {
143                  $log->warn("fixed _page parametar to 1");                  $log->warn("fixed _page parametar to 1");
144          }          }
145    
         my @attr;  
146          my @words;          my @words;
147          # default operator to join fields/words          # default operator to join fields/words
148          my $operator = 'AND';          my $operator = 'AND';
# Line 124  sub results : Local { Line 154  sub results : Local {
154                  my $v = $params->{$f} || next;                  my $v = $params->{$f} || next;
155    
156                  if (my $op = $params->{ '_' . $f}) {                  if (my $op = $params->{ '_' . $f}) {
157                          if ($v =~ $hest_op_regex) {                          if ($v =~ /$hest_op_regex/) {
158                                  # don't split words if there is Hyper Estraier                                  # don't split words if there is Hyper Estraier
159                                  # operator in them                                  # operator in them
160                                  push @words, $v;                                  push @words, $v;
# Line 138  sub results : Local { Line 168  sub results : Local {
168                  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
169    
170                  if ($v !~ /\s/) {                  if ($v !~ /\s/) {
171                          push @attr, "$f ISTRINC $v";                          push @{ $c->stash->{attr} }, "$f ISTRINC $v";
172                  } else {                  } else {
173                          map {                          map {
174                                  push @attr, "$f ISTRINC $_";                                  push @{ $c->stash->{attr} }, "$f ISTRINC $_";
175                          } grep { ! $hest_op_regex } split(/\s+/, $v);                          } grep { ! /$hest_op_regex/ } split(/\s+/, $v);
176                  }                  }
177          }          }
178    
179        $c->forward('filter_database');
180    
181          my $q = join(" $operator ", @words);          my $q = join(" $operator ", @words);
182    
183          my $template = $params->{'_template'} || $c->config->{webpac}->{template};          my $template = $params->{'_template'} || $c->config->{webpac}->{template};
# Line 160  sub results : Local { Line 192  sub results : Local {
192                  my $res = $webpac->search(                  my $res = $webpac->search(
193                          phrase => $q,                          phrase => $q,
194                          template => $template,                          template => $template,
195                          add_attr => \@attr,                          add_attr => $c->{stash}->{attr},
196                          get_attr => [ '@uri' ],                          get_attr => [ '@uri' ],
197                          max => $hits_on_page,                          max => $hits_on_page,
198                          page => $params->{'_page'},                          page => $params->{'_page'},
# Line 170  sub results : Local { Line 202  sub results : Local {
202          };          };
203    
204          $c->stash->{phrase} = $q;          $c->stash->{phrase} = $q;
         $c->stash->{attr} = \@attr;  
205          $c->stash->{page} = $params->{'_page'};          $c->stash->{page} = $params->{'_page'};
206          $c->stash->{hits_on_page} = $hits_on_page;          $c->stash->{hits_on_page} = $hits_on_page;
207    
208          $c->stash->{template} = 'results.tt';          $c->stash->{template} = 'results.tt';
209    
210    }
211    
212    =item filter_database
213    
214    Takes C<< $c->req->params >> and adds Hyper Estraier
215    filters for checked databases to C<< $c->stash->{attr} >>.
216    
217    =cut
218    
219    sub filter_database : Private {
220            my ( $self, $c ) = @_;
221    
222            my $params = $c->req->params;
223    
224            if ($params->{_database}) {
225                    my $type = $c->config->{hyperstraier}->{type} || 'search';
226                    my $attr;
227                    if (ref($params->{_database}) eq 'ARRAY') {
228                            # FIXME do we need to add $ at end?
229                            $attr .= '(' . join("|",@{$params->{_database}}) . ')';
230                    } else {
231                            $attr .= $params->{_database};
232                    }
233                    push @{ $c->stash->{attr} }, '@uri STRRX /' . $type . '/' . $attr . '/';
234                    $c->log->debug("filter_database: " . join(",", @{ $c->stash->{attr} }) );
235            }
236    
237    
238  }  }
239    
240  =item record  =item record

Legend:
Removed from v.200  
changed lines
  Added in v.382

  ViewVC Help
Powered by ViewVC 1.1.26