From c8c1cf02a3ccc4dfb25e3e655f8f8c999f91c656 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 30 Nov 2013 04:29:14 -0800 Subject: [PATCH 1/7] Adding paginated top.php top scores --- php/includes/storage.php | 140 +++++++++++++++++++++++++++++++++++---- php/stats.php | 12 ++-- 2 files changed, 135 insertions(+), 17 deletions(-) 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

From 520784ae28be9e5dca94441a09cd2c29613acc1d Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 30 Nov 2013 04:30:08 -0800 Subject: [PATCH 2/7] Adding top.php --- php/top.php | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 php/top.php diff --git a/php/top.php b/php/top.php new file mode 100644 index 0000000..1ee463d --- /dev/null +++ b/php/top.php @@ -0,0 +1,175 @@ + + +'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 <?php echo $timeDesc; ?> · 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
"; + $result = array(); + $result[0] = array('round_num'=>'', 'channel'=>'', 'question'=>'', 'line_num'=>''); + } catch(StorageConnectionException $e) { + echo "
Error: Database is not available
"; + $result = array(); + $result[0] = array('round_num'=>'', 'channel'=>'', 'question'=>'', 'line_num'=>''); + } + $storage->close(); + ?> + + + + + + + + + + '; + echo ''; + echo ''; + echo ''; + echo ''; + $currentRank++; + } + ?> + +
#UsernameScore
' . $currentRank . '' . $res['username'] . '' . $res['points'] . '
+ paginate(); + ?> +
+
+ + +
+ + + + + + From fe7490fe5bbaabcad5dab3e459d81dcde24654a8 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 30 Nov 2013 04:40:24 -0800 Subject: [PATCH 3/7] Fixing empty result array on error --- php/top.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/php/top.php b/php/top.php index 1ee463d..42ac8c9 100644 --- a/php/top.php +++ b/php/top.php @@ -109,6 +109,7 @@ function replaceTimespanVariable($t) {

Top Scores for

getWeekTopScores($page, $maxResults); @@ -125,12 +126,8 @@ function replaceTimespanVariable($t) { } } catch(StorageSchemaException $e) { echo "
Error: Database schema is not queryable
"; - $result = array(); - $result[0] = array('round_num'=>'', 'channel'=>'', 'question'=>'', 'line_num'=>''); } catch(StorageConnectionException $e) { echo "
Error: Database is not available
"; - $result = array(); - $result[0] = array('round_num'=>'', 'channel'=>'', 'question'=>'', 'line_num'=>''); } $storage->close(); ?> From 9f1a967b8ca6711e698aa7329c15e79cdaddeb5a Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 30 Nov 2013 04:44:49 -0800 Subject: [PATCH 4/7] Linking to profile --- php/top.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/top.php b/php/top.php index 42ac8c9..fa55387 100644 --- a/php/top.php +++ b/php/top.php @@ -145,7 +145,7 @@ function replaceTimespanVariable($t) { foreach($result as $res) { echo ''; echo '' . $currentRank . ''; - echo '' . $res['username'] . ''; + echo '' . $res['username'] . ''; echo '' . $res['points'] . ''; echo ''; $currentRank++; From a94f6563b6c88f87b4a76f952401a22c300cce3d Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 30 Nov 2013 07:24:27 -0800 Subject: [PATCH 5/7] Turn off AcceptPathInfo --- php/.htaccess | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From ef67517c2f7d2b22702a53d378de4ca3bca710d8 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 30 Nov 2013 07:36:01 -0800 Subject: [PATCH 6/7] Shrinking div for tops --- php/top.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php/top.php b/php/top.php index fa55387..fe0d19e 100644 --- a/php/top.php +++ b/php/top.php @@ -105,7 +105,7 @@ function replaceTimespanVariable($t) {
-
+

Top Scores for

paginate(); ?>
+