Commit d8a4d54e authored by alexpott's avatar alexpott
Browse files

Issue #2281475 by roderik, slashrsm: Deprecate comment_new_page_count() and...

Issue #2281475 by roderik, slashrsm: Deprecate comment_new_page_count() and move it functionality into Comment storage controller.
parent 47d6bc0d
......@@ -235,81 +235,27 @@ function comment_permission() {
/**
* Calculates the page number for the first new comment.
*
* @param int $num_comments
* Number of comments.
* @param int $new_replies
* Number of new replies.
* @param int $total_comments
* The total number of comments that the entity has.
* @param int $new_comments
* The number of new comments that the entity has.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The first new comment entity.
* The entity to which the comments belong.
* @param string $field_name
* The field name on the entity to which comments are attached to.
* The field name on the entity to which comments are attached.
*
* @return array|null
* An array "page=X" if the page number is greater than zero; NULL otherwise.
*
* @deprecated Deprecated since Drupal 8.x-dev, to be removed in Drupal 8.0.
* Use \Drupal\comment\CommentStorageInterface::getNewCommentPageNumber();
* Note this returns an integer instead of array|null.
*/
function comment_new_page_count($num_comments, $new_replies, ContentEntityInterface $entity, $field_name = 'comment') {
$field_definition = $entity->getFieldDefinition($field_name);
$mode = $field_definition->getSetting('default_mode');
$comments_per_page = $field_definition->getSetting('per_page');
$pagenum = NULL;
$flat = $mode == CommentManagerInterface::COMMENT_MODE_FLAT ? TRUE : FALSE;
if ($num_comments <= $comments_per_page) {
// Only one page of comments.
$pageno = 0;
}
elseif ($flat) {
// Flat comments.
$count = $num_comments - $new_replies;
$pageno = $count / $comments_per_page;
}
else {
// Threaded comments: we build a query with a subquery to find the first
// thread with a new comment.
// 1. Find all the threads with a new comment.
$unread_threads_query = db_select('comment')
->fields('comment', array('thread'))
->condition('entity_id', $entity->id())
->condition('entity_type', $entity->getEntityTypeId())
->condition('field_name', $field_name)
->condition('status', CommentInterface::PUBLISHED)
->orderBy('created', 'DESC')
->orderBy('cid', 'DESC')
->range(0, $new_replies);
// 2. Find the first thread.
$first_thread_query = db_select($unread_threads_query, 'thread');
$first_thread_query->addExpression('SUBSTRING(thread, 1, (LENGTH(thread) - 1))', 'torder');
$first_thread = $first_thread_query
->fields('thread', array('thread'))
->orderBy('torder')
->range(0, 1)
->execute()
->fetchField();
// Remove the final '/'.
$first_thread = substr($first_thread, 0, -1);
// Find the number of the first comment of the first unread thread.
$count = db_query('SELECT COUNT(*) FROM {comment} WHERE entity_id = :entity_id
AND entity_type = :entity_type
AND field_name = :field_name
AND status = :status AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array(
':status' => CommentInterface::PUBLISHED,
':entity_id' => $entity->id(),
':field_name' => $field_name,
':entity_type' => $entity->getEntityTypeId(),
':thread' => $first_thread,
))->fetchField();
$pageno = $count / $comments_per_page;
}
if ($pageno >= 1) {
$pagenum = array('page' => intval($pageno));
}
function comment_new_page_count($total_comments, $new_comments, ContentEntityInterface $entity, $field_name = 'comment') {
$page_number = \Drupal::entityManager()->getStorage('comment')
->getNewCommentPageNumber($total_comments, $new_comments, $entity, $field_name);
return $pagenum;
return $page_number ? array('page' => $page_number) : NULL;
}
/**
......
......@@ -8,7 +8,7 @@
namespace Drupal\comment;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\ContentEntityDatabaseStorage;
......@@ -80,7 +80,7 @@ public function updateEntityStatistics(CommentInterface $comment) {
/**
* {@inheritdoc}
*/
public function getMaxThread(EntityInterface $comment) {
public function getMaxThread(CommentInterface $comment) {
$query = $this->database->select('comment', 'c')
->condition('entity_id', $comment->getCommentedEntityId())
->condition('field_name', $comment->getFieldName())
......@@ -93,7 +93,7 @@ public function getMaxThread(EntityInterface $comment) {
/**
* {@inheritdoc}
*/
public function getMaxThreadPerThread(EntityInterface $comment) {
public function getMaxThreadPerThread(CommentInterface $comment) {
$query = $this->database->select('comment', 'c')
->condition('entity_id', $comment->getCommentedEntityId())
->condition('field_name', $comment->getFieldName())
......@@ -136,6 +136,64 @@ public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $div
return ($divisor > 1) ? floor($ordinal / $divisor) : $ordinal;
}
/**
* {@inheritdoc}
*/
public function getNewCommentPageNumber($total_comments, $new_comments, ContentEntityInterface $entity, $field_name = 'comment') {
$instance = $entity->getFieldDefinition($field_name);
$comments_per_page = $instance->getSetting('per_page');
if ($total_comments <= $comments_per_page) {
// Only one page of comments.
$count = 0;
}
elseif ($instance->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) {
// Flat comments.
$count = $total_comments - $new_comments;
}
else {
// Threaded comments.
// 1. Find all the threads with a new comment.
$unread_threads_query = $this->database->select('comment')
->fields('comment', array('thread'))
->condition('entity_id', $entity->id())
->condition('entity_type', $entity->getEntityTypeId())
->condition('field_name', $field_name)
->condition('status', CommentInterface::PUBLISHED)
->orderBy('created', 'DESC')
->orderBy('cid', 'DESC')
->range(0, $new_comments);
// 2. Find the first thread.
$first_thread_query = $this->database->select($unread_threads_query, 'thread');
$first_thread_query->addExpression('SUBSTRING(thread, 1, (LENGTH(thread) - 1))', 'torder');
$first_thread = $first_thread_query
->fields('thread', array('thread'))
->orderBy('torder')
->range(0, 1)
->execute()
->fetchField();
// Remove the final '/'.
$first_thread = substr($first_thread, 0, -1);
// Find the number of the first comment of the first unread thread.
$count = $this->database->query('SELECT COUNT(*) FROM {comment} WHERE entity_id = :entity_id
AND entity_type = :entity_type
AND field_name = :field_name
AND status = :status AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array(
':status' => CommentInterface::PUBLISHED,
':entity_id' => $entity->id(),
':field_name' => $field_name,
':entity_type' => $entity->getEntityTypeId(),
':thread' => $first_thread,
))->fetchField();
}
return $comments_per_page > 0 ? (int) ($count / $comments_per_page) : 0;
}
/**
* {@inheritdoc}
*/
......
......@@ -7,7 +7,7 @@
namespace Drupal\comment;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
/**
......@@ -16,27 +16,44 @@
interface CommentStorageInterface extends EntityStorageInterface {
/**
* Get the maximum encoded thread value for the top level comments.
* Gets the maximum encoded thread value for the top level comments.
*
* @param EntityInterface $comment
* @param \Drupal\comment\CommentInterface $comment
* A comment entity.
*
* @return string
* The maximum encoded thread value among the top level comments of the
* node $comment belongs to.
*/
public function getMaxThread(EntityInterface $comment);
public function getMaxThread(CommentInterface $comment);
/**
* Get the maximum encoded thread value for the children of this comment.
* Gets the maximum encoded thread value for the children of this comment.
*
* @param EntityInterface $comment
* @param \Drupal\comment\CommentInterface $comment
* A comment entity.
*
* @return string
* The maximum encoded thread value among all replies of $comment.
*/
public function getMaxThreadPerThread(EntityInterface $comment);
public function getMaxThreadPerThread(CommentInterface $comment);
/**
* Calculates the page number for the first new comment.
*
* @param int $total_comments
* The total number of comments that the entity has.
* @param int $new_comments
* The number of new comments that the entity has.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity to which the comments belong.
* @param string $field_name
* The field name on the entity to which comments are attached.
*
* @return array|null
* The page number where first new comment appears. (First page returns 0.)
*/
public function getNewCommentPageNumber($total_comments, $new_comments, ContentEntityInterface $entity, $field_name = 'comment');
/**
* Gets the display ordinal or page number for a comment.
......
......@@ -337,7 +337,10 @@ public static function attachNewCommentsLinkMetadata(array $element, array $cont
->getStorage($context['entity_type'])
->load($context['entity_id']);
$field_name = $context['field_name'];
$query = comment_new_page_count($entity->{$field_name}->comment_count, $new, $entity);
$page_number = \Drupal::entityManager()
->getStorage('comment')
->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new, $entity);
$query = $page_number ? array('page' => $page_number) : NULL;
// Attach metadata.
$element['#attached']['js'][] = array(
......
......@@ -290,7 +290,9 @@ public function renderNewCommentsNodeLinks(Request $request) {
foreach ($nids as $nid) {
$node = node_load($nid);
$new = $this->commentManager->getCountNewComments($node);
$query = comment_new_page_count($node->{$field_name}->comment_count, $new, $node);
$page_number = $this->entityManager()->getStorage('comment')
->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node);
$query = $page_number ? array('page' => $page_number) : NULL;
$links[$nid] = array(
'new_comment_count' => (int) $new,
'first_new_comment_link' => $this->getUrlGenerator()->generateFromPath('node/' . $node->id(), array('query' => $query, 'fragment' => 'new')),
......
......@@ -151,9 +151,11 @@ protected function renderLink($data, ResultRow $values) {
'nid' => $this->getValue($values, 'nid'),
'type' => $this->getValue($values, 'type'),
));
$page_number = \Drupal::entityManager()->getStorage('comment')
->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node);
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = 'node/' . $node->id();
$this->options['alter']['query'] = comment_new_page_count($this->getValue($values, 'comment_count'), $this->getValue($values), $node);
$this->options['alter']['query'] = $page_number ? array('page' => $page_number) : NULL;
$this->options['alter']['fragment'] = 'new';
}
......
......@@ -247,8 +247,8 @@ function testCommentNewPageIndicator() {
$node = node_load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
$returned = comment_new_page_count($node->get('comment')->comment_count, $new_replies, $node);
$returned_page = is_array($returned) ? $returned['page'] : 0;
$returned_page = \Drupal::entityManager()->getStorage('comment')
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node);
$this->assertIdentical($expected_page, $returned_page, format_string('Flat mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
}
......@@ -266,8 +266,8 @@ function testCommentNewPageIndicator() {
\Drupal::entityManager()->getStorage('node')->resetCache(array($node->id()));
$node = node_load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
$returned = comment_new_page_count($node->get('comment')->comment_count, $new_replies, $node);
$returned_page = is_array($returned) ? $returned['page'] : 0;
$returned_page = \Drupal::entityManager()->getStorage('comment')
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node);
$this->assertEqual($expected_page, $returned_page, format_string('Threaded mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
}
}
......
......@@ -573,8 +573,11 @@ function template_preprocess_forums(&$variables) {
$variables['topics'][$id]->new_url = '';
if ($topic->new_replies) {
$page_number = \Drupal::entityManager()->getStorage('comment')
->getNewCommentPageNumber($topic->comment_count, $topic->new_replies, $topic, 'comment_forum');
$query = $page_number ? array('page' => $page_number) : NULL;
$variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new post<span class="visually-hidden"> in topic %title</span>', '@count new posts<span class="visually-hidden"> in topic %title</span>', array('%title' => $variables['topics'][$id]->label()));
$variables['topics'][$id]->new_url = url('node/' . $topic->id(), array('query' => comment_new_page_count($topic->comment_count, $topic->new_replies, $topic, 'comment_forum'), 'fragment' => 'new'));
$variables['topics'][$id]->new_url = url('node/' . $topic->id(), array('query' => $query, 'fragment' => 'new'));
}
// Build table rows from topics.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment