--- trunk/html/swish.cgi 2003/04/30 12:40:09 32 +++ trunk/html/swish.cgi 2004/01/29 18:25:55 60 @@ -3,10 +3,14 @@ use strict; use CGI qw/:standard -no_xhtml/; use CGI::Carp qw(fatalsToBrowser); -use SWISH; +use SWISH::API; use XML::Simple; use Lingua::Spelling::Alternative; use Text::Iconv; +use Data::Pageset; + +# for pager +my $pages_per_set = 20; Text::Iconv->raise_error(0); # Conversion errors raise exceptions my $config=XMLin(undef, @@ -31,22 +35,29 @@ return $out; } -my $spelling_alt; +my @spellings; # FIX: doesn't work very well if ($config->{findaffix}) { - $spelling_alt = new Lingua::Spelling::Alternative; - $spelling_alt->load_findaffix(x($config->{findaffix})); + foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) { + my $spelling_alt = new Lingua::Spelling::Alternative; + $spelling_alt->load_findaffix($findaffix); + push @spellings,$spelling_alt; + } } if ($config->{affix}) { - $spelling_alt = new Lingua::Spelling::Alternative; - $spelling_alt->load_affix(x($config->{affix})); + foreach my $affix (split(/[, ]+/,x($config->{affix}))) { + my $spelling_alt = new Lingua::Spelling::Alternative; + $spelling_alt->load_affix($affix); + push @spellings,$spelling_alt; + } } my $hits=0; -my $max_hits=x($config->{max_hits}); +my $max_hits=param('max_hits') || x($config->{max_hits}); my %labels; foreach (@{$config->{labels}->{label}}) { + next if (! $_->{value}); # skip unlimited (0) $labels{$_->{value}} = x($_->{content}); } @@ -54,6 +65,8 @@ my %path_label; my @path_name; foreach (@{$config->{paths}->{path}}) { + +print STDERR "##: $_->{limit}",x($_->{content}),"\n"; push @path_name,x($_->{limit}); $path_label{$_->{limit}} = x($_->{content}); } @@ -71,7 +84,7 @@ print x($config->{text}->{documents}); print textfield('search'); print submit(-value=> x($config->{text}->{submit})); -print br,checkbox(-name=>'no_affix', -checked=>0, -label=>x($config->{text}->{no_spell})) if ($spelling_alt); +print br,checkbox(-name=>'no_affix', -checked=>0, -label=>x($config->{text}->{no_spell})) if (@spellings); print checkbox(-name=>'no_properties', -checked=>0, -label=>($config->{text}->{no_properties})) if (@properties); if (@path_name) { print br,x($config->{text}->{limit}); @@ -92,22 +105,32 @@ $search =~ s/^\s+//; $search =~ s/\s+$//; + my %words; + foreach (split(/\s+/,$search)) { if (m/^([+-])(\S+)/) { $s.= ($s) ? "and " : ""; $s.="not " if ($1 eq "-"); - if ($spelling_alt && !param('no_affix')) { + if (@spellings && !param('no_affix')) { my $w = $2; $w =~ s/[\*\s]+//g; $w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/; - $s.="$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 "; + my $or=""; + foreach my $spelling_alt (@spellings) { + $s.="$or$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 "; + $or = "or "; + } } else { $s.="$2* "; } } else { - if ($spelling_alt && !param('no_affix')) { + if (@spellings && !param('no_affix')) { my $w = $_; $w =~ s/[\*\s]+//g; #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) "; - $s.="(".join("* or ",$spelling_alt->alternatives($w))."*) "; + my $or=""; + foreach my $spelling_alt (@spellings) { + $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) "; + $or = "or "; + } } else { $s.="$_* "; } @@ -120,53 +143,174 @@ $s=~s/\*\*+/*/g; # limit to some path - $s = "swishdocpath=(\"$path\") and $s" if ($path); + $s = "swishdocpath=(\"*$path*\") and $s" if ($path); my %params; # optional parametars for swish # default format for output my $hit_fmt = "%s [%s]
\n"; + # output start of table + print qq{ + + }; + # html before and after each hit + my $tr_pre = qq{ + + }; + if (@properties) { $hit_fmt = x($config->{hit}) if (! param('no_properties')); - $params{properties} = \@properties if (@properties); + $params{properties} = \@properties; } else { $hit_fmt = x($config->{hit}) if (x($config->{hit})); } - my $sh = SWISH->connect('Fork', - prog => x($config->{prog}), - indexes => x($config->{index}), - results => sub { - my ($sh,$hit) = @_; +# my $sh = SWISH->connect('Fork', +# prog => x($config->{prog}), +# indexes => x($config->{index}), +# results => sub { +# my ($sh,$hit) = @_; +# +# if ($config->{url}) { +# printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties)); +# } else { +# printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) ); +# +# } +# +## print $_[1]->as_string,"
\n"; +## my @fields = $hit->field_names; +## print "Field '$_' = '", $hit->$_, "'
\n" for sort @fields; +# }, +# maxhits => param('max_hits') || $max_hits, +# \%params, +# ); +# +# die $SWISH::errstr unless $sh; +# +# $hits = $sh->query($s); +# +# if ($hits && $hits > 0) { +# print p,hr; +# printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); +# } else { +# print p; +# printf (x($config->{text}->{no_hits}),$s,$sh->errstr); +# } +# if ($hits && $hits > 0) { +# print p,hr; +# printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); +# } else { +# print p; +# printf (x($config->{text}->{no_hits}),$s,$sh->errstr); +# } + + my $swish = SWISH::API->new($config->{index}); + + $swish->AbortLastError if $swish->Error; + + my $results = $swish->Query($s); + + my $hits = $results->Hits; + + + + # build pager + my $current_page = param('page') || 1; + + my $pager = Data::Pageset->new({ + 'total_entries' => $hits, + 'entries_per_page' => $max_hits, + 'current_page' => $current_page, + 'pages_per_set' => $pages_per_set, + }); + + $results->SeekResult( $pager->first - 1 ); + + # get number of entries on this page + my $i = $pager->entries_on_this_page; + + # print number of hits or error message + if ( !$hits ) { + printf (x($config->{text}->{no_hits}),$s,$swish->ErrorString); + } else { + printf (x($config->{text}->{hits}),$i,$results->Hits,$s); + } - if ($config->{url}) { - printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties)); - } else { - printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) ); + for(my $i=$pager->first; $i<=$pager->last; $i++) { + + my $result = $results->NextResult; + last if (! $result); + + my @arr; + foreach my $prop (@properties) { + if ($prop =~ m/swishdescription/) { + my $tmp = $result->Property($prop); + $tmp =~ s/<[^>]+>//g; + push @arr, $tmp; + } else { + push @arr, $result->Property($prop); } + } -# print $_[1]->as_string,"
\n"; -# my @fields = $hit->field_names; -# print "Field '$_' = '", $hit->$_, "'
\n" for sort @fields; - }, - maxhits => param('max_hits') || $max_hits, - \%params, - ); + print $tr_pre,$i,". "; + if ($config->{url}) { + printf($hit_fmt, "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath"), + e($result->Property("swishtitle")) || 'untitled', + $result->Property("swishrank"), + @arr); + } else { + printf($hit_fmt, $result->Property("swishdocpath"), + e($result->Property("swishtitle")) || 'untitled', + $result->Property("swishrank"), + @arr); + } + print $tr_post; - die $SWISH::errstr unless $sh; + } + # pager navigation + my $nav_html; - $hits = $sh->query($s); + my $nav_fmt=qq{ %s }; - if ($hits > 0) { - print p,hr; - printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); - } else { - print p; - printf (x($config->{text}->{no_hits}),$s,$sh->errstr); + if ($pager->previous_set) { + param('page', $pager->previous_set); + $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'<<'); + } + + + foreach my $p (@{$pager->pages_in_set()}) { + next if ($p < 0); +# for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) { + if($p == $pager->current_page()) { + $nav_html .= "$p "; + } else { + param('page', $p); + $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),$p); + } + } + + if ($pager->next_set) { + param('page', $pager->next_set); + $nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'>>'); } + + # end html table + print qq{ + +
+ }; + my $tr_post = qq{ +
+Pages: $nav_html +
+ }; + + + } else { print p(x($config->{text}->{footer})); }