Skip to main content.
home | support | download

Back to List Archive

Here's the cgi script

From: Rich Thomas <thomasr(at)not-real.buffalo.edu>
Date: Fri Jan 25 2002 - 12:45:59 GMT
Bill,

You said you'd need to see my cgi script to see why I get no Descriptions.
Here's the script and the output:

#!/usr/bin/perl
# Change the above line to reflect the location of your installation of PERL
#
# -------------------------------------------------------------------
# Program: swish-cgi.pl
# Author : John Millard (millarj@muohio.edu)
#
# Purpose: A gateway interface (CGI) to the SWISH Searcher/indexer
#
# Instructions:
#
# 1. Install and configure SWISH --
#    Available from Enterprise Integration Technologies at
#    http://www.eit.com/goodies/software/swish/swish.html
#
# 2. Index your site so that SWISH returns the url for each file
#    ie.  Swish should return http://www.yoursite.edu/file_name.html
#    See the SWISH documentation about REPLACE_RULES to see how.
#
# 3. Customize the User-Defined variables below to reflect your site.
#
# 4. Install this file in your cgi directory.  This may vary
#    from site to site, but is usually in a directory like cgi-bin
#
# 5. Create a link from your pages to the cgi
#    ex.  http://www.yoursite.edu/cgi-bin/swish-cgi
#    Running the cgi as a URL will generate a blank query form on the fly.
#
#
# Note: if you don't like the the initial form that comes up, you can modify
#       the print_form subroutine
#
#       To change the format of the returned results, you may modify the
#       print_results subroutine

# -------- User defined configuration variables -----------

# Absolute path and command to execute the SWISH searcher
   $swish = "/usr/local/bin/swish-e";

# URL of where you put this cgi
   $swishcgi = "/cgi-bin/swish-cgi.pl";

# Optional parameters to pass to the SWISH searcher
   $params = " ";

# Absolute path and filename of your created swish index file
   $index = "/usr/local/bin/index.swish-e";

# The Full name of your organization -- Printed with Search Results
   $organization = "University Libraries";

# The full name of your department -- Printed with search Results
   $department = "Electronic Information Services and Instruction Office";

# ------ End of Configuration Variables ------------


#sub read_form
# Reads in form data if it exists

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

     # Split the Name value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);

    # Un-Webify plus signs and %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    push(@search_tags, $value), next if ($name eq 'search_tags');

    $FORM{$name} = $value
}
$query = $FORM{'query'};
$results = $FORM{'results'};

if (@search_tags)
   {
     $tags = join("",@search_tags);
     $search_tags = "\-t $tags";
   } else
     {
       $search_tags = "";
     }

 if ($query)
    {
     &search_parse;
    } else
      {
        &print_form;
      }



 sub print_form
 {
 &html_header(" ");
 # To change the form that get's generated on the fly, edit the HTML below.
 print <<EOF;

<CENTER><H2>Search Form</H2></CENTER>

<FORM ACTION="$swishcgi" method=post>

Enter word(s). You can connect terms with <b>and</b> or <b>or</b><p>
    <b>and</b> will find items that contain both terms<br>
    <b>or</b> will find items that contain either word, but not necessarily
    both<p>
   Example: disadvantaged and students<p>
   <INPUT NAME="query"  SIZE=50><p>

<B>Maximum # of Items</b>
<SELECT name=results>
<OPTION value=0> No Limit
<OPTION value=10> 10
<OPTION value=20> 20
<OPTION value=30> 30
<OPTION value=40> 40
<OPTION value=50> 50
<OPTION value=100> 100
  </SELECT>
<P>
Search In the following Tags: Leave Blank to search everything<br>
<table border>
<tr><td><input type=checkbox name=search_tags value="t">Title Tags</td>
<td><input type=checkbox name=search_tags value="h">Heading Tags</td>
<td><input type=checkbox name=search_tags value="c">Comment Tags</td>
<td><input type=checkbox name=search_tags value="e">Emphasized Text<td></tr>
</table>
<P>


<INPUT TYPE="submit" VALUE="Start Search">
<inPUT TYPE="reset" VALUE="Clear Form">
<HR>
</FORM>

</BODY>
</HTML>
EOF
&html_trailer;
}

sub search_parse
# Run SWISH and parse output
{
#Initialize counter variable for number of results
$count = 0;
open(SWISH, "$swish -w $query -m $results $search_tags -f $index|");
while (<SWISH>)
{
  # First, check to see if search produced an error
   chop;
   if ($_ eq "err: no results")
   {&search_error("There were no items that matched your search request");}

   if ($_ eq "err: could not open index file")
   {&search_error("Could not open SWISH Index File $index");}

   if ($_ eq "err: no search words specified")
   {&search_error("Please Enter at least one Search Word");}

   if ($_ eq "err: a word is too common")
 {&search_error("One of your search terms is too common, please try
again");}

# Next Line ignores lines that begin with a non-digit
  next if /^\D/;
  $count++;
  push(@results, $_);

}
 &html_header("Your Search Results");
    print "Swish found the following items that might be relevant to
your\n";
    print "search topic.  A higher relevancy score means the item is
more\n";
    print " likely to be what you are looking for.<p>\n";
    print "Your Search for <strong>$query</strong>, returned $count
Items\n";
    print "<hr><dl><dt>\n";
foreach (@results)
{

 select(STDOUT);
 ($stringone, $title, $filesize) = split(/\"/, $_);
 ($rank, $url) = split(/ /, $stringone);
 print "<dd><a href=\"$url\">$title</a><br>\n";
 print "<dd>Relevancy Score: $rank  Size of Document: $filesize Bytes<p>\n";
}
print "</dl><P>\n";
&html_trailer;
}

sub search_error
{
 &html_header("Your Search Results");
 $error_message = $_[0];
 print "$error_message\n";
 &html_trailer;
}

sub html_header
# This subroutine takes the document title as a command
# line parameter and adds header information to the top
# of the HTML document to be returned.

{
    $document_title = $_[0];
    print "Content-type: text/html\n\n";
    print "<HTML>\n";
    print "<HEAD>\n";
    print "<TITLE>$document_title</TITLE>\n";
    print "</HEAD>\n";
    print "<H3>$document_title</H3>\n";
    print "<hr>\n";

}


sub html_trailer
# This subroutine prints a suitable HTML trailer
{

print "<P>\n";
print "$organization<br>\n";
print "$department<P></body>\n";
print "</body>\n</html>\n";
exit;
}

OUTPUT

Your Search Results

----------------------------------------------------------------------------
----
Swish found the following items that might be relevant to your search topic.
A higher relevancy score means the item is more likely to be what you are
looking for.
Your Search for buffalo, returned 4 Items


----------------------------------------------------------------------------
----

University at Buffalo Libraries WebCatalog

Relevancy Score: 1000 Size of Document: 1368 Bytes

A/A/E/9/001 University at Buffalo Libraries Web Catalog

Relevancy Score: 908 Size of Document: 1231 Bytes

Thanks for all your help,

Rich
Received on Fri Jan 25 12:46:39 2002