web/drupal/modules/statistics/statistics.admin.inc
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: statistics.admin.inc,v 1.6 2008/01/08 10:35:42 goba Exp $
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * Admin page callbacks for the statistics module.
       
     7  */
       
     8 
       
     9 /**
       
    10  * Menu callback; presents the "recent hits" page.
       
    11  */
       
    12 function statistics_recent_hits() {
       
    13   $header = array(
       
    14     array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'),
       
    15     array('data' => t('Page'), 'field' => 'a.path'),
       
    16     array('data' => t('User'), 'field' => 'u.name'),
       
    17     array('data' => t('Operations'))
       
    18   );
       
    19 
       
    20   $sql = 'SELECT a.aid, a.path, a.title, a.uid, u.name, a.timestamp FROM {accesslog} a LEFT JOIN {users} u ON u.uid = a.uid'. tablesort_sql($header);
       
    21 
       
    22   $result = pager_query($sql, 30);
       
    23   $rows = array();
       
    24   while ($log = db_fetch_object($result)) {
       
    25     $rows[] = array(
       
    26       array('data' => format_date($log->timestamp, 'small'), 'class' => 'nowrap'),
       
    27       _statistics_format_item($log->title, $log->path),
       
    28       theme('username', $log),
       
    29       l(t('details'), "admin/reports/access/$log->aid"));
       
    30   }
       
    31 
       
    32   if (empty($rows)) {
       
    33     $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4));
       
    34   }
       
    35 
       
    36   $output = theme('table', $header, $rows);
       
    37   $output .= theme('pager', NULL, 30, 0);
       
    38   return $output;
       
    39 }
       
    40 
       
    41 /**
       
    42  * Menu callback; presents the "top pages" page.
       
    43  */
       
    44 function statistics_top_pages() {
       
    45   // MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list
       
    46   $sql = "SELECT COUNT(path) AS hits, path, MAX(title) AS title, AVG(timer) AS average_time, SUM(timer) AS total_time FROM {accesslog} GROUP BY path";
       
    47   $sql_cnt = "SELECT COUNT(DISTINCT(path)) FROM {accesslog}";
       
    48 
       
    49   $header = array(
       
    50     array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
       
    51     array('data' => t('Page'), 'field' => 'path'),
       
    52     array('data' => t('Average page generation time'), 'field' => 'average_time'),
       
    53     array('data' => t('Total page generation time'), 'field' => 'total_time')
       
    54   );
       
    55   $sql .= tablesort_sql($header);
       
    56   $result = pager_query($sql, 30, 0, $sql_cnt);
       
    57 
       
    58   $rows = array();
       
    59   while ($page = db_fetch_object($result)) {
       
    60     $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)));
       
    61   }
       
    62 
       
    63   if (empty($rows)) {
       
    64     $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4));
       
    65   }
       
    66 
       
    67   drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))));
       
    68   $output = theme('table', $header, $rows);
       
    69   $output .= theme('pager', NULL, 30, 0);
       
    70   return $output;
       
    71 }
       
    72 
       
    73 /**
       
    74  * Menu callback; presents the "top visitors" page.
       
    75  */
       
    76 function statistics_top_visitors() {
       
    77 
       
    78   $header = array(
       
    79     array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
       
    80     array('data' => t('Visitor'), 'field' => 'u.name'),
       
    81     array('data' => t('Total page generation time'), 'field' => 'total'),
       
    82     array('data' => t('Operations'))
       
    83   );
       
    84 
       
    85   $sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname, SUM(a.timer) AS total, ac.aid FROM {accesslog} a LEFT JOIN {access} ac ON ac.type = 'host' AND LOWER(a.hostname) LIKE (ac.mask) LEFT JOIN {users} u ON a.uid = u.uid GROUP BY a.hostname, a.uid, u.name, ac.aid". tablesort_sql($header);
       
    86   $sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(uid, hostname))) FROM {accesslog}";
       
    87   $result = pager_query($sql, 30, 0, $sql_cnt);
       
    88 
       
    89   $rows = array();
       
    90   while ($account = db_fetch_object($result)) {
       
    91     $qs = drupal_get_destination();
       
    92     $ban_link = $account->aid ? l(t('unban'), "admin/user/rules/delete/$account->aid", array('query' => $qs)) : l(t('ban'), "admin/user/rules/add/$account->hostname/host", array('query' => $qs));
       
    93     $rows[] = array($account->hits, ($account->uid ? theme('username', $account) : $account->hostname), format_interval(round($account->total / 1000)), $ban_link);
       
    94   }
       
    95 
       
    96   if (empty($rows)) {
       
    97     $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4));
       
    98   }
       
    99 
       
   100   drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))));
       
   101   $output = theme('table', $header, $rows);
       
   102   $output .= theme('pager', NULL, 30, 0);
       
   103   return $output;
       
   104 }
       
   105 
       
   106 /**
       
   107  * Menu callback; presents the "referrer" page.
       
   108  */
       
   109 function statistics_top_referrers() {
       
   110   $query = "SELECT url, COUNT(url) AS hits, MAX(timestamp) AS last FROM {accesslog} WHERE url NOT LIKE '%%%s%%' AND url <> '' GROUP BY url";
       
   111   $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%%%s%%'";
       
   112   drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))));
       
   113 
       
   114   $header = array(
       
   115     array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
       
   116     array('data' => t('Url'), 'field' => 'url'),
       
   117     array('data' => t('Last visit'), 'field' => 'last'),
       
   118   );
       
   119 
       
   120   $query .= tablesort_sql($header);
       
   121   $result = pager_query($query, 30, 0, $query_cnt, $_SERVER['HTTP_HOST']);
       
   122 
       
   123   $rows = array();
       
   124   while ($referrer = db_fetch_object($result)) {
       
   125     $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(time() - $referrer->last))));
       
   126   }
       
   127 
       
   128   if (empty($rows)) {
       
   129     $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 3));
       
   130   }
       
   131 
       
   132   $output = theme('table', $header, $rows);
       
   133   $output .= theme('pager', NULL, 30, 0);
       
   134   return $output;
       
   135 }
       
   136 
       
   137 /**
       
   138  * Menu callback; Displays recent page accesses.
       
   139  */
       
   140 function statistics_access_log($aid) {
       
   141   $result = db_query('SELECT a.*, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid WHERE aid = %d', $aid);
       
   142   if ($access = db_fetch_object($result)) {
       
   143     $rows[] = array(
       
   144       array('data' => t('URL'), 'header' => TRUE),
       
   145       l(url($access->path, array('absolute' => TRUE)), $access->path)
       
   146     );
       
   147     // It is safe to avoid filtering $access->title through check_plain because
       
   148     // it comes from drupal_get_title().
       
   149     $rows[] = array(
       
   150       array('data' => t('Title'), 'header' => TRUE),
       
   151       $access->title
       
   152     );
       
   153     $rows[] = array(
       
   154       array('data' => t('Referrer'), 'header' => TRUE),
       
   155       ($access->url ? l($access->url, $access->url) : '')
       
   156     );
       
   157     $rows[] = array(
       
   158       array('data' => t('Date'), 'header' => TRUE),
       
   159       format_date($access->timestamp, 'large')
       
   160     );
       
   161     $rows[] = array(
       
   162       array('data' => t('User'), 'header' => TRUE),
       
   163       theme('username', $access)
       
   164     );
       
   165     $rows[] = array(
       
   166       array('data' => t('Hostname'), 'header' => TRUE),
       
   167       check_plain($access->hostname)
       
   168     );
       
   169 
       
   170     return theme('table', array(), $rows);
       
   171   }
       
   172   else {
       
   173     drupal_not_found();
       
   174   }
       
   175 }
       
   176 
       
   177 /**
       
   178  * Form builder; Configure access logging.
       
   179  *
       
   180  * @ingroup forms
       
   181  * @see system_settings_form()
       
   182  */
       
   183 function statistics_access_logging_settings() {
       
   184   // Access log settings:
       
   185   $options = array('1' => t('Enabled'), '0' => t('Disabled'));
       
   186   $form['access'] = array(
       
   187     '#type' => 'fieldset',
       
   188     '#title' => t('Access log settings'));
       
   189   $form['access']['statistics_enable_access_log'] = array(
       
   190     '#type' => 'radios',
       
   191     '#title' => t('Enable access log'),
       
   192     '#default_value' => variable_get('statistics_enable_access_log', 0),
       
   193     '#options' => $options,
       
   194     '#description' => t('Log each page access. Required for referrer statistics.'));
       
   195   $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
       
   196   $form['access']['statistics_flush_accesslog_timer'] = array(
       
   197     '#type' => 'select',
       
   198     '#title' => t('Discard access logs older than'),
       
   199     '#default_value'   => variable_get('statistics_flush_accesslog_timer', 259200),
       
   200     '#options' => $period,
       
   201     '#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'))));
       
   202 
       
   203   // count content views settings
       
   204   $form['content'] = array(
       
   205     '#type' => 'fieldset',
       
   206     '#title' => t('Content viewing counter settings'));
       
   207   $form['content']['statistics_count_content_views'] = array(
       
   208     '#type' => 'radios',
       
   209     '#title' => t('Count content views'),
       
   210     '#default_value' => variable_get('statistics_count_content_views', 0),
       
   211     '#options' => $options,
       
   212     '#description' => t('Increment a counter each time content is viewed.'));
       
   213 
       
   214   return system_settings_form($form);
       
   215 }