Index: modules/statistics/statistics.install =================================================================== RCS file: /cvs/drupal/drupal/modules/statistics/statistics.install,v retrieving revision 1.13 diff -u -p -r1.13 statistics.install --- modules/statistics/statistics.install 18 Dec 2007 12:59:22 -0000 1.13 +++ modules/statistics/statistics.install 9 Sep 2008 02:05:07 -0000 @@ -112,6 +112,7 @@ function statistics_schema() { 'indexes' => array( 'accesslog_timestamp' => array('timestamp'), 'uid' => array('uid'), + 'accesslog_path_sid' => array('path', 'sid'), ), 'primary key' => array('aid'), ); @@ -119,3 +120,11 @@ function statistics_schema() { return $schema; } +/** + * Adds index for repeat content view checking. + */ +function statistics_update_7000() { + $ret = array(); + db_add_index($ret, 'accesslog', 'accesslog_path_sid', array('path', 'sid')); + return $ret; +} Index: modules/statistics/statistics.module =================================================================== RCS file: /cvs/drupal/drupal/modules/statistics/statistics.module,v retrieving revision 1.281 diff -u -p -r1.281 statistics.module --- modules/statistics/statistics.module 6 Sep 2008 08:36:21 -0000 1.281 +++ modules/statistics/statistics.module 9 Sep 2008 02:05:08 -0000 @@ -49,20 +49,27 @@ function statistics_exit() { 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. - $fields = array( - 'daycount' => 1, - 'totalcount' => 1, - 'nid' => arg(1), - 'timestamp' => $_SERVER['REQUEST_TIME'], - ); - db_merge('node_counter') - ->fields($fields) - ->expression('daycount', 'daycount + 1') - ->expression('totalcount', 'totalcount + 1') - ->execute(); - } + if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') { + // With enabled access log, we can determine whether this is a unique + // page view or not. We are checking this by using the history table and + // checking for the same Session ID and path in the accesslog table. + $user_viewed = $user->uid && db_result(db_query("SELECT 1 FROM {history} WHERE nid = %d AND uid = %d", arg(1), $user->uid)); + $anon_viewed= !$user->uid && variable_get('statistics_enable_access_log', 0) && db_result(db_query_range("SELECT 1 FROM {accesslog} WHERE path = '%s' AND sid = '%s'", array($_GET['q'], session_id()), 0, 1)); + if (!$user_viewed && !$anon_viewed) { + // A node has been viewed, so update the node's counters. + $fields = array( + 'daycount' => 1, + 'totalcount' => 1, + 'nid' => arg(1), + 'timestamp' => $_SERVER['REQUEST_TIME'], + ); + db_merge('node_counter') + ->fields($fields) + ->expression('daycount', 'daycount + 1') + ->expression('totalcount', 'totalcount + 1') + ->execute(); + } + } } if (variable_get('statistics_enable_access_log', 0)) { // Log this page access.