You are here

function comment_get_recent in Drupal 6

Same name and namespace in other branches
  1. 5 modules/comment/comment.module \comment_get_recent()
  2. 7 modules/comment/comment.module \comment_get_recent()

Find a number of recent comments. This is done in two steps. 1. Find the n (specified by $number) nodes that have the most recent comments. This is done by querying node_comment_statistics which has an index on last_comment_timestamp, and is thus a fast query. 2. Loading the information from the comments table based on the nids found in step 1.

Parameters

$number: (optional) The maximum number of comments to find.

Return value

An array of comment objects each containing a nid, subject, cid, and timestamp, or an empty array if there are no recent comments visible to the current user.

1 call to comment_get_recent()
theme_comment_block in modules/comment/comment.module
Returns a formatted list of recent comments to be displayed in the comment block.

File

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

Code

function comment_get_recent($number = 10) {

  // Select the $number nodes (visible to the current user) with the most
  // recent comments. This is efficient due to the index on
  // last_comment_timestamp.
  $result = db_query_range(db_rewrite_sql("SELECT nc.nid FROM {node_comment_statistics} nc WHERE nc.comment_count > 0 ORDER BY nc.last_comment_timestamp DESC", 'nc'), 0, $number);
  $nids = array();
  while ($row = db_fetch_object($result)) {
    $nids[] = $row->nid;
  }
  $comments = array();
  if (!empty($nids)) {

    // From among the comments on the nodes selected in the first query,
    // find the $number most recent comments.
    $result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN (' . implode(',', $nids) . ') AND n.status = 1 AND c.status = %d ORDER BY c.cid DESC', COMMENT_PUBLISHED, 0, $number);
    while ($comment = db_fetch_object($result)) {
      $comments[] = $comment;
    }
  }
  return $comments;
}