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 { |
43 |
|
|
44 |
=head2 take_action |
=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 |
=cut |
51 |
|
|
52 |
sub take_action { |
sub take_action { |
53 |
my $self = shift; |
my $self = shift; |
54 |
|
|
55 |
my $search = Webpacus::Model::Search->load( $self->argument_value( 'field' ) ); |
my $search = Webpacus::Model::Search->load( $self->argument_value( 'field' ) ); |
56 |
|
my $query = $self->argument_value( 'query' ); |
57 |
|
|
58 |
warn "## search = ",dump( $search ); |
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 = |
my $message = |
76 |
_('Results for') . " '" . $self->argument_value( 'query' ) . "' " . |
_('Found') . " $total_hits " . |
77 |
_('on field') . ' ' . $search->name; |
_('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 ); |
$self->result->message( $message ); |
89 |
|
|
90 |
return 1; |
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; |
1; |
229 |
|
|