webmaster@1: t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'), webmaster@1: array('data' => t('Page'), 'field' => 'a.path'), webmaster@1: array('data' => t('User'), 'field' => 'u.name'), webmaster@1: array('data' => t('Operations')) webmaster@1: ); webmaster@1: webmaster@1: $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); webmaster@1: webmaster@1: $result = pager_query($sql, 30); webmaster@1: $rows = array(); webmaster@1: while ($log = db_fetch_object($result)) { webmaster@1: $rows[] = array( webmaster@1: array('data' => format_date($log->timestamp, 'small'), 'class' => 'nowrap'), webmaster@1: _statistics_format_item($log->title, $log->path), webmaster@1: theme('username', $log), webmaster@1: l(t('details'), "admin/reports/access/$log->aid")); webmaster@1: } webmaster@1: webmaster@1: if (empty($rows)) { webmaster@1: $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4)); webmaster@1: } webmaster@1: webmaster@1: $output = theme('table', $header, $rows); webmaster@1: $output .= theme('pager', NULL, 30, 0); webmaster@1: return $output; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Menu callback; presents the "top pages" page. webmaster@1: */ webmaster@1: function statistics_top_pages() { webmaster@1: // MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list webmaster@1: $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"; webmaster@1: $sql_cnt = "SELECT COUNT(DISTINCT(path)) FROM {accesslog}"; webmaster@1: webmaster@1: $header = array( webmaster@1: array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'), webmaster@1: array('data' => t('Page'), 'field' => 'path'), webmaster@1: array('data' => t('Average page generation time'), 'field' => 'average_time'), webmaster@1: array('data' => t('Total page generation time'), 'field' => 'total_time') webmaster@1: ); webmaster@1: $sql .= tablesort_sql($header); webmaster@1: $result = pager_query($sql, 30, 0, $sql_cnt); webmaster@1: webmaster@1: $rows = array(); webmaster@1: while ($page = db_fetch_object($result)) { webmaster@1: $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))); webmaster@1: } webmaster@1: webmaster@1: if (empty($rows)) { webmaster@1: $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4)); webmaster@1: } webmaster@1: webmaster@1: drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200))))); webmaster@1: $output = theme('table', $header, $rows); webmaster@1: $output .= theme('pager', NULL, 30, 0); webmaster@1: return $output; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Menu callback; presents the "top visitors" page. webmaster@1: */ webmaster@1: function statistics_top_visitors() { webmaster@1: webmaster@1: $header = array( webmaster@1: array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'), webmaster@1: array('data' => t('Visitor'), 'field' => 'u.name'), webmaster@1: array('data' => t('Total page generation time'), 'field' => 'total'), webmaster@1: array('data' => t('Operations')) webmaster@1: ); webmaster@1: webmaster@1: $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); webmaster@1: $sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(uid, hostname))) FROM {accesslog}"; webmaster@1: $result = pager_query($sql, 30, 0, $sql_cnt); webmaster@1: webmaster@1: $rows = array(); webmaster@1: while ($account = db_fetch_object($result)) { webmaster@1: $qs = drupal_get_destination(); webmaster@1: $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)); webmaster@1: $rows[] = array($account->hits, ($account->uid ? theme('username', $account) : $account->hostname), format_interval(round($account->total / 1000)), $ban_link); webmaster@1: } webmaster@1: webmaster@1: if (empty($rows)) { webmaster@1: $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4)); webmaster@1: } webmaster@1: webmaster@1: drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200))))); webmaster@1: $output = theme('table', $header, $rows); webmaster@1: $output .= theme('pager', NULL, 30, 0); webmaster@1: return $output; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Menu callback; presents the "referrer" page. webmaster@1: */ webmaster@1: function statistics_top_referrers() { webmaster@1: $query = "SELECT url, COUNT(url) AS hits, MAX(timestamp) AS last FROM {accesslog} WHERE url NOT LIKE '%%%s%%' AND url <> '' GROUP BY url"; webmaster@1: $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%%%s%%'"; webmaster@1: drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200))))); webmaster@1: webmaster@1: $header = array( webmaster@1: array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'), webmaster@1: array('data' => t('Url'), 'field' => 'url'), webmaster@1: array('data' => t('Last visit'), 'field' => 'last'), webmaster@1: ); webmaster@1: webmaster@1: $query .= tablesort_sql($header); webmaster@1: $result = pager_query($query, 30, 0, $query_cnt, $_SERVER['HTTP_HOST']); webmaster@1: webmaster@1: $rows = array(); webmaster@1: while ($referrer = db_fetch_object($result)) { webmaster@1: $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(time() - $referrer->last)))); webmaster@1: } webmaster@1: webmaster@1: if (empty($rows)) { webmaster@1: $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 3)); webmaster@1: } webmaster@1: webmaster@1: $output = theme('table', $header, $rows); webmaster@1: $output .= theme('pager', NULL, 30, 0); webmaster@1: return $output; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Menu callback; Displays recent page accesses. webmaster@1: */ webmaster@1: function statistics_access_log($aid) { webmaster@1: $result = db_query('SELECT a.*, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid WHERE aid = %d', $aid); webmaster@1: if ($access = db_fetch_object($result)) { webmaster@1: $rows[] = array( webmaster@1: array('data' => t('URL'), 'header' => TRUE), webmaster@1: l(url($access->path, array('absolute' => TRUE)), $access->path) webmaster@1: ); webmaster@1: // It is safe to avoid filtering $access->title through check_plain because webmaster@1: // it comes from drupal_get_title(). webmaster@1: $rows[] = array( webmaster@1: array('data' => t('Title'), 'header' => TRUE), webmaster@1: $access->title webmaster@1: ); webmaster@1: $rows[] = array( webmaster@1: array('data' => t('Referrer'), 'header' => TRUE), webmaster@1: ($access->url ? l($access->url, $access->url) : '') webmaster@1: ); webmaster@1: $rows[] = array( webmaster@1: array('data' => t('Date'), 'header' => TRUE), webmaster@1: format_date($access->timestamp, 'large') webmaster@1: ); webmaster@1: $rows[] = array( webmaster@1: array('data' => t('User'), 'header' => TRUE), webmaster@1: theme('username', $access) webmaster@1: ); webmaster@1: $rows[] = array( webmaster@1: array('data' => t('Hostname'), 'header' => TRUE), webmaster@1: check_plain($access->hostname) webmaster@1: ); webmaster@1: webmaster@1: return theme('table', array(), $rows); webmaster@1: } webmaster@1: else { webmaster@1: drupal_not_found(); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Form builder; Configure access logging. webmaster@1: * webmaster@1: * @ingroup forms webmaster@1: * @see system_settings_form() webmaster@1: */ webmaster@1: function statistics_access_logging_settings() { webmaster@1: // Access log settings: webmaster@1: $options = array('1' => t('Enabled'), '0' => t('Disabled')); webmaster@1: $form['access'] = array( webmaster@1: '#type' => 'fieldset', webmaster@1: '#title' => t('Access log settings')); webmaster@1: $form['access']['statistics_enable_access_log'] = array( webmaster@1: '#type' => 'radios', webmaster@1: '#title' => t('Enable access log'), webmaster@1: '#default_value' => variable_get('statistics_enable_access_log', 0), webmaster@1: '#options' => $options, webmaster@1: '#description' => t('Log each page access. Required for referrer statistics.')); webmaster@1: $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'); webmaster@1: $form['access']['statistics_flush_accesslog_timer'] = array( webmaster@1: '#type' => 'select', webmaster@1: '#title' => t('Discard access logs older than'), webmaster@1: '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200), webmaster@1: '#options' => $period, webmaster@1: '#description' => t('Older access log entries (including referrer statistics) will be automatically discarded. (Requires a correctly configured cron maintenance task.)', array('@cron' => url('admin/reports/status')))); webmaster@1: webmaster@1: // count content views settings webmaster@1: $form['content'] = array( webmaster@1: '#type' => 'fieldset', webmaster@1: '#title' => t('Content viewing counter settings')); webmaster@1: $form['content']['statistics_count_content_views'] = array( webmaster@1: '#type' => 'radios', webmaster@1: '#title' => t('Count content views'), webmaster@1: '#default_value' => variable_get('statistics_count_content_views', 0), webmaster@1: '#options' => $options, webmaster@1: '#description' => t('Increment a counter each time content is viewed.')); webmaster@1: webmaster@1: return system_settings_form($form); webmaster@1: }