/[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

Annotation of /Webpacus2/lib/Webpacus/Action/Search.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 945 - (hide annotations)
Wed Oct 31 19:05:54 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 2574 byte(s)
eval require Webpacus::Webpac to work-round chicken and the egg
problem of first deployment

1 dpavlin 930 use strict;
2     use warnings;
3    
4     =head1 NAME
5    
6     Webpacus::Action::Search
7    
8     =cut
9    
10     package Webpacus::Action::Search;
11     use base qw/Webpacus::Action Jifty::Action/;
12    
13     use Data::Dump qw/dump/;
14 dpavlin 945 # enables start witout this file which is created by WebPAC later
15     eval { require Webpacus::Webpac };
16 dpavlin 934 use KinoSearch::Simple;
17 dpavlin 930
18     use Jifty::Param::Schema;
19     use Jifty::Action schema {
20    
21 dpavlin 931 param field =>
22 dpavlin 930 label is _("Field name"),
23     available are defer {
24     my $coll = Webpacus::Model::SearchCollection->new;
25     $coll->unlimit;
26     [ '', {
27     display_from => 'name',
28     value_from => 'id',
29     collection => $coll,
30     }];
31     },
32     render as 'Select';
33    
34 dpavlin 931 param query =>
35 dpavlin 930 label is _("Search for"),
36     is mandatory;
37    
38    
39     };
40    
41     sub sticky_on_success { 1 }
42     sub sticky_on_failure { 1 }
43    
44     =head2 take_action
45    
46     =cut
47    
48     sub take_action {
49     my $self = shift;
50    
51 dpavlin 931 my $search = Webpacus::Model::Search->load( $self->argument_value( 'field' ) );
52 dpavlin 934 my $query = $self->argument_value( 'query' );
53 dpavlin 930
54     warn "## search = ",dump( $search );
55    
56 dpavlin 934 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 dpavlin 931 my $message =
72 dpavlin 934 _('Found') . " $total_hits " .
73 dpavlin 940 _('results for') . " '$query'";
74    
75     $message .= " " . _('on field') . ' ' . $search->name if $search->name;
76 dpavlin 930
77 dpavlin 935 $self->result->content(
78     results => Webpacus::Search::Results->new({
79     count => $total_hits,
80     index => $index,
81     }),
82     );
83 dpavlin 934
84 dpavlin 930 $self->result->message( $message );
85    
86     return 1;
87     }
88    
89 dpavlin 935 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 dpavlin 943 use lib '/data/webpac2/lib';
100     use WebPAC::Store;
101    
102 dpavlin 935 use Data::Dump qw/dump/;
103    
104 dpavlin 943 my $debug = 1;
105    
106     my $store;
107    
108 dpavlin 935 sub next {
109     my $self = shift;
110    
111 dpavlin 943 if ( ! $store ) {
112     $store = WebPAC::Store->new({
113     debug => $debug,
114     });
115     warn "## create WebPAC::Store\n";
116     }
117 dpavlin 935
118 dpavlin 943 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 dpavlin 935 return $row;
148     }
149    
150 dpavlin 930 1;
151    

  ViewVC Help
Powered by ViewVC 1.1.26