326 lines
13 KiB
Plaintext
326 lines
13 KiB
Plaintext
<?php
|
|
// $Id$
|
|
|
|
/**
|
|
* @file
|
|
* Logs access statistics for your site.
|
|
*/
|
|
|
|
/**
|
|
* Implementation of hook_help().
|
|
*/
|
|
function statistics_help($path, $arg) {
|
|
switch ($path) {
|
|
case 'admin/help#statistics':
|
|
$output = '<p>'. t('The statistics module keeps track of numerous statistics of site usage. It counts how many times, and from where each of your posts is viewed. The statistics module can be used to learn many useful things about how users are interacting with each other and with your site.') .'</p>';
|
|
$output .= t('<p>Statistics module features</p>
|
|
<ul>
|
|
<li>Logs show statistics for how many times your site and specific content on your site has been accessed.</li>
|
|
<li>Referrers tells you from where visitors came from (referrer URL).</li>
|
|
<li>Top pages shows you what\'s hot, what is the most popular content on your site.</li>
|
|
<li>Top users shows you the most active users for your site.</li>
|
|
<li>Recent hits displays information about the latest activity on your site.</li>
|
|
<li>Node count displays the number of times a node has been accessed in the node\'s link section next to <em># comments</em>.</li>
|
|
<li>Popular content block creates a block that can display the day\'s top viewed content, the all time top viewed content, and the last content viewed.</li>
|
|
</ul>
|
|
');
|
|
$output .= t('<p>Configuring the statistics module</p>
|
|
<ul>
|
|
<li>Enable access log allows you to turn the access log on and off. This log is used to store data about every page accessed, such as the remote host\'s IP address, where they came from (referrer), what node they\'ve viewed, and their user name. Enabling the log adds one database call per page displayed by Drupal.</li>
|
|
<li>Discard access logs older than allows you to configure how long an access log entry is saved, after which time it is deleted from the database table. To use this you need to run <em>cron.php</em></li>
|
|
<li>Enable node view counter allows you to turn on and off the node-counting functionality of this module. If it is turned on, an extra database query is added for each node displayed, which increments a counter.</li>
|
|
</ul>
|
|
');
|
|
$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@statistics">Statistics page</a>.', array('@statistics' => 'http://drupal.org/handbook/modules/statistics/')) .'</p>';
|
|
return $output;
|
|
case 'admin/logs/settings':
|
|
return '<p>'. t('Settings for the statistical information that Drupal will keep about the site. See <a href="@statistics">site statistics</a> for the actual information.', array('@statistics' => url('admin/logs/hits'))) .'</p>';
|
|
case 'admin/logs/hits':
|
|
return '<p>'. t('This page shows you the most recent hits.') .'</p>';
|
|
case 'admin/logs/referrers':
|
|
return '<p>'. t('This page shows you all external referrers. These are links pointing to your website from outside your website.') .'</p>';
|
|
case 'admin/logs/visitors':
|
|
return '<p>'. t("When you ban a visitor, you prevent the visitor's IP address from accessing your site. Unlike blocking a user, banning a visitor works even for anonymous users. The most common use for this is to block bots/web crawlers that are consuming too many resources.") .'</p>';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implementation of hook_exit().
|
|
*
|
|
* This is where statistics are gathered on page accesses.
|
|
*/
|
|
function statistics_exit() {
|
|
global $user, $recent_activity;
|
|
|
|
drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
|
|
|
|
if (variable_get('statistics_count_content_views', 0)) {
|
|
// We are counting content views.
|
|
if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') {
|
|
// A node has been viewed, so update the node's counters.
|
|
db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), arg(1));
|
|
// If we affected 0 rows, this is the first time viewing the node.
|
|
if (!db_affected_rows()) {
|
|
// We must create a new row to store counters for the new node.
|
|
db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES (%d, 1, 1, %d)', arg(1), time());
|
|
}
|
|
}
|
|
}
|
|
if ((variable_get('statistics_enable_access_log', 0)) && (module_invoke('throttle', 'status') == 0)) {
|
|
// Log this page access.
|
|
db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp) values('%s', '%s', '%s', '%s', %d, '%s', %d, %d)", strip_tags(drupal_get_title()), $_GET['q'], referer_uri(), ip_address(), $user->uid, session_id(), timer_read('page'), time());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implementation of hook_perm().
|
|
*/
|
|
function statistics_perm() {
|
|
return array('access statistics', 'view post access counter');
|
|
}
|
|
|
|
/**
|
|
* Implementation of hook_link().
|
|
*/
|
|
function statistics_link($type, $node = NULL, $teaser = FALSE) {
|
|
global $id;
|
|
$links = array();
|
|
|
|
if ($type != 'comment' && user_access('view post access counter')) {
|
|
$statistics = statistics_get($node->nid);
|
|
if ($statistics) {
|
|
$links['statistics_counter']['title'] = format_plural($statistics['totalcount'], '1 read', '@count reads');
|
|
}
|
|
}
|
|
|
|
return $links;
|
|
}
|
|
|
|
/**
|
|
* Implementation of hook_menu().
|
|
*/
|
|
function statistics_menu() {
|
|
$items['admin/logs/hits'] = array(
|
|
'title' => 'Recent hits',
|
|
'description' => 'View pages that have recently been visited.',
|
|
'page callback' => 'statistics_recent_hits',
|
|
'access arguments' => array('access statistics'),
|
|
'file' => 'statistics.admin.inc',
|
|
);
|
|
$items['admin/logs/pages'] = array(
|
|
'title' => 'Top pages',
|
|
'description' => 'View pages that have been hit frequently.',
|
|
'page callback' => 'statistics_top_pages',
|
|
'access arguments' => array('access statistics'),
|
|
'weight' => 1,
|
|
'file' => 'statistics.admin.inc',
|
|
);
|
|
$items['admin/logs/visitors'] = array(
|
|
'title' => 'Top visitors',
|
|
'description' => 'View visitors that hit many pages.',
|
|
'page callback' => 'statistics_top_visitors',
|
|
'access arguments' => array('access statistics'),
|
|
'weight' => 2,
|
|
'file' => 'statistics.admin.inc',
|
|
);
|
|
$items['admin/logs/referrers'] = array(
|
|
'title' => 'Top referrers',
|
|
'description' => 'View top referrers.',
|
|
'page callback' => 'statistics_top_referrers',
|
|
'access arguments' => array('access statistics'),
|
|
'file' => 'statistics.admin.inc',
|
|
);
|
|
$items['admin/logs/access/%'] = array(
|
|
'title' => 'Details',
|
|
'description' => 'View access log.',
|
|
'page callback' => 'statistics_access_log',
|
|
'page arguments' => array(3),
|
|
'access arguments' => array('access statistics'),
|
|
'type' => MENU_CALLBACK,
|
|
'file' => 'statistics.admin.inc',
|
|
);
|
|
$items['admin/logs/settings'] = array(
|
|
'title' => 'Access log settings',
|
|
'description' => 'Control details about what and how your site logs.',
|
|
'page callback' => 'drupal_get_form',
|
|
'page arguments' => array('statistics_access_logging_settings'),
|
|
'access arguments' => array('administer site configuration'),
|
|
'type' => MENU_NORMAL_ITEM,
|
|
'weight' => 3,
|
|
'file' => 'statistics.admin.inc',
|
|
);
|
|
$items['user/%user/track/navigation'] = array(
|
|
'title' => 'Track page visits',
|
|
'page callback' => 'statistics_user_tracker',
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access statistics'),
|
|
'type' => MENU_LOCAL_TASK,
|
|
'weight' => 2,
|
|
'file' => 'statistics.pages.inc',
|
|
);
|
|
$items['node/%node/track'] = array(
|
|
'title' => 'Track',
|
|
'page callback' => 'statistics_node_tracker',
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access statistics'),
|
|
'type' => MENU_LOCAL_TASK,
|
|
'weight' => 2,
|
|
'file' => 'statistics.pages.inc',
|
|
);
|
|
|
|
return $items;
|
|
}
|
|
|
|
/**
|
|
* Implementation of hook_user().
|
|
*/
|
|
function statistics_user($op, &$edit, &$user) {
|
|
if ($op == 'delete') {
|
|
db_query('UPDATE {accesslog} SET uid = 0 WHERE uid = %d', $user->uid);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implementation of hook_cron().
|
|
*/
|
|
function statistics_cron() {
|
|
$statistics_timestamp = variable_get('statistics_day_timestamp', '');
|
|
|
|
if ((time() - $statistics_timestamp) >= 86400) {
|
|
/* reset day counts */
|
|
db_query('UPDATE {node_counter} SET daycount = 0');
|
|
variable_set('statistics_day_timestamp', time());
|
|
}
|
|
|
|
/* clean expired access logs */
|
|
db_query('DELETE FROM {accesslog} WHERE timestamp < %d', time() - variable_get('statistics_flush_accesslog_timer', 259200));
|
|
}
|
|
|
|
/**
|
|
* Returns all time or today top or last viewed node(s).
|
|
*
|
|
* @param $dbfield
|
|
* one of
|
|
* - 'totalcount': top viewed content of all time.
|
|
* - 'daycount': top viewed content for today.
|
|
* - 'timestamp': last viewed node.
|
|
*
|
|
* @param $dbrows
|
|
* number of rows to be returned.
|
|
*
|
|
* @return
|
|
* A query result containing n.nid, n.title, u.uid, u.name of the selected node(s)
|
|
* or FALSE if the query could not be executed correctly.
|
|
*/
|
|
function statistics_title_list($dbfield, $dbrows) {
|
|
return db_query_range(db_rewrite_sql("SELECT n.nid, n.title, u.uid, u.name FROM {node} n INNER JOIN {node_counter} s ON n.nid = s.nid INNER JOIN {users} u ON n.uid = u.uid WHERE %s <> '0' AND n.status = 1 ORDER BY %s DESC"), 's.'. $dbfield, 's.'. $dbfield, 0, $dbrows);
|
|
}
|
|
|
|
|
|
/**
|
|
* Retrieves a node's "view statistics".
|
|
*
|
|
* @param $nid
|
|
* node ID
|
|
*
|
|
* @return
|
|
* An array with three entries: [0]=totalcount, [1]=daycount, [2]=timestamp
|
|
* - totalcount: count of the total number of times that node has been viewed.
|
|
* - daycount: count of the total number of times that node has been viewed "today".
|
|
* For the daycount to be reset, cron must be enabled.
|
|
* - timestamp: timestamp of when that node was last viewed.
|
|
*/
|
|
function statistics_get($nid) {
|
|
|
|
if ($nid > 0) {
|
|
/* retrieves an array with both totalcount and daycount */
|
|
$statistics = db_fetch_array(db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = %d', $nid));
|
|
}
|
|
|
|
return $statistics;
|
|
}
|
|
|
|
/**
|
|
* Implementation of hook_block().
|
|
*/
|
|
function statistics_block($op = 'list', $delta = 0, $edit = array()) {
|
|
switch ($op) {
|
|
case 'list':
|
|
if (variable_get('statistics_count_content_views', 0)) {
|
|
$blocks[0]['info'] = t('Popular content');
|
|
// Too dynamic to cache.
|
|
$blocks[0]['cache'] = BLOCK_NO_CACHE;
|
|
return $blocks;
|
|
}
|
|
break;
|
|
|
|
case 'configure':
|
|
// Popular content block settings
|
|
$numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40));
|
|
$form['statistics_block_top_day_num'] = array('#type' => 'select', '#title' => t("Number of day's top views to display"), '#default_value' => variable_get('statistics_block_top_day_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "day" list.'));
|
|
$form['statistics_block_top_all_num'] = array('#type' => 'select', '#title' => t('Number of all time views to display'), '#default_value' => variable_get('statistics_block_top_all_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "all time" list.'));
|
|
$form['statistics_block_top_last_num'] = array('#type' => 'select', '#title' => t('Number of most recent views to display'), '#default_value' => variable_get('statistics_block_top_last_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "recently viewed" list.'));
|
|
return $form;
|
|
|
|
case 'save':
|
|
variable_set('statistics_block_top_day_num', $edit['statistics_block_top_day_num']);
|
|
variable_set('statistics_block_top_all_num', $edit['statistics_block_top_all_num']);
|
|
variable_set('statistics_block_top_last_num', $edit['statistics_block_top_last_num']);
|
|
break;
|
|
|
|
case 'view':
|
|
if (user_access('access content')) {
|
|
$content = array();
|
|
|
|
$daytop = variable_get('statistics_block_top_day_num', 0);
|
|
if ($daytop && ($result = statistics_title_list('daycount', $daytop)) && ($node_title_list = node_title_list($result, t("Today's:")))) {
|
|
$content[] = $node_title_list;
|
|
}
|
|
|
|
$alltimetop = variable_get('statistics_block_top_all_num', 0);
|
|
if ($alltimetop && ($result = statistics_title_list('totalcount', $alltimetop)) && ($node_title_list = node_title_list($result, t('All time:')))) {
|
|
$content[] = $node_title_list;
|
|
}
|
|
|
|
$lasttop = variable_get('statistics_block_top_last_num', 0);
|
|
if ($lasttop && ($result = statistics_title_list('timestamp', $lasttop)) && ($node_title_list = node_title_list($result, t('Last viewed:')))) {
|
|
$content[] = $node_title_list;
|
|
}
|
|
|
|
if (count($content)) {
|
|
$block['content'] = implode('<br />', $content);
|
|
$block['subject'] = t('Popular content');
|
|
return $block;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* It is possible to adjust the width of columns generated by the
|
|
* statistics module.
|
|
*/
|
|
function _statistics_link($path, $width = 35) {
|
|
$title = drupal_get_path_alias($path);
|
|
$title = truncate_utf8($title, $width, FALSE, TRUE);
|
|
return l($title, $path);
|
|
}
|
|
|
|
function _statistics_format_item($title, $path) {
|
|
$path = ($path ? $path : '/');
|
|
$output = ($title ? "$title<br />" : '');
|
|
$output .= _statistics_link($path);
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* Implementation of hook_nodeapi().
|
|
*/
|
|
function statistics_nodeapi(&$node, $op, $arg = 0) {
|
|
switch ($op) {
|
|
case 'delete':
|
|
// clean up statistics table when node is deleted
|
|
db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid);
|
|
}
|
|
}
|