/[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 405 by dpavlin, Sun Feb 19 22:40:40 2006 UTC
# Line 5  use warnings; Line 5  use warnings;
5  use base 'Catalyst::Controller';  use base 'Catalyst::Controller';
6    
7  use lib '/data/webpac2/lib';  use lib '/data/webpac2/lib';
8  use WebPAC::Search::Estraier;  use WebPAC::Search::Estraier 0.03;
9    
10    
11  =head1 NAME  =head1 NAME
12    
13  Webpacus::Controller::Search - Catalyst Controller  Webpacus::Controller::Search - Search WebPAC data
14    
15  =head1 SYNOPSIS  =head1 SYNOPSIS
16    
17  See L<Webpacus>  See L<WebPAC>, L<Webpacus>
18    
19  =head1 DESCRIPTION  =head1 DESCRIPTION
20    
21  Catalyst Controller for autocompleting search fields.  Catalyst Controller for search fields Hyper Estraier
22    
23  =head1 METHODS  =head1 METHODS
24    
# Line 29  Catalyst Controller for autocompleting s Line 30  Catalyst Controller for autocompleting s
30    
31  sub default : Private {  sub default : Private {
32      my ( $self, $c ) = @_;      my ( $self, $c ) = @_;
33    
34            $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    If C<search> field has magic value <all>, it will search over all data, not
50    just one specified field:
51    
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 $search = $c->req->params->{search};
60            my $show = $c->req->params->{show};
61    
62      my $what = $c->request->snippets->[0];          my $log = $c->log;
63    
64      my $log = $c->log;          my $webpac = $c->comp('Model::WebPAC');
65            #$c->log->dumper( $c->stash, 'stash' );
66            $webpac->setup_site( $c->stash->{site} );
67    
68   my $est = new WebPAC::Search::Estraier(          my $q = $c->req->params->{ $search || 'all' } || $c->response->body("no results");
         url => 'http://localhost:1978/node/webpac2',  
         user => 'admin',  
         passwd => 'admin',  
         encoding => 'UTF-8',  
         log => $c->log,  
  );  
       
   
     my $q = $c->req->params->{$what};  
     my @suggestions;  
   
         $log->info("search for '$q' in $what\n");  
     my @hits = $est->search( phrase => $q, max => 10, get_attr => [ $what ] );  
   
     my $used;  
   
     foreach my $res (@hits) {  
         my $v = $res->{ $what } || next;  
         next if ($used->{ $v }++);  
         push @suggestions, $v;  
     }  
69    
70      $c->res->body( $c->prototype->auto_complete_result( \@suggestions ) );          $log->info("search for '$q' in $search and display $show\n");
71    
72            my $max = $c->config->{'hits_for_suggest'};
73            if (! $max) {
74                    $log->info("hits_for_suggest isn't defined, defaulting to 10");
75                    $c->config->{'hits_for_suggest'} = 10;
76                    $max = 10;
77            }
78    
79            $c->forward('filter_database');
80    
81            my $hits = $webpac->search(
82                    phrase => $q,
83                    add_attr => $c->stash->{attr},
84                    get_attr => [ $show ],
85                    max => $max,
86            );
87    
88            my $used;
89            my @suggestions;
90    
91            foreach my $res (@{$hits}) {
92                    my $v = $res->{ $show } || next;
93                    next if ($used->{ $v }++);
94                    push @suggestions, $v;
95            }
96    
97            $log->debug( ($#suggestions + 1) . " unique hits returned");
98    
99            $c->response->body( $c->prototype->auto_complete_result( \@suggestions ) );
100  }  }
101    
 =back  
102    
103    =item results
104    
105    Returns results for search query
106    
107    =cut
108    
109    sub results : Local {
110            my ( $self, $c ) = @_;
111    
112            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            $webpac->setup_site( $c->stash->{site} );
138            my $log = $c->log;
139    
140            $log->dumper($params, 'params');
141    
142            if (! $params->{_page} || $params->{_page} < 1) {
143                    $params->{_page} = 1;
144                    $log->warn("fixed _page parametar to 1");
145            }
146    
147            my @words;
148            # default operator to join fields/words
149            my $operator = 'AND';
150    
151            foreach my $f (keys %{ $params }) {
152    
153                    next if ($f =~ m/^_/o);
154    
155                    my $v = $params->{$f} || next;
156    
157                    if (my $op = $params->{ '_' . $f}) {
158                            if ($v =~ /$hest_op_regex/) {
159                                    # don't split words if there is Hyper Estraier
160                                    # operator in them
161                                    push @words, $v;
162                            } else {
163                                    push @words, join(" $op ", split(/\s+/, $v) );
164                            }
165                    } else {
166                            push @words, $v;
167                    }
168    
169                    next if ($f eq 'all');  # don't add_attr for magic field all
170    
171                    if ($v !~ /\s/) {
172                            push @{ $c->stash->{attr} }, "$f ISTRINC $v";
173                    } else {
174                            map {
175                                    push @{ $c->stash->{attr} }, "$f ISTRINC $_";
176                            } grep { ! /$hest_op_regex/ } split(/\s+/, $v);
177                    }
178            }
179    
180        $c->forward('filter_database');
181    
182            my $q = join(" $operator ", @words);
183    
184            my $template = $params->{'_template'} || $c->config->{webpac}->{template};
185    
186            $log->die("can't find _template or default from configuration!") unless ($template);
187    
188            my $hits_on_page = $c->config->{'hyperestraier'}->{'hits_on_page'} || 10;
189    
190            $log->debug("using template $template to produce $hits_on_page results");
191    
192            $c->stash->{html_results} = $webpac->search(
193                            phrase => $q,
194                            template => $template,
195                            add_attr => $c->{stash}->{attr},
196                            get_attr => [ '@uri' ],
197                            max => $hits_on_page,
198                            page => $params->{'_page'},
199            );
200    
201            $c->stash->{hints} = $webpac->hints;
202    
203            $c->stash->{phrase} = $q;
204            $c->stash->{page} = $params->{'_page'};
205            $c->stash->{hits_on_page} = $hits_on_page;
206    
207            $c->stash->{template} = 'results.tt';
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
240    
241    forwarded to C</editor/record>
242    
243    =cut
244    
245    sub record : Local {
246            my ( $self, $c ) = @_;
247    
248            $c->forward( '/editor/record' );
249    }
250    
251    =back
252    
253  =head1 AUTHOR  =head1 AUTHOR
254    
255  Dobrica Pavlinusic,,,  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>
256    
257  =head1 LICENSE  =head1 LICENSE
258    

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

  ViewVC Help
Powered by ViewVC 1.1.26