/[webpac2]/Webpacus2/lib/Webpacus/Action/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 /Webpacus2/lib/Webpacus/Action/Search.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 930 by dpavlin, Wed Oct 31 10:34:38 2007 UTC revision 945 by dpavlin, Wed Oct 31 19:05:54 2007 UTC
# Line 11  package Webpacus::Action::Search; Line 11  package Webpacus::Action::Search;
11  use base qw/Webpacus::Action Jifty::Action/;  use base qw/Webpacus::Action Jifty::Action/;
12    
13  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
14    # enables start witout this file which is created by WebPAC later
15    eval { require Webpacus::Webpac };
16    use KinoSearch::Simple;
17    
18  use Jifty::Param::Schema;  use Jifty::Param::Schema;
19  use Jifty::Action schema {  use Jifty::Action schema {
20    
21  param f =>  param field =>
22          label is _("Field name"),          label is _("Field name"),
23          available are defer {          available are defer {
24                  my $coll = Webpacus::Model::SearchCollection->new;                  my $coll = Webpacus::Model::SearchCollection->new;
# Line 28  param f => Line 31  param f =>
31          },          },
32          render as 'Select';          render as 'Select';
33    
34  param q =>  param query =>
35          label is _("Search for"),          label is _("Search for"),
36          is mandatory;          is mandatory;
37                    
# Line 45  sub sticky_on_failure { 1 } Line 48  sub sticky_on_failure { 1 }
48  sub take_action {  sub take_action {
49          my $self = shift;          my $self = shift;
50    
51          my $search = Perly::Model::Search->load( $self->argument_value( 'search' ) );          my $search = Webpacus::Model::Search->load( $self->argument_value( 'field' ) );
52            my $query = $self->argument_value( 'query' );
53    
54          warn "## search = ",dump( $search );          warn "## search = ",dump( $search );
55    
56          my $message = _('Results for query') . $self->argument_value( 'q' ) . _('on field') . $self->argument_value( 'v' );          my $index_path = Webpacus::Webpac->index_path;
57    
58            warn "## index_path = $index_path";
59    
60            my $index = KinoSearch::Simple->new(
61                    path => $index_path,
62                    language => 'en',
63            );
64    
65            my $total_hits = $index->search(
66                    query      => $query,
67                    offset     => 0,
68                    num_wanted => 100,
69            );
70    
71            my $message =
72                    _('Found') . " $total_hits " .
73                    _('results for') . " '$query'";
74            
75            $message .= " " . _('on field') . ' ' . $search->name if $search->name;
76    
77            $self->result->content(
78                    results => Webpacus::Search::Results->new({
79                            count => $total_hits,
80                            index => $index,
81                    }),
82            );
83    
84          $self->result->message( $message );          $self->result->message( $message );
85            
86          return 1;          return 1;
87  }  }
88    
89    package Webpacus::Search::Results;
90    
91    use strict;
92    use warnings;
93    use base qw( Class::Accessor );
94    __PACKAGE__->mk_accessors( qw(
95            count
96            index
97    ) );
98    
99    use lib '/data/webpac2/lib';
100    use WebPAC::Store;
101    
102    use Data::Dump qw/dump/;
103    
104    my $debug = 1;
105    
106    my $store;
107    
108    sub next {
109            my $self = shift;
110    
111            if ( ! $store ) {
112                    $store = WebPAC::Store->new({
113                            debug => $debug,
114                    });
115                    warn "## create WebPAC::Store\n";
116            }
117    
118            my $hit = $self->index->fetch_hit_hashref;
119    
120            return unless $hit;
121    
122            warn "## next hit = ", dump( $hit ) if $debug;
123    
124            my $ds = $store->load_ds(
125                    database => $hit->{database},
126                    input    => $hit->{input},
127                    id       => $hit->{id},
128            );
129    
130            if ( ! $ds ) {
131                    warn "can't find ds for hit ", dump( $hit ), $/ unless $ds;
132                    return;
133            }
134    
135            my $row;
136    
137            foreach my $f ( keys %$ds ) {
138                    $row->{$f} = $ds->{$f}->{display} if defined $ds->{$f}->{display};
139            }
140    
141            return unless $row;
142    
143            $row->{$_} ||= $hit->{$_} foreach ( qw/database input id/ );
144    
145            warn "## next row = ", dump( $row ) if $debug;
146    
147            return $row;
148    }
149    
150  1;  1;
151    

Legend:
Removed from v.930  
changed lines
  Added in v.945

  ViewVC Help
Powered by ViewVC 1.1.26