2007-08-23 16:34:44 +00:00
< ? php
// $Id$
/**
* @ file
* Admin page callbacks for the statistics module .
*/
/**
* Menu callback ; presents the " recent hits " page .
*/
function statistics_recent_hits () {
$header = array (
array ( 'data' => t ( 'Timestamp' ), 'field' => 'a.timestamp' , 'sort' => 'desc' ),
array ( 'data' => t ( 'Page' ), 'field' => 'a.path' ),
array ( 'data' => t ( 'User' ), 'field' => 'u.name' ),
array ( 'data' => t ( 'Operations' ))
);
2009-04-13 10:40:13 +00:00
$query = db_select ( 'accesslog' , 'a' ) -> extend ( 'PagerDefault' ) -> extend ( 'TableSort' );
$query -> join ( 'users' , 'u' , 'a.uid = u.uid' );
$query
-> fields ( 'a' , array ( 'aid' , 'timestamp' , 'path' , 'title' , 'uid' ))
-> fields ( 'u' , array ( 'name' ))
-> limit ( 30 )
2009-05-22 11:33:18 +00:00
-> orderByHeader ( $header );
2009-04-13 10:40:13 +00:00
$result = $query -> execute ();
2007-08-23 16:34:44 +00:00
$rows = array ();
2009-04-13 10:40:13 +00:00
foreach ( $result as $log ) {
2007-08-23 16:34:44 +00:00
$rows [] = array (
array ( 'data' => format_date ( $log -> timestamp , 'small' ), 'class' => 'nowrap' ),
_statistics_format_item ( $log -> title , $log -> path ),
theme ( 'username' , $log ),
2007-10-20 21:57:50 +00:00
l ( t ( 'details' ), " admin/reports/access/ $log->aid " ));
2007-08-23 16:34:44 +00:00
}
if ( empty ( $rows )) {
$rows [] = array ( array ( 'data' => t ( 'No statistics available.' ), 'colspan' => 4 ));
}
$output = theme ( 'table' , $header , $rows );
2009-04-26 19:44:40 +00:00
$output .= theme ( 'pager' , NULL );
2007-08-23 16:34:44 +00:00
return $output ;
}
/**
* Menu callback ; presents the " top pages " page .
*/
function statistics_top_pages () {
$header = array (
array ( 'data' => t ( 'Hits' ), 'field' => 'hits' , 'sort' => 'desc' ),
array ( 'data' => t ( 'Page' ), 'field' => 'path' ),
array ( 'data' => t ( 'Average page generation time' ), 'field' => 'average_time' ),
array ( 'data' => t ( 'Total page generation time' ), 'field' => 'total_time' )
);
2009-04-13 10:40:13 +00:00
$query = db_select ( 'accesslog' ) -> extend ( 'PagerDefault' ) -> extend ( 'TableSort' );
$query -> addExpression ( 'COUNT(path)' , 'hits' );
// MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list
$query -> addExpression ( 'MAX(title)' , 'title' );
$query -> addExpression ( 'AVG(timer)' , 'average_time' );
$query -> addExpression ( 'SUM(timer)' , 'total_time' );
$query
-> fields ( 'accesslog' , array ( 'path' ))
-> groupBy ( 'path' )
-> limit ( 30 )
2009-05-22 11:33:18 +00:00
-> orderByHeader ( $header );
2009-04-13 10:40:13 +00:00
$count_query = db_select ( 'accesslog' );
$count_query -> addExpression ( 'COUNT(DISTINCT path)' );
$query -> setCountQuery ( $count_query );
$result = $query -> execute ();
2007-08-23 16:34:44 +00:00
$rows = array ();
2009-04-13 10:40:13 +00:00
foreach ( $result as $page ) {
2007-08-23 16:34:44 +00:00
$rows [] = array ( $page -> hits , _statistics_format_item ( $page -> title , $page -> path ), t ( '%time ms' , array ( '%time' => round ( $page -> average_time ))), format_interval ( round ( $page -> total_time / 1000 )));
}
if ( empty ( $rows )) {
$rows [] = array ( array ( 'data' => t ( 'No statistics available.' ), 'colspan' => 4 ));
}
2008-10-13 00:33:05 +00:00
drupal_set_title ( t ( 'Top pages in the past %interval' , array ( '%interval' => format_interval ( variable_get ( 'statistics_flush_accesslog_timer' , 259200 )))), PASS_THROUGH );
2007-08-23 16:34:44 +00:00
$output = theme ( 'table' , $header , $rows );
2009-04-26 19:44:40 +00:00
$output .= theme ( 'pager' , NULL );
2007-08-23 16:34:44 +00:00
return $output ;
}
/**
* Menu callback ; presents the " top visitors " page .
*/
function statistics_top_visitors () {
$header = array (
array ( 'data' => t ( 'Hits' ), 'field' => 'hits' , 'sort' => 'desc' ),
array ( 'data' => t ( 'Visitor' ), 'field' => 'u.name' ),
array ( 'data' => t ( 'Total page generation time' ), 'field' => 'total' ),
2008-05-07 19:17:50 +00:00
array ( 'data' => user_access ( 'block IP addresses' ) ? t ( 'Operations' ) : '' , 'colspan' => 2 ),
2007-08-23 16:34:44 +00:00
);
2009-04-13 10:40:13 +00:00
$query = db_select ( 'accesslog' , 'a' ) -> extend ( 'PagerDefault' ) -> extend ( 'TableSort' );
$query -> leftJoin ( 'blocked_ips' , 'bl' , 'a.hostname = bl.ip' );
$query -> leftJoin ( 'users' , 'u' , 'a.uid = u.uid' );
$query -> addExpression ( 'COUNT(a.uid)' , 'hits' );
$query -> addExpression ( 'SUM(a.timer)' , 'total' );
$query
-> fields ( 'a' , array ( 'uid' , 'hostname' ))
-> fields ( 'u' , array ( 'name' ))
-> fields ( 'bl' , array ( 'iid' ))
-> groupBy ( 'a.hostname' )
-> groupBy ( 'a.uid' )
-> groupBy ( 'u.name' )
-> groupBy ( 'bl.iid' )
-> limit ( 30 )
2009-05-22 11:33:18 +00:00
-> orderByHeader ( $header );
2009-04-13 10:40:13 +00:00
$count_query = db_select ( 'accesslog' );
$count_query -> addExpression ( 'COUNT(DISTINCT CONCAT(CAST(uid AS char), hostname))' );
$query -> setCountQuery ( $count_query );
$result = $query -> execute ();
2007-08-23 16:34:44 +00:00
$rows = array ();
2009-04-13 10:40:13 +00:00
foreach ( $result as $account ) {
2007-08-23 16:34:44 +00:00
$qs = drupal_get_destination ();
2008-05-07 19:17:50 +00:00
$ban_link = $account -> iid ? l ( t ( 'unblock IP address' ), " admin/settings/ip-blocking/delete/ $account->iid " , array ( 'query' => $qs )) : l ( t ( 'block IP address' ), " admin/settings/ip-blocking/ $account->hostname " , array ( 'query' => $qs ));
$rows [] = array ( $account -> hits , ( $account -> uid ? theme ( 'username' , $account ) : $account -> hostname ), format_interval ( round ( $account -> total / 1000 )), ( user_access ( 'block IP addresses' ) && ! $account -> uid ) ? $ban_link : '' );
2007-08-23 16:34:44 +00:00
}
if ( empty ( $rows )) {
$rows [] = array ( array ( 'data' => t ( 'No statistics available.' ), 'colspan' => 4 ));
}
2008-10-13 00:33:05 +00:00
drupal_set_title ( t ( 'Top visitors in the past %interval' , array ( '%interval' => format_interval ( variable_get ( 'statistics_flush_accesslog_timer' , 259200 )))), PASS_THROUGH );
2007-08-23 16:34:44 +00:00
$output = theme ( 'table' , $header , $rows );
2009-04-26 19:44:40 +00:00
$output .= theme ( 'pager' , NULL );
2007-08-23 16:34:44 +00:00
return $output ;
}
/**
* Menu callback ; presents the " referrer " page .
*/
function statistics_top_referrers () {
2008-10-13 00:33:05 +00:00
drupal_set_title ( t ( 'Top referrers in the past %interval' , array ( '%interval' => format_interval ( variable_get ( 'statistics_flush_accesslog_timer' , 259200 )))), PASS_THROUGH );
2007-08-23 16:34:44 +00:00
$header = array (
array ( 'data' => t ( 'Hits' ), 'field' => 'hits' , 'sort' => 'desc' ),
array ( 'data' => t ( 'Url' ), 'field' => 'url' ),
array ( 'data' => t ( 'Last visit' ), 'field' => 'last' ),
);
2009-04-13 10:40:13 +00:00
$query = db_select ( 'accesslog' , 'a' ) -> extend ( 'PagerDefault' ) -> extend ( 'TableSort' );
$query -> addExpression ( 'COUNT(url)' , 'hits' );
$query -> addExpression ( 'MAX(timestamp)' , 'last' );
$query
-> fields ( 'a' , array ( 'url' ))
-> where ( 'LOWER(url) NOT LIKE :host' , array ( ':host' => '%' . $_SERVER [ 'HTTP_HOST' ] . '%' ))
-> condition ( 'url' , '' , '<>' )
-> groupBy ( 'url' )
-> limit ( 30 )
2009-05-22 11:33:18 +00:00
-> orderByHeader ( $header );
2009-04-13 10:40:13 +00:00
$count_query = db_select ( 'accesslog' );
$count_query -> addExpression ( 'COUNT(DISTINCT url)' );
$count_query
-> where ( 'LOWER(url) NOT LIKE :host' , array ( ':host' => '%' . $_SERVER [ 'HTTP_HOST' ] . '%' ))
-> condition ( 'url' , '' , '<>' );
$query -> setCountQuery ( $count_query );
$result = $query -> execute ();
2007-08-23 16:34:44 +00:00
$rows = array ();
2009-04-13 10:40:13 +00:00
foreach ( $result as $referrer ) {
2008-09-17 07:11:59 +00:00
$rows [] = array ( $referrer -> hits , _statistics_link ( $referrer -> url ), t ( '@time ago' , array ( '@time' => format_interval ( REQUEST_TIME - $referrer -> last ))));
2007-08-23 16:34:44 +00:00
}
if ( empty ( $rows )) {
$rows [] = array ( array ( 'data' => t ( 'No statistics available.' ), 'colspan' => 3 ));
}
$output = theme ( 'table' , $header , $rows );
2009-04-26 19:44:40 +00:00
$output .= theme ( 'pager' , NULL );
2007-08-23 16:34:44 +00:00
return $output ;
}
/**
* Menu callback ; Displays recent page accesses .
*/
function statistics_access_log ( $aid ) {
2009-04-13 10:40:13 +00:00
$access = db_query ( 'SELECT a.*, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid WHERE aid = :aid' , array ( ':aid' => $aid )) -> fetch ();
if ( $access ) {
2007-08-23 16:34:44 +00:00
$rows [] = array (
array ( 'data' => t ( 'URL' ), 'header' => TRUE ),
l ( url ( $access -> path , array ( 'absolute' => TRUE )), $access -> path )
);
// It is safe to avoid filtering $access->title through check_plain because
// it comes from drupal_get_title().
$rows [] = array (
array ( 'data' => t ( 'Title' ), 'header' => TRUE ),
$access -> title
);
$rows [] = array (
array ( 'data' => t ( 'Referrer' ), 'header' => TRUE ),
( $access -> url ? l ( $access -> url , $access -> url ) : '' )
);
$rows [] = array (
array ( 'data' => t ( 'Date' ), 'header' => TRUE ),
format_date ( $access -> timestamp , 'large' )
);
$rows [] = array (
array ( 'data' => t ( 'User' ), 'header' => TRUE ),
theme ( 'username' , $access )
);
$rows [] = array (
array ( 'data' => t ( 'Hostname' ), 'header' => TRUE ),
check_plain ( $access -> hostname )
);
return theme ( 'table' , array (), $rows );
}
else {
drupal_not_found ();
}
}
/**
* Form builder ; Configure access logging .
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see system_settings_form ()
2007-08-23 16:34:44 +00:00
*/
2009-01-22 12:01:24 +00:00
function statistics_settings_form () {
2009-04-27 18:58:41 +00:00
// Access log settings.
2007-08-23 16:34:44 +00:00
$form [ 'access' ] = array (
'#type' => 'fieldset' ,
2009-04-27 18:58:41 +00:00
'#title' => t ( 'Access log settings' ),
);
2007-08-23 16:34:44 +00:00
$form [ 'access' ][ 'statistics_enable_access_log' ] = array (
2009-04-27 18:58:41 +00:00
'#type' => 'checkbox' ,
2007-08-23 16:34:44 +00:00
'#title' => t ( 'Enable access log' ),
2009-01-11 21:19:19 +00:00
'#default_value' => 0 ,
2009-04-27 18:58:41 +00:00
'#description' => t ( 'Log each page access. Required for referrer statistics.' ),
);
2007-08-23 16:34:44 +00:00
$form [ 'access' ][ 'statistics_flush_accesslog_timer' ] = array (
'#type' => 'select' ,
'#title' => t ( 'Discard access logs older than' ),
2009-04-27 18:58:41 +00:00
'#default_value' => 259200 ,
'#options' => drupal_map_assoc ( array ( 3600 , 10800 , 21600 , 32400 , 43200 , 86400 , 172800 , 259200 , 604800 , 1209600 , 2419200 , 4838400 , 9676800 ), 'format_interval' ),
'#description' => t ( 'Older access log entries (including referrer statistics) will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)' , array ( '@cron' => url ( 'admin/reports/status' ))),
);
2007-08-23 16:34:44 +00:00
2009-04-27 18:58:41 +00:00
// Content counter settings.
2007-08-23 16:34:44 +00:00
$form [ 'content' ] = array (
'#type' => 'fieldset' ,
2009-04-27 18:58:41 +00:00
'#title' => t ( 'Content viewing counter settings' ),
);
2007-08-23 16:34:44 +00:00
$form [ 'content' ][ 'statistics_count_content_views' ] = array (
2009-04-27 18:58:41 +00:00
'#type' => 'checkbox' ,
2007-08-23 16:34:44 +00:00
'#title' => t ( 'Count content views' ),
2009-01-11 21:19:19 +00:00
'#default_value' => 0 ,
2009-04-27 18:58:41 +00:00
'#description' => t ( 'Increment a counter each time content is viewed.' ),
);
2007-08-23 16:34:44 +00:00
2009-04-27 18:58:41 +00:00
return system_settings_form ( $form );
2007-08-23 16:34:44 +00:00
}