diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index f02c700..612ee80 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1114,6 +1114,8 @@ function comment_load($cid, $reset = FALSE) { /** * Gets the number of new comments for the current user and the specified node. * + * @deprecated Use \Drupal\comment\CommentManager::getCountNewComments() + * * @param int $entity_id * Entity ID of the entity to which the comments are attached. * @param string $entity_type @@ -1127,45 +1129,8 @@ function comment_load($cid, $reset = FALSE) { * The number of new comments or FALSE if the user is not logged in. */ function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestamp = 0) { - if (\Drupal::currentUser()->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) { - // Retrieve the timestamp at which the current user last viewed this entity. - if (!$timestamp) { - if ($entity_type == 'node') { - $timestamp = history_read($entity_id); - } - else { - $function = $entity_type . '_last_viewed'; - if (function_exists($function)) { - $timestamp = $function($entity_id); - } - else { - // Default to 30 days ago. - // @todo Remove once http://drupal.org/node/1029708 lands. - $timestamp = COMMENT_NEW_LIMIT; - } - } - } - $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT); - - // Use the timestamp to retrieve the number of new comments. - $query = db_select('comment', 'c'); - $query->addExpression('COUNT(cid)'); - $query->condition('c.entity_type', $entity_type) - ->condition('c.entity_id', $entity_id) - ->condition('c.status', CommentInterface::PUBLISHED) - ->condition('c.created', $timestamp, '>'); - if ($field_name) { - // Limit to a particular field. - $query->condition('c.field_id', $entity_type . '__' . $field_name); - } - - return $query->execute() - ->fetchField(); - } - else { - return FALSE; - } - + $entity = entity_load($entity_type, $entity_id); + return \Drupal::service('comment.manager')->getCountNewComments($entity, $field_name, $timestamp); } /** diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index bcfb009..edbca77 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -7,7 +7,7 @@ services: comment.manager: class: Drupal\comment\CommentManager - arguments: ['@field.info', '@entity.manager', '@current_user', '@config.factory', '@string_translation', '@url_generator'] + arguments: ['@field.info', '@entity.manager', '@entity.query', '@current_user', '@config.factory', '@string_translation', '@url_generator'] comment.route_enhancer: class: Drupal\comment\Routing\CommentBundleEnhancer diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc index eab4d3e..0f25b41 100644 --- a/core/modules/comment/comment.tokens.inc +++ b/core/modules/comment/comment.tokens.inc @@ -266,7 +266,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = break; case 'comment-count-new': - $replacements[$original] = comment_num_new($entity->id(), $entity->entityType()); + $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity); break; } } diff --git a/core/modules/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php index e1075b3..095aca6 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManager.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php @@ -11,6 +11,7 @@ use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\TranslationInterface; @@ -36,6 +37,13 @@ class CommentManager implements CommentManagerInterface { protected $entityManager; /** + * The entity query factory. + * + * @var \Drupal\Core\Entity\Query\QueryFactory + */ + protected $queryFactory; + + /** * The current user. * * @var \Drupal\Core\Session\AccountInterface $current_user @@ -77,6 +85,8 @@ class CommentManager implements CommentManagerInterface { * The field info service. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager service. + * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory + * The entity query factory. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. * @param \Drupal\Core\Config\ConfigFactory $config_factory @@ -86,9 +96,10 @@ class CommentManager implements CommentManagerInterface { * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * The url generator service. */ - public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager, AccountInterface $current_user, ConfigFactory $config_factory, TranslationInterface $translation_manager, UrlGeneratorInterface $url_generator) { + public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager, QueryFactory $query_factory, AccountInterface $current_user, ConfigFactory $config_factory, TranslationInterface $translation_manager, UrlGeneratorInterface $url_generator) { $this->fieldInfo = $field_info; $this->entityManager = $entity_manager; + $this->queryFactory = $query_factory; $this->currentUser = $current_user; $this->userConfig = $config_factory->get('user.settings'); $this->translationManager = $translation_manager; @@ -296,6 +307,48 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) { } /** + * {@inheritdoc} + */ + public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0, AccountInterface $account = NULL) { + // @todo Replace module handler with optional history service injection + // after http://drupal.org/node/2081585 + if (\Drupal::currentUser()->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) { + // Retrieve the timestamp at which the current user last viewed this entity. + if (!$timestamp) { + if ($entity->entityType() == 'node') { + $timestamp = history_read($entity->id()); + } + else { + $function = $entity->entityType() . '_last_viewed'; + if (function_exists($function)) { + $timestamp = $function($entity->id()); + } + else { + // Default to 30 days ago. + // @todo Remove once http://drupal.org/node/1029708 lands. + $timestamp = COMMENT_NEW_LIMIT; + } + } + } + $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT); + + // Use the timestamp to retrieve the number of new comments. + $query = $this->queryFactory->get('comment') + ->condition('entity_type', $entity->entityType()) + ->condition('entity_id', $entity->id()) + ->condition('created', $timestamp, '>') + ->condition('status', CommentInterface::PUBLISHED); + if ($field_name) { + // Limit to a particular field. + $query->condition('field_id', $entity->entityType() . '__' . $field_name); + } + + return $query->count()->execute(); + } + return FALSE; + } + + /** * Translates a string to the current language or to a given language. * * See the t() documentation for details. diff --git a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php index 8226e23..029ed3c 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php @@ -6,7 +6,9 @@ */ namespace Drupal\comment; + use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Session\AccountInterface; /** * Comment manager contains common functions to manage comment fields. @@ -104,4 +106,22 @@ public function getFieldUIPageTitle($commented_entity_type, $field_name); */ public function forbiddenMessage(EntityInterface $entity, $field_name); + /** + * Returns the number of new comments available on a given entity for a user. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to which the comments are attached to. + * @param string $field_name + * The field_name to count comments for. Defaults to any field. + * @param int $timestamp + * Time to count from. Defaults to time of last user access the entity. + * @param \Drupal\Core\Session\AccountInterface $account + * The user for which to count the new comments. Defaults to the current + * user. + * + * @return int|false + * The number of new comments or FALSE if the user is not authenticated. + */ + public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0, AccountInterface $account = NULL); + } diff --git a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php index 15f09a7..9bae041 100644 --- a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php +++ b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php @@ -299,7 +299,7 @@ public function renderNewCommentsNodeLinks(Request $request) { $links = array(); foreach ($nids as $nid) { $node = node_load($nid); - $new = comment_num_new($node->id(), 'node'); + $new = $this->commentManager->getCountNewComments($node); $query = comment_new_page_count($node->{$field_name}->comment_count, $new, $node); $links[$nid] = array( 'new_comment_count' => (int) $new, diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php index 0e69739..1a3f274 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php @@ -69,7 +69,7 @@ function testCommentInstallAfterContentModule() { // Drop default comment field added in CommentTestBase::setup(). entity_load('field_entity', 'node.comment')->delete(); - if ($field = $this->container->get('field.info')->getField('node', 'comment_node_forum')) { + if ($field = $this->container->get('field.info')->getField('node', 'comment_forum')) { $field->delete(); } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php index 7b9b9ff..ba93373 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php @@ -103,10 +103,12 @@ function testCommentTokenReplacement() { $tests['[entity:comment-count-new]'] = 2; // Also test the deprecated legacy token. $tests['[node:comment-count]'] = 2; + $tests['[node:comment-count-new]'] = 2; foreach ($tests as $input => $expected) { $output = $token_service->replace($input, array('entity' => $node, 'node' => $node), array('langcode' => $language_interface->id)); $this->assertEqual($output, $expected, format_string('Node comment token %token replaced.', array('%token' => $input))); } } + } diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index 679b52a..cb835d7 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -798,7 +798,7 @@ function template_preprocess_forum_topic_list(&$variables) { $variables['topics'][$id]->new_url = ''; if ($topic->new_replies) { $variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new post in topic %title', '@count new posts in topic %title', 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_node_forum'), 'fragment' => 'new')); + $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')); } } diff --git a/core/modules/forum/forum.services.yml b/core/modules/forum/forum.services.yml index f27ffc0..1817811 100644 --- a/core/modules/forum/forum.services.yml +++ b/core/modules/forum/forum.services.yml @@ -1,7 +1,7 @@ services: forum_manager: class: Drupal\forum\ForumManager - arguments: ['@config.factory', '@entity.manager', '@database', '@field.info', '@string_translation'] + arguments: ['@config.factory', '@entity.manager', '@database', '@field.info', '@string_translation', '@comment.manager'] forum.breadcrumb: class: Drupal\forum\ForumBreadcrumbBuilder arguments: ['@entity.manager', '@config.factory', '@forum_manager'] diff --git a/core/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php index 5c530c7..07fad60 100644 --- a/core/modules/forum/lib/Drupal/forum/ForumManager.php +++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php @@ -12,6 +12,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\comment\CommentInterface; +use Drupal\comment\CommentManagerInterface; use Drupal\field\FieldInfo; use Drupal\node\NodeInterface; @@ -62,6 +63,13 @@ class ForumManager implements ForumManagerInterface { protected $connection; /** + * The comment manager service. + * + * @var \Drupal\comment\CommentManagerInterface + */ + protected $commentManager; + + /** * Array of last post information keyed by forum (term) id. * * @var array @@ -123,13 +131,16 @@ class ForumManager implements ForumManagerInterface { * The field info service. * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager * The translation manager service. + * @param \Drupal\comment\CommentManagerInterface $comment_manager + * The comment manager service. */ - public function __construct(ConfigFactory $config_factory, EntityManagerInterface $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager) { + public function __construct(ConfigFactory $config_factory, EntityManagerInterface $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager, CommentManagerInterface $comment_manager) { $this->configFactory = $config_factory; $this->entityManager = $entity_manager; $this->connection = $connection; $this->fieldInfo = $field_info; $this->translationManager = $translation_manager; + $this->commentManager = $comment_manager; } /** @@ -239,7 +250,7 @@ public function getTopics($tid) { } else { $history = $this->lastVisit($topic->id()); - $topic->new_replies = $this->numberNew($topic->id(), $history); + $topic->new_replies = $this->commentManager->getCountNewComments($topic, 'comment_forum', $history); $topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history); } } @@ -303,21 +314,6 @@ protected function getTopicOrder($sortby) { } /** - * Wraps comment_num_new() in a method. - * - * @param int $nid - * Node ID. - * @param int $timestamp - * Timestamp of last read. - * - * @return int - * Number of new comments. - */ - protected function numberNew($nid, $timestamp) { - return comment_num_new($nid, $timestamp); - } - - /** * Gets the last time the user viewed a node. * * @param int $nid diff --git a/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php b/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php index 8d3be5f..3389e6d 100644 --- a/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php +++ b/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php @@ -9,6 +9,12 @@ use Drupal\Tests\UnitTestCase; +// @todo Remove once the constants are replaced with constants on classes. +// https://drupal.org/node/2169361 +if (!defined('COMMENT_OPEN')) { + define('COMMENT_OPEN', 2); +} + /** * Tests the ForumManager. * @@ -73,12 +79,17 @@ public function testGetIndex() { ->disableOriginalConstructor() ->getMock(); + $comment_manager = $this->getMockBuilder('\Drupal\comment\CommentManager') + ->disableOriginalConstructor() + ->getMock(); + $manager = $this->getMock('\Drupal\forum\ForumManager', array('getChildren'), array( $config_factory, $entity_manager, $connection, $field_info, $translation_manager, + $comment_manager, )); $manager->expects($this->once()) diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc index 332d9ac..72987e6 100644 --- a/core/modules/tracker/tracker.pages.inc +++ b/core/modules/tracker/tracker.pages.inc @@ -74,7 +74,7 @@ function tracker_page($account = NULL) { if ($node->comment_count) { $comments = $node->comment_count; - if ($new = comment_num_new($node->id(), 'node')) { + if ($new = \Drupal::service('comment.manager')->getCountNewComments($node)) { $comments .= '
'; $comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->id(), array('fragment' => 'new')); }