[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/statistics/ -> statistics.admin.inc (source)

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7