<?php /* Don't remove this line, it calls the b2 function files ! */ $blog=1; include ("blog.header.php"); ?>
<?
/* 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($result0);
}

/** 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($result0);
}

/** 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('<pingback />')."' " .
" AND substring(comment_content,1,13) <> '" mysql_escape_string('<trackback />')."' " ;
$result mysql_query($sql) or die(mysql_error());
return 
mysql_result($result0);
}

/** 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 "<br />\n") - text/html to output after each item
*/
function stats_get_mostcommented($num 10$before='<li><small>'$after="</small></li>\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('<pingback />')."' " .
" AND substring(comment_content,1,13) <> '" mysql_escape_string('<trackback />')."' " .
" 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.'<a href="'.$blogfilename.'?p='.$row[0].'&amp;c=1&amp;more=1">'.stripslashes($row[1]).'</a> (+'.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 "<br />\n") - text/html to output after each item
*/
function stats_get_lastestposts($num 5$before=''$after="<br />\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.'<a href="'.$blogfilename.'?p='.$row[0].'&amp;c=1&amp;more=1">'.stripslashes($row[1]).'</a>'.$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 "<br />\n") - text/html to output after each item
*/
function stats_get_lastestcomments($num 5$before='<li><small>'$after="</small></li>\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('<pingback />')."' " .
" AND substring(comment_content,1,13) <> '" mysql_escape_string('<trackback />')."' " .
" ORDER BY $tablecomments.comment_date DESC LIMIT $num";
$result mysql_query($sql) or die(mysql_error());
while (
$row mysql_fetch_row($result)) {
echo 
$before.'<a href="'.$blogfilename.'?p='.$row[0].'&amp;c=1&amp;more=1">'.stripslashes($row[1]).'</a> ('.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 "<br />\n") - text/html to output after each item
*/
function stats_get_membercommentstats($num = -1$include_pos=true$author_link=true$before=''$middle1=' - ',
$middle2=' - '$after="<br />\n") {

global 
$siteurl$blogfilename$tableposts$tablecomments;

$sql =
"SELECT comment_author, COUNT(*) AS 'total' FROM $tablecomments " .
" WHERE substring(comment_content,1,12) <> '" .mysql_escape_string('<pingback />')."' " .
" AND substring(comment_content,1,13) <> '" mysql_escape_string('<trackback />')."' " .
" GROUP BY comment_author ORDER BY total DESC";
if ((
$num != -1) && ($num 0)) {
$sql .= " LIMIT $num";
}
$result mysql_query($sql) or die(mysql_error()."<br/>\n".$sql);
$i 1;
while (
$row mysql_fetch_row($result)) {
echo 
$before;
if (
$include_pos) {
echo 
$i$middle1;
}
if (
$author_link) {
echo 
"<a href=\"$siteurl/statistics.php?user=".$row[0]."\">";
}
echo 
$row[0];
if (
$author_link) {
echo 
'</a>';
}
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 "<br />\n") - text/html to output after each item
*/
function stats_get_categorystats($order='T'$before=''$between=' - '$after="<br />\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()."<br/>\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 "<p></p><div class=\"library\"><ul>\n";
echo 
"<li><strong>".stats_get_totalposts()."</strong> posts</li>\n";
echo 
"<li><strong>".stats_get_totalcomments()."</strong> comments</li>\n";
echo 
"<li><strong>".stats_get_totalcommentposters()."</strong> people are represented in the comments</li>\n";
echo 
"</ul></div>";



// Latest 10 Blog Entries
echo "<strong>Latest 10 News Entries</strong><ul>\n";
stats_get_lastestposts(10'<li> '"</li>\n");
echo 
"</ul>";

// Latest 10 Comments
echo "<strong>Latest 10 Comments</strong><ul>\n";
stats_get_lastestcomments(10'<li>'"</li>\n");
echo 
"</ul>";

// Top 10 Most Commented Blog
echo "<strong>10 Most Commented Articles</strong><ul>\n";
stats_get_mostcommented(10'<li> '"</li>\n");
echo 
"</ul>";

// Comments' Members Stats
echo "<strong>Comments' Members Stats</strong><ul>\n";
stats_get_membercommentstats(-1truetrue"<li>\n"". - "" (+"")</li>\n");
echo 
"</ul>\n";



// Categories Stats
echo "<strong>Categories Stats</strong><ul>";

stats_get_categorystats('T'"<li><strong>""</strong> ("" posts)</li>\n");
echo 
"</ul>\n";

}
else {
// ------------ If $user set ------------ //

// SNIP... Same as previous version

// end else user set
?>