Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.822
diff -u -p -r1.822 comment.module
--- modules/comment/comment.module	26 Dec 2009 16:50:08 -0000	1.822
+++ modules/comment/comment.module	30 Dec 2009 05:08:30 -0000
@@ -910,7 +910,7 @@ function comment_links($comment, $node) 
       }
     }
     elseif (user_access('post comments')) {
-      if (comment_access('edit', $comment)) {
+      if (comment_access('edit', $comment, $node)) {
         $links['comment_edit'] = array(
           'title' => t('edit'),
           'href' => "comment/$comment->cid/edit",
@@ -1249,14 +1249,19 @@ function comment_user_cancel($edit, $acc
  *   The operation that is to be performed on the comment. Only 'edit' is recognized now.
  * @param $comment
  *   The comment object.
+ * @param $node
+ *   The node which the comment is attached to, optional.
  * @return
  *   TRUE if the current user has acces to the comment, FALSE otherwise.
  */
-function comment_access($op, $comment) {
+function comment_access($op, $comment, $node = NULL) {
+  if (!isset($node)) {
+    $node = node_load($comment->nid);
+  }
   global $user;
 
   if ($op == 'edit') {
-    return ($user->uid && $user->uid == $comment->uid && comment_num_replies($comment->cid) == 0) || user_access('administer comments');
+    return ($user->uid && $user->uid == $comment->uid && !comment_has_replies($comment->cid, $node)) || user_access('administer comments');
   }
 }
 
@@ -1544,24 +1549,27 @@ class CommentController extends DrupalDe
 }
 
 /**
- * Get replies count for a comment.
+ * Determine if a comment has replies.
  *
- * @param $pid
+ * @param $cid
  *   The comment id.
+ *
  * @return
- *   The replies count.
+ *   Boolean to indicate whether the comment has any replies.
  */
-function comment_num_replies($pid) {
-  $cache = &drupal_static(__FUNCTION__, array());
-
-  if (!isset($cache[$pid])) {
-    $cache[$pid] = db_query('SELECT COUNT(cid) FROM {comment} WHERE pid = :pid AND status = :status', array(
-      ':pid' => $pid,
-      ':status' => COMMENT_PUBLISHED,
-    ))->fetchField();
+function comment_has_replies($cid, $node) {
+  // When comments are flat, they are not considered to have replies, so always
+  // return FALSE in this case.
+  if (variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED) == COMMENT_MODE_FLAT) {
+    return FALSE;
+  }
+  else {
+    $cache = &drupal_static(__FUNCTION__, array());
+    if (!isset($cache[$cid])) {
+      $cache[$cid] = (bool) db_query_range('SELECT 1 FROM {comment} WHERE pid = :cid AND status = 1', 0, 1, array('cid' => $cid))->fetchField();
+    }
+    return $cache[$cid];
   }
-
-  return $cache[$pid];
 }
 
 /**
