| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# htwrap.cgi |
| 4 |
# |
| 5 |
# by John Grohol ([email protected]) |
| 6 |
# Freeware |
| 7 |
# v1.00 - 5 Oct 1998 |
| 8 |
# |
| 9 |
# Simple wrapper script for htsearch to |
| 10 |
# do some basic sanity checking on the query |
| 11 |
# and tries to re-form it into a valid htsearch query. |
| 12 |
# |
| 13 |
# This script must be called using the GET method! |
| 14 |
# |
| 15 |
#_______________________________________________________ |
| 16 |
# Set some defaults here |
| 17 |
# These can be overridden in the calling form |
| 18 |
|
| 19 |
$config = "htdig"; # htDig config file |
| 20 |
$exclude = ""; # exclude this url |
| 21 |
$restrict = ""; # restrict to this url |
| 22 |
$format = "builtin-long"; # results format |
| 23 |
$method = "and"; # default method |
| 24 |
$dir = "/usr/httpd/cgi-bin"; # Set cgi-bin dir |
| 25 |
|
| 26 |
#_______________________________________________________ |
| 27 |
# Rest of program |
| 28 |
|
| 29 |
$| = 1; |
| 30 |
|
| 31 |
# Get the form variables |
| 32 |
|
| 33 |
&ParseTags($ENV{'PATH_INFO'}); |
| 34 |
&ParseTags($ENV{'QUERY_STRING'}); |
| 35 |
|
| 36 |
$squery = $tags{'words'}; |
| 37 |
$restrict = $tags{'restrict'}; |
| 38 |
$method = $tags{'method'}; |
| 39 |
$format = $tags{'format'}; |
| 40 |
$page = $tags{'page'}; |
| 41 |
|
| 42 |
if (not($page)) { $page=1; } |
| 43 |
|
| 44 |
$squery =~ s/\+//g; |
| 45 |
$squery =~ s/\-//g; |
| 46 |
$squery =~ s/the//g; |
| 47 |
$squery =~ s/not//g; |
| 48 |
$squery =~ s/what//g; |
| 49 |
|
| 50 |
# If someone puts "and" or "or" in the query, |
| 51 |
# then it should be a boolean query |
| 52 |
|
| 53 |
if (($squery =~ " and ") || ($squery =~ " or ")) { |
| 54 |
$method = "boolean"; } |
| 55 |
|
| 56 |
# How many words are there in the query? |
| 57 |
@words = split(/ /,$squery); |
| 58 |
foreach $word (@words) { $xwd++; } |
| 59 |
|
| 60 |
# If there are quotes in the query, we have to |
| 61 |
# turn them into parantheses and make it boolean |
| 62 |
|
| 63 |
if (($squery =~ "\"")) { |
| 64 |
$oo = (index($squery,"\""))+1; |
| 65 |
$od = (index($squery,"\"",$oo))-1; |
| 66 |
$op = $od - $oo +1; |
| 67 |
$yty = substr($squery,$oo,$op); |
| 68 |
@wrds = split(/ /,$yty); |
| 69 |
foreach $wrd (@wrds) { $xww++; } |
| 70 |
|
| 71 |
|
| 72 |
if ($xww eq 2) { # Right now, can only handle 2-word phrases |
| 73 |
$oi = (index($yty," ")); |
| 74 |
if ($oi > -1) { |
| 75 |
$ytt = substr($yty,0,$oi); |
| 76 |
$john = $od - $oi +1; |
| 77 |
$yte = substr($yty,$oi+1,$john); |
| 78 |
$james = substr($squery,$od+2); |
| 79 |
$james =~ s/ and//g; |
| 80 |
$james =~ s/ / and /g; |
| 81 |
$squery = "($ytt and $yte) $james"; # We turn it into a |
| 82 |
$method = "boolean"; # boolean query |
| 83 |
} |
| 84 |
|
| 85 |
# More than 2 words in quotes (phrase), just |
| 86 |
# turn it into one big string of words and set method to "and" |
| 87 |
|
| 88 |
} else { |
| 89 |
$squery =~ s/\"//g; # Dump quotes |
| 90 |
$squery =~ s/ and//g; # Dump and's |
| 91 |
$squery =~ s/ or//g; # Dump or's |
| 92 |
$method = "and"; |
| 93 |
$yty = ""; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
# Set the environmental variables |
| 98 |
|
| 99 |
$ENV{'REQUEST_METHOD'} = 'GET'; |
| 100 |
$ENV{'QUERY_STRING'} = "config=$config&restrict=$restrict&exclude=$exclude&words=$squery&method=$method&format=$format&page=$page" |
| 101 |
; |
| 102 |
|
| 103 |
# Run htsearch |
| 104 |
|
| 105 |
system("$dir/htsearch"); |
| 106 |
|
| 107 |
exit; |
| 108 |
|
| 109 |
sub ParseTags { |
| 110 |
local($_) = @_; |
| 111 |
local(@terms, $tag, $val); |
| 112 |
s|^/||; |
| 113 |
@terms = split('&'); |
| 114 |
foreach $term (@terms) { |
| 115 |
($tag,$val) = split('=',$term,2); |
| 116 |
$val =~ tr/+/ /; |
| 117 |
$val =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; |
| 118 |
$val =~ s/<!--(.|\n)*-->//g; |
| 119 |
$val =~ s/<([^>]|\n)*>//g; |
| 120 |
# may override previous value |
| 121 |
$tags{$tag} = $val; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
1; |