Skip to main content.
home | support | download

Back to List Archive

Older scripts working with swish dev ?

From: Andrew Lord <andrewlord(at)>
Date: Fri Jun 28 2002 - 15:17:14 GMT
Hi Bill,

Back on line again after a very frustrating hard-drive failure.  Still
catching up but back into things again.

A number of the changes we have discussed have been integrated into the
script (dictionary.cgi, shown below) and while previously I had believed it
to be working correctly, I hadn't realised that the script now accesses only
 a generic/default index ('index.swish') located in the same directory as the
 script itself (ie in cgi-bin).  It seems that when sub makewordlist is
 called in the Main Program, the Main Program is not looking to the index
 that has been selected from the dropdown menu ($index) and which resides in
/SWISH-E/swish ($swishdir).

This is exhibited in the following scenario; if I remove index.swish-e from
cgi-bin before selecting an index from the dropdown menu and then clicking
any letter, I get the following error message: "could not open index
'index.swish-e' ".

How should the correct $swishdir and $index be referenced by the Main Program
or reference to the correct $swishdir and $index be integrated into sub
makewordlist ?


Andrew Lord

// #!/usr/local/bin/perl5
# Copyright (c) 2000 Bas Meijer
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#- Configurable Variables --------------------------------------------#
$searchscript = 'lookup.cgi';
# Absolute path and command to execute the SWISH searcher
$swish = '/usr/local/bin/swish-e';

#- Standard Stuff you won't need to edit -----------------------------#
# configure the sections from the configuration files.
my $swishdir = "$ENV{'DOCUMENT_ROOT'}/SWISH-E/swish";
# find the configfile that belongs to the index
@sections = glob("$swishdir/*.conf");
foreach $section (@sections){
	open CONF,$section;
		if (/^IndexFile\s(.*)$/){
			$indexfile = $1;
			$parts{$indexfile} = $1;
		if (/^IndexName\s(.*)$/){
			$indexname = $1;
			$indexname =~ s/"//g;
	 		$parts{$indexfile} = $indexname;
		# ReplaceRules should no longer be quoted for this to work
		s|["']||g; # if they still are they're trimmed
		if( m|ReplaceRules\sreplace\s(\S*)\s(\S*)|i){
					$replace{$2} = $1;
	close CONF;

print "Content-type: text/html\n\n";

# Parse web input

# When the pattern is used act accordingly

$swishtags = "index=$input{index}&search=";

my $pattern = $input{'l'};

my $index = $input{'index'};

# data filled in so do da thang!

# generate the select with the sections

my $select = &makeselect;

use Symbol;

my @stuff = makewordlist( $pattern, $swish );



        $wordlist = (&tableize(6,@stuff));



# S U B R O U T I N E S

sub printform{
	my $letter;
	print '<HTML><HEAD><TITLE>Dictionary</TITLE></HEAD>';
	<FORM ACTION="/cgi-bin/dictionary.cgi" METHOD=POST NAME=catalog>
	<INPUT TYPE=hidden NAME="index" VALUE="$input{index}">
	# Convenience function for large databases
    print '<A NAME="letters">Dictionary: show words starting with:<BR><FONT
    for $letter ('a'..'m'){
        print qq|<INPUT TYPE=SUBMIT NAME=l VALUE="$letter">|;
	#print '<BR>';
    for $letter ('n'..'z'){
        print qq|<INPUT TYPE=SUBMIT NAME=l VALUE="$letter">|;
    print '</FONT></TD></TR>';
	print "<TR><TD>in: $select";
	print " </TD></TR>";
    print "</TABLE></FORM><BR CLEAR=ALL>$wordlist";
	print '</BODY></HTML>';
# generate the pulldown menu with the sections of the archive
sub makeselect{
	$html = '<SELECT NAME="index">';
	foreach $key (sort keys %parts){
		if($key eq $FORM{'index'}){
			$html .= qq|<OPTION SELECTED VALUE="$key">$parts{$key}|;
			$section = $parts{$key};
			$html .= qq|<OPTION VALUE="$key">$parts{$key}|;
	$html .= '</SELECT>';
	return $html;
# get data from httpd and put it in global hash input
sub getinput{
	my ($qstring,$i,$value);
	my @querylist;

		read(STDIN, $qstring, $ENV{'CONTENT_LENGTH'});
		$qstring = $ENV{'QUERY_STRING'};
	# decode input into hash table
	@querylist = split(m'&', $qstring);
	for $i (@querylist)
		($key,$value) = split(/=/, $i);
		$value =~ tr/+/ /;
		$value =~ s/%(..)/pack('c',hex($1))/eg;
		$input{$key} = $value;
	return %input;

sub makewordlist {

    my $swishdir = "$ENV{'DOCUMENT_ROOT'}/SWISH-E/swish";

    my $swish = '/usr/local/bin/swish-e';

    my ( $letter, $swish ) = @_;

    # pick which letters are ok
    unless ( $letter =~ /^[a-zA-Z]$/ ) {
        warn "invalid letter '$letter'";

    my $fh = gensym;

    open( $fh, "$swish -k $letter|" ) or
        die "Failed to run '$swish -k $letter': $!";

    # -k
    my @words;

    # format is index-file: space separated list of words

    while ( <$fh> ) {
        next if /^#/;  # skip comments
        if ( my $words = ( split /\s*:\s*/ )[1] ) {
            push @words, split /\s+/, $words;

    close $fh
       or warn "Failed to close '$swish -k $letter': "
       . ($! || "Exit status: $?");

    return @words;
    # or return \@words to return a "reference" instead
    # of the entire list if you want to avoid the copy


# make a html-table from a list
sub tableize {
    my($columns,@elements) = @_;
    $rows = int(0.99 + @elements/$columns);
    # rearrange into a pretty table
    $result = "<TABLE CELLPADDING=2>";
    for ($row=0;$row<$rows;$row++) {
        $result .= "<TR>";
        for ($column=0;$column<$columns;$column++) {
            $result .= qq|<TD><A HREF="$searchscript?$swishtags| .
			$elements[$column*$rows + $row] . '">' .
			$elements[$column*$rows + $row] . '</A></TD>';
        $result .= "</TR>";
    $result .= "</TABLE>";
    return $result;
# end of file


Received on Fri Jun 28 15:20:54 2002