Skip to main content.
home | support | download

Back to List Archive

Re: modifying swish.cgi output

From: Bill Moseley <moseley(at)not-real.hank.org>
Date: Sat Mar 15 2003 - 21:43:20 GMT
On Sat, 15 Mar 2003, J Robinson wrote:

> Will these changes be wrapped into a dev build? I
> imagine that wanting just a simple text search box  is
> not that unusual.

By the way:

The swish.cgi script is bloated because if offers a bunch of features all
in one package.  If all you want is a simple search script you might want
to write your own.

Here's a quick script (meaning not well tested) that is reasonably basic.
Less than 100 lines of code.  It uses Template-Toolkit to make the output
generation "easy" (the template is below the script).  No term
highlighting, but you could add that without much problem (see swish.cgi
for how).

#!/usr/bin/perl -w
use strict;
use SWISH::API;
use CGI;
use Template;

my $index = 'index.swish-e';

my @props = qw/
    swishtitle
    swishdocpath
    swishlastmodified
    swishdocsize
    swishrank
    swishdescription
    swishreccount
/;

my $cgi = CGI->new;

# Get starting record number

my $start = $cgi->param('start') || 1;
$start = 1 unless $start =~ /^\d+$/;


my %vars; # hash to hold template variables
$vars{cgi} = $cgi; 

if ( my $query = $cgi->param('query')) {
   $vars{query} = $query;
   $vars{start} = $start;
   $vars{swish} = run_query($query, $index, $start, @props);
}

print $cgi->header;

my $template = Template->new;
$template->process( \*DATA, \%vars ) 
    || die $template->error;

#-------------------------------------------------------
sub run_query {
    my ( $query, $index, $start, @props ) = @_;
    my %results; # hash to return

    my $page_size = 10;

    # Create search object
    my $swish = SWISH::API->new($index);
    $swish->AbortLastError if $swish->Error;

    my $search = $swish->New_Search_Object;
    my $results = $search->Execute($query);

    if ( $swish->Error ) {
	$swish->AbortLastError if $swish->CriticalError;
	$results{message} = join ' ',$swish->ErrorString,
                                     $swish->LastErrorMsg;
	return \%results;
    }

    $results{hits} = $results->Hits;

    # any results?
    unless ( $results{hits} ) {
        $results{message} = 'No Results';
        return \%results;
      }

    # Set navigation
    if ( $start > 1 ) {
	$results{prev} = $start - $page_size > 1
                       ? $start - $page_size
                       : 1;
    }

    if ( $start + $page_size <= $results{hits} ) {
	$results{next} = $start + $page_size;
    }

    # seek to start location
    $results->SeekResult( $start-1 );

    my $rec_count = $page_size;

    while ( my $result = $results->NextResult ) {
        my %props = map { $_ => $result->Property($_) || '' } @props;
	push @{$results{results}}, \%props;
        last unless --$rec_count;
    }

    return \%results;
}

__END__
<html>
<head>
<title>
   Search our site
</title>
</head>
<body>
[% PROCESS show_message %]
[% PROCESS show_form %]
[% IF swish.results; PROCESS show_results; END %]
</body>
</html>

[% BLOCK show_message %]
    [% IF swish.message %]
        <font size="+2" color="red">[% swish.message | html %]</font><br>
    [% END %]
[% END %]


[% BLOCK show_form %]
    <form method="get" action="[% cgi.url %]" 
        enctype="application/x-www-form-urlencoded">

        <input maxlength="200" value="[% query | html %]" 
           size="32" type="text" name="query"/>

        <input value="Search!" type="submit" name="submit"/><br>

    </form>
[% END %]

[% BLOCK show_results %]


    [% USE date %]
    Found [% swish.hits %] hits for <b>[% query|html %]</b>

    [% PROCESS navigation start = swish.prev text = '&lt;&lt;prev' %]
    &nbsp;
    [% PROCESS navigation start = swish.next text = 'next&gt;&gt;' %]
    <br>

    [% FOREACH item = swish.results %]
        <dl>
            <dt> 
                [% item.swishreccount %]: 
                <a href="[% item.swishdocpath | uri | html %]">
                [% item.swishtitle | html %]
                </a>

                <small>-- rank: <b>[% item.swishrank %]</b></small>
            </dt>

            <dd>
                [% item.swishdescription | html %]
                <br>
                <small>
                    [% item.swishdocpath | html %]
                    - [% item.swishdocsize %] bytes
                    - [% date.format(item.swishlastmodified) %]
                </small>
            </dd>
        </dl>
    [% END %]
[% END %]

[% BLOCK navigation %]
   [% IF start %]
       <a href="[% cgi.url %]?query=[% query|uri|html %];start=[% start %]">
          [% text %]
       </a>
   [% END %]
[% END %]


-- 
Bill Moseley moseley@hank.org
Received on Sat Mar 15 21:47:04 2003