Only in drupal-5.1-patched/modules/comment: .DS_Store diff -urp drupal-5.1/modules/comment/comment.module drupal-5.1-patched/modules/comment/comment.module --- drupal-5.1/modules/comment/comment.module 2007-01-29 15:51:53.000000000 -0600 +++ drupal-5.1-patched/modules/comment/comment.module 2007-05-19 16:06:24.000000000 -0500 @@ -1948,6 +1948,7 @@ function _comment_update_node_statistics if ($count > 0) { $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = %d ORDER BY cid DESC', $nid, COMMENT_PUBLISHED, 0, 1)); db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $nid); + db_query('UPDATE {comments} SET last_comment_timestamp = %d WHERE nid = %d', $last_reply->timestamp, $nid); } // no comments diff -urp drupal-5.1/modules/system/system.install drupal-5.1-patched/modules/system/system.install --- drupal-5.1/modules/system/system.install 2007-01-10 17:22:34.000000000 -0600 +++ drupal-5.1-patched/modules/system/system.install 2007-05-19 17:00:07.000000000 -0500 @@ -3508,6 +3508,28 @@ function system_update_1021() { * @} End of "defgroup updates-4.7-to-5.0" */ +/** + * Update indexes and table structure for new tracker. + */ +function system_update_1022() { + $ret = array(); + switch ($GLOBALS['db_type']) { + case 'mysql': + case 'mysqli': + $ret[] = update_sql('ALTER TABLE {node} DROP INDEX tracker_user, ADD INDEX tracker_user (uid, status, changed)'); + $ret[] = update_sql('ALTER TABLE {node} DROP INDEX tracker_global, ADD INDEX tracker_global (status, changed)'); + $ret[] = update_sql('ALTER TABLE {comments} ADD last_comment_timestamp INT(11) NOT NULL'); + $ret[] = update_sql('UPDATE {comments} c SET last_comment_timestamp = (SELECT last_comment_timestamp FROM {node_comment_statistics} l WHERE l.nid = c.nid)'); + $ret[] = update_sql('ALTER TABLE {comments} DROP INDEX tracker_user, ADD INDEX tracker_user (uid, status, last_comment_timestamp)'); + $ret[] = update_sql('ALTER TABLE {comments} DROP INDEX tracker_global, ADD INDEX tracker_global (status, last_comment_timestamp)'); + break; + case 'pgsql': + // TODO: PostgreSQL updates + break; + } + + return $ret; +} /** * @defgroup updates-5.0-to-x.x System updates from 5.0 to x.x Only in drupal-5.1-patched/modules/tracker: .DS_Store diff -urp drupal-5.1/modules/tracker/tracker.module drupal-5.1-patched/modules/tracker/tracker.module --- drupal-5.1/modules/tracker/tracker.module 2007-01-10 09:17:51.000000000 -0600 +++ drupal-5.1-patched/modules/tracker/tracker.module 2007-05-19 16:03:38.000000000 -0500 @@ -70,30 +70,104 @@ function tracker_track_user() { } /** + * Hack to allow manual updates to pager statistics and slice up the pages. + */ +function _tracker_pager_update($results, $limit = 10, $element = 0) { + global $pager_page_array, $pager_total, $pager_total_items; + $page = isset($_GET['page']) ? $_GET['page'] : ''; + $pager_page_array = explode(',', $page); + + // We calculate the total of pages as ceil(items / limit). + $pager_total_items[$element] = count($results); + $pager_total[$element] = ceil($pager_total_items[$element] / $limit); + $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1)); + + // Return only results for the current page + return array_slice($results, $pager_page_array[$element] * $limit, $limit); +} + +/** * Menu callback. Prints a listing of active nodes on the site. */ function tracker_page($uid = 0) { // Add CSS drupal_add_css(drupal_get_path('module', 'tracker') .'/tracker.css', 'module', 'all', FALSE); - // TODO: These queries are very expensive, see http://drupal.org/node/105639 + // Only restrict to a uid if one is provided + $node_uid_condition = '1'; + $comment_uid_condition = '1'; if ($uid) { - $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {comments} c ON n.nid = c.nid AND (c.status = %d OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d) ORDER BY last_updated DESC'; - $sql = db_rewrite_sql($sql); - $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n LEFT JOIN {comments} c ON n.nid = c.nid AND (c.status = %d OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d)'; - $sql_count = db_rewrite_sql($sql_count); - $result = pager_query($sql, 25, 0, $sql_count, COMMENT_PUBLISHED, $uid, $uid); + $node_uid_condition = 'n.uid = %d'; + $comment_uid_condition = 'c.uid = %d'; } - else { - $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, l.comment_count FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 ORDER BY last_updated DESC'; - $sql = db_rewrite_sql($sql); - $sql_count = 'SELECT COUNT(n.nid) FROM {node} n WHERE n.status = 1'; - $sql_count = db_rewrite_sql($sql_count); - $result = pager_query($sql, 25, 0, $sql_count); + + // Set a limit for the overall result set. Calculating the full, actual size requires creating the full result set, which is too expensive. + $sanity_limit = variable_get('tracker_limit', 1000); + + // Find the latest nodes created by the user + $nodes_sql = 'SELECT n.nid, n.title, n.type, n.changed, n.uid, n.changed AS last_updated FROM {node} n WHERE n.status = 1 AND ' . $node_uid_condition . ' ORDER BY last_updated DESC'; + $nodes_sql = db_rewrite_sql($nodes_sql); + // DISTINCT() causes the query to require a temporary table, and it doesn't matter much if we get duplicates here, anyway + // The process of combining the result sets will remove duplicates + $nodes_sql = str_replace('DISTINCT(n.nid)', 'n.nid', $nodes_sql); + $node_results = db_query_range($nodes_sql, $uid, 0, $sanity_limit); + + // Find the latest comments created by the user + $comments_sql = 'SELECT n.nid, n.title, n.type, n.changed, n.uid, c.last_comment_timestamp AS last_updated FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE n.status = 1 AND c.status = %d AND ' . $comment_uid_condition . ' ORDER BY last_updated DESC'; + $comments_sql = db_rewrite_sql($comments_sql); + // The same problem with DISTINCT() occurs here + $comments_sql = str_replace('DISTINCT(n.nid)', 'n.nid', $comments_sql); + $comment_results = db_queryd($comments_sql, COMMENT_PUBLISHED, $uid, 0, $sanity_limit); + + // Combine the results while avoiding duplication + $results = array(); + $node = db_fetch_object($node_results); + $commented_node = db_fetch_object($comment_results); + while ($node || $commented_node) { + // Make sure we have the most recent node that's not currently listed + while (isset($results[$node->nid])) { + $node = db_fetch_object($node_results); + } + // Make sure we have the most recent commented node that's not currently listed + while (isset($results[$commented_node->nid])) { + $commented_node = db_fetch_object($comment_results); + } + + // In the case where both exist + if ($node && $commented_node) { + // ...and the node was updated last + if ($node->last_updated > $commented_node->last_updated) { + $results[$node->nid] = $node; + } + + // ...and the comment was updated last + else { + $results[$commented_node->nid] = $commented_node; + } + } + // Case where only $node is set + else if ($node) { + $results[$node->nid] = $node; + } + // Case where only $commented_node is set + else if ($commented_node) { + $results[$commented_node->nid] = $commented_node; + } } + + // Update the pager and get the current page's results + $results = _tracker_pager_update($results, 25); $rows = array(); - while ($node = db_fetch_object($result)) { + $names = array(); + foreach ($results as $node) { + // Load these data here (instead of in a JOIN) to only load data for the current page + $node->comment_count = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $node->nid)); + if (!isset($names[$node->uid])) { + $names[$node->uid] = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $node->uid)); + } + $node->name = $names[$node->uid]; + // Determine the number of comments: $comments = 0; if (module_exists('comment') && $node->comment_count) {