subscriptions is sending wrong urls if a new comment is not on the 1st page of comments

urls now look like
http://www.example.com/?q=node/1234#comment-5738

but should look like
http://www.example.com/?q=node/1234&page=3#comment-5738

i think we need a function to figure out the number of a page with new comment

i found some similar functions but i dont have enough knowledge to use or implement them. Any help would be appreciated.

<?php
 /**
 * Return an URL query string that points to the page the comment is on.
 * Used in links to comments (e.g. 'recent comments' block). 
 *
 * @param $comment 
 *   A comment object.
 *   Required fields: 'nid', 'timestamp', 'thread'
 *   Optional field: 'comment_count' (to save a db query get this field by
 *     doing a JOIN of the node_comment_statistics on the comments table)
 */
function comment_page_query($comment) {
  $page = _comment_page($comment);
  return $page ? "page=$page" : NULL;
}

/**
 * Return an URL query string that points to the page the node's first new
 * comment is on. Used in links to new comments ('# new' links). 
 *
 * @param $nid
 *   A node ID.
 */
function comment_page_new_query($nid) {
  $comments_per_page = _comment_get_display_setting('comments_per_page');
  $comments_num = comment_num_all($nid, user_access('administer comments'));
  if ($comments_num <= $comments_per_page) {
    // one page of comments only
    return NULL;
  }
  else {
    // get the node's first new comment
    $cid = _comment_new_comments($nid, 'first');
    $comment = db_fetch_object(db_query('SELECT cid, nid, timestamp, thread FROM {comments} WHERE cid = %d', $cid));
    return comment_page_query($comment);
  }
}

/**
 * Return the page a comment is on
 */
function _comment_page($comment) {
  $comments_per_page = _comment_get_display_setting('comments_per_page');
  if (user_access('administer comments')) {
    // For users with 'administer comments' permission
    // we have to count published and unpublished comments.
    $comments_num = comment_num_all($comment->nid, TRUE);
  }
  else {
    $comments_num = isset($comment->comment_count) ? $comment->comment_count : comment_num_all($comment->nid);
  }
  if ($comments_num <= $comments_per_page) {
    // one page of comments only
    return 0;
  }
  else {
    // Build the database query that retrieves the comment's position
    // This follows the same sheme as in comment_render().
    // See comments there for an explanation.
    $query = 'SELECT COUNT(*) FROM {comments} WHERE nid = %d';
    $query_args = array($comment->nid);
    
    if (!user_access('administer comments')) {
      $query .= ' AND status = %d';
      $query_args[] = COMMENT_PUBLISHED;
    }

    $mode = _comment_get_display_setting('mode');
    $order = _comment_get_display_setting('sort');
    if ($order == COMMENT_ORDER_NEWEST_FIRST) {
      if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
        $query .= ' AND timestamp > %d';
        $query_args[] = $comment->timestamp;
      }
      else {
        $query .= " AND thread > '%s'";
        $query_args[] = $comment->thread;
      }
    }
    else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
      if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
        $query .= ' AND timestamp < %d';
        $query_args[] = $comment->timestamp;
      }
      else {
        $query .= " AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < '%s'";
        $query_args[] = substr($comment->thread, 0, -1);
      }
    }
    
    $count = db_result(db_query($query, $query_args));
    return floor($count / $comments_per_page);
  }
}
?>

Comments

arthurf’s picture

Status: Active » Closed (fixed)

Thanks for including the code snippit. I created a function to generate the page tag and have included it in the 4.7 and 5.0 versions.