/[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 30 - (hide annotations)
Sun Feb 23 07:09:11 2003 UTC (16 years, 9 months ago) by dpavlin
File size: 4539 byte(s)
misc updates

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     for(my $i = 1; $i <=10; $i++) {
71    
72 dpavlin 9 return show_index($self, $i) if ($q->param("f".$i."_index"));
73     next if (! $q->param("f$i"));
74 dpavlin 7 next if (! $q->param("v$i"));
75    
76     # re-write query from +/- to and/and not
77     my $s;
78     my $search = $q->param("v$i");
79     while ($search =~ s/\s*("[^"]+")\s*/ /) {
80     $s .= "$1 ";
81     }
82     $search =~ s/^\s+//;
83     $search =~ s/\s+$//;
84    
85     foreach (split(/\s+/,$search)) {
86     if (m/^([+-])(\S+)/) {
87     $s.= ($s) ? "and " : "";
88     $s.="not " if ($1 eq "-");
89     $s.="$2* ";
90     } else {
91     $s.="$_* ";
92     }
93     }
94 dpavlin 16 $s =~ s/\*+/*/g;
95 dpavlin 7
96 dpavlin 9 push @s_arr,$q->param("f$i")."_swish=($s)";
97 dpavlin 7 }
98    
99 dpavlin 9 my $tmpl = $self->load_tmpl('results.html');
100    
101 dpavlin 7 # call swish
102     my $sh = SWISH->connect('Fork',
103     prog => $SWISH,
104     indexes => $INDEX,
105 dpavlin 13 properties => [qw/swishdocpath swishrank swishtitle headline html/],
106 dpavlin 7 results => sub {
107     my ($sh,$hit) = @_;
108    
109     push @swish_results, {
110     nr => ($#swish_results + 2),
111     path => $hit->swishdocpath,
112 dpavlin 14 headline => $from_utf8->convert($hit->headline),
113     html => back2html($from_utf8->convert($hit->html)),
114 dpavlin 7 rank => $hit->swishrank };
115    
116     },
117     #startnum => 0,
118     maxhits => $MAX_HITS,
119     );
120    
121     die $SWISH::errstr unless $sh;
122    
123     my $hits = $sh->query(join(" and ",@s_arr)) || 0; # FIX: and/or
124    
125     $tmpl->param('hits',$hits);
126     $tmpl->param('search',join(" and ",@s_arr));
127    
128     # create a Pager object
129     my $pager = HTML::Pager->new(
130     # required parameters
131     query => $q,
132     get_data_callback => sub {
133     my ($offset, $rows) = @_;
134    
135     my @result;
136     for (my $i=0; $i<$rows; $i++) {
137     push @result, $swish_results[$offset+$i] if $swish_results[$offset+$i];
138     }
139     return \@result;
140     },
141     rows => $hits,
142     page_size => $ON_PAGE,
143     # some optional parameters
144     persist_vars => [
145     'rm',
146     'f1', 'v1',
147     'f2', 'v2',
148     'f3', 'v3',
149     'f4', 'v4',
150     'f5', 'v5',
151     'f6', 'v6',
152     'f7', 'v7',
153     'f8', 'v8',
154     'f9', 'v9',
155     ],
156     #cell_space_color => '#000000',
157     #cell_background_color => '#ffffff',
158     #nav_background_color => '#dddddd',
159     #javascript_presubmit => 'last_minute_javascript()',
160     debug => 1,
161     template => $tmpl,
162     );
163    
164     my $html = $pager->output;
165    
166     return $html;
167     }
168    
169 dpavlin 9 sub show_index {
170     my $self = shift;
171     my $i = shift; # field number
172    
173     my $q = $self->query();
174    
175 dpavlin 11 my $field = $q->param("f$i");
176     my $limit = $q->param("v$i");
177    
178 dpavlin 9 my $html;
179    
180 dpavlin 11 my $index = new index_DBI();
181 dpavlin 9
182 dpavlin 12 my $total = $index->check($field);
183     if (! $total) {
184 dpavlin 11 my $tmpl = $self->load_tmpl('no_index.html');
185     $tmpl->param('field',$field);
186     $html = $tmpl->output;
187     return $html;
188     }
189 dpavlin 9
190 dpavlin 12 my $tmpl = $self->load_tmpl('index_res.html');
191     $tmpl->param('field',$field);
192     $tmpl->param('limit',$limit);
193     $tmpl->param('total',$total);
194 dpavlin 11
195 dpavlin 16 # FIX: I should set offset and leave out limit from fetch!!
196     # if (! $q->param("PAGER_offset") {
197     # $q->param("Pager_offet)
198     # }
199    
200 dpavlin 12 my $pager = HTML::Pager->new(
201     query => $q,
202     get_data_callback => sub {
203     my ($offset, $rows) = @_;
204 dpavlin 11
205 dpavlin 12 my @result = $index->fetch($field,'item',$limit, $offset, $rows);
206     return \@result;
207     },
208     rows => $total,
209     page_size => $ON_PAGE,
210     persist_vars => [
211     'rm',
212     "f$i", "v$i", "f".$i."_index",
213     'offset',
214     ],
215     debug => 1,
216     template => $tmpl,
217     );
218 dpavlin 11
219 dpavlin 12 return $pager->output;
220 dpavlin 9 }
221    
222 dpavlin 7 1;

Properties

Name Value
cvs2svn:cvs-rev 1.9

  ViewVC Help
Powered by ViewVC 1.1.26