#!/usr/bin/perl use DB_File; require '/home/rickheit/public_html/dtrad/common.pl'; chdir($dtdir); tie %tle, DB_File, $titldb, O_RDONLY, 0, $DB_HASH; %arg = &cgi_handle; $q = $arg{'query'} || 'stuff'; # turn that into a glimpse query $q =~ s/\s+/ /g; $q =~ s/^ | $//g; $q =~ s/ AND /;/ig; $q =~ s/ OR /,/ig; $q =~ s/ +/;/g; $q =~ s/[\\']/\\$&/g; # call glimpse open(IN, "/usr/local/bin/glimpse -yiNQ -H $dtdir '$q' 2>/dev/null|"); while () { ($url, $match) = /^(\S+).*\[\s*(.*)\s*]/g; @matches = split ' ', $match; push @res, { 'url' => $url, 'hits' => 0+@matches, 'matches' => [@matches] }; } close(IN); @res = sort { $$b{'hits'} <=> $$a{'hits'} } @res; $count = @res; # format the results, starting at start... $start = $arg{'start'}; $high = $start; for (splice @res, $start, 100) { $url = $$_{'url'}; $title = ''; $tn = ''; if ($url =~ /ti=([^\&]*)/) { next unless exists $tle{"ti$1"}; $title = $tle{"ti$1"}; if ($url =~ /tt=([^\&]*?)/) { $tn = " (with tune)"; } } else { if ($url =~ /tt=([^\&;]*)/) { $title = $tle{"tn$1"}; } $tn = " (tune only)"; } $url =~ s/;/;tt/g; $url =~ s/\&*(t.)=/;$1/g; $url =~ s/^;//; $results .= qq(
  • $title$tn\n); $high++; } if ($high) { $ct = "(". ($start+1) ." - $high)"; } $ct = '' if $ct eq "(1 - 1)"; $q = $arg{'query'}; &fixch($q); unless ($count) { $results = "No matching results"; } if ($high < $count) { $more = <results are available, for EOF } print < Search Results

    Results $ct of search for '$q'

    Search for:
      $results
    $more a total of $count matches.

    Contents: ? A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Main Page EOF sub cgi_handle { local(@a); if ($ENV{'REQUEST_METHOD'} eq 'POST') { ($stuff = <>) =~ s/^\s*|\s*$//g; } else { $stuff = $ENV{'QUERY_STRING'}; } @a = split(/[\&\=]/, $stuff); for (@a) { s/\+/ /g; s/%(..)/sprintf("%c", hex($1))/eg; } return @a; } # Turns HTML-illegal characters into their cute little ol' coding. sub fixch { $_[0] =~ s/\W/'%'.sprintf('%02X', ord($&))/eg; } __END__ glimpse -yiNQ -H /home/rickheit/public_html/dtrad