This mod allows you to search theweb, categories, or both... Keep in mind I have posted the code here as if you are using templates. It shouldn't be too hard to figure out how to change it...
You can see this in action here:
www.magicdirectory.com/cgi-bin/search.cgi?query=david+blaine&search=both
or just the web search:
www.magicdirectory.com/cgi-bin/search.cgi?query=david+blaine&search=theweb
or just the category search:
www.magicdirectory.com/cgi-bin/search.cgi?query=david+blaine
In order for the option to search both the web and categories follow these instructions...
In search.cgi under:
(keys %in <= 0) and &site_html_search_form() and return;
put this:
($in{'search'} eq "both") and &altavista;
also at the bottom of search.cgi add these routines...
# --------------------------------------------------------
# Search Altavista
my %in = &parse_form;
use LWP::Simple;
my $query = $in{'query'};
my $query_q = &urlencode($query);
my $page = ($in{'nh'} * 10) | | 0;
my $pageXX = $page - 10;
my $url = "http://www.altavista.com/cgi-bin/query?q=$query_q&text=yes&stq=$pageXX";
my $results = get ($url);
$results or &site_html_search_failure ({ error => "No Results Available.", %in }) and return;
# If we see this, then we couldn't get a match.
my $none = quotemeta ("AltaVista found no document matching your query.");
if ($results =~ m/$none/) {
&site_html_search_failure ({ error => "No Matching Links", %in }) and return;
}
else {
if ($results =~ /([^>\s]+) pages found./) { $link_hits = $1; }
else { &site_html_search_failure ({ error => "Can't Parse Results", %in }) and return;
}
my $FONT = 'font face="verdana, arial" size="2"';
my $FONT_SMALL = 'font face="verdana, arial" size="1"';
if ($link_hits) {
while ($results =~ m#<dl><dt>\n<b>(\d+)\.\s*</b>\n<a href="([^"]+)">\n<b>([^<]+)</b></a></dt>\n<dd>([^<]*)</dd>\n<br><b>URL:</b> <font color="\#808080">([^<]+)</font>\n<br><font color="\#808080">\n([^<]+\n[^<]+)</font>#sog) {
($count, $url, $title, $description, $url2, $last) = ($1, $2, $3, $4, $5, $6);$url2 =~ s,/$,,;
$last =~ s,\n,,;$last =~ s,\s\s+, ,;
$link_results .= qq~<p>\n<$FONT_SMALL><b>$count.</b></font> <$FONT><a href="$url">$title</a><br>\n$description</font><br>\n<$FONT_SMALL><b>URL:</b> <font color="#808080">http://$url2</font></font>\n~;
}
}
}
$next = &next_hits($page, $link_hits, 10)
if ($in{'search'} eq "both") {
$link_results2 = $link_results;
$link_hits2 = $link_hits;
$next2 = $next;
return;
}
&site_html_search_results;
exit;
}The routine above this has changed...
# ----------------------------------------------
# Creates a next hits toolbar.
#
my ($page, $numhits, $maxhits) = @_;
my ($left, $right, $upper, $lower, $next_hit, $prev_hit, $i);
my $nh = int ($page / $maxhits) | | 1;
my $output = '';
$numhits =~ s/,//g;
$numhits = int ($numhits);
my $next_url = $ENV{'QUERY_STRING'};
$next_url =~ s/\&nh=\d+//;
$next_hit = $nh + 1; $prev_hit = $nh - 1;
# First, set how many pages we have on the left and the right.
$left = $nh; $right = int($numhits/$maxhits) - $nh;
# Then work out what page number we can go above and below.
($left > 7) ? ($lower = $left - 7) : ($lower = 1);
($right > 7) ? ($upper = $nh + 7) : ($upper = int($numhits/$maxhits) + 1);
# Finally, adjust those page numbers if we are near an endpoint.
(7 - $nh >= 0) and ($upper = $upper + (8 - $nh));
($nh > ($numhits/$maxhits - 7)) and ($lower = $lower - ($nh - int($numhits/$maxhits - 7) - 1));
# Then let's go through the pages and build the HTML.
($nh > 1) and ($output .= qq~<a href="$ENV{'SCRIPT_NAME'}?$next_url&nh=$prev_hit">[<<]</a> ~);
for ($i = 1; $i <= int($numhits/$maxhits) + 1; $i++) {
last if ($i > 19);
if ($i < $lower) { $output .= " ... "; $i = ($lower-1); next; }
if ($i > $upper) { $output .= " ... "; last; }
($i == $nh) ?
($output .= qq~$i ~) :
($output .= qq~<a href="$ENV{'SCRIPT_NAME'}?$next_url&nh=$i">$i</a> ~);
(($i * $maxhits) >= $numhits) and print "Exiting3" and last; # Special case if we hit exact.
}
$output .= qq~<a href="$ENV{'SCRIPT_NAME'}?$next_url&nh=$next_hit">[>>]</a> ~ unless (($nh == $i) or ($nh == 19));
return $output;
}
In site_html_templates.pl add these under sub site_html_search_results:
next2 => $next2,
link_results2 => $link_results2,
Now in your actual template add:
<%link_hits2%>
<%endif%>
<%if next2%>
<%next2%>
<%endif%>
<%if link_results2%>
<%link_results2%>
<%endif%>
Thats it! Keep in mind that you can edit the failure page to display one result if the other isn't working etc...!
Don't forget about deleting the space between | |.
------------------
- Jonathan Gamble
www.magicdirectory.com
[This message has been edited by jdgamble (edited April 09, 2000).]
You can see this in action here:
www.magicdirectory.com/cgi-bin/search.cgi?query=david+blaine&search=both
or just the web search:
www.magicdirectory.com/cgi-bin/search.cgi?query=david+blaine&search=theweb
or just the category search:
www.magicdirectory.com/cgi-bin/search.cgi?query=david+blaine
In order for the option to search both the web and categories follow these instructions...
In search.cgi under:
Code:
# Display the form if called with no input. (keys %in <= 0) and &site_html_search_form() and return;
put this:
Code:
($in{'search'} eq "theweb") and &altavista; ($in{'search'} eq "both") and &altavista;
also at the bottom of search.cgi add these routines...
Code:
sub altavista { # --------------------------------------------------------
# Search Altavista
my %in = &parse_form;
use LWP::Simple;
my $query = $in{'query'};
my $query_q = &urlencode($query);
my $page = ($in{'nh'} * 10) | | 0;
my $pageXX = $page - 10;
my $url = "http://www.altavista.com/cgi-bin/query?q=$query_q&text=yes&stq=$pageXX";
my $results = get ($url);
$results or &site_html_search_failure ({ error => "No Results Available.", %in }) and return;
# If we see this, then we couldn't get a match.
my $none = quotemeta ("AltaVista found no document matching your query.");
if ($results =~ m/$none/) {
&site_html_search_failure ({ error => "No Matching Links", %in }) and return;
}
else {
if ($results =~ /([^>\s]+) pages found./) { $link_hits = $1; }
else { &site_html_search_failure ({ error => "Can't Parse Results", %in }) and return;
}
my $FONT = 'font face="verdana, arial" size="2"';
my $FONT_SMALL = 'font face="verdana, arial" size="1"';
if ($link_hits) {
while ($results =~ m#<dl><dt>\n<b>(\d+)\.\s*</b>\n<a href="([^"]+)">\n<b>([^<]+)</b></a></dt>\n<dd>([^<]*)</dd>\n<br><b>URL:</b> <font color="\#808080">([^<]+)</font>\n<br><font color="\#808080">\n([^<]+\n[^<]+)</font>#sog) {
($count, $url, $title, $description, $url2, $last) = ($1, $2, $3, $4, $5, $6);$url2 =~ s,/$,,;
$last =~ s,\n,,;$last =~ s,\s\s+, ,;
$link_results .= qq~<p>\n<$FONT_SMALL><b>$count.</b></font> <$FONT><a href="$url">$title</a><br>\n$description</font><br>\n<$FONT_SMALL><b>URL:</b> <font color="#808080">http://$url2</font></font>\n~;
}
}
}
$next = &next_hits($page, $link_hits, 10)
if ($in{'search'} eq "both") {
$link_results2 = $link_results;
$link_hits2 = $link_hits;
$next2 = $next;
return;
}
&site_html_search_results;
exit;
}
Code:
sub next_hits { # ----------------------------------------------
# Creates a next hits toolbar.
#
my ($page, $numhits, $maxhits) = @_;
my ($left, $right, $upper, $lower, $next_hit, $prev_hit, $i);
my $nh = int ($page / $maxhits) | | 1;
my $output = '';
$numhits =~ s/,//g;
$numhits = int ($numhits);
my $next_url = $ENV{'QUERY_STRING'};
$next_url =~ s/\&nh=\d+//;
$next_hit = $nh + 1; $prev_hit = $nh - 1;
# First, set how many pages we have on the left and the right.
$left = $nh; $right = int($numhits/$maxhits) - $nh;
# Then work out what page number we can go above and below.
($left > 7) ? ($lower = $left - 7) : ($lower = 1);
($right > 7) ? ($upper = $nh + 7) : ($upper = int($numhits/$maxhits) + 1);
# Finally, adjust those page numbers if we are near an endpoint.
(7 - $nh >= 0) and ($upper = $upper + (8 - $nh));
($nh > ($numhits/$maxhits - 7)) and ($lower = $lower - ($nh - int($numhits/$maxhits - 7) - 1));
# Then let's go through the pages and build the HTML.
($nh > 1) and ($output .= qq~<a href="$ENV{'SCRIPT_NAME'}?$next_url&nh=$prev_hit">[<<]</a> ~);
for ($i = 1; $i <= int($numhits/$maxhits) + 1; $i++) {
last if ($i > 19);
if ($i < $lower) { $output .= " ... "; $i = ($lower-1); next; }
if ($i > $upper) { $output .= " ... "; last; }
($i == $nh) ?
($output .= qq~$i ~) :
($output .= qq~<a href="$ENV{'SCRIPT_NAME'}?$next_url&nh=$i">$i</a> ~);
(($i * $maxhits) >= $numhits) and print "Exiting3" and last; # Special case if we hit exact.
}
$output .= qq~<a href="$ENV{'SCRIPT_NAME'}?$next_url&nh=$next_hit">[>>]</a> ~ unless (($nh == $i) or ($nh == 19));
return $output;
}
In site_html_templates.pl add these under sub site_html_search_results:
Code:
link_hits2 => $link_hits2, next2 => $next2,
link_results2 => $link_results2,
Now in your actual template add:
Code:
<%if link_hits2%> <%link_hits2%>
<%endif%>
<%if next2%>
<%next2%>
<%endif%>
<%if link_results2%>
<%link_results2%>
<%endif%>
Thats it! Keep in mind that you can edit the failure page to display one result if the other isn't working etc...!
Don't forget about deleting the space between | |.
------------------
- Jonathan Gamble
www.magicdirectory.com
[This message has been edited by jdgamble (edited April 09, 2000).]