? 2
? files
? node_78.patch
? schema-0517_0.patch
? split_tracker_queries_2.diff
? tracker.patch
? user_email_settings_code_style.patch_1.txt
? modules/tracker/patch.patch
? sites/default/settings.php
Index: modules/tracker/tracker.install
===================================================================
RCS file: modules/tracker/tracker.install
diff -N modules/tracker/tracker.install
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/tracker/tracker.install	18 May 2007 01:35:00 -0000
@@ -0,0 +1,66 @@
+<?php
+// $Id$
+
+/**
+ * Implementation of hook_install().
+ */
+function tracker_install() {
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      db_query("ALTER TABLE {comments} ADD INDEX {tracker}_user (uid, status, timestamp)");
+      db_query("ALTER TABLE {comments} ADD INDEX {tracker}_global (status, timestamp)");
+      db_query("ALTER TABLE {node} ADD INDEX {tracker}_user (uid, status, changed)");
+      db_query("ALTER TABLE {node} ADD INDEX {tracker}_global (status, changed)");
+      break;
+    case 'pgsql':
+      db_query("CREATE INDEX {tracker}_user ON {comment} (uid, status, timestamp)");
+      db_query("CREATE INDEX {tracker}_global ON {comment} (status, timestamp)");
+      db_query("CREATE INDEX {tracker}_user ON {node} (uid, status, timestamp)");
+      db_query("CREATE INDEX {tracker}_global ON {node} (status, timestamp)");
+  }
+}
+
+/**
+ * Add indexes to comment and node tables for better tracker performance.
+ */
+function tracker_update_6000() {
+  $ret = array();
+
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql("ALTER TABLE {comments} ADD INDEX {tracker}_user (uid, status, timestamp)");
+      $ret[] = update_sql("ALTER TABLE {comments} ADD INDEX {tracker}_global (status, timestamp)");
+      $ret[] = update_sql("ALTER TABLE {node} ADD INDEX {tracker}_user (uid, status, changed)");
+      $ret[] = update_sql("ALTER TABLE {node} ADD INDEX {tracker}_global (status, changed)");
+      break;
+    case 'pgsql':
+      $ret[] = update_sql("CREATE INDEX {tracker}_user ON {comment} (uid, status, timestamp)");
+      $ret[] = update_sql("CREATE INDEX {tracker}_global ON {comment} (status, timestamp)");
+      $ret[] = update_sql("CREATE INDEX {tracker}_user ON {node} (uid, status, timestamp)");
+      $ret[] = update_sql("CREATE INDEX {tracker}_global ON {node} (status, timestamp)");
+  }
+
+  return $ret;
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function tracker_uninstall() {
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      db_query("ALTER TABLE {comments} DROP INDEX {tracker}_user");
+      db_query("ALTER TABLE {comments} DROP INDEX {tracker}_global");
+      db_query("ALTER TABLE {node} DROP INDEX {tracker}_user");
+      db_query("ALTER TABLE {node} DROP INDEX {tracker}_global");
+      break;
+    case 'pgsql':
+      db_query("DROP INDEX {tracker}_user ON {comment}");
+      db_query("DROP INDEX {tracker}_global ON {comment}");
+      db_query("DROP INDEX {tracker}_user ON {node}");
+      db_query("DROP INDEX {tracker}_global ON {node}");
+  }  
+}
Index: modules/tracker/tracker.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/tracker/tracker.module,v
retrieving revision 1.148
diff -u -p -r1.148 tracker.module
--- modules/tracker/tracker.module	16 May 2007 13:45:16 -0000	1.148
+++ modules/tracker/tracker.module	18 May 2007 01:35:00 -0000
@@ -75,31 +75,104 @@ function tracker_track_user() {
 }
 
 /**
+ * Allows manual updates to pager statistics and slices 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 user ID 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.timestamp AS last_updated FROM {comments} c INNER JOIN {node} n 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_query_range($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, store whichever was updated last.
+    // If only $node or $comment exist, store that value.
+    if ($node && $commented_node) {
+      if ($node->last_updated > $commented_node->last_updated) {
+        $results[$node->nid] = $node;          
+      }
+      else {
+        $results[$commented_node->nid] = $commented_node;
+      }
+    }
+    else if ($node) {
+      $results[$node->nid] = $node;
+    }
+    else {
+      $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)) {
-    // Determine the number of comments:
+  $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) {
       $comments = $node->comment_count;
