You are here

function _comment_update_node_statistics in Drupal 7

Same name and namespace in other branches
  1. 4 modules/comment.module \_comment_update_node_statistics()
  2. 5 modules/comment/comment.module \_comment_update_node_statistics()
  3. 6 modules/comment/comment.module \_comment_update_node_statistics()

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.
2 calls to _comment_update_node_statistics()
comment_delete_multiple in modules/comment/comment.module
Delete comments and all their replies.
comment_save in modules/comment/comment.module
Accepts a submission of new or changed comment content.

File

modules/comment/comment.module, line 2446
Enables users to comment on published content.

Code

function _comment_update_node_statistics($nid) {

  // Allow bulk updates and inserts to temporarily disable the
  // maintenance of the {node_comment_statistics} table.
  if (!variable_get('comment_maintain_node_statistics', TRUE)) {
    return;
  }
  $count = db_query('SELECT COUNT(cid) FROM {comment} WHERE nid = :nid AND status = :status', array(
    ':nid' => $nid,
    ':status' => COMMENT_PUBLISHED,
  ))
    ->fetchField();
  if ($count > 0) {

    // Comments exist.
    $last_reply = db_query_range('SELECT cid, name, changed, uid FROM {comment} WHERE nid = :nid AND status = :status ORDER BY cid DESC', 0, 1, array(
      ':nid' => $nid,
      ':status' => COMMENT_PUBLISHED,
    ))
      ->fetchObject();
    db_update('node_comment_statistics')
      ->fields(array(
      'cid' => $last_reply->cid,
      'comment_count' => $count,
      'last_comment_timestamp' => $last_reply->changed,
      'last_comment_name' => $last_reply->uid ? '' : $last_reply->name,
      'last_comment_uid' => $last_reply->uid,
    ))
      ->condition('nid', $nid)
      ->execute();
  }
  else {

    // Comments do not exist.
    $node = db_query('SELECT uid, created FROM {node} WHERE nid = :nid', array(
      ':nid' => $nid,
    ))
      ->fetchObject();
    db_update('node_comment_statistics')
      ->fields(array(
      'cid' => 0,
      'comment_count' => 0,
      'last_comment_timestamp' => $node->created,
      'last_comment_name' => '',
      'last_comment_uid' => $node->uid,
    ))
      ->condition('nid', $nid)
      ->execute();
  }
}