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 |
dpavlin |
954 |
$message .= " " . _('on field') . ' ' . $search->name if $search; |
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 |
954 |
return sub { |
148 |
|
|
my $name = shift || die "no name?"; |
149 |
|
|
if ( defined $row->{$name} ) { |
150 |
|
|
# XXX disabled, Template::Declare always want scalars :-( |
151 |
|
|
if ( 0 && wantarray ) { |
152 |
|
|
if ( ref($row->{$name}) eq 'ARRAY' ) { |
153 |
|
|
return $row->{$name}; |
154 |
|
|
} else { |
155 |
|
|
return [ $row->{$name} ]; |
156 |
|
|
} |
157 |
|
|
} else { |
158 |
|
|
if ( ref($row->{$name}) eq 'ARRAY' ) { |
159 |
|
|
return join(' ', @{ $row->{$name} }); |
160 |
|
|
} else { |
161 |
|
|
return $row->{$name}; |
162 |
|
|
} |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
return; |
166 |
|
|
} |
167 |
dpavlin |
935 |
} |
168 |
|
|
|
169 |
dpavlin |
930 |
1; |
170 |
|
|
|