#!/usr/bin/perl
#
# search-report: report on search queries and results from visitors
#
# Copyright (c) 2000 Chris Lightfoot. All Rights Reserved.
#
# $Log: search-report,v $
# Revision 1.1  2000/08/20 21:38:27  chris
# Initial revision
#
#

$rcsid = '$Id: search-report,v 1.1 2000/08/20 21:38:27 chris Exp chris $';

sub http_unescape {
        my ($foo) = @_;
        $foo =~ tr/+/ /;
        $foo =~ s/%(..)/chr(hex($1))/ge;
        return $foo;
}

sub looks_like_query {
        my ($foo) = @_;
        return ($foo =~ /[A-Za-z0-9 +\-()]+/ and length($foo) > 3 and $foo !~ /^cache:/);
}

$time = scalar(localtime());

print <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<!--
     Generated by $rcsid
-->
<title>
What the Search Engine Found
</title>
<body bgcolor="#ffffff" text="#000000" link="#ff0000" vlink="#7f0000">

<h1>
What the Search Engine Found
</h1>

<p>
[ <a href="/~chris/">Home Page</a> ]
</p>

<p>
This is a compilation of search-engine queries (established from the HTTP
Referer: field in the logs) which led people to pages on this site. The
script which generated this report is available as
<a href="http://www.ex-parrot.com/~chris/stuff/search-report">http://www.ex-parrot.com/~chris/stuff/search-report</a>. You can also read
<a href="search-report-expln.html">an explanation</a> of what this is all
about.</p>

<p>
This was (sort of) current at $time.
</p>

<table>
<tr>
<td>
Somebody found...
</td>
<td>
by searching for...
</td>
</tr>
EOF

@search_args = ("q", "qr", "p", "ask", "MT", "query", "qt", "search", "ask");

while ($l = <STDIN>) {
        ($url, $referrer) = $l =~
                /"(?:GET|POST) ([^ ]+) [^"]*" [0-9]+ [0-9]+ "([^"]+)/;

        $url =~ s/\/$//;
        $url = http_unescape($url);

        if ($referrer eq '-') { next; }

        ($script, $args) = $referrer =~ /([^?]+)\?(.*)/;

        if ($args ne undef) {
                %a = ( );
                @foo = split('&', $args);
                foreach $f (@foo) {
                        ($k, $v) = split('=', $f);
                        $a{http_unescape($k)} = http_unescape($v);
                }

                foreach $s (@search_args) {
                        if (defined($a{$s}) and looks_like_query($a{$s})) {
                                $res{$url}->{$a{$s}}++;
                                last;
                        }
                }
        }
}

foreach $url (keys %res) {
        print <<EOF;
<tr>
<td>
<a href="$url">$url</a>
</td>
EOF
        $first = 1;
        foreach $search (sort(keys(%{$res{$url}}))) {
                if (!$first) {
                        print <<EOF;
<tr>
<td></td>
EOF
                }
                print <<EOF;
<td>$search</td>
</tr>
EOF
                $first = 0;
        }
}

print <<EOF;
</table>
<hr>

<p>
Copyright (c) 2000 <a href="mailto:chris\@ex-parrot.com">Chris Lightfoot</a>.
All Rights Reserved.
</p>

</body>
</html>
EOF
