Display useful site statistics - Combine Site summary and Site statistics

xstatistics is a contributed drupal module that provides useful statistics to user. There are two block that give the site statistics.

I found it useful to combine those two blocks into one and provide all the Site Statistics in one place.

Create a new block and paste the code below, select input format as "php"


<?php

$res = db_query('SELECT totalcount, daycount FROM {node_counter}');
while($count = db_fetch_object($res)) {
$count_nodes_view_tot = $count_nodes_view_tot + $count->totalcount;
$count_nodes_view_day = $count_nodes_view_day + $count->daycount;
}

$count_referrer_ext_day = db_fetch_array(db_query("SELECT COUNT(DISTINCT(url)) AS referrers FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%%%s%%' AND timestamp >= %d", $_SERVER['HTTP_HOST'], (time()-86400)));

$count_referrer_int_day = db_fetch_array(db_query("SELECT COUNT(DISTINCT(url)) AS referrers FROM {accesslog} WHERE url <> '' AND url LIKE '%%%s%%' AND timestamp >= %d", $_SERVER['HTTP_HOST'], (time()-86400)));

$count_hits_day = db_fetch_array(db_query('SELECT COUNT(aid) AS hits FROM {accesslog} WHERE timestamp >= %d', (time()-86400)));

$count_pageviews_day = db_fetch_array(db_query('SELECT COUNT(path) AS hits FROM {accesslog} WHERE timestamp >= %d', (time()-86400)));

$count_RSS_subscr = db_fetch_array(db_query("SELECT COUNT(DISTINCT(hostname)) AS hostname FROM {accesslog} WHERE path LIKE '%/feed' OR path LIKE 'rss.xml'"));

$count_nodes_tot = db_fetch_array(db_query('SELECT COUNT(nid) FROM {node}'));
$count_nodes_pub = db_fetch_array(db_query('SELECT COUNT(nid) FROM {node} WHERE status=1'));
$count_nodes_que = db_fetch_array(db_query('SELECT COUNT(nid) FROM {node} WHERE moderate=1'));

//prepare amount of users
$count_users_tot = db_fetch_array(db_query('SELECT COUNT(uid) FROM {users}'));

//prepare amount of comments
$count_comments_tot = db_fetch_array(db_query('SELECT COUNT(cid) FROM {comments} WHERE status=0'));

$header = array('description', 'value');
$rows = array(
array(t('Members'), $count_users_tot['COUNT(uid)']-1),
array(t('Posts'), $count_nodes_tot['COUNT(nid)']),
array(t('Comments'), $count_comments_tot['COUNT(cid)']),
// without -1, it counts anonymous as a user
array(t('Reads today'), $count_nodes_view_day),
array(t('Reads all time'), $count_nodes_view_tot),
array(t('Hits today'), $count_hits_day['hits']),

array(t('External referrers today'), $count_referrer_ext_day['referrers']),
array(t('Internal referrers today'), $count_referrer_int_day['referrers']),

array(t('RSS feed subscribers'), $count_RSS_subscr['hostname'])
);

return theme('table', $header, $rows); ?>