function _comment_update_node_statistics

7.x comment.module _comment_update_node_statistics($nid)
6.x comment.module _comment_update_node_statistics($nid)

Updates the comment statistics for a given node. This should be called any time a comment is added, deleted, or updated.

The following fields are contained in the node_comment_statistics table.

  • last_comment_timestamp: the timestamp of the last comment for this node or the node create stamp if no comments exist for the node.
  • last_comment_name: the name of the anonymous poster for the last comment
  • last_comment_uid: the uid of the poster for the last comment for this node or the node authors uid if no comments exists for the node.
  • comment_count: the total number of approved/published comments on this node.
4 calls to _comment_update_node_statistics()
comment_admin_overview_submit in drupal-6.x/modules/comment/comment.admin.inc
Process comment_admin_overview form submissions.
comment_confirm_delete_submit in drupal-6.x/modules/comment/comment.admin.inc
Process comment_confirm_delete form submissions.
comment_multiple_delete_confirm_submit in drupal-6.x/modules/comment/comment.admin.inc
Process comment_multiple_delete_confirm form submissions.
comment_save in drupal-6.x/modules/comment/comment.module
Accepts a submission of new or changed comment content.

File

drupal-6.x/modules/comment/comment.module, line 1938
Enables users to comment on published content.

Code

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));

  // comments exist
  if ($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);
  }

  // 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);
  }
}