Index: modules/statistics/statistics.module =================================================================== RCS file: /cvs/drupal/drupal/modules/statistics/statistics.module,v retrieving revision 1.287 diff -u -r1.287 statistics.module --- modules/statistics/statistics.module 2 Dec 2008 21:24:34 -0000 1.287 +++ modules/statistics/statistics.module 8 Dec 2008 17:12:06 -0000 @@ -50,18 +50,26 @@ 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' => REQUEST_TIME, - ); - db_merge('node_counter') - ->fields($fields) - ->expression('daycount', 'daycount + 1') - ->expression('totalcount', 'totalcount + 1') - ->execute(); + // We're trying to determine whether this is a unique page view or not: + // For authenticated users we can check against past hits in the history. + $user_viewed = $user->uid && db_result(db_query("SELECT 1 FROM {history} WHERE nid = %d AND uid = %d", arg(1), $user->uid)); + // For anonymous users this depends on the access log being enabled. + // If yes, we can check against past hits with the same Session ID. + $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' => 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)) { Index: modules/statistics/statistics.install =================================================================== RCS file: /cvs/drupal/drupal/modules/statistics/statistics.install,v retrieving revision 1.15 diff -u -r1.15 statistics.install --- modules/statistics/statistics.install 3 Dec 2008 19:22:09 -0000 1.15 +++ modules/statistics/statistics.install 8 Dec 2008 17:12:06 -0000 @@ -111,6 +111,7 @@ 'indexes' => array( 'accesslog_timestamp' => array('timestamp'), 'uid' => array('uid'), + 'accesslog_path_sid' => array('path', 'sid'), ), 'primary key' => array('aid'), ); @@ -126,3 +127,11 @@ db_change_field($ret, 'accesslog', 'url', 'url', array('type' => 'text', 'not null' => FALSE)); return $ret; } +/** + * Adds index for checking against repeated content view. + */ +function statistics_update_7001() { + $ret = array(); + db_add_index($ret, 'accesslog', 'accesslog_path_sid', array('path', 'sid')); + return $ret; +}