/[webpac]/trunk/WebPac.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 /trunk/WebPac.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 47 - (hide annotations)
Sun Mar 23 01:17:49 2003 UTC (16 years, 8 months ago) by dpavlin
File size: 4788 byte(s)
show just hits or all details buttons on result page

1 dpavlin 7 package WebPac;
2    
3     use base 'CGI::Application';
4     use strict;
5    
6     use HTML::Pager;
7     use HTML::FillInForm;
8     use SWISH;
9 dpavlin 14 use Text::Iconv;
10 dpavlin 9 use DBI;
11 dpavlin 7
12 dpavlin 11 use lib '..';
13     use index_DBI;
14 dpavlin 13 use back2html;
15 dpavlin 11
16 dpavlin 7 # configuration options
17     # FIX: they really should go in configuration file!
18     my $TEMPLATE_PATH = '/data/webpac/template_html';
19     my $CHARSET = 'ISO-8859-2';
20 dpavlin 13 my $SWISH = '/usr/bin/swish-e';
21 dpavlin 7 my $INDEX = '/data/webpac/index/isis.index';
22     my $MAX_HITS = 500;
23     my $ON_PAGE = 10;
24    
25 dpavlin 30 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
26 dpavlin 7
27 dpavlin 14 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
28    
29 dpavlin 7 sub setup {
30     my $self = shift;
31     $self->tmpl_path($TEMPLATE_PATH);
32     $self->run_modes(
33     'search' => 'show_search_form',
34     'results' => 'show_results_list',
35 dpavlin 9 # 'user' => 'show_user_detail',
36     'index' => 'show_index',
37 dpavlin 7 );
38     $self->start_mode('search');
39     $self->mode_param('rm');
40    
41     $self->header_props(-charset=>$CHARSET);
42     }
43    
44     sub show_search_form {
45     my $self = shift;
46    
47     # Get the CGI.pm query object
48     my $q = $self->query();
49    
50     my $tmpl = $self->load_tmpl('search.html');
51     my $html = $tmpl->output;
52    
53     my $fif = new HTML::FillInForm;
54    
55     return $fif->fill(scalarref => \$html, fobject => $q,
56     target => 'search');
57     }
58    
59     sub show_results_list {
60     my $self = shift;
61    
62     my $q = $self->query();
63    
64     my @swish_results; # results from swish
65    
66     # load template for this page
67    
68     my @s_arr; # all queries are located here
69    
70 dpavlin 47 my @path_arr = $q->param('path');
71     my $full = $q->param('full');
72    
73 dpavlin 7 for(my $i = 1; $i <=10; $i++) {
74    
75 dpavlin 9 return show_index($self, $i) if ($q->param("f".$i."_index"));
76     next if (! $q->param("f$i"));
77 dpavlin 7 next if (! $q->param("v$i"));
78    
79     # re-write query from +/- to and/and not
80     my $s;
81     my $search = $q->param("v$i");
82     while ($search =~ s/\s*("[^"]+")\s*/ /) {
83     $s .= "$1 ";
84     }
85     $search =~ s/^\s+//;
86     $search =~ s/\s+$//;
87    
88     foreach (split(/\s+/,$search)) {
89     if (m/^([+-])(\S+)/) {
90     $s.= ($s) ? "and " : "";
91     $s.="not " if ($1 eq "-");
92     $s.="$2* ";
93     } else {
94     $s.="$_* ";
95     }
96     }
97 dpavlin 16 $s =~ s/\*+/*/g;
98 dpavlin 7
99 dpavlin 9 push @s_arr,$q->param("f$i")."_swish=($s)";
100 dpavlin 7 }
101    
102 dpavlin 9 my $tmpl = $self->load_tmpl('results.html');
103    
104 dpavlin 7 # call swish
105     my $sh = SWISH->connect('Fork',
106     prog => $SWISH,
107     indexes => $INDEX,
108 dpavlin 13 properties => [qw/swishdocpath swishrank swishtitle headline html/],
109 dpavlin 7 results => sub {
110     my ($sh,$hit) = @_;
111    
112     push @swish_results, {
113     nr => ($#swish_results + 2),
114     path => $hit->swishdocpath,
115 dpavlin 41 headline => $from_utf8->convert($hit->headline),
116     html => back2html($from_utf8->convert($hit->html)),
117 dpavlin 7 rank => $hit->swishrank };
118    
119     },
120     #startnum => 0,
121 dpavlin 47 maxhits => $MAX_HITS
122 dpavlin 7 );
123    
124     die $SWISH::errstr unless $sh;
125    
126 dpavlin 47 # construct swish query
127     my $sw_q = join(" and ",@s_arr);
128     if (@path_arr) {
129     $sw_q .= "and (swishdocpath=\"";
130     $sw_q .= join("\" or swishdocpath=\"",@path_arr);
131     $sw_q .= "\")";
132     $tmpl->param('full',1); # show full records
133     }
134 dpavlin 7
135 dpavlin 47 my $hits = $sh->query($sw_q);
136    
137 dpavlin 7 $tmpl->param('hits',$hits);
138 dpavlin 47 $tmpl->param('search',$sw_q);
139 dpavlin 7
140     # create a Pager object
141     my $pager = HTML::Pager->new(
142     # required parameters
143     query => $q,
144     get_data_callback => sub {
145     my ($offset, $rows) = @_;
146    
147     my @result;
148     for (my $i=0; $i<$rows; $i++) {
149     push @result, $swish_results[$offset+$i] if $swish_results[$offset+$i];
150     }
151     return \@result;
152     },
153     rows => $hits,
154     page_size => $ON_PAGE,
155     # some optional parameters
156     persist_vars => [
157     'rm',
158     'f1', 'v1',
159     'f2', 'v2',
160     'f3', 'v3',
161     'f4', 'v4',
162     'f5', 'v5',
163     'f6', 'v6',
164     'f7', 'v7',
165     'f8', 'v8',
166     'f9', 'v9',
167     ],
168     #cell_space_color => '#000000',
169     #cell_background_color => '#ffffff',
170     #nav_background_color => '#dddddd',
171     #javascript_presubmit => 'last_minute_javascript()',
172     debug => 1,
173     template => $tmpl,
174     );
175    
176     my $html = $pager->output;
177    
178     return $html;
179     }
180    
181 dpavlin 9 sub show_index {
182     my $self = shift;
183     my $i = shift; # field number
184    
185     my $q = $self->query();
186    
187 dpavlin 11 my $field = $q->param("f$i");
188     my $limit = $q->param("v$i");
189    
190 dpavlin 9 my $html;
191    
192 dpavlin 11 my $index = new index_DBI();
193 dpavlin 9
194 dpavlin 12 my $total = $index->check($field);
195     if (! $total) {
196 dpavlin 11 my $tmpl = $self->load_tmpl('no_index.html');
197     $tmpl->param('field',$field);
198     $html = $tmpl->output;
199     return $html;
200     }
201 dpavlin 9
202 dpavlin 12 my $tmpl = $self->load_tmpl('index_res.html');
203     $tmpl->param('field',$field);
204     $tmpl->param('limit',$limit);
205     $tmpl->param('total',$total);
206 dpavlin 11
207 dpavlin 16 # FIX: I should set offset and leave out limit from fetch!!
208     # if (! $q->param("PAGER_offset") {
209     # $q->param("Pager_offet)
210     # }
211    
212 dpavlin 12 my $pager = HTML::Pager->new(
213     query => $q,
214     get_data_callback => sub {
215     my ($offset, $rows) = @_;
216 dpavlin 11
217 dpavlin 12 my @result = $index->fetch($field,'item',$limit, $offset, $rows);
218     return \@result;
219     },
220     rows => $total,
221     page_size => $ON_PAGE,
222     persist_vars => [
223     'rm',
224     "f$i", "v$i", "f".$i."_index",
225     'offset',
226     ],
227     debug => 1,
228     template => $tmpl,
229     );
230 dpavlin 11
231 dpavlin 12 return $pager->output;
232 dpavlin 9 }
233    
234 dpavlin 7 1;

Properties

Name Value
cvs2svn:cvs-rev 1.12

  ViewVC Help
Powered by ViewVC 1.1.26