In the nodeapi the comment statistics are loaded for every node_load.

function comment_nodeapi(&$node, $op, $arg = 0) {
  switch ($op) {
    case 'load':
        return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, comment_count FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
      break;

Why not make it something like this?

function comment_nodeapi(&$node, $op, $arg = 0) {
  switch ($op) {
    case 'load':
      if (variable_get('comment_'.$node->type, COMMENT_NODE_READ_WRITE) != COMMENT_NODE_DISABLED) {
        return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, comment_count FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
      }
      break;

NB: I found this in drupal 5.7 but checked and it still applies to 6 and 7-dev.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

StevenPatz’s picture

Status: Needs review » Active

There is no patch attached.

R.Muilwijk’s picture

Status: Active » Needs review
FileSize
607 bytes

My bad here it is

catch’s picture

Status: Needs review » Needs work

Patch looks sensible, however you've not indented within the new if statement.

catch’s picture

Category: feature » bug

And this qualifies as a bug IMO.

R.Muilwijk’s picture

Category: bug » feature
Status: Needs work » Needs review
FileSize
774 bytes

Sorry catch I am not really familiar yet with the categories.

New patch is up

R.Muilwijk’s picture

Category: feature » bug

Huh somehow I changed it back to feature request. Back to bug report

Dries’s picture

Status: Needs review » Fixed

I've fixed a code style issue in your patch, and I've committed it to CVS HEAD.

Damien Tournoud’s picture

Status: Fixed » Needs work

The patch is wrong: the display of comments on a particular node is governed not by the type-wide parameter 'comment_'.$node->type but by the node-specific parameter $node->comment. See node_show():

  if (function_exists('comment_render') && $node->comment) {
    $output .= comment_render($node, $cid);
  }

Oh, by the way: the current comment count (as shown for example in the tracker) is actually wrong (it should be 0 when comments are disabled).

Dries’s picture

You're right, Daniel. I've rolled back the patch. This illustrates the need to write tests for patches.

catch’s picture

We could presumably do the same thing with if ($node->comment) right?

Damien Tournoud’s picture

@Dries: No hard feelings, but it also illustrates the importance of the community review :)

@catch: completely.

R.Muilwijk’s picture

Status: Needs work » Needs review
FileSize
727 bytes

new patch making the code like this:

function comment_nodeapi(&$node, $op, $arg = 0) {
  switch ($op) {
    case 'load':
      if ($node->comment != COMMENT_NODE_DISABLED) {
        return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, comment_count FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
      }
      break;
R.Muilwijk’s picture

Could someone please review the patch applied above.

Here are the backported patches for 6-dev and 5-dev.

chx’s picture

Status: Needs review » Reviewed & tested by the community

Yeah -- for read only you still display them so it's a nice catch.

R.Muilwijk’s picture

the 14th a comment code cleanup was submitted. Patch is still working however.

patch -p0 < comment_nodeapi_load_1.patch
patching file modules/comment/comment.module

R.Muilwijk’s picture

http://drupal.org/files/issues/comment_nodeapi_load_1.patch is ready to be comitted. Hope to get it backported to 6 soon

R.Muilwijk’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
3.54 KB

The patch only applied with a offset. I added a test for checking whether or not the comment pager works. Comment pager uses the information form nodeapi load so it should test whether or not this damages anything.

The test passes before and after applying patch. Because of this addition the status is back to patch (code needs review).

catch’s picture

Status: Needs review » Reviewed & tested by the community

Test looks and works good, and is worthwhile in it's own right.

Dries’s picture

Version: 7.x-dev » 6.x-dev

Thanks for writing these tests. Much appreciated. Committed to CVS HEAD.

R.Muilwijk’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1.08 KB

6 doesn't have the .test file so the patch for 6 only contains the .module patch.

R.Muilwijk’s picture

FileSize
938 bytes

Unfortunatly there was a little typo in my patch. For head the following patch is provided. The patch for 6.x is already fixed and valid.

R.Muilwijk’s picture

Version: 6.x-dev » 7.x-dev
Dries’s picture

Version: 7.x-dev » 6.x-dev

I've committed #22 to HEAD, but I leave #21 for Gabor to review. Lowering version to 6.x.

Thanks R.

samirnassar’s picture

patch at #21 is not necessary.

patch at #20 applies cleanly.

Is there a way I should test the patch further?

m3avrck’s picture

Can't this patch be further expanded to improve the search index and results as well?

@@ -412,15 +417,23 @@
 
     case 'update index':
       $text = '';
-      $comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = %d', $node->nid, COMMENT_PUBLISHED);
-      while ($comment = db_fetch_object($comments)) {
-        $text .= '<h2>'. check_plain($comment->subject) .'</h2>'. check_markup($comment->comment, $comment->format, FALSE);
+      if ($node->comment != COMMENT_NODE_DISABLED) {
+        $comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = %d', $node->nid, COMMENT_PUBLISHED);
+        while ($comment = db_fetch_object($comments)) {
+          $text .= '<h2>'. check_plain($comment->subject) .'</h2>'. check_markup($comment->comment, $comment->format, FALSE);
+        }
       }
       return $text;
 
     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');
+      if ($node->comment != COMMENT_NODE_DISABLED) {
+        $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');
+      }
+      else {
+        return '';
+      }
+      break;      
andypost’s picture

Suppose comments should be loaded anyway because I can change $node->comment by editing node params for example to stop flame but for history purposes I need ability to watch them. This patch stops ability to load old comments and only way put duplicate code of nodeapi in own module.

Damien Tournoud’s picture

@andypost: that's the way it currently works. Your module can really call comment_nodeapi() manually after altering the $node->comment parameter if needed.

@m3avrck: in fact it really should... so as _nodeapi($op = 'update index'), by the way.

m3avrck’s picture

FileSize
2.22 KB
2.72 KB
1.99 KB

Ok here are 3 patches...

  • For D7, add $node->comment checking to search ops
  • For D6, entire patch, with statistics and search ops
  • For D5, ditto (I know this won't be committed but I am running it on MothersClick.com for anyone that may want it)
m3avrck’s picture

Bumping this still needs attention & review...

catch’s picture

Version: 6.x-dev » 7.x-dev

Seems like the search patch should go into D7 (where it'll get more attention), then it'll just be a straight backport of all changes to Drupal 6. So bumping this back up for a bit.

Status: Needs review » Needs work

The last submitted patch failed testing.

sun’s picture

The function signature of check_markup() changed.

Those queries might as well be converted to DBTNG when touching them.

andypost’s picture

Status: Needs work » Needs review
FileSize
1.89 KB

So this is reroll with checking for hidden comments

andypost’s picture

Before print link in RSS there should be a check if comments enabled

So maybe CommentRSSUnitTest should be changed? or this for another issue #335893: Option to not include comment element in Feeds

andypost’s picture

Issue tags: +Needs backport to D6
FileSize
2.72 KB

Patch is here

Status: Needs review » Needs work

The last submitted patch failed testing.

andypost’s picture

Status: Needs work » Needs review
FileSize
1.89 KB
andypost’s picture

FileSize
1.93 KB

reroll against current HEAD

catch’s picture

Patch makes sense to me. It's a slight change to search behaviour - currently we index comments with nodes even if they're hidden, but hide them on view (but you could still see the comment at comment/1 for example). Now they'll not be indexed either.

Doesn't look like there's any existing tests for comments appearing in the search index (or those nodeapi hooks in general). Would you be up for writing those? Looking at the search test, it seems like it'd be mainly copying and pasting from one of the existing tests which creates a node and indexes it, but adding a comment and changing the settings.

andypost’s picture

FileSize
3.13 KB

So here patch with tests included

Added test to modules/search/search.test
Logic - change node to hide comments, reindex search, try to find comment body

catch’s picture

Status: Needs review » Reviewed & tested by the community

Looks good.

Dries’s picture

Version: 7.x-dev » 6.x-dev
Status: Reviewed & tested by the community » Needs work

Committed to CVS HEAD. If we want to back-port this to Drupal 6 (per the tag), we'll have to re-roll.

andypost’s picture

Status: Needs work » Needs review
FileSize
2.37 KB

reroll for D6 against current 6-dev

R.Muilwijk’s picture

Status: Needs review » Reviewed & tested by the community

Works oke for me on d6

Status: Reviewed & tested by the community » Needs work

The last submitted patch, comment-query.patch, failed testing.

Anonymous’s picture

Status: Needs work » Closed (won't fix)

I'm going to guess this is a won't fix for D6, since D6 commits focus on security fixes at this point.