/* b2stats.php
* An addon to B2 which diplays b2 stats
* Demo of this file @ http://www.orientek.net/kore/gamerz/b2stats.php
* Created By Lestern "GamerZ" Cha - http:/www.gamerz.per.sg
* Copyright ® 2002 Lester "GamerZ" Chan. All Rights Reserved.
*
* B2 Created By Michel V - http://www.cafelog.com/
*
* You may modify this file to suit your owns needs, but please keep the copyright intact. Thanks
* Any problems, email me @ lesterch@singnet.com.sg or post it in the B2 forums @ http://tidakada.com/board/
*/
dbconnect();
/** Get Total Number of posts
** Params: None
** Returns: number of posts
*/
function stats_get_totalposts() {
global $tableposts;
$sql = "SELECT COUNT(ID) FROM $tableposts";
$result = mysql_query($sql) or die(mysql_error());
return mysql_result($result, 0);
}
/** Get Total Number of comments
** Params: None
** Returns: number of comments
*/
function stats_get_totalcomments() {
global $tablecomments;
$sql = "SELECT COUNT(DISTINCT comment_ID) FROM $tablecomments";
$result = mysql_query($sql) or die(mysql_error());
return mysql_result($result, 0);
}
/** Get Total Number of unique (by comment_author) commenters
** It excludes trackbacks and pingback
** Params: None
** Returns: number of commenters
*/
function stats_get_totalcommentposters() {
global $tablecomments;
$sql = "SELECT COUNT(DISTINCT comment_author) FROM $tablecomments" .
" WHERE substring(comment_content,1,12) <> '" .mysql_escape_string('')."' " .
" AND substring(comment_content,1,13) <> '" . mysql_escape_string('')."' " ;
$result = mysql_query($sql) or die(mysql_error());
return mysql_result($result, 0);
}
/** Get n most commented posts
** outputs the results directly into the page it's called from.
** It excludes trackbacks and pingback
** Params: num (default 5) - Number of posts to get
** before (default '') - text/html to output before each item
** after (default "
\n") - text/html to output after each item
*/
function stats_get_mostcommented($num = 10, $before='
', $after="\n") {
global $siteurl, $blogfilename, $tableposts, $tablecomments;
$sql =
"SELECT ID, post_title, COUNT($tablecomments.comment_post_ID) AS 'commentstotal' " .
" FROM $tableposts LEFT JOIN $tablecomments ON $tableposts.ID=$tablecomments.comment_post_ID " .
" WHERE substring(comment_content,1,12) <> '" .mysql_escape_string('')."' " .
" AND substring(comment_content,1,13) <> '" . mysql_escape_string('')."' " .
" GROUP BY $tablecomments.comment_post_ID ORDER BY commentstotal DESC LIMIT $num";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_row($result)) {
echo $before.''.stripslashes($row[1]).' (+'.stripslashes($row[2]).')'.$after;
}
}
/** Get n latest posts
** outputs the results directly into the page it's called from.
** Params: num (default 5) - Number of posts to get
** before (default '') - text/html to output before each item
** after (default "
\n") - text/html to output after each item
*/
function stats_get_lastestposts($num = 5, $before='', $after="
\n") {
global $siteurl, $blogfilename, $tableposts;
$sql = "SELECT ID, post_title FROM $tableposts ORDER BY post_date DESC LIMIT $num";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_row($result)) {
echo $before.''.stripslashes($row[1]).''.$after;
}
}
/** Get n latest comments
** outputs the results directly into the page it's called from.
** It excludes trackbacks and pingback
** Params: num (default 5) - Number of comments to get
** before (default '') - text/html to output before each item
** after (default "
\n") - text/html to output after each item
*/
function stats_get_lastestcomments($num = 5, $before='', $after="\n") {
global $siteurl, $blogfilename, $tableposts, $tablecomments;
$sql =
"SELECT ID, post_title, comment_author, comment_date " .
" FROM $tableposts LEFT JOIN $tablecomments ON $tableposts.ID=$tablecomments.comment_post_ID " .
" WHERE substring(comment_content,1,12) <> '" .mysql_escape_string('')."' " .
" AND substring(comment_content,1,13) <> '" . mysql_escape_string('')."' " .
" ORDER BY $tablecomments.comment_date DESC LIMIT $num";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_row($result)) {
echo $before.''.stripslashes($row[1]).' ('.stripslashes($row[2]).')'.$after;
}
}
/** Get n most prolific commenters
** outputs the results directly into the page it's called from.
** It excludes trackbacks and pingback
** Params: num (default -1) - Number of commenters to get. -1 means get all of them
** include_pos (default true) - whether to include position
** author_link (default true) - whether to make the author's name into a link back to this page
** before (default '') - text/html to output before each item
** middle1 (default ' - ') - text/html to output after position before comment_author
** (only used if include_position is true
** middle2 (default ' - ') - text/html to output after comment_author before total
** after (default "
\n") - text/html to output after each item
*/
function stats_get_membercommentstats($num = -1, $include_pos=true, $author_link=true, $before='', $middle1=' - ',
$middle2=' - ', $after="
\n") {
global $siteurl, $blogfilename, $tableposts, $tablecomments;
$sql =
"SELECT comment_author, COUNT(*) AS 'total' FROM $tablecomments " .
" WHERE substring(comment_content,1,12) <> '" .mysql_escape_string('')."' " .
" AND substring(comment_content,1,13) <> '" . mysql_escape_string('')."' " .
" GROUP BY comment_author ORDER BY total DESC";
if (($num != -1) && ($num > 0)) {
$sql .= " LIMIT $num";
}
$result = mysql_query($sql) or die(mysql_error()."
\n".$sql);
$i = 1;
while ($row = mysql_fetch_row($result)) {
echo $before;
if ($include_pos) {
echo $i. $middle1;
}
if ($author_link) {
echo "";
}
echo $row[0];
if ($author_link) {
echo '';
}
echo $middle2; // holds the name
echo $row[1].$after; // holds the number of comments
$i++;
}
}
/** Get category stats
** outputs the results directly into the page it's called from.
** Params: order (default 'T') - Order results by reverse total (most first).
** Other values, 'A' alphabetical, and 'N' category_id order
** before (default '') - text/html to output before each item
** between (default ' - ') - text/html to output after category name before total
** after (default "
\n") - text/html to output after each item
*/
function stats_get_categorystats($order='T', $before='', $between=' - ', $after="
\n") {
global $siteurl, $blogfilename, $tableposts, $tablecategories;
$sql =
"SELECT cat_name , COUNT(*) AS 'total' " .
" FROM $tableposts LEFT JOIN $tablecategories ON $tableposts.post_category = $tablecategories.cat_ID " .
" GROUP BY $tableposts.post_category ";
if ($order == 'T') {
$sql .= " ORDER BY total DESC";
}
else if ($order == 'A') {
$sql .= " ORDER BY $tablecategories.cat_name ASC";
}
else if ($order == 'N') {
$sql .= " ORDER BY $tablecategories.cat_ID ASC";
}
$result = mysql_query($sql) or die(mysql_error()."
\n".$sql);
while ($row = mysql_fetch_row($result)) {
echo $before.$row[0].$between.$row[1].$after;
}
}
// Check $user isset
if(!isset($user) || $user == "") {
// Output General Stats
echo "\n";
echo "- ".stats_get_totalposts()." posts
\n";
echo "- ".stats_get_totalcomments()." comments
\n";
echo "- ".stats_get_totalcommentposters()." people are represented in the comments
\n";
echo "
";
// Latest 10 Blog Entries
echo "Latest 10 News Entries\n";
stats_get_lastestposts(10, '- ', "
\n");
echo "
";
// Latest 10 Comments
echo "Latest 10 Comments\n";
stats_get_lastestcomments(10, '- ', "
\n");
echo "
";
// Top 10 Most Commented Blog
echo "10 Most Commented Articles\n";
stats_get_mostcommented(10, '- ', "
\n");
echo "
";
// Comments' Members Stats
echo "Comments' Members Stats\n";
stats_get_membercommentstats(-1, true, true, "- \n", ". - ", " (+", ")
\n");
echo "
\n";
// Categories Stats
echo "Categories Stats";
stats_get_categorystats('T', "- ", " (", " posts)
\n");
echo "
\n";
}
else {
// ------------ If $user set ------------ //
// SNIP... Same as previous version
} // end else user set
?>