/[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 440 by dpavlin, Sun Apr 30 23:20:12 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    use Data::SpreadPagination;
10    
11    
12  =head1 NAME  =head1 NAME
# Line 33  Catalyst Controller for search fields Hy Line 32  Catalyst Controller for search fields Hy
32  sub default : Private {  sub default : Private {
33      my ( $self, $c ) = @_;      my ( $self, $c ) = @_;
34    
35      $c->log->debug("default search got param: ".Dumper($c->req->params));          $c->log->dumper($c->req->params, 'params');
36    
37      $c->stash->{template} = 'search.tt';      $c->stash->{template} = 'search.tt';
38  }  }
39    
40  =item suggest  =item suggest
41    
42  Returns results for URLs like C<search/suggest/FieldName>  Returns results for REST URIs like:
43    
44    C<search/suggest?search=FieldName&show=TitleProper&FieldName=query%20string>
45    
46    It will use C<search> field to filter results (and using additional
47    C<FieldName> as value of search) and return C<TitleProper> field
48    for results.
49    
50  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
51  same filed in paramerers (comming from form or URL) or if it doesn't  just one specified field:
52  exist field named C<all>.  
53    C<search/suggest?search=all&show=TitleProper&all=query%20string>
54    
55  =cut  =cut
56    
57  sub suggest : Regex('^search/suggest/*([^/]*)') {  sub suggest : Local {
58          my ( $self, $c ) = @_;          my ( $self, $c ) = @_;
59    
60          my $what = $c->request->snippets->[0];          my $search = $c->req->params->{search};
61            my $show = $c->req->params->{show};
62    
63          my $log = $c->log;          my $log = $c->log;
64    
65          my $webpac = $c->comp('Model::WebPAC');          my $webpac = $c->comp('Model::WebPAC');
66            #$c->log->dumper( $c->stash, 'stash' );
67            $webpac->setup_site( $c->stash->{site} );
68    
69          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");  
   
70    
71          $log->info("search for '$q' in $what\n");          $log->info("search for '$q' in $search and display $show\n");
72    
73          my $max = $c->config->{'hits_for_suggest'};          my $max = $c->config->{'hits_for_suggest'};
74          if (! $max) {          if (! $max) {
# Line 70  sub suggest : Regex('^search/suggest/*([ Line 77  sub suggest : Regex('^search/suggest/*([
77                  $max = 10;                  $max = 10;
78          }          }
79    
80          my @hits = $webpac->search(          $c->forward('filter_database');
81    
82            my $hits = $webpac->search(
83                  phrase => $q,                  phrase => $q,
84                    add_attr => $c->stash->{attr},
85                    get_attr => [ $show ],
86                  max => $max,                  max => $max,
                 get_attr => [ $what ],  
87          );          );
88    
89          my $used;          my $used;
90          my @suggestions;          my @suggestions;
91    
92          foreach my $res (@hits) {          foreach my $res (@{$hits}) {
93                  my $v = $res->{ $what } || next;                  my $v = $res->{ $show } || next;
94                  next if ($used->{ $v }++);                  next if ($used->{ $v }++);
95                  push @suggestions, $v;                  push @suggestions, $v;
96          }          }
# Line 125  sub results_ajax : Path( 'results/ajax' Line 135  sub results_ajax : Path( 'results/ajax'
135    
136          my $params = $c->req->params;          my $params = $c->req->params;
137          my $webpac = $c->comp('Model::WebPAC');          my $webpac = $c->comp('Model::WebPAC');
138            $webpac->setup_site( $c->stash->{site} );
139          my $log = $c->log;          my $log = $c->log;
140    
141          $log->debug("results got params: " . Dumper( $params ) );          $log->dumper($params, 'params');
142    
143          if (! $params->{_page} || $params->{_page} < 1) {          if (! $params->{_page} || $params->{_page} < 1) {
144                  $params->{_page} = 1;                  $params->{_page} = 1;
145                  $log->warn("fixed _page parametar to 1");                  $log->warn("fixed _page parametar to 1");
146          }          }
147    
         my @attr;  
148          my @words;          my @words;
149          # default operator to join fields/words          # default operator to join fields/words
150          my $operator = 'AND';          my $operator = 'AND';
# Line 160  sub results_ajax : Path( 'results/ajax' Line 170  sub results_ajax : Path( 'results/ajax'
170                  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
171    
172                  if ($v !~ /\s/) {                  if ($v !~ /\s/) {
173                          push @attr, "$f ISTRINC $v";                          push @{ $c->stash->{attr} }, "$f ISTRINC $v";
174                  } else {                  } else {
175                          map {                          map {
176                                  push @attr, "$f ISTRINC $_";                                  push @{ $c->stash->{attr} }, "$f ISTRINC $_";
177                          } grep { ! /$hest_op_regex/ } split(/\s+/, $v);                          } grep { ! /$hest_op_regex/ } split(/\s+/, $v);
178                  }                  }
179          }          }
180    
181          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 . '/';  
         }  
182    
183          my $q = join(" $operator ", @words);          my $q = join(" $operator ", @words);
184    
# Line 190  sub results_ajax : Path( 'results/ajax' Line 190  sub results_ajax : Path( 'results/ajax'
190    
191          $log->debug("using template $template to produce $hits_on_page results");          $log->debug("using template $template to produce $hits_on_page results");
192    
193          $c->stash->{html_results} = sub {          $c->stash->{html_results} = $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 => $hits_on_page,                          max => $hits_on_page,
199                          page => $params->{'_page'},                          page => $params->{'_page'},
200                  );          );
201  #               $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res ));  
202                  return $res;          $c->stash->{hints} = $webpac->hints;
         };  
203    
204          $c->stash->{phrase} = $q;          $c->stash->{phrase} = $q;
205          $c->stash->{attr} = \@attr;          $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            # create pager
209            $c->stash->{pager} = new Data::SpreadPagination({
210                    totalEntries => $webpac->hints->{hit},
211                    entriesPerPage => $hits_on_page,
212                    currentPage => $params->{_page},
213                    maxPages => $c->config->{pager}->{max_pages} || 10,
214            });
215    
216          $c->stash->{template} = 'results.tt';          $c->stash->{template} = 'results.tt';
217    
218  }  }
219    
220    =item filter_database
221    
222    Takes C<< $c->req->params >> and adds Hyper Estraier
223    filters for checked databases to C<< $c->stash->{attr} >>.
224    
225    =cut
226    
227    sub filter_database : Private {
228            my ( $self, $c ) = @_;
229    
230            my $params = $c->req->params;
231    
232            if ($params->{_database}) {
233                    my $type = $c->config->{hyperstraier}->{type} || 'search';
234                    my $attr;
235                    if (ref($params->{_database}) eq 'ARRAY') {
236                            # FIXME do we need to add $ at end?
237                            $attr .= '(' . join("|",@{$params->{_database}}) . ')';
238                    } else {
239                            $attr .= $params->{_database};
240                    }
241                    push @{ $c->stash->{attr} }, '@uri STRRX /' . $type . '/' . $attr . '/';
242                    $c->log->debug("filter_database: " . join(",", @{ $c->stash->{attr} }) );
243            }
244    
245    
246    }
247    
248  =item record  =item record
249    
250  forwarded to C</editor/record>  forwarded to C</editor/record>
# Line 221  forwarded to C</editor/record> Line 254  forwarded to C</editor/record>
254  sub record : Local {  sub record : Local {
255          my ( $self, $c ) = @_;          my ( $self, $c ) = @_;
256    
257          $c->forward( '/editor/record' );          $c->detach( '/editor/record' );
258  }  }
259    
260  =back  =back

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

  ViewVC Help
Powered by ViewVC 1.1.26