/[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 962 - (show annotations)
Fri Nov 2 12:23:40 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 3307 byte(s)
 r1466@llin:  dpavlin | 2007-11-02 13:23:22 +0100
 more hacking on output, total mess for now

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 =cut
47
48 sub take_action {
49 my $self = shift;
50
51 my $search = Webpacus::Model::Search->load( $self->argument_value( 'field' ) );
52 my $query = $self->argument_value( 'query' );
53
54 warn "## search = ",dump( $search );
55
56 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 my $message =
72 _('Found') . " $total_hits " .
73 _('results for') . " '$query'";
74
75 $message .= " " . _('on field') . ' ' . $search->name if $search;
76
77 $self->result->content(
78 results => Webpacus::Search::Results->new({
79 count => $total_hits,
80 index => $index,
81 }),
82 );
83
84 $self->result->message( $message );
85
86 return 1;
87 }
88
89 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 use lib '/data/webpac2/lib';
100 use WebPAC::Store;
101
102 use Data::Dump qw/dump/;
103
104 my $debug = 1;
105
106 my $store;
107
108 sub next {
109 my $self = shift;
110
111 if ( ! $store ) {
112 $store = WebPAC::Store->new({
113 debug => $debug,
114 });
115 warn "## create WebPAC::Store\n";
116 }
117
118 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 return sub {
148 my ( $name, $delimiter ) = @_;
149 # default delimiter is space
150 $delimiter ||= ' ';
151 die "no name?" unless $name;
152 if ( defined $row->{$name} ) {
153 # XXX disabled, Template::Declare always want scalars :-(
154 if ( 0 && wantarray ) {
155 if ( ref($row->{$name}) eq 'ARRAY' ) {
156 return $row->{$name};
157 } else {
158 return [ $row->{$name} ];
159 }
160 } else {
161 if ( ref($row->{$name}) eq 'ARRAY' && $delimiter ne 'ARRAY' ) {
162 warn "is array ", wantarray ? 'wantarray' : 'scalar', " $name";
163 return @{$row->{$name}} if wantarray;
164 return join( $delimiter, @{ $row->{$name} });
165 } else {
166 warn "not array ", wantarray ? 'wantarray' : 'scalar', " $name";
167 return $row->{$name};
168 }
169 }
170 }
171 return;
172 }
173 }
174
175 1;
176

  ViewVC Help
Powered by ViewVC 1.1.26