#!/usr/local/bin/perl
#<META TITLE="Most Popular Searches of Clint's Media">


require "./querystatconstants.pl";

require "../lib/string.pl";
require "../lib/syscall.pl";
require "../lib/counter.pl";
require "../lib/constants.pl";
require "../lib/header.pl";
require "../lib/footer.pl";
require "../lib/Parse_Data.pl";

## NOT JUST MYCURRENT IPS, BUT ANY IPS I *EVER* USED ON THIS PAGE:
#my @IGNOREIP = ("206.161.92.52", "206.161.92.53", "206.161.92.54", "63.102.109.222",
# 	"64.50.179.160","64.50.179.161","64.50.179.162","216.200.103.193",
#	"216.200.103.194","216.200.103.195");
my @IGNOREIP = @IP_ALL;


my $SHORT_NAME="querystats";	#no extension
my $TITLE="Most Popular Searches of Clint's Media";
my $URL_TO_THIS_PROGRAM="$BASECGIURL/media/$SHORT_NAME" . ".pl";
my $URL_TO_QUERY_PROGRAM=$URL_TO_THIS_PROGRAM;
$URL_TO_QUERY_PROGRAM =~ s/\/[^\/]*$/\/query.pl/;
my $LOG_FILE="$LOGDIR/query.log";		#must be synced with query.pl!!

my %REQUEST_COUNTS=();
my $requestsMade=0;
my %FOUND=();

&Print_Header;
&Parse_Data;
&Massage_Data;



print "<html><head><title>$TITLE</title></head><body>\n";

print qq[
<table width=100% border=0><Tr valign=top><td align=center>
<script type="text/javascript"><!--
google_ad_client = "pub-1303586900976050";
/* ACM Footer, 728x90, created 6/13/08 */
google_ad_slot = "9449894613";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td></tr></table>
];

#print "Hello?!<BR>";
#goat
#@IGNOREIP
#foreach $tmpip (@IP_ALL) { print "ignoring $tmpip ...<BR>"; }
#print "bye?!<BR>";

&QueryStats;
exit;       					    #Just in case there are any doubts.




#####################################################################
sub Massage_Data {
if ($FORM{reverse} eq "")  { $FORM{reverse}="0";   }

#WORKS--DEFAULTS SEARCH TO #OFSEARCHES: 
if ($FORM{type}    eq "")  { $FORM{type}="count";  }
#BREAKS:
#if ($FORM{type}    eq "")  { $FORM{type}="sort";  }
#DO THIS TO MAKE DEFAULT SEARCH BY #OFRESULTS NOT #OFSEARCHES
#if ($FORM{type}    eq "")  { $FORM{type}="results";  }

if ($FORM{reverse} eq "1") { $FORM{reversent}="0"; } 
else 			   { $FORM{reversent}="1"; }

if ($FORM{includeclint} eq "") { $FORM{includeclint}="0"; }

if ($FORM{includeclint} eq "1") { @IGNOREIP=(); }
}#endsub Massage_Data
#####################################################################

###############################################################################
sub QueryStats {

print qq[<a href="$URL_TO_QUERY_PROGRAM">Go back</a> to the search engine...<BR><BR>];
&footer({counteronly=>1});
print "<BR>" x 2;
&open_files;

&gather_data;
&report;

&close_files;
&footer({linksonly=>1});

}#endsub Query Stats
###############################################################################

#############################################################################################################################
sub gather_data {
my $key="";
my $line="";
my $value="";
my $chunk="";
my @chunks=();
my $tmpFound="";
my $tmpRequest="";
my $IGNORING=0;

#$DEBUG=5;#print "delimiter is $DELIMITER, sub-delim is $SUB_DELIMITER<BR>";#

while ($line=<LOG>) {
  next if $line !~ /^REQ$SUB_DELIMITER/;
  chomp $line;
  if ($DEBUG>=7) { print "line=$line<BR>"; }

  ##### Clear temp values so they don't accidentally get reused:
  $tmpFound="";
  $tmpRequest="";
  $IGNORING=0;

  @chunks=split(/$DELIMITER/,"$line");
  foreach $chunk (sort @chunks) {	#IP, then M, then REQ
	($key,$value)=split(/$SUB_DELIMITER/,"$chunk");

	if ($DEBUG>=5) { print "<BR>checking key=$key,value=$value\n"; }
	
	##### It's important that any values that are NOT dependent of Ip address
	##### are gathered before we look at the IP address next, because of the
	##### horrible 'goto' statement:

	## check if we should ignore this IP or not
	if ($key eq "IP") {
		$IGNORING=0;
		foreach $ip (@IGNOREIP) {
			if ($value eq $ip) { 
				$IGNORING=1; 
				#DON'T DO THIS ANYMORE!: goto IGNORETHISLINE; 
			}
		}#endforeach
	}#endif


	## keep track of the results found:
	if ($key eq "F") {
		$tmpFound=$value;
		#DEBUG: if (1) { print "<BR>found key of \"F\"... key=$key,value=$value,tmpFound=$tmpFound"; }
	}#endif

	## add the request (ie what we are looking for) to the appropriate hashes:
	if ($key eq "REQ") {
		if ($value eq "") { goto IGNORETHISLINE; }
		$tmpRequest=$value;
		if (!$IGNORING) { 
			$REQUEST_COUNTS{$value}++; 
			if ($DEBUG>=6) { print qq[<BR>Upping \$REQUEST_COUNTS{$value} to "$REQUEST_COUNTS{$value}"<BR>]; }
		}#debug
	}#endif   
 
  }#endforeach

  ##### Keep fact of the track that a request WAS made (made==processed, actually):
  if (!$IGNORING) { $requestsMade++; }


  ##### We want to keep track of the # found, for accuracy's sake, EVEN IF we are 
  ##### ignoring the current line (ie simpsons found is 50 for the last random person
  ##### search, but 500 now and clint has searched since, well if someone is ignoring
  ##### clint's searches it'll say only 50 simpsons were found unless we go out of our
  ##### way to NOT ignore those lines...

  ##### Keep track of how many we found:
  if ($tmpFound > $FOUND{$tmpRequest}) {
	$FOUND{$tmpRequest}=$tmpFound;
	#DEBUG:	if(1){print qq[<BR>setting \$FOUND{$tmpRequest}="$tmpFound";\n];}#
  }#endif

  IGNORETHISLINE:			#do not erase this line!


}#endwhile
}#endsub gather_data
#############################################################################################################################

#############################################################################################
sub report {
my $elem="";
my @keys=();
my %COUNTS=();
my @elements=();
my $key,$value=("","");
my $searchfor,$count=("","");

foreach $key (sort keys %REQUEST_COUNTS) {
	$value=$REQUEST_COUNTS{$key};
	push(@{$COUNTS{$value}},"$key"."::::"."$value");

	if ($DEBUG>=3) { print "key=$key,value=$REQUEST_COUNTS{$key}<BR>"; }
	if ($DEBUG>=3) { print qq[push(\@{\$COUNTS{$value}},"$key::::$value");<BR>\n]; }
}#endforeach

my $reverse=$FORM{reverse};

if ($FORM{type} eq "count") { $reverse=!$reverse; }

#For the sort situation, $a and $b would be what they are sorting for, and
#$FOUND{$a} and $FOUND{$b} would be how many results that we found doing so,
#and $REQUEST_COUNTS{$a} and {$b} be how many times it was searched for...

if ($FORM{type} eq "search") {
	#@keys=sort { $REQUEST_COUNTS{$a} cmp $REQUEST_COUNTS{$b} } keys %REQUEST_COUNTS;
	#@keys=sort keys %REQUEST_COUNTS;
	my ($lca,$lcb);
	@keys=sort {
		$lca=lc($a); $lcb=lc($b);
		
		#DEBUG: print "[FA $FOUND{$a},$REQUEST_COUNTS{$a} $a / FB $FOUND{$b},$REQUEST_COUNTS{$b} $b]<BR>";
		if ($lca ne $lcb) {
			#a is first because this is the criterion and it should be alphabetized
			return($lca cmp $lcb);
		} elsif ($FOUND{$b} ne $FOUND{$a}) {
			return($FOUND{$b}<=>$FOUND{$a});
		} elsif ($REQUEST_COUNTS{$b} ne $REQUEST_COUNTS{$a}) {
			return($REQUEST_COUNTS{$b} <=> $REQUEST_COUNTS{$a});
		}#endif
	} keys %REQUEST_COUNTS;
} elsif ($FORM{type} eq "results") {
	@keys=sort {
		#DEBUG: print "[FA $FOUND{$a},$REQUEST_COUNTS{$a} $a / FB $FOUND{$b},$REQUEST_COUNTS{$b} $b] ";
		if ($FOUND{$b} ne $FOUND{$a}) {
			return($FOUND{$b}<=>$FOUND{$a});
		} elsif ($REQUEST_COUNTS{$b} ne $REQUEST_COUNTS{$a}) {
			return($REQUEST_COUNTS{$b} <=> $REQUEST_COUNTS{$a});
		} else {
			#a is first because this is the criterion and it should be alphabetized
			return(lc($a) cmp lc($b));
		}#endif
	} keys %REQUEST_COUNTS;
} else {
	#FORM{type} eq "count"
	#@keys=sort { $a<=>$b } keys %COUNTS;
	@keys=sort {
		if ($REQUEST_COUNTS{$b} ne $REQUEST_COUNTS{$a}) {
			return($REQUEST_COUNTS{$a} <=> $REQUEST_COUNTS{$b});
		} elsif ($FOUND{$b} ne $FOUND{$a}) {
			return($FOUND{$a}<=>$FOUND{$b});
		} else {
			#a is first because this is the criterion and it should be alphabetized
			return(lc($b) cmp lc($a));
		}#endif
	} keys %REQUEST_COUNTS;
}#endif

if ($reverse) { @keys=reverse @keys; }

#DEBUG: print @keys."keys are @keys<BR>\n";#

my $numCols=7;
print qq[<table cellpadding=0 cellspacing=0 border=0 align=center>\n];
		print qq[<TR valign="middle" bgcolor=dddddd>];
		print qq[<td colspan=$numCols align=center>];
		print qq[<font size=5><B>Top Searches</B></font><BR><BR>];
		print "[";
		print qq[<a href="$URL_TO_THIS_PROGRAM?includeclint=];
		print $FORM{includeclint}?"0":"1";
		print qq[&type=$FORM{type}&reverse=$FORM{reverse}">];
		if ($FORM{includeclint}) { print  "ignore searches performed by Clint"; } 
		else 		         { print "include searches performed by Clint"; }
		print qq[</a>];
		print "]";
		print qq[<BR>];
		print qq[<br>];
		print qq[</td>];
		print qq[</tr>];


		print qq[<TR valign="middle" bgcolor=dddddd>];
		print qq[<TD align="center">];
		print qq[<a href="$URL_TO_THIS_PROGRAM?];
		if ($FORM{type} eq "search") {
			print qq[reverse=$FORM{reversent}];
		}#endif
		print qq[&includeclint=$FORM{includeclint}];
		print qq[&type=search">];
		print "<B>search</B></a>";
		print qq[</TD>];

		print qq[<td width=30></td>];

		print qq[<TD align="center">];
		print qq[<a href="$URL_TO_THIS_PROGRAM?];
		if ($FORM{type} eq "count") {
			print qq[reverse=$FORM{reversent}];
		}#endif
		print qq[&includeclint=$FORM{includeclint}];
		print qq[&type=count">];
		print "<B># of searches</B></a>";
		print qq[</TD>];

		print qq[<td width=30></td>];

		print qq[<TD align="center">];
		print qq[<a href="$URL_TO_THIS_PROGRAM?];
		if ($FORM{type} eq "results") {
			print qq[reverse=$FORM{reversent}];
		}#endif
		print qq[&includeclint=$FORM{includeclint}];
		print qq[&type=results">];
		print "<B># of results</B></a>";
		print qq[</TD>];

		print qq[<td width=30></td>];

		print qq[<TD align="center"><a href="$URL_TO_THIS_PROGRAM?];
		print qq[$ENV{QUERY_STRING}];
		print "\"><B>view<br>results</B></a>";
		print qq[</TD>];

		print qq[</TR>];
my $j=0;
my $bgcolor="";

if ($FORM{type} eq "countGOAT") {	#this if block is obsolete!
	foreach $key (@keys) {
		#DEBUG:print "key[1] is $key<BR>";#
		@elements=@{$COUNTS{$key}};
		foreach $elem (@elements) {
			($searchfor,$count)=split(/::::/,"$elem");

			$bgcolor=$j++%2?"EEEEEE":"FFFFFF";
			print qq[<TR valign="middle" bgcolor="$bgcolor">];
			print qq[<TD align="center">];
			print $searchfor;
			print qq[</TD>];

			print qq[<td>&nbsp;</td>];

			print qq[<TD align="center">];
			print $count;
			print qq[</TD>];	

			print qq[<td>&nbsp;</td>];

			print &numResults($searchfor);

			print qq[<td>&nbsp;</td>];

			print qq[<TD align="center">];
			print "[";
			#DEBUG: print "searchfor is $searchfor, urlencode($searchfor) is \"".&urlencode($searchfor)."\"<br>\n";
			my $mode=search;
			my $tmpsearchfor=&urlencode($searchfor);
			print qq[<a href="$URL_TO_QUERY_PROGRAM?mode=search&searchfor=];
			print qq[$tmpsearchfor">view</a>];
			print "]" . qq[</TD></TR>];
			print qq[</TR>];
		}#endif
	}#endif
} else {	#if $FORM{type} eq "search" or "results"
	foreach $key (@keys) {
		#DEBUG: print "key is $key<BR>";#

		$bgcolor=$j++%2?"EEEEEE":"FFFFFF";
		print qq[<TR valign="middle" bgcolor="$bgcolor">];
		print qq[<TD align="center">];
		print $key;
		print qq[</TD>];

		print qq[<td>&nbsp;</td>];

		print qq[<TD align="center">];
		print $REQUEST_COUNTS{$key};
		print qq[</TD>];

		print qq[<td>&nbsp;</td>];

		print &numResults($key);

		print qq[<td>&nbsp;</td>];

			print qq[<TD align="center">];
			print "[";
			#DEBUG: print "searchfor is $searchfor, urlencode($searchfor) is \"".&urlencode($searchfor)."\"<br>\n";
			my $mode=search;
			my $tmpsearchfor=&urlencode($key);
			print qq[<a href="$URL_TO_QUERY_PROGRAM?mode=search&searchfor=];
			print qq[$tmpsearchfor">];
			print "view";
			print qq[</a>];
			print "]";
			print qq[</TD>];
			print qq[</TR>];

		print qq[</TR>];
	}#endif
}#endif

print qq[<tr valign=middle bgcolor=dddddd><td align=center colspan=$numCols><BR>];
print qq[<font size=4 face="arial"><B>*** $requestsMade total requests ***</b></font><BR><BR>];
print qq[</td></tr>];
print qq[</table>\n];

}#endsub report
#############################################################################################

################################################################################################
sub numResults {
my $key = $_[0];
my $ZEROTEXT="";	#text to display if no results, ie the number '0' or 'none' or &nbsp;

	#$ZEROTEXT="<font size=2>(none)</font>";
	#$ZEROTEXT="<font size=1 face=arial>NONE</font>";
	#$ZEROTEXT="<font size=2><i>0</i></font>";
	#$ZEROTEXT="&nbsp;";
	$ZEROTEXT="0";

	my $html="";
	$html .= qq[<TD align="center"><font size=3>];
	#DEBUG: $html .= qq[key=$key];

	if ($FOUND{$key} ne "") { $html .= $FOUND{$key}; } 
	else			{ $html .= $ZEROTEXT;    }

	$html .= qq[</font></TD>\n];

return($html);
}#endsub numResults
################################################################################################

############################################################################
sub open_files {
open(LOG,$LOG_FILE)||die("Could not open log file $LOG_FILE !!!");
}#endsub open_files
############################################################################

#############################
sub close_files {
close(LOG);
}#endsub close_files
#############################
