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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 999 - (show annotations)
Sun Nov 4 17:03:55 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 4300 byte(s)
 r1538@llin:  dpavlin | 2007-11-04 18:03:57 +0100
 add database selection

1 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 # 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;
19 use Jifty::Action schema {
20
21 param field =>
22 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 param database =>
35 label is _("From database"),
36 available are defer {
37 my $coll = Webpacus::Model::SearchCollection->new;
38 $coll->group_by( column => 'from_database' );
39 $coll->unlimit;
40 [{
41 display_from => 'from_database',
42 value_from => 'from_database',
43 collection => $coll,
44 }];
45 },
46 render as 'Select';
47
48 param query =>
49 label is _("Search for"),
50 is mandatory;
51
52
53 };
54
55 sub sticky_on_success { 1 }
56 sub sticky_on_failure { 1 }
57
58 =head2 take_action
59
60 Create C<results> which can be accessed from L<Template::Declare> like this:
61
62 my $results = $search->result->content('results');
63
64 =cut
65
66 sub take_action {
67 my $self = shift;
68
69 my $search = Webpacus::Model::Search->load( $self->argument_value( 'field' ) );
70 my $query = $self->argument_value( 'query' );
71 my $database = $self->argument_value( 'database' );
72
73 warn "## search = ",dump( $search );
74
75 my $index_path = Webpacus::Webpac->index_path;
76 $index_path .= '/' . $database;
77
78 warn "## index_path = $index_path";
79
80 my $index = KinoSearch::Simple->new(
81 path => $index_path,
82 language => 'en',
83 );
84
85 my $total_hits = $index->search(
86 query => $query,
87 offset => 0,
88 num_wanted => 100,
89 );
90
91 my $message =
92 _('Found') . " $total_hits " .
93 _('results for') . " '$query'";
94
95 $message .= " " . _('on field') . ' ' . $search->name if $search;
96
97 $self->result->content(
98 results => Webpacus::Search::Results->new({
99 count => $total_hits,
100 index => $index,
101 }),
102 );
103
104 $self->result->message( $message );
105
106 return 1;
107 }
108
109 package Webpacus::Search::Results;
110
111 use strict;
112 use warnings;
113 use base qw( Class::Accessor );
114 __PACKAGE__->mk_accessors( qw(
115 count
116 index
117 ) );
118
119 use lib '/data/webpac2/lib';
120 use WebPAC::Store;
121
122 use Data::Dump qw/dump/;
123
124 my $debug = 1;
125
126 my $store;
127
128 =head2 count
129
130 Returns number of records in results
131
132 my $nr_records = $results->count;
133
134 =head2 next
135
136 Fetch next result and return C<Webpacus::Search::DS> object
137
138 while ( my $ds = $results->next ) {
139 # do something with $ds object
140 }
141
142 =cut
143
144 sub next {
145 my $self = shift;
146
147 if ( ! $store ) {
148 $store = WebPAC::Store->new({
149 debug => $debug,
150 });
151 warn "## create WebPAC::Store\n";
152 }
153
154 my $hit = $self->index->fetch_hit_hashref;
155
156 return unless $hit;
157
158 warn "## next hit = ", dump( $hit ) if $debug;
159
160 my $ds = $store->load_ds(
161 database => $hit->{database},
162 input => $hit->{input},
163 id => $hit->{id},
164 );
165
166 if ( ! $ds ) {
167 warn "can't find ds for hit ", dump( $hit ), $/ unless $ds;
168 return;
169 }
170
171 # add permanent fields
172 $ds->{$_} ||= { display => $hit->{$_} } foreach ( qw/database input id/ );
173
174 return Webpacus::Action::DS->new( $ds );
175
176 }
177
178 package Webpacus::Action::DS;
179
180 use warnings;
181 use strict;
182
183 use Data::Dump qw/dump/;
184 use Carp qw/confess/;
185
186 sub new {
187 my $class = shift;
188
189 my ( $ds ) = @_;
190
191 my $self = {
192 ds => $ds,
193 };
194 bless ($self, $class);
195
196 return $self;
197
198 }
199
200 sub _row {
201 my ( $self, $type, $field, $delimiter ) = @_;
202
203 confess "no type?" unless $type;
204 confess "no field?" unless $field;
205 confess "no ds?" unless $self->{ds};
206
207 $delimiter ||= '[x]';
208
209 my $ds = $self->{ds};
210
211 warn "### ds = ",dump( $ds ) if $debug;
212
213 return unless defined $ds->{$field}->{$type};
214
215 if ( my $v = $ds->{$field}->{$type} ) {
216
217 if ( ref($v) eq 'ARRAY' ) {
218 warn "is array ", wantarray ? 'wantarray' : 'scalar', " $field $type" if $debug;
219 return @$v if wantarray;
220 return join( $delimiter, @$v );
221 } else {
222 warn "not array ", wantarray ? 'wantarray' : 'scalar', " $field $type" if $debug;
223 return $v;
224 }
225
226 } else {
227 return;
228 }
229
230 }
231
232 =head2 display
233
234 $ds->display( 'TitleProper' );
235
236 =cut
237
238 sub display {
239 my $self = shift;
240 warn "## display(",dump(@_),")\n";
241 return $self->_row('display',@_);
242 }
243
244 1;
245

  ViewVC Help
Powered by ViewVC 1.1.26