/[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 116 by dpavlin, Wed Nov 23 21:52:20 2005 UTC revision 271 by dpavlin, Sat Dec 17 03:19:58 2005 UTC
# Line 4  use strict; Line 4  use strict;
4  use warnings;  use warnings;
5  use base 'Catalyst::Controller';  use base 'Catalyst::Controller';
6    
7    use Data::Dumper;
8    
9  use lib '/data/webpac2/lib';  use lib '/data/webpac2/lib';
10  use WebPAC::Search::Estraier;  use WebPAC::Search::Estraier 0.03;
11    
12    
13  =head1 NAME  =head1 NAME
14    
15  Webpacus::Controller::Search - Catalyst Controller  Webpacus::Controller::Search - Search WebPAC data
16    
17  =head1 SYNOPSIS  =head1 SYNOPSIS
18    
19  See L<Webpacus>  See L<WebPAC>, L<Webpacus>
20    
21  =head1 DESCRIPTION  =head1 DESCRIPTION
22    
23  Catalyst Controller for autocompleting search fields.  Catalyst Controller for search fields Hyper Estraier
24    
25  =head1 METHODS  =head1 METHODS
26    
# Line 29  Catalyst Controller for autocompleting s Line 32  Catalyst Controller for autocompleting s
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    
# Line 36  sub default : Private { Line 42  sub default : Private {
42    
43  Returns results for URLs like C<search/suggest/FieldName>  Returns results for URLs like C<search/suggest/FieldName>
44    
45    It will return field from URL (C<FieldName> in example>) and use
46    same filed in paramerers (comming from form or URL) or if it doesn't
47    exist field named C<all>.
48    
49  =cut  =cut
50    
51  sub suggest : Regex('^search/suggest/*([^/]*)') {  sub suggest : Regex('^search/suggest/*([^/]*)') {
52      my ( $self, $c ) = @_;          my ( $self, $c ) = @_;
53    
54            my $what = $c->request->snippets->[0];
55    
56      my $what = $c->request->snippets->[0];          my $log = $c->log;
57    
58      my $log = $c->log;          my $webpac = $c->comp('Model::WebPAC');
59    
60   my $est = new WebPAC::Search::Estraier(          my $q = $c->req->params->{$what} ||
61          url => 'http://localhost:1978/node/webpac2',                  $c->req->params->{all} || $c->response->body("no results");
         user => 'admin',  
         passwd => 'admin',  
         encoding => 'UTF-8',  
         log => $c->log,  
  );  
       
62    
     my $q = $c->req->params->{$what};  
     my @suggestions;  
63    
64          $log->info("search for '$q' in $what\n");          $log->info("search for '$q' in $what\n");
     my @hits = $est->search( phrase => $q, max => 10, get_attr => [ $what ] );  
65    
66      my $used;          my $max = $c->config->{'hits_for_suggest'};
67            if (! $max) {
68                    $log->info("hits_for_suggest isn't defined, defaulting to 10");
69                    $c->config->{'hits_for_suggest'} = 10;
70                    $max = 10;
71            }
72    
73            my @hits = $webpac->search(
74                    phrase => $q,
75                    max => $max,
76                    get_attr => [ $what ],
77            );
78    
79            my $used;
80            my @suggestions;
81    
82            foreach my $res (@hits) {
83                    my $v = $res->{ $what } || next;
84                    next if ($used->{ $v }++);
85                    push @suggestions, $v;
86            }
87    
88      foreach my $res (@hits) {          $log->debug( ($#suggestions + 1) . " unique hits returned");
         my $v = $res->{ $what } || next;  
         next if ($used->{ $v }++);  
         push @suggestions, $v;  
     }  
89    
90      $c->res->body( $c->prototype->auto_complete_result( \@suggestions ) );          $c->response->body( $c->prototype->auto_complete_result( \@suggestions ) );
91    }
92    
93    
94    =item results
95    
96    Returns results for search query
97    
98    =cut
99    
100    sub results : Local {
101            my ( $self, $c ) = @_;
102    
103            my $params = $c->req->params;
104    
105            # oh, client didn't have ability to setup AJAX, fallback to
106            # page refresh
107            if (! $params->{'_ajax'}) {
108                    $params->{_ajax} = 1;
109                    $c->stash->{results} = $c->subreq('/search/results/ajax', {}, $params);
110            $c->stash->{template} = 'search.tt';
111            } else {
112                    $c->forward('/search/results/ajax');
113            $c->stash->{template} = 'results.tt';
114            }
115  }  }
116    
 =back  
117    
118    =item results_ajax
119    
120    Private method which uses C<Model::WebPAC> and returns results for search
121    query It generatets just I<inner> HTML for results div, so it has C<_ajax>
122    in name.
123    
124    =cut
125    
126    # specify all Hyper Estraier operators which should stop this module
127    # from splitting search query and joining it with default operator
128    my $hest_op_regex = qr/(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)/o;
129    
130    sub results_ajax : Path( 'results/ajax' ) {
131            my ( $self, $c ) = @_;
132    
133            my $params = $c->req->params;
134            my $webpac = $c->comp('Model::WebPAC');
135            my $log = $c->log;
136    
137            $log->debug("results got params: " . Dumper( $params ) );
138    
139            if (! $params->{_page} || $params->{_page} < 1) {
140                    $params->{_page} = 1;
141                    $log->warn("fixed _page parametar to 1");
142            }
143    
144            my @attr;
145            my @words;
146            # default operator to join fields/words
147            my $operator = 'AND';
148    
149            foreach my $f (keys %{ $params }) {
150    
151                    next if ($f =~ m/^_/o);
152    
153                    my $v = $params->{$f} || next;
154    
155                    if (my $op = $params->{ '_' . $f}) {
156                            if ($v =~ $hest_op_regex) {
157                                    # don't split words if there is Hyper Estraier
158                                    # operator in them
159                                    push @words, $v;
160                            } else {
161                                    push @words, join(" $op ", split(/\s+/, $v) );
162                            }
163                    } else {
164                            push @words, $v;
165                    }
166    
167                    next if ($f eq 'all');  # don't add_attr for magic field all
168    
169                    if ($v !~ /\s/) {
170                            push @attr, "$f ISTRINC $v";
171                    } else {
172                            map {
173                                    push @attr, "$f ISTRINC $_";
174                            } grep { ! $hest_op_regex } split(/\s+/, $v);
175                    }
176            }
177    
178            my $q = join(" $operator ", @words);
179    
180            my $template = $params->{'_template'} || $c->config->{webpac}->{template};
181    
182            $log->die("can't find _template or default from configuration!") unless ($template);
183    
184            my $hits_on_page = $c->config->{'hyperestraier'}->{'hits_on_page'} || 10;
185    
186            $log->debug("using template $template to produce $hits_on_page results");
187    
188            $c->stash->{html_results} = sub {
189                    my $res = $webpac->search(
190                            phrase => $q,
191                            template => $template,
192                            add_attr => \@attr,
193                            get_attr => [ '@uri' ],
194                            max => $hits_on_page,
195                            page => $params->{'_page'},
196                    );
197    #               $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res ));
198                    return $res;
199            };
200    
201            $c->stash->{phrase} = $q;
202            $c->stash->{attr} = \@attr;
203            $c->stash->{page} = $params->{'_page'};
204            $c->stash->{hits_on_page} = $hits_on_page;
205    
206            $c->stash->{template} = 'results.tt';
207    
208    }
209    
210    =item record
211    
212    forwarded to C</editor/record>
213    
214    =cut
215    
216    sub record : Local {
217            my ( $self, $c ) = @_;
218    
219            $c->forward( '/editor/record' );
220    }
221    
222    =back
223    
224  =head1 AUTHOR  =head1 AUTHOR
225    
226  Dobrica Pavlinusic,,,  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>
227    
228  =head1 LICENSE  =head1 LICENSE
229    

Legend:
Removed from v.116  
changed lines
  Added in v.271

  ViewVC Help
Powered by ViewVC 1.1.26