/[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 963 - (show annotations)
Fri Nov 2 12:59:39 2007 UTC (16 years, 5 months ago) by dpavlin
File size: 3891 byte(s)
 r1469@llin:  dpavlin | 2007-11-02 13:59:39 +0100
 rewrote results handling in Template::Declare... Now individual hits
 are returned as Webpacus::Search::DS which is data_structure from
 normalization but with accessors to get it's values

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

  ViewVC Help
Powered by ViewVC 1.1.26