/[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 313 by dpavlin, Wed Dec 21 22:18:56 2005 UTC revision 405 by dpavlin, Sun Feb 19 22:40:40 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 0.03;  use WebPAC::Search::Estraier 0.03;
9    
# Line 33  Catalyst Controller for search fields Hy Line 31  Catalyst Controller for search fields Hy
31  sub default : Private {  sub default : Private {
32      my ( $self, $c ) = @_;      my ( $self, $c ) = @_;
33    
34      $c->log->debug("default search got param: ".Dumper($c->req->params));          $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 use C<search> field to filter results (and using additional
46    C<FieldName> as value of search) and return C<TitleProper> field
47    for results.
48    
49  It will return field from URL (C<FieldName> in example>) and use  If C<search> field has magic value <all>, it will search over all data, not
50  same filed in paramerers (comming from form or URL) or if it doesn't  just one specified field:
51  exist field named C<all>.  
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->response->body("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 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 125  sub results_ajax : Path( 'results/ajax' Line 134  sub results_ajax : Path( 'results/ajax'
134    
135          my $params = $c->req->params;          my $params = $c->req->params;
136          my $webpac = $c->comp('Model::WebPAC');          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) {          if (! $params->{_page} || $params->{_page} < 1) {
143                  $params->{_page} = 1;                  $params->{_page} = 1;
144                  $log->warn("fixed _page parametar to 1");                  $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 160  sub results_ajax : Path( 'results/ajax' Line 169  sub results_ajax : Path( 'results/ajax'
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          if ($params->{_database}) {      $c->forward('filter_database');
                 my $type = $c->config->{hyperstraier}->{type} || 'search';  
                 my $attr;  
                 if (ref($params->{_database}) eq 'ARRAY') {  
                         # FIXME do we need to add $ at end?  
                         $attr .= '(' . join("|",@{$params->{_database}}) . ')';  
                 } else {  
                         $attr .= $params->{_database};  
                 }  
                 push @attr, '@uri STRRX /' . $type . '/' . $attr . '/';  
         }  
181    
182          my $q = join(" $operator ", @words);          my $q = join(" $operator ", @words);
183    
# Line 190  sub results_ajax : Path( 'results/ajax' Line 189  sub results_ajax : Path( 'results/ajax'
189    
190          $log->debug("using template $template to produce $hits_on_page results");          $log->debug("using template $template to produce $hits_on_page results");
191    
192          $c->stash->{html_results} = sub {          $c->stash->{html_results} = $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'},
199                  );          );
200  #               $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res ));  
201                  return $res;          $c->stash->{hints} = $webpac->hints;
         };  
202    
203          $c->stash->{phrase} = $q;          $c->stash->{phrase} = $q;
         $c->stash->{attr} = \@attr;  
204          $c->stash->{page} = $params->{'_page'};          $c->stash->{page} = $params->{'_page'};
205          $c->stash->{hits_on_page} = $hits_on_page;          $c->stash->{hits_on_page} = $hits_on_page;
206    
# Line 212  sub results_ajax : Path( 'results/ajax' Line 208  sub results_ajax : Path( 'results/ajax'
208    
209  }  }
210    
211    =item filter_database
212    
213    Takes C<< $c->req->params >> and adds Hyper Estraier
214    filters for checked databases to C<< $c->stash->{attr} >>.
215    
216    =cut
217    
218    sub filter_database : Private {
219            my ( $self, $c ) = @_;
220    
221            my $params = $c->req->params;
222    
223            if ($params->{_database}) {
224                    my $type = $c->config->{hyperstraier}->{type} || 'search';
225                    my $attr;
226                    if (ref($params->{_database}) eq 'ARRAY') {
227                            # FIXME do we need to add $ at end?
228                            $attr .= '(' . join("|",@{$params->{_database}}) . ')';
229                    } else {
230                            $attr .= $params->{_database};
231                    }
232                    push @{ $c->stash->{attr} }, '@uri STRRX /' . $type . '/' . $attr . '/';
233                    $c->log->debug("filter_database: " . join(",", @{ $c->stash->{attr} }) );
234            }
235    
236    
237    }
238    
239  =item record  =item record
240    
241  forwarded to C</editor/record>  forwarded to C</editor/record>

Legend:
Removed from v.313  
changed lines
  Added in v.405

  ViewVC Help
Powered by ViewVC 1.1.26