diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 391a255..d298a6d 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -26,7 +26,7 @@ function comment_uninstall() { */ function comment_install() { // By default, maintain entity statistics for comments. - // @see \Drupal\comment\CommentStorage::updateEntityStatistics(). + // @see \Drupal\comment\CommentStatisticsInterface \Drupal::state()->set('comment.maintain_entity_statistics', TRUE); } diff --git a/core/modules/comment/src/CommentStatistics.php b/core/modules/comment/src/CommentStatistics.php index fbbdff4..b914e1e 100644 --- a/core/modules/comment/src/CommentStatistics.php +++ b/core/modules/comment/src/CommentStatistics.php @@ -68,8 +68,9 @@ public function __construct(Connection $database, AccountInterface $current_user /** * {@inheritdoc} */ - public function read($entities, $entity_type) { - return $this->database->select('comment_entity_statistics', 'ces') + public function read($entities, $entity_type, $accurate = TRUE) { + $options = $accurate ? array() : array('target' => 'replica'); + return $this->database->select('comment_entity_statistics', 'ces', $options) ->fields('ces') ->condition('ces.entity_id', array_keys($entities)) ->condition('ces.entity_type', $entity_type) diff --git a/core/modules/comment/src/CommentStatisticsInterface.php b/core/modules/comment/src/CommentStatisticsInterface.php index dc34e96..0b3c779 100644 --- a/core/modules/comment/src/CommentStatisticsInterface.php +++ b/core/modules/comment/src/CommentStatisticsInterface.php @@ -32,11 +32,15 @@ public function getRankingInfo(); * Array of entities on which commenting is enabled, keyed by id * @param string $entity_type * The entity type of the passed entities. + * @param boolean $accurate + * An indicator of whether the results must be completely up to date. + * If set to FALSE, the function may return slightly out of date information + * while using less processing time. * * @return object[] * Array of statistics records keyed by entity id. */ - public function read($entities, $entity_type); + public function read($entities, $entity_type, $accurate = TRUE); /** * Delete comment statistics records for an entity. diff --git a/core/modules/comment/src/CommentStorage.php b/core/modules/comment/src/CommentStorage.php index 2cffb5e..2c5873e 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -23,43 +23,6 @@ class CommentStorage extends ContentEntityDatabaseStorage implements CommentStorageInterface { /** - * The comment statistics service. - * - * @var \Drupal\comment\CommentStatisticsInterface - */ - protected $statistics; - - /** - * Constructs a CommentStorage object. - * - * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info - * An array of entity info for the entity type. - * @param \Drupal\Core\Database\Connection $database - * The database connection to be used. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. - * @param \Drupal\comment\CommentStatisticsInterface $comment_statistics - * The comment statistics service. - */ - public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, CommentStatisticsInterface $comment_statistics) { - parent::__construct($entity_info, $database, $entity_manager); - $this->statistics = $comment_statistics; - } - - /** - * {@inheritdoc} - */ - public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) { - return new static( - $entity_info, - $container->get('database'), - $container->get('entity.manager'), - $container->get('comment.statistics') - ); - } - - - /** * {@inheritdoc} */ protected function buildQuery($ids, $revision_id = FALSE) { @@ -85,13 +48,6 @@ protected function mapFromStorageRecords(array $records) { /** * {@inheritdoc} */ - public function updateEntityStatistics(CommentInterface $comment) { - $this->statistics->update($comment); - } - - /** - * {@inheritdoc} - */ public function getMaxThread(EntityInterface $comment) { $query = $this->database->select('comment', 'c') ->condition('entity_id', $comment->getCommentedEntityId()) diff --git a/core/modules/comment/src/CommentStorageInterface.php b/core/modules/comment/src/CommentStorageInterface.php index c1587a5..fb23869 100644 --- a/core/modules/comment/src/CommentStorageInterface.php +++ b/core/modules/comment/src/CommentStorageInterface.php @@ -48,22 +48,4 @@ public function getMaxThreadPerThread(EntityInterface $comment); */ public function getChildCids(array $comments); - /** - * Updates the comment statistics for a given node. - * - * The {comment_entity_statistics} table has the following fields: - * - last_comment_timestamp: The timestamp of the last comment for the entity, - * or the entity created timestamp if no comments exist for the entity. - * - last_comment_name: The name of the anonymous poster for the last comment. - * - last_comment_uid: The user ID of the poster for the last comment for - * this entity, or the entity author's user ID if no comments exist for the - * entity. - * - comment_count: The total number of approved/published comments on this - * entity. - * - * @param \Drupal\comment\CommentInterface $comment - * The comment being saved. - */ - public function updateEntityStatistics(CommentInterface $comment); - } diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index d7c7d11..aeea20b 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -75,8 +75,6 @@ public function preSave(EntityStorageInterface $storage) { $thread = $this->getThread(); if (empty($thread)) { if ($this->threadLock) { - // As preSave() is protected, this can only happen when this class - // is extended in a faulty manner. throw new \LogicException('preSave is called again without calling postSave() or releaseThreadLock()'); } if (!$this->hasParentComment()) { @@ -145,7 +143,7 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) { $this->releaseThreadLock(); // Update the {comment_entity_statistics} table prior to executing the hook. - $storage->updateEntityStatistics($this); + \Drupal::service('comment.statistics')->update($this); if ($this->isPublished()) { \Drupal::moduleHandler()->invokeAll('comment_publish', array($this)); } @@ -171,7 +169,7 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti entity_delete_multiple('comment', $child_cids); foreach ($entities as $id => $entity) { - $storage->updateEntityStatistics($entity); + \Drupal::service('comment.statistics')->update($entity); } } diff --git a/core/modules/comment/tests/src/Entity/CommentLockTest.php b/core/modules/comment/tests/src/Entity/CommentLockTest.php index 887d3ca..a058ddd 100644 --- a/core/modules/comment/tests/src/Entity/CommentLockTest.php +++ b/core/modules/comment/tests/src/Entity/CommentLockTest.php @@ -37,6 +37,7 @@ public function testLocks() { $container->set('module_handler', $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface')); $container->set('current_user', $this->getMock('Drupal\Core\Session\AccountInterface')); $container->set('cache.test', $this->getMock('Drupal\Core\Cache\CacheBackendInterface')); + $container->set('comment.statistics', $this->getMock('Drupal\comment\CommentStatisticsInterface')); $container->setParameter('cache_bins', array('cache.test' => 'test')); $container->register('request', 'Symfony\Component\HttpFoundation\Request'); $lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface'); @@ -93,7 +94,10 @@ public function testLocks() { ->method('getListCacheTags') ->will($this->returnValue(array('comments' => TRUE))); $storage = $this->getMock('Drupal\comment\CommentStorageInterface'); + + // preSave() should acquire the lock, which is what's being tested. $comment->preSave($storage); + // Release the lock before exiting the test. $comment->postSave($storage); } diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc index e2fbad1..019bcd4 100644 --- a/core/modules/tracker/tracker.pages.inc +++ b/core/modules/tracker/tracker.pages.inc @@ -47,29 +47,15 @@ function tracker_page($account = NULL) { if (!empty($tracker_data)) { $nids = array_keys($tracker_data); $nodes = node_load_multiple($nids); - // @todo This should be actually filtering on the desired language and just - // fall back to the default language. - $result = db_query(" - SELECT - n.nid, - SUM(l.comment_count) AS comment_count - FROM {node_field_data} n - INNER JOIN {comment_entity_statistics} l - ON n.nid = l.entity_id AND l.entity_type = 'node' - INNER JOIN {users} u - ON n.uid = u.uid - WHERE n.nid IN (:nids) - AND n.default_langcode = 1 - GROUP BY n.nid - ORDER BY n.changed DESC", array( - ':nids' => array_keys($nodes) - ), array('target' => 'replica'))->fetchAllKeyed(); - foreach ($result as $nid => $comment_count) { + + // Enrich the node data. + $result = \Drupal::service('comment.statistics')->read($nodes, 'node', FALSE); + foreach ($result as $nid => $statistics) { $nodes[$nid]->last_activity = $tracker_data[$nid]->changed; - $nodes[$nid]->comment_count = $comment_count; + $nodes[$nid]->comment_count = $statistics->comment_count; } - // Display the data. + // Display the data in its original order. foreach ($nodes as $node) { // Determine the number of comments. $comments = 0;