/[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

Annotation of /Webpacus2/lib/Webpacus/Action/Search.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 943 - (hide 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 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 934 use Webpacus::Webpac;
15     use KinoSearch::Simple;
16 dpavlin 930
17     use Jifty::Param::Schema;
18     use Jifty::Action schema {
19    
20 dpavlin 931 param field =>
21 dpavlin 930 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 dpavlin 931 param query =>
34 dpavlin 930 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 dpavlin 931 my $search = Webpacus::Model::Search->load( $self->argument_value( 'field' ) );
51 dpavlin 934 my $query = $self->argument_value( 'query' );
52 dpavlin 930
53     warn "## search = ",dump( $search );
54    
55 dpavlin 934 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 dpavlin 931 my $message =
71 dpavlin 934 _('Found') . " $total_hits " .
72 dpavlin 940 _('results for') . " '$query'";
73    
74     $message .= " " . _('on field') . ' ' . $search->name if $search->name;
75 dpavlin 930
76 dpavlin 935 $self->result->content(
77     results => Webpacus::Search::Results->new({
78     count => $total_hits,
79     index => $index,
80     }),
81     );
82 dpavlin 934
83 dpavlin 930 $self->result->message( $message );
84    
85     return 1;
86     }
87    
88 dpavlin 935 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 dpavlin 943 use lib '/data/webpac2/lib';
99     use WebPAC::Store;
100    
101 dpavlin 935 use Data::Dump qw/dump/;
102    
103 dpavlin 943 my $debug = 1;
104    
105     my $store;
106    
107 dpavlin 935 sub next {
108     my $self = shift;
109    
110 dpavlin 943 if ( ! $store ) {
111     $store = WebPAC::Store->new({
112     debug => $debug,
113     });
114     warn "## create WebPAC::Store\n";
115     }
116 dpavlin 935
117 dpavlin 943 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 dpavlin 935 return $row;
147     }
148    
149 dpavlin 930 1;
150    

  ViewVC Help
Powered by ViewVC 1.1.26