--- ./modules/system/system.install.orig 2007-05-09 15:37:47.000000000 +0100 +++ ./modules/system/system.install 2007-05-09 16:24:48.000000000 +0100 @@ -497,10 +497,9 @@ db_query("CREATE TABLE {node_counter} ( nid int NOT NULL default '0', - totalcount bigint unsigned NOT NULL default '0', - daycount mediumint unsigned NOT NULL default '0', - timestamp int unsigned NOT NULL default '0', - PRIMARY KEY (nid) + count bigint unsigned NOT NULL default '0', + timestamp int(10) unsigned NOT NULL default '0', + PRIMARY KEY (nid,timestamp) ) /*!40100 DEFAULT CHARACTER SET UTF8 */ "); db_query("CREATE TABLE {system} ( @@ -984,10 +983,9 @@ db_query("CREATE TABLE {node_counter} ( nid int NOT NULL default '0', - totalcount bigint_unsigned NOT NULL default '0', - daycount int_unsigned NOT NULL default '0', + count bigint_unsigned NOT NULL default '0', timestamp int_unsigned NOT NULL default '0', - PRIMARY KEY (nid) + PRIMARY KEY (nid, timestamp) )"); db_query("CREATE TABLE {system} ( @@ -3878,3 +3876,29 @@ * @} End of "defgroup updates-5.x-to-6.x" * The next series of updates should start at 7000. */ + +/** + * Alter node_counter table to collect historical information. + */ +function system_update_7000() { + $ret = array(); + + switch ($GLOBALS['db_type']) { + case 'pgsql': + // TODO: + break; + case 'mysql': + case 'mysqli': + // The daycount field is unnecessary now, this can be calculated. + ALTER TABLE {node_counter} DROP daycount; + // Name remaining field generically. + ALTER TABLE {node_counter} CHANGE totalcount count bigint unsigned NOT NULL default '0'; + // All counts prior to this patch are lumped together. + UPDATE {node_counter} SET timestamp = 0; + // Use a two-column key as the nid is no longer unique. + ALTER TABLE {node_counter} DROP PRIMARY KEY; + ALTER TABLE {node_counter} ADD PRIMARY KEY (nid,timestamp); + } + + return $ret; +} --- ./modules/statistics/statistics.module.orig 2007-05-09 15:31:15.000000000 +0100 +++ ./modules/statistics/statistics.module 2007-05-09 16:41:05.000000000 +0100 @@ -56,13 +56,13 @@ if (variable_get('statistics_count_content_views', 0)) { // We are counting content views. - if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') { + if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == '' || arg(2) == 'view')) { // 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)); + db_query('UPDATE {node_counter} SET count = count + 1 WHERE nid = %d AND timestamp = %d', arg(1), date('YmdH')); // 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()); + db_query('INSERT INTO {node_counter} (nid, count, timestamp) VALUES (%d, 1, %d)', arg(1), date('YmdH')); } } } @@ -89,7 +89,7 @@ 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'); + $links['statistics_counter']['title'] = format_plural($statistics, '1 read', '@count reads'); } } @@ -421,12 +421,6 @@ 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)); } @@ -448,6 +442,7 @@ * or FALSE if the query could not be executed correctly. */ function statistics_title_list($dbfield, $dbrows) { + // TODO: Build a query that works with the new node_counter table format 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); } @@ -459,17 +454,13 @@ * 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. + * The total view count for the specified node. */ 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)); + // Returns the total count for a given nid. + $statistics = db_result(db_query('SELECT SUM(count) FROM {node_counter} WHERE nid = %d', $nid)); } return $statistics; --- ./modules/node/node.module.orig 2007-05-09 16:18:25.000000000 +0100 +++ ./modules/node/node.module 2007-05-09 16:19:47.000000000 +0100 @@ -965,6 +965,7 @@ $ranking[] = '%d * (2.0 - 2.0 / (1.0 + nc.totalcount * %f))'; $arguments2[] = $weight; $arguments2[] = $scale; + // TODO: New implementation to work with new node_counter structure. $join2 .= ' LEFT JOIN {node_counter} nc ON nc.nid = i.sid'; $total += $weight; } @@ -2517,6 +2518,7 @@ // Store the maximum possible comments per thread (used for ranking by reply count) variable_set('node_cron_comments_scale', 1.0 / max(1, db_result(db_query('SELECT MAX(comment_count) FROM {node_comment_statistics}')))); + // TODO: Build new query to determine story with most views. variable_set('node_cron_views_scale', 1.0 / max(1, db_result(db_query('SELECT MAX(totalcount) FROM {node_counter}')))); $result = db_query_range('SELECT GREATEST(IF(c.last_comment_timestamp IS NULL, 0, c.last_comment_timestamp), n.changed) as last_change, n.nid FROM {node} n LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid WHERE n.status = 1 AND ((GREATEST(n.changed, c.last_comment_timestamp) = %d AND n.nid > %d) OR (n.changed > %d OR c.last_comment_timestamp > %d)) ORDER BY GREATEST(n.changed, c.last_comment_timestamp) ASC, n.nid ASC', $last, $last_nid, $last, $last, $last, 0, $limit);