import_names("F"); # Makes all form values available as F::.
# $F::debug = 1;
$::db = Mysql->Connect("localhost", "webstats")
|| die "Can't connect to database server";
$::db = $::db; # Make -w shut up.
if ($F::days !~ m/^\d+$/) {
Punt("Invalid number of days specified.");
}
my $enddate = time2str("%Y-%m-%d", time() - 24*60*60); # Yesterday.
my $startdate = time2str("%Y-%m-%d", time() - $F::days * 24*60*60);
my @qstr;
foreach my $pattern (split(/\n/, $F::pages)) {
my $p = trim($pattern);
if ($p eq "") {
next;
}
$p = SqlQuote($p);
if ($F::type eq "exact") {
push(@qstr, "names.name = $p");
} elsif ($F::type eq "substr") {
push(@qstr, "instr(names.name, $p)");
} elsif ($F::type eq "regexp") {
push(@qstr, "names.name regexp $p");
}
}
if (0 == @qstr) {
Punt("No pages were specified.");
}
my $namepart = join(" or ", @qstr);
my $query = Query ("select count(*) from names where $namepart");
my @row = $query->fetchrow();
if ($row[0] > 10) {
Punt("Sorry; you selected $row[0] pages, but I can only graph up to 10.");
}
$query = Query("select names.name, stats.date, stats.count from stats, names where stats.id = names.id and stats.date >= '$startdate' and stats.date <= '$enddate' and ($namepart) order by stats.date, names.name");
my %counts;
my %names;
while (my @row = $query->fetchrow()) {
my ($name, $date, $count) = (@row);
# print "$name $date $count <BR>\n";
if (!exists $counts{$date}) {
$counts{$date} = {};
}
$counts{$date}->{$name} = $count;
$names{$name} = 1;
}
# Go shuffle the data around in the way that the charting package wants.