Index: modules/tracker/tracker.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/tracker/tracker.module,v
retrieving revision 1.141
diff -u -r1.141 tracker.module
--- modules/tracker/tracker.module	10 Dec 2006 09:54:34 -0000	1.141
+++ modules/tracker/tracker.module	12 Dec 2006 03:33:29 -0000
@@ -77,14 +77,14 @@
   drupal_add_css(drupal_get_path('module', 'tracker') .'/tracker.css', 'module', 'all', FALSE);
 
   if ($uid) {
-    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, l.last_comment_timestamp AS last_post, 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_post DESC';
+    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, l.last_comment_timestamp AS last_post, l.comment_count FROM {node} n LEFT 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_post 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);
   }
   else {
-    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, l.last_comment_timestamp AS last_post, 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_post DESC';
+    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, l.last_comment_timestamp AS last_post, l.comment_count FROM {node} n INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 ORDER BY last_post 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);
@@ -103,13 +103,14 @@
         $comments .= l(format_plural($new, '1 new', '@count new'), "node/$node->nid", NULL, NULL, 'new');
       }
     }
-
+    // If comment.module was not enabled when the node was created then last_post can be null
+    $last_post = $node->last_post ? $node->last_post : $node->changed;
     $rows[] = array(
       node_get_types('name', $node->type),
       l($node->title, "node/$node->nid") .' '. theme('mark', node_mark($node->nid, $node->changed)),
       theme('username', $node),
       array('class' => 'replies', 'data' => $comments),
-      t('@time ago', array('@time' => format_interval(time() - $node->last_post)))
+      t('@time ago', array('@time' => format_interval(time() - $last_post)))
     );
   }
 
Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.510
diff -u -r1.510 comment.module
--- modules/comment/comment.module	11 Dec 2006 17:13:45 -0000	1.510
+++ modules/comment/comment.module	12 Dec 2006 03:33:29 -0000
@@ -334,7 +334,9 @@
 
     case 'search result':
       $comments = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $node->nid));
-      return format_plural($comments, '1 comment', '@count comments');
+      // Use intval because $comments can be "false" when nodes were created and comment.module was disabled.
+      return format_plural(intval($comments), '1 comment', '@count comments'); 
+      
 
     case 'rss item':
       if ($node->comment != COMMENT_NODE_DISABLED) {
@@ -1827,18 +1829,30 @@
  * - comment_count: the total number of approved/published comments on this node.
  */
 function _comment_update_node_statistics($nid) {
-  $count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = %d', $nid, COMMENT_PUBLISHED));
+  $result = db_fetch_object(db_query('SELECT COUNT(c.cid) count, ncs.nid  FROM {comments} c LEFT JOIN {node_comment_statistics} ncs ON c.nid = ncs.nid  WHERE c.nid = %d AND c.status = %d GROUP BY ncs.nid', $nid, COMMENT_PUBLISHED));
 
   // comments exist
-  if ($count > 0) {
+  if ($result->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);
+    // From first query result node_comment_statistics has no record. Insert one.
+    if (!isset($result->nid)) {
+      db_query("INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid,  comment_count) values(%d, %d, '%s', %d, %d)", $nid, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $result->count); 
+    }
+    else {
+      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", $result->count, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $nid);
+    }
   }
 
   // no comments
   else {
     $node = db_fetch_object(db_query("SELECT uid, created FROM {node} WHERE nid = %d", $nid));
-    db_query("UPDATE {node_comment_statistics} SET comment_count = 0, last_comment_timestamp = %d, last_comment_name = '', last_comment_uid = %d WHERE nid = %d", $node->created, $node->uid, $nid);
+    // From first query result node_comment_statistics has no record. Insert one.
+    if (!isset($result->nid)) {
+      db_query("INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid,  comment_count) values (%d, %d, '', %d, 0)", $nid, $node->created, $node->uid);  
+    }
+    else {
+      db_query("UPDATE {node_comment_statistics} SET comment_count = 0, last_comment_timestamp = %d, last_comment_name = '', last_comment_uid = %d WHERE nid = %d", $node->created, $node->uid, $nid);
+    }
   }
 }
 
@@ -1892,4 +1906,3 @@
 function vancode2int($c = '00') {
   return base_convert(substr($c, 1), 36, 10);
 }
-
