--- trunk/html/swish.cgi 2003/06/01 12:13:36 41
+++ trunk/html/swish.cgi 2004/02/06 13:27:51 62
@@ -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,
@@ -16,6 +20,7 @@
my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset});
sub x {
+ return if (! defined $_[0]);
return $from_utf8->convert($_[0]);
}
@@ -49,10 +54,11 @@
}
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});
}
@@ -66,7 +72,7 @@
$path_label{$_->{limit}} = x($_->{content});
}
-my @properties = split(/\s+/,x($config->{properties}));
+my @properties = split(/\s+/,x($config->{properties})) if ($config->{properties});
if ($config->{charset}) {
print header(-charset=>x($config->{charset}));
@@ -145,6 +151,18 @@
# 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{
+
+ };
+ my $tr_post = qq{
+ |
+ };
+
if (@properties) {
$hit_fmt = x($config->{hit}) if (! param('no_properties'));
$params{properties} = \@properties;
@@ -152,39 +170,148 @@
$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 && $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{
+
+Pages: $nav_html
+ |
+
+ };
+
+
+
} else {
print p(x($config->{text}->{footer}));
}