--- tracker.module.old 2007-01-29 22:01:25.000000000 -0600 +++ tracker.module 2007-05-17 11:07:46.000000000 -0500 @@ -70,30 +70,89 @@ function tracker_track_user() { } /** + * Hack to allow manual updates to pager statistics. + */ +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 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 + $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 = 1000; + + // Find the latest nodes created by the user + $nodes_sql = 'SELECT n.nid, n.title, n.type, n.changed, n.uid, u.name, n.changed AS last_updated FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND ' . $node_uid_condition . ' ORDER BY last_updated DESC'; + $nodes_sql = db_rewrite_sql($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, u.name, l.last_comment_timestamp AS last_updated FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {comments} c ON n.nid = c.nid INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND (c.status = %d OR c.status IS NULL) AND ' . $comment_uid_condition . ' ORDER BY last_updated DESC'; + $comments_sql = db_rewrite_sql($comments_sql); + $comment_results = db_query_range($comments_sql, COMMENT_PUBLISHED, $uid, 0, $sanity_limit); + + // Combine the results while avoiding duplication + $results = array(); + while (($node = db_fetch_object($node_results)) || ($commented_node = db_fetch_object($comment_results))) { + // Remove from consideration nodes that already appear in the result set + if (isset($results[$node->nid])) { + $node = FALSE; + } + if (isset($results[$commented_node->nid])) { + $commented_node = FALSE; + } + + // In the case where both exist + if ($node && $commented_node) { + drupal_set_message('Node and comment'); + + // ...and the node was updated last + if ($node->last_updated > $commented_node->last_updated) { + $results[$node->nid] = $node; + $results[$commented_node->nid] = $commented_node; + } + + // ...and the comment was updated last + else { + $results[$commented_node->nid] = $commented_node; + $results[$node->nid] = $node; + } + } + // Case where only $node is set + else if ($node) { + $results[$node->nid] = $node; + } + // Case where only $node is set + else { + $results[$commented_node->nid] = $commented_node; + } } + + $results = _tracker_pager_update($results, 25); $rows = array(); - while ($node = db_fetch_object($result)) { + foreach ($results as $node) { // Determine the number of comments: $comments = 0; if (module_exists('comment') && $node->comment_count) {