/[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 943 - (show annotations)
Wed Oct 31 14:12:01 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 2495 byte(s)
use WebPAC::Store to fetch ds of search result and display TitleProper
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 use Webpacus::Webpac;
15 use KinoSearch::Simple;
16
17 use Jifty::Param::Schema;
18 use Jifty::Action schema {
19
20 param field =>
21 label is _("Field name"),
22 available are defer {
23 my $coll = Webpacus::Model::SearchCollection->new;
24 $coll->unlimit;
25 [ '', {
26 display_from => 'name',
27 value_from => 'id',
28 collection => $coll,
29 }];
30 },
31 render as 'Select';
32
33 param query =>
34 label is _("Search for"),
35 is mandatory;
36
37
38 };
39
40 sub sticky_on_success { 1 }
41 sub sticky_on_failure { 1 }
42
43 =head2 take_action
44
45 =cut
46
47 sub take_action {
48 my $self = shift;
49
50 my $search = Webpacus::Model::Search->load( $self->argument_value( 'field' ) );
51 my $query = $self->argument_value( 'query' );
52
53 warn "## search = ",dump( $search );
54
55 my $index_path = Webpacus::Webpac->index_path;
56
57 warn "## index_path = $index_path";
58
59 my $index = KinoSearch::Simple->new(
60 path => $index_path,
61 language => 'en',
62 );
63
64 my $total_hits = $index->search(
65 query => $query,
66 offset => 0,
67 num_wanted => 100,
68 );
69
70 my $message =
71 _('Found') . " $total_hits " .
72 _('results for') . " '$query'";
73
74 $message .= " " . _('on field') . ' ' . $search->name if $search->name;
75
76 $self->result->content(
77 results => Webpacus::Search::Results->new({
78 count => $total_hits,
79 index => $index,
80 }),
81 );
82
83 $self->result->message( $message );
84
85 return 1;
86 }
87
88 package Webpacus::Search::Results;
89
90 use strict;
91 use warnings;
92 use base qw( Class::Accessor );
93 __PACKAGE__->mk_accessors( qw(
94 count
95 index
96 ) );
97
98 use lib '/data/webpac2/lib';
99 use WebPAC::Store;
100
101 use Data::Dump qw/dump/;
102
103 my $debug = 1;
104
105 my $store;
106
107 sub next {
108 my $self = shift;
109
110 if ( ! $store ) {
111 $store = WebPAC::Store->new({
112 debug => $debug,
113 });
114 warn "## create WebPAC::Store\n";
115 }
116
117 my $hit = $self->index->fetch_hit_hashref;
118
119 return unless $hit;
120
121 warn "## next hit = ", dump( $hit ) if $debug;
122
123 my $ds = $store->load_ds(
124 database => $hit->{database},
125 input => $hit->{input},
126 id => $hit->{id},
127 );
128
129 if ( ! $ds ) {
130 warn "can't find ds for hit ", dump( $hit ), $/ unless $ds;
131 return;
132 }
133
134 my $row;
135
136 foreach my $f ( keys %$ds ) {
137 $row->{$f} = $ds->{$f}->{display} if defined $ds->{$f}->{display};
138 }
139
140 return unless $row;
141
142 $row->{$_} ||= $hit->{$_} foreach ( qw/database input id/ );
143
144 warn "## next row = ", dump( $row ) if $debug;
145
146 return $row;
147 }
148
149 1;
150

  ViewVC Help
Powered by ViewVC 1.1.26