/[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 270 by dpavlin, Sat Dec 17 00:37:12 2005 UTC revision 382 by dpavlin, Sun Jan 22 02:52:24 2006 UTC
# Line 40  sub default : Private { Line 40  sub default : Private {
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  It will return field from URL (C<FieldName> in example>) and use  C<search/suggest?search=FieldName&show=TitleProper&FieldName=query%20string>
46  same filed in paramerers (comming from form or URL) or if it doesn't  
47  exist field named C<all>.  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  =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->response->body("no results");  
69    
70            $log->info("search for '$q' in $search and display $show\n");
         $log->info("search for '$q' in $what\n");  
71    
72          my $max = $c->config->{'hits_for_suggest'};          my $max = $c->config->{'hits_for_suggest'};
73          if (! $max) {          if (! $max) {
# Line 70  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          }          }
# Line 102  sub results : Local { Line 111  sub results : Local {
111    
112          my $params = $c->req->params;          my $params = $c->req->params;
113    
114          # oh, client didn't have ability to setup AJAX, fallback to          # do full-page refresh for clients without JavaScript
115          # page refresh          $c->stash->{results} = $c->subreq('/search/results/ajax', {}, $params);
116          if (! $params->{'_ajax'}) {          $c->stash->{template} = 'search.tt';
                 $params->{_ajax} = 1;  
                 $c->stash->{results} = $c->subreq('/search/results/ajax', {}, $params);  
         $c->stash->{template} = 'search.tt';  
         } else {  
                 $c->forward('/search/results/ajax');  
         }  
117  }  }
118    
119    
# Line 124  in name. Line 127  in name.
127    
128  # specify all Hyper Estraier operators which should stop this module  # specify all Hyper Estraier operators which should stop this module
129  # from splitting search query and joining it with default operator  # from splitting search query and joining it with default operator
130  my $hest_op_regex = qr/(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)/o;  my $hest_op_regex = '(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)';
131    
132  sub results_ajax : Path( 'results/ajax' ) {  sub results_ajax : Path( 'results/ajax' ) {
133          my ( $self, $c ) = @_;          my ( $self, $c ) = @_;
# Line 140  sub results_ajax : Path( 'results/ajax' Line 143  sub results_ajax : Path( 'results/ajax'
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 152  sub results_ajax : Path( 'results/ajax' Line 154  sub results_ajax : Path( 'results/ajax'
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 166  sub results_ajax : Path( 'results/ajax' Line 168  sub results_ajax : Path( 'results/ajax'
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 188  sub results_ajax : Path( 'results/ajax' Line 192  sub results_ajax : Path( 'results/ajax'
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 198  sub results_ajax : Path( 'results/ajax' Line 202  sub results_ajax : Path( 'results/ajax'
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    
# Line 206  sub results_ajax : Path( 'results/ajax' Line 209  sub results_ajax : Path( 'results/ajax'
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
241    
242  forwarded to C</editor/record>  forwarded to C</editor/record>

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

  ViewVC Help
Powered by ViewVC 1.1.26