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

Diff of /trunk/WebPac.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 9 by dpavlin, Sat Jan 11 19:55:30 2003 UTC revision 73 by dpavlin, Sat Jul 5 22:31:46 2003 UTC
# Line 6  use strict; Line 6  use strict;
6  use HTML::Pager;  use HTML::Pager;
7  use HTML::FillInForm;  use HTML::FillInForm;
8  use SWISH;  use SWISH;
9  use Unicode::String qw(utf8 utf16);  use Text::Iconv;
 require Unicode::Map8;  
10  use DBI;  use DBI;
11    use Config::IniFiles;
12    use Text::Unaccent;
13    
14    use lib '..';
15    use index_DBI;
16    use back2html;
17    
18  # configuration options  # configuration options
19  # FIX: they really should go in configuration file!  # FIXME they really should go in configuration file!
20  my $TEMPLATE_PATH = '/data/webpac/template_html';  my $TEMPLATE_PATH = '/data/webpac/template_html';
21  my $CHARSET = 'ISO-8859-2';  my $CHARSET = 'ISO-8859-2';
22  my $SWISH = '/data/swish/swish-e';  my $SWISH = '/usr/bin/swish-e';
23  my $INDEX = '/data/webpac/index/isis.index';  my $INDEX = '/data/webpac/index/isis.index';
24  my $MAX_HITS = 500;  my $MAX_HITS = 0;
25  my $ON_PAGE = 10;  my $ON_PAGE = 10;
26    
27    Text::Iconv->raise_error(0);     # Conversion errors raise exceptions
28    
29    my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
30    
31    # read global.conf configuration
32    my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
33    
34    
35  sub setup {  sub setup {
36          my $self = shift;          my $self = shift;
# Line 61  sub show_results_list { Line 73  sub show_results_list {
73    
74          my @s_arr;      # all queries are located here          my @s_arr;      # all queries are located here
75    
76          for(my $i = 1; $i <=10; $i++) {          my @path_arr = $q->param('path');
77            my $full = $q->param('full');
78    
79            for(my $i = 1; $i <=30; $i++) {
80    
81                  return show_index($self, $i) if ($q->param("f".$i."_index"));                  return show_index($self, $i) if ($q->param("f".$i."_index"));
                 next if (! $q->param("f$i"));  
82                  next if (! $q->param("v$i"));                  next if (! $q->param("v$i"));
83                    next if (! $q->param("f$i"));
84    
85                  # re-write query from +/- to and/and not                  # re-write query from +/- to and/and not
86                  my $s;                  my @param_vals = $q->param("v$i");
87                  my $search = $q->param("v$i");                  my @swish_q;
88                  while ($search =~ s/\s*("[^"]+")\s*/ /) {                  while (my $search = shift @param_vals) {
89                          $s .= "$1 ";                          my $s;
90                  }                          # remove accents
91                  $search =~ s/^\s+//;                          $search = unac_string($CHARSET,$search);
92                  $search =~ s/\s+$//;                          while ($search =~ s/\s*("[^"]+")\s*/ /) {
93                                    $s .= "$1 ";
94                            }
95                            $search =~ s/^\s+//;
96                            $search =~ s/\s+$//;
97    
98                  foreach (split(/\s+/,$search)) {                          foreach (split(/\s+/,$search)) {
99                          if (m/^([+-])(\S+)/) {                                  if (m/^([+-])(\S+)/) {
100                                  $s.= ($s) ? "and " : "";                                          $s.= ($s) ? "and " : "";
101                                  $s.="not " if ($1 eq "-");                                          $s.="not " if ($1 eq "-");
102                                  $s.="$2* ";                                          $s.="$2* ";
103                          } else {                                  } elsif (m/(and|or|not)/i) {
104                                  $s.="$_* ";                                          $s.="$_ ";
105                                    } else {
106                                            $s.="$_* ";
107                                    }
108                          }                          }
109                            $s =~ s/\*+/*/g;
110                            push @swish_q,$s;
111                  }                  }
112                    # FIXME default operator for multi-value fields is or. There is
113                  push @s_arr,$q->param("f$i")."_swish=($s)";                  # no way to change it, except here for now. Is there need?
114                    push @s_arr, $q->param("f$i")."_swish=(".join(" or ",@swish_q).")";
115          }          }
116    
117          my $tmpl = $self->load_tmpl('results.html');          my $tmpl = $self->load_tmpl('results.html');
118    
         my $l2_map = Unicode::Map8->new($CHARSET) || die;  
         my $us = Unicode::String->new();  
   
119          # call swish          # call swish
120          my $sh = SWISH->connect('Fork',          my $sh = SWISH->connect('Fork',
121                  prog     => $SWISH,                  prog     => $SWISH,
122                  indexes  => $INDEX,                  indexes  => $INDEX,
123                  #properties  => [qw/god br nr/],                  properties  => [qw/swishdocpath swishrank swishtitle headline html/],
124                  results  => sub {                  results  => sub {
125                          my ($sh,$hit) = @_;                          my ($sh,$hit) = @_;
126    
                         $us->utf8($hit->swishtitle);  
   
127                          push @swish_results, {                          push @swish_results, {
128                                  nr => ($#swish_results + 2),                                  nr => ($#swish_results + 2),
129                                  path => $hit->swishdocpath,                                  path => $hit->swishdocpath,
130                                  title => $l2_map->to8($us->utf16),                                  headline => $from_utf8->convert($hit->headline),
131                                    html => back2html($from_utf8->convert($hit->html)),
132                                  rank => $hit->swishrank };                                  rank => $hit->swishrank };
133    
 #                       my @fields = $hit->field_names;  
 #                       print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields;  
134                  },                  },
135                  #startnum => 0,                  #startnum => 0,
136                  maxhits => $MAX_HITS,                  maxhits => $MAX_HITS
137          );          );
138    
139          die $SWISH::errstr unless $sh;          die $SWISH::errstr unless $sh;
140            # construct swish query
141            my $sw_q = join(" and ",@s_arr);
142            if (@path_arr) {
143                    $sw_q .= "and (swishdocpath=\"";
144                    $sw_q .= join("\" or swishdocpath=\"",@path_arr);
145                    $sw_q .= "\")";
146                    $tmpl->param('full',1); # show full records
147            }
148    
149          my $hits = $sh->query(join(" and ",@s_arr)) || 0;       # FIX: and/or          my $hits = $sh->query($sw_q);
150    
151          $tmpl->param('hits',$hits);          $tmpl->param('hits',$hits);
152          $tmpl->param('search',join(" and ",@s_arr));          $tmpl->param('search',$sw_q);
153    
154            $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 1);
155            $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 1);
156    
157          # create a Pager object          # create a Pager object
158          my $pager = HTML::Pager->new(          my $pager = HTML::Pager->new(
# Line 171  sub show_index { Line 201  sub show_index {
201    
202          my $q = $self->query();          my $q = $self->query();
203    
204            my $field = $q->param("f$i");
205            my $limit = $q->param("v$i");
206    
207          my $html;          my $html;
208    
209          $html .= "show index of ".$q->param("f$i")." for ".$q->param("v$i");          my $index = new index_DBI(
210                    $cfg_global->val('global', 'dbi_dbd'),
211                    $cfg_global->val('global', 'dbi_dsn'),
212                    $cfg_global->val('global', 'dbi_user'),
213                    $cfg_global->val('global', 'dbi_passwd') || ''
214            );
215    
216            my $total = $index->check($field);
217            if (! $total) {
218                    my $tmpl = $self->load_tmpl('no_index.html');
219                    $tmpl->param('field',$field);
220                    $html = $tmpl->output;
221                    return $html;
222            }
223    
224          return $html;          my $tmpl = $self->load_tmpl('index_res.html');
225            $tmpl->param('field',$field);
226            $tmpl->param('limit',$limit);
227            $tmpl->param('total',$total);
228    
229    # FIXME I should set offset and leave out limit from fetch!!
230    #       if (! $q->param("PAGER_offset") {
231    #               $q->param("Pager_offet)
232    #       }
233    
234            my $pager = HTML::Pager->new(
235                    query => $q,
236                    get_data_callback => sub {
237                            my ($offset, $rows) = @_;
238    
239                            my @result = $index->fetch($field,'item',$limit, $offset, $rows);
240                            return \@result;
241                    },
242                    rows => $total,
243                    page_size => $ON_PAGE,
244                    persist_vars => [
245                            'rm',
246                            "f$i", "v$i", "f".$i."_index",
247                            'offset',
248                            ],
249                    debug => 1,
250                    template => $tmpl,
251            );
252    
253            return $pager->output;
254  }  }
255    
256  1;  1;

Legend:
Removed from v.9  
changed lines
  Added in v.73

  ViewVC Help
Powered by ViewVC 1.1.26