diff --git a/php/.htaccess b/php/.htaccess
index fa6de1d..6de0dd3 100644
--- a/php/.htaccess
+++ b/php/.htaccess
@@ -5,4 +5,5 @@
Order Allow,Deny
Deny from all
-
\ No newline at end of file
+
+AcceptPathInfo Off
diff --git a/php/includes/storage.php b/php/includes/storage.php
index cf6fede..c671f0b 100644
--- a/php/includes/storage.php
+++ b/php/includes/storage.php
@@ -147,15 +147,29 @@
return $result;
}
- public function getDayTopScores() {
+ public function getDayTopScores($page=1, $max=10) {
if(!$this->isConnected()) {
throw new StorageConnectionException();
}
+ if($page < 1) {
+ $page = 1;
+ }
+ if($max < 1) {
+ $max = 1;
+ }
$day = date('j');
$month = date('m');
$year = date('Y');
- $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE day=:day AND year=:year AND month=:month GROUP BY username_canonical ORDER BY points DESC LIMIT 10");
- $q->execute(array(':day'=>$day, ':year'=>$year, ':month'=>$month));
+ $q = $this->db->prepare("SELECT username,
+ sum(points_made) as points
+ FROM triviauserlog
+ WHERE day=:day
+ AND year=:year
+ AND month=:month
+ GROUP BY username_canonical
+ ORDER BY points DESC
+ LIMIT :offset, :maxResults");
+ $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':day'=>$day, ':year'=>$year, ':month'=>$month));
if ($q === false) {
throw new StorageSchemaException();
}
@@ -163,10 +177,27 @@
return $result;
}
- public function getWeekTopScores() {
+ public function getCountDayTopScores() {
if(!$this->isConnected()) {
throw new StorageConnectionException();
}
+ $day = date('j');
+ $month = date('m');
+ $year = date('Y');
+ $q = $this->db->prepare('SELECT count(distinct(username_canonical))
+ FROM triviauserlog
+ WHERE day=:day
+ AND year=:year
+ AND month=:month');
+ $q->execute(array(':day'=>$day, ':year'=>$year, ':month'=>$month));
+ if ($q === false) {
+ throw new StorageSchemaException();
+ }
+ $result = $q->fetchColumn();
+ return $result;
+ }
+
+ protected function generateWeekSqlClause() {
$sqlClause = '';
$day = date('N')-1;
$week = new DateTime();
@@ -183,7 +214,22 @@
')';
$week->add($interval);
}
- $q = $this->db->query("SELECT username, sum(points_made) as points FROM triviauserlog WHERE $sqlClause GROUP BY username_canonical ORDER BY points DESC LIMIT 10");
+ return $sqlClause;
+ }
+
+ public function getWeekTopScores($page=1, $max=10) {
+ if(!$this->isConnected()) {
+ throw new StorageConnectionException();
+ }
+ if($page < 1) {
+ $page = 1;
+ }
+ if($max < 1) {
+ $max = 1;
+ }
+ $sqlClause = $this->generateWeekSqlClause();
+ $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE $sqlClause GROUP BY username_canonical ORDER BY points DESC LIMIT :offset, :maxResults");
+ $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max));
if ($q === false) {
throw new StorageSchemaException();
}
@@ -191,32 +237,93 @@
return $result;
}
- public function getMonthTopScores() {
+ public function getCountWeekTopScores() {
+ if(!$this->isConnected()) {
+ throw new StorageConnectionException();
+ }
+ $sqlClause = $this->generateWeekSqlClause();
+ $q = $this->db->query('SELECT count(distinct(username_canonical))
+ FROM triviauserlog
+ WHERE $sqlClause');
+ if ($q === false) {
+ throw new StorageSchemaException();
+ }
+ $result = $q->fetchColumn();
+ return $result;
+ }
+
+ public function getMonthTopScores($page=1, $max=10) {
+ if(!$this->isConnected()) {
+ throw new StorageConnectionException();
+ }
+ if($page < 1) {
+ $page = 1;
+ }
+ if($max < 1) {
+ $max = 1;
+ }
+ $month = date('m');
+ $year = date('Y');
+ $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE year=:year AND month=:month GROUP BY username_canonical ORDER BY points DESC LIMIT :offset, :maxResults");
+ $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':year'=>$year, ':month'=>$month));
+ if ($q === false) {
+ throw new StorageSchemaException();
+ }
+ $result = $q->fetchAll();
+ return $result;
+ }
+
+ public function getCountMonthTopScores() {
if(!$this->isConnected()) {
throw new StorageConnectionException();
}
$month = date('m');
$year = date('Y');
- $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE year=:year AND month=:month GROUP BY username_canonical ORDER BY points DESC LIMIT 10");
+ $q = $this->db->prepare('SELECT count(distinct(username_canonical))
+ FROM triviauserlog
+ WHERE year=:year
+ AND month=:month');
$q->execute(array(':year'=>$year, ':month'=>$month));
if ($q === false) {
throw new StorageSchemaException();
}
+ $result = $q->fetchColumn();
+ return $result;
+ }
+
+ public function getYearTopScores($page=1, $max=10) {
+ if(!$this->isConnected()) {
+ throw new StorageConnectionException();
+ }
+ if($page < 1) {
+ $page = 1;
+ }
+ if($max < 1) {
+ $max = 1;
+ }
+ $year = date('Y');
+ $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE year=:year GROUP BY username_canonical ORDER BY points DESC LIMIT :offset, :maxResults");
+ $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':year'=>$year));
+ if ($q === false) {
+ throw new StorageSchemaException();
+ }
$result = $q->fetchAll();
return $result;
}
- public function getYearTopScores() {
+ public function getCountYearTopScores() {
if(!$this->isConnected()) {
throw new StorageConnectionException();
}
$year = date('Y');
- $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE year=:year GROUP BY username_canonical ORDER BY points DESC LIMIT 10");
+ $q = $this->db->prepare('SELECT count(distinct(username_canonical))
+ FROM triviauserlog
+ WHERE year=:year');
$q->execute(array(':year'=>$year));
if ($q === false) {
throw new StorageSchemaException();
}
- $result = $q->fetchAll();
+ $result = $q->fetchColumn();
return $result;
}
@@ -239,7 +346,8 @@
group by tl.username_canonical
limit :offset, :maxResults
');
- $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':username'=>'%'.$usernameCanonical.'%'));
+ $likeString = '%'.$this->escapeLikeQuery($usernameCanonical).'%';
+ $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':username'=>$likeString));
if ($q === false) {
throw new StorageSchemaException();
}
@@ -256,7 +364,8 @@
from triviauserlog tl
where tl.username_canonical like :username
');
- $q->execute(array(':username'=>'%'.$usernameCanonical.'%'));
+ $likeString = '%'.$this->escapeLikeQuery($usernameCanonical).'%';
+ $q->execute(array(':username'=>$likeString));
if ($q === false) {
throw new StorageSchemaException();
}
@@ -305,12 +414,17 @@
return $result;
}
- private function isConnected() {
+ public function isConnected() {
if(is_null($this->db)) {
return false;
}
return true;
}
+
+ protected function escapeLikeQuery($s) {
+ $translations = array("%"=>"\\%", "_"=>"\\_");
+ return strtr($s, $translations);
+ }
}
class StorageException extends Exception { }
diff --git a/php/stats.php b/php/stats.php
index b02ec52..620e394 100644
--- a/php/stats.php
+++ b/php/stats.php
@@ -58,7 +58,7 @@ try {
getDayTopScores();
+ $result = $storage->getDayTopScores(1, 10);
} catch(StorageSchemaException $e) {
echo "
Error: Database schema is not queryable
";
} catch(StorageConnectionException $e) {
@@ -85,6 +85,7 @@ try {
?>
+ View all
@@ -92,7 +93,7 @@ try {
getWeekTopScores();
+ $result = $storage->getWeekTopScores(1, 10);
} catch(StorageSchemaException $e) {
echo "
Error: Database schema is not queryable
";
} catch(StorageConnectionException $e) {
@@ -119,6 +120,7 @@ try {
?>
+
View all
@@ -127,7 +129,7 @@ try {
getMonthTopScores();
+ $result = $storage->getMonthTopScores(1, 10);
} catch(StorageSchemaException $e) {
echo "
Error: Database schema is not queryable
";
} catch(StorageConnectionException $e) {
@@ -154,13 +156,14 @@ try {
?>
+
View all
Year Top Scores
getYearTopScores();
+ $result = $storage->getYearTopScores(1, 10);
} catch(StorageSchemaException $e) {
echo "
Error: Database schema is not queryable
";
} catch(StorageConnectionException $e) {
@@ -188,6 +191,7 @@ try {
?>
+
View all
diff --git a/php/top.php b/php/top.php
new file mode 100644
index 0000000..28c5164
--- /dev/null
+++ b/php/top.php
@@ -0,0 +1,173 @@
+
+
+'Day', 'w'=>'Week', 'm'=>'Month', 'y'=>'Year');
+$timespan = 'd';
+$timeDesc = 'Day';
+if(array_key_exists('t', $_GET)) {
+ if(array_key_exists(strtolower($_GET['t']), $timespans)) {
+ $timespan = strtolower($_GET['t']);
+ $timeDesc = $timespans[$timespan];
+ }
+}
+
+if(array_key_exists('page', $_GET)) {
+ $page = $_GET['page'];
+}
+if(!isset($page)) {
+ $page = 1;
+}
+if($page < 1) {
+ $page = 1;
+}
+
+$maxResults = 20;
+
+function replaceTimespanVariable($t) {
+ $pathInfo = parse_url($_SERVER['REQUEST_URI']);
+ if(array_key_exists('query', $pathInfo)) {
+ $queryString = $pathInfo['query'];
+ } else {
+ $queryString = '';
+ }
+ parse_str($queryString, $queryArray);
+ $queryArray['t'] = $t;
+ if($t == 'd') {
+ unset($queryArray['t']);
+ }
+ $queryArray['page'] = 1;
+ unset($queryArray['page']);
+ $queryString = http_build_query($queryArray);
+ $new = $pathInfo['path'];
+ if($queryString != ''){
+ $new .= '?' . $queryString;
+ }
+ return $new;
+}
+
+?>
+
+
+ Top Scores for · TriviaTime
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Top Scores
+
Player rankings.
+
+
+
+
+
Top Scores for
+ getWeekTopScores($page, $maxResults);
+ $resultCount = $storage->getCountWeekTopScores();
+ } else if ($timespan == 'm') {
+ $result = $storage->getMonthTopScores($page, $maxResults);
+ $resultCount = $storage->getCountMonthTopScores();
+ } else if ($timespan == 'y') {
+ $result = $storage->getYearTopScores($page, $maxResults);
+ $resultCount = $storage->getCountYearTopScores();
+ } else {
+ $result = $storage->getDayTopScores($page, $maxResults);
+ $resultCount = $storage->getCountDayTopScores();
+ }
+ } catch(StorageSchemaException $e) {
+ echo "
Error: Database schema is not queryable
";
+ } catch(StorageConnectionException $e) {
+ echo "
Error: Database is not available
";
+ }
+ $storage->close();
+ ?>
+
+
+
+ | # |
+ Username |
+ Score |
+
+
+
+ ';
+ echo '' . $currentRank . ' | ';
+ echo '' . $res['username'] . ' | ';
+ echo '' . number_format($res['points'],0) . ' | ';
+ echo '';
+ $currentRank++;
+ }
+ ?>
+
+
+ paginate();
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+