comment_get_recent

Versions
5 – 7
comment_get_recent($number = 10)

Find the most recent comments that are available to the current user.

This is done in two steps: 1. Query the {node_comment_statistics} table to find n number of nodes that have the most recent comments. This table is indexed on last_comment_timestamp, thus making it a fast query. 2. Load the information from the comments table based on the nids found in step 1.

Parameters

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

Return value

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

▾ 1 function calls 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.

Code

modules/comment/comment.module, line 387

<?php
function comment_get_recent($number = 10) {
  // Step 1: Select a $number of nodes which have new comments,
  //         and are visible to the current user.
  $nids = db_query_range("SELECT nc.nid FROM {node_comment_statistics} nc WHERE nc.comment_count > 0 ORDER BY nc.last_comment_timestamp DESC", 0, $number)->fetchCol();

  $comments = array();
  if (!empty($nids)) {
    // Step 2: From among the comments on the nodes selected in the first query,
    //         find the $number of most recent comments.
    // Using Query Builder here for the IN-Statement.
    $query = db_select('comment', 'c');
    $query->innerJoin('node', 'n', 'n.nid = c.nid');
    return $query
      ->fields('c', array('nid', 'subject', 'cid', 'created', 'changed'))
      ->condition('c.nid', $nids, 'IN')
      ->condition('c.status', COMMENT_PUBLISHED)
      ->condition('n.status', 1)
      ->orderBy('c.cid', 'DESC')
      ->range(0, $number)
      ->execute()
      ->fetchAll();
  }

  return $comments;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.