2007-08-23 16:34:44 +00:00
< ? php
/**
* @ file
2012-04-19 15:17:35 +00:00
* Admin page callbacks for the Statistics module .
2007-08-23 16:34:44 +00:00
*/
2012-06-02 19:41:40 +00:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException ;
2007-08-23 16:34:44 +00:00
/**
2012-04-19 15:17:35 +00:00
* Page callback : Displays the " recent hits " page .
*
2012-04-19 17:40:55 +00:00
* This displays the pages with recent hits in a given time interval that
* haven ' t been flushed yet . The flush interval is set on the statistics
2012-04-19 15:17:35 +00:00
* settings form , but is dependent on cron running .
*
* @ return array
* A render array containing information about the most recent hits .
2007-08-23 16:34:44 +00:00
*/
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' ),
2012-10-09 19:49:07 +00:00
t ( 'Operations' ),
2007-08-23 16:34:44 +00:00
);
2012-06-05 05:42:19 +00:00
$query = db_select ( 'accesslog' , 'a' , array ( 'target' => 'slave' ))
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
2012-06-08 12:26:56 +00:00
-> extend ( 'Drupal\Core\Database\Query\TableSortExtender' );
2009-04-13 10:40:13 +00:00
$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 ) {
2012-10-09 19:49:07 +00:00
$row = array ();
$row [] = array ( 'data' => format_date ( $log -> timestamp , 'short' ), 'class' => array ( 'nowrap' ));
$row [] = _statistics_format_item ( $log -> title , $log -> path );
$row [] = theme ( 'username' , array ( 'account' => $log ));
$links = array ();
$links [ 'details' ] = array (
'title' => t ( 'details' ),
'href' => " admin/reports/access/ $log->aid " ,
);
$row [] = array (
'data' => array (
'#type' => 'operations' ,
'#links' => $links ,
),
);
$rows [] = $row ;
2007-08-23 16:34:44 +00:00
}
2009-07-29 06:39:35 +00:00
$build [ 'statistics_table' ] = array (
2009-10-29 07:17:22 +00:00
'#theme' => 'table' ,
'#header' => $header ,
2009-07-29 06:39:35 +00:00
'#rows' => $rows ,
2009-12-02 14:56:32 +00:00
'#empty' => t ( 'No statistics available.' ),
2009-07-29 06:39:35 +00:00
);
$build [ 'statistics_pager' ] = array ( '#theme' => 'pager' );
return $build ;
2007-08-23 16:34:44 +00:00
}
/**
2012-04-19 15:17:35 +00:00
* Page callback : Displays statistics for the " top pages " ( most accesses ) .
*
* This displays the pages with the most hits ( the " top pages " ) within a given
* time period that haven ' t been flushed yet . The flush interval is set on the
* statistics settings form , but is dependent on cron running .
*
* @ return array
* A render array containing information about the the top pages .
2007-08-23 16:34:44 +00:00
*/
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' )
);
2012-06-05 05:42:19 +00:00
$query = db_select ( 'accesslog' , 'a' , array ( 'target' => 'slave' ))
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
2012-06-08 12:26:56 +00:00
-> extend ( 'Drupal\Core\Database\Query\TableSortExtender' );
2009-04-13 10:40:13 +00:00
$query -> addExpression ( 'COUNT(path)' , 'hits' );
2012-04-19 15:17:35 +00:00
// MAX(title) avoids having empty node titles which otherwise causes
// duplicates in the top pages list.
2009-04-13 10:40:13 +00:00
$query -> addExpression ( 'MAX(title)' , 'title' );
$query -> addExpression ( 'AVG(timer)' , 'average_time' );
$query -> addExpression ( 'SUM(timer)' , 'total_time' );
2009-05-24 17:39:35 +00:00
2009-04-13 10:40:13 +00:00
$query
2009-12-20 19:41:57 +00:00
-> fields ( 'a' , array ( 'path' ))
2009-04-13 10:40:13 +00:00
-> groupBy ( 'path' )
-> limit ( 30 )
2009-05-22 11:33:18 +00:00
-> orderByHeader ( $header );
2009-04-13 10:40:13 +00:00
2009-12-20 19:41:57 +00:00
$count_query = db_select ( 'accesslog' , 'a' , array ( 'target' => 'slave' ));
2009-04-13 10:40:13 +00:00
$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 )));
}
2012-08-07 19:49:48 +00:00
drupal_set_title ( t ( 'Top pages in the past %interval' , array ( '%interval' => format_interval ( config ( 'statistics.settings' ) -> get ( 'access_log.max_lifetime' )))), PASS_THROUGH );
2009-07-29 06:39:35 +00:00
$build [ 'statistics_top_pages_table' ] = array (
2009-10-29 07:17:22 +00:00
'#theme' => 'table' ,
'#header' => $header ,
2009-07-29 06:39:35 +00:00
'#rows' => $rows ,
2009-12-02 14:56:32 +00:00
'#empty' => t ( 'No statistics available.' ),
2009-07-29 06:39:35 +00:00
);
$build [ 'statistics_top_pages_pager' ] = array ( '#theme' => 'pager' );
return $build ;
2007-08-23 16:34:44 +00:00
}
/**
2012-04-19 15:17:35 +00:00
* Page callback : Displays the " top visitors " page .
*
* This displays the pages with the top number of visitors in a given time
2012-04-19 17:40:55 +00:00
* interval that haven ' t been flushed yet . The flush interval is set on the
2012-04-19 15:17:35 +00:00
* statistics settings form , but is dependent on cron running .
*
* @ return array
* A render array containing the top visitors information .
2007-08-23 16:34:44 +00:00
*/
function statistics_top_visitors () {
2012-09-24 02:17:45 +00:00
$ban_exists = module_exists ( 'ban' );
2007-08-23 16:34:44 +00:00
$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' ),
2012-10-09 19:49:07 +00:00
$ban_exists && user_access ( 'Ban IP addresses' ) ? t ( 'Operations' ) : '' ,
2007-08-23 16:34:44 +00:00
);
2012-06-05 05:42:19 +00:00
$query = db_select ( 'accesslog' , 'a' , array ( 'target' => 'slave' ))
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
2012-06-08 12:26:56 +00:00
-> extend ( 'Drupal\Core\Database\Query\TableSortExtender' );
2012-09-24 02:17:45 +00:00
if ( $ban_exists ) {
$query -> leftJoin ( 'ban_ip' , 'b' , 'a.hostname = b.ip' );
}
2009-04-13 10:40:13 +00:00
$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' ))
-> groupBy ( 'a.hostname' )
-> groupBy ( 'a.uid' )
-> groupBy ( 'u.name' )
2012-09-24 02:17:45 +00:00
-> limit ( 30 );
if ( $ban_exists ) {
$query
-> fields ( 'b' , array ( 'iid' ))
-> groupBy ( 'b.iid' );
}
$query -> orderByHeader ( $header );
2009-04-13 10:40:13 +00:00
2010-01-26 08:19:45 +00:00
$uniques_query = db_select ( 'accesslog' ) -> distinct ();
$uniques_query -> fields ( 'accesslog' , array ( 'uid' , 'hostname' ));
$count_query = db_select ( $uniques_query );
$count_query -> addExpression ( 'COUNT(*)' );
2009-04-13 10:40:13 +00:00
$query -> setCountQuery ( $count_query );
2009-05-24 17:39:35 +00:00
2009-04-13 10:40:13 +00:00
$result = $query -> execute ();
2007-08-23 16:34:44 +00:00
$rows = array ();
2009-09-29 15:31:17 +00:00
$destination = drupal_get_destination ();
2009-04-13 10:40:13 +00:00
foreach ( $result as $account ) {
2012-10-09 19:49:07 +00:00
$links = array ();
if ( $ban_exists && user_access ( 'ban IP addresses' ) && ! $account -> uid ) {
2012-09-24 02:17:45 +00:00
if ( $account -> iid ) {
2012-10-09 19:49:07 +00:00
$links [ 'unban' ] = array (
'title' => t ( 'unban IP address' ),
'href' => " admin/config/people/ban/delete/ $account->iid " ,
'query' => $destination ,
);
2012-09-24 02:17:45 +00:00
}
else {
2012-10-09 19:49:07 +00:00
$links [ 'ban' ] = array (
'title' => t ( 'ban IP address' ),
'href' => " admin/config/people/ban/ $account->hostname " ,
'query' => $destination ,
);
2012-09-24 02:17:45 +00:00
}
}
$row = array ();
$row [] = $account -> hits ;
$row [] = ( $account -> uid ? theme ( 'username' , array ( 'account' => $account )) : $account -> hostname );
$row [] = format_interval ( round ( $account -> total / 1000 ));
2012-10-09 19:49:07 +00:00
$row [] = array (
'data' => array (
'#type' => 'operations' ,
'#links' => $links ,
),
);
2012-09-24 02:17:45 +00:00
$rows [] = $row ;
2007-08-23 16:34:44 +00:00
}
2012-08-07 19:49:48 +00:00
drupal_set_title ( t ( 'Top visitors in the past %interval' , array ( '%interval' => format_interval ( config ( 'statistics.settings' ) -> get ( 'access_log.max_lifetime' )))), PASS_THROUGH );
2009-07-29 06:39:35 +00:00
$build [ 'statistics_top_visitors_table' ] = array (
2009-10-29 07:17:22 +00:00
'#theme' => 'table' ,
'#header' => $header ,
2009-07-29 06:39:35 +00:00
'#rows' => $rows ,
2009-12-02 14:56:32 +00:00
'#empty' => t ( 'No statistics available.' ),
2009-07-29 06:39:35 +00:00
);
$build [ 'statistics_top_visitors_pager' ] = array ( '#theme' => 'pager' );
return $build ;
2007-08-23 16:34:44 +00:00
}
/**
2012-04-19 15:17:35 +00:00
* Page callback : Displays the " top referrers " in the access logs .
*
2012-04-19 17:40:55 +00:00
* This displays the pages with the top referrers in a given time interval that
* haven ' t been flushed yet . The flush interval is set on the statistics
2012-04-19 15:17:35 +00:00
* settings form , but is dependent on cron running .
*
* @ return array
* A render array containing the top referrers information .
2007-08-23 16:34:44 +00:00
*/
function statistics_top_referrers () {
2012-08-07 19:49:48 +00:00
drupal_set_title ( t ( 'Top referrers in the past %interval' , array ( '%interval' => format_interval ( config ( 'statistics.settings' ) -> get ( 'access_log.max_lifetime' )))), 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' ),
);
2012-06-05 05:42:19 +00:00
$query = db_select ( 'accesslog' , 'a' )
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
2012-06-08 12:26:56 +00:00
-> extend ( 'Drupal\Core\Database\Query\TableSortExtender' );
2009-04-13 10:40:13 +00:00
$query -> addExpression ( 'COUNT(url)' , 'hits' );
$query -> addExpression ( 'MAX(timestamp)' , 'last' );
$query
-> fields ( 'a' , array ( 'url' ))
2010-02-28 20:10:34 +00:00
-> condition ( 'url' , '%' . $_SERVER [ 'HTTP_HOST' ] . '%' , 'NOT LIKE' )
2009-04-13 10:40:13 +00:00
-> condition ( 'url' , '' , '<>' )
-> groupBy ( 'url' )
-> limit ( 30 )
2009-05-22 11:33:18 +00:00
-> orderByHeader ( $header );
2009-04-13 10:40:13 +00:00
2009-12-20 19:41:57 +00:00
$count_query = db_select ( 'accesslog' , 'a' , array ( 'target' => 'slave' ));
2009-04-13 10:40:13 +00:00
$count_query -> addExpression ( 'COUNT(DISTINCT url)' );
$count_query
2010-02-28 20:10:34 +00:00
-> condition ( 'url' , '%' . $_SERVER [ 'HTTP_HOST' ] . '%' , 'NOT LIKE' )
2009-04-13 10:40:13 +00:00
-> 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
}
2009-07-29 06:39:35 +00:00
$build [ 'statistics_top_referrers_table' ] = array (
2009-10-29 07:17:22 +00:00
'#theme' => 'table' ,
'#header' => $header ,
2009-07-29 06:39:35 +00:00
'#rows' => $rows ,
2009-12-02 14:56:32 +00:00
'#empty' => t ( 'No statistics available.' ),
2009-07-29 06:39:35 +00:00
);
$build [ 'statistics_top_referrers_pager' ] = array ( '#theme' => 'pager' );
return $build ;
2007-08-23 16:34:44 +00:00
}
/**
2012-04-19 15:17:35 +00:00
* Page callback : Gathers page access statistics suitable for rendering .
*
* @ param int $aid
* The unique accesslog ID .
*
* @ return array
* A render array containing page access statistics . If information for the
2012-06-02 19:41:40 +00:00
* page was not found , a NotFoundHttpException is thrown .
2007-08-23 16:34:44 +00:00
*/
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 ),
2009-08-25 10:27:15 +00:00
format_date ( $access -> timestamp , 'long' )
2007-08-23 16:34:44 +00:00
);
$rows [] = array (
array ( 'data' => t ( 'User' ), 'header' => TRUE ),
2009-10-09 01:00:08 +00:00
theme ( 'username' , array ( 'account' => $access ))
2007-08-23 16:34:44 +00:00
);
$rows [] = array (
array ( 'data' => t ( 'Hostname' ), 'header' => TRUE ),
check_plain ( $access -> hostname )
);
2009-07-29 06:39:35 +00:00
$build [ 'statistics_table' ] = array (
2009-10-29 07:17:22 +00:00
'#theme' => 'table' ,
2009-07-29 06:39:35 +00:00
'#rows' => $rows ,
);
return $build ;
2007-08-23 16:34:44 +00:00
}
else {
2012-06-02 19:41:40 +00:00
throw new NotFoundHttpException ();
2007-08-23 16:34:44 +00:00
}
}
/**
2012-04-19 15:17:35 +00:00
* Form constructor for the statistics administration form .
2007-08-23 16:34:44 +00:00
*
* @ ingroup forms
2012-08-07 19:49:48 +00:00
* @ see statistics_settings_form_submit () .
2007-08-23 16:34:44 +00:00
*/
2012-08-07 19:49:48 +00:00
function statistics_settings_form ( $form , & $form_state ) {
$config = config ( 'statistics.settings' );
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' ),
2012-08-07 19:49:48 +00:00
'#default_value' => $config -> get ( 'access_log.enabled' ),
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' ),
2012-08-07 19:49:48 +00:00
'#default_value' => $config -> get ( 'access_log.max_lifetime' ),
2009-08-29 03:32:46 +00:00
'#options' => array ( 0 => t ( 'Never' )) + drupal_map_assoc ( array ( 3600 , 10800 , 21600 , 32400 , 43200 , 86400 , 172800 , 259200 , 604800 , 1209600 , 2419200 , 4838400 , 9676800 ), 'format_interval' ),
2009-04-27 18:58:41 +00:00
'#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' ),
2012-08-07 19:49:48 +00:00
'#default_value' => $config -> get ( 'count_content_views' ),
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
2012-08-07 19:49:48 +00:00
return system_config_form ( $form , $form_state );
}
/**
* Form submission handler for statistics_settings_form () .
*/
function statistics_settings_form_submit ( $form , & $form_state ) {
config ( 'statistics.settings' )
-> set ( 'access_log.enabled' , $form_state [ 'values' ][ 'statistics_enable_access_log' ])
-> set ( 'access_log.max_lifetime' , $form_state [ 'values' ][ 'statistics_flush_accesslog_timer' ])
-> set ( 'count_content_views' , $form_state [ 'values' ][ 'statistics_count_content_views' ])
-> save ();
2007-08-23 16:34:44 +00:00
}