CommentStatistics.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace Drupal\comment;
  3. use Drupal\Core\Database\Connection;
  4. use Drupal\Core\Entity\FieldableEntityInterface;
  5. use Drupal\Core\Entity\EntityChangedInterface;
  6. use Drupal\Core\Entity\EntityInterface;
  7. use Drupal\Core\Entity\EntityManagerInterface;
  8. use Drupal\Core\State\StateInterface;
  9. use Drupal\Core\Session\AccountInterface;
  10. use Drupal\user\EntityOwnerInterface;
  11. class CommentStatistics implements CommentStatisticsInterface {
  12. /**
  13. * The current database connection.
  14. *
  15. * @var \Drupal\Core\Database\Connection
  16. */
  17. protected $database;
  18. /**
  19. * The current logged in user.
  20. *
  21. * @var \Drupal\Core\Session\AccountInterface
  22. */
  23. protected $currentUser;
  24. /**
  25. * The entity manager service.
  26. *
  27. * @var \Drupal\Core\Entity\EntityManagerInterface
  28. */
  29. protected $entityManager;
  30. /**
  31. * The state service.
  32. *
  33. * @var \Drupal\Core\State\StateInterface
  34. */
  35. protected $state;
  36. /**
  37. * Constructs the CommentStatistics service.
  38. *
  39. * @param \Drupal\Core\Database\Connection $database
  40. * The active database connection.
  41. * @param \Drupal\Core\Session\AccountInterface $current_user
  42. * The current logged in user.
  43. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  44. * The entity manager service.
  45. * @param \Drupal\Core\State\StateInterface $state
  46. * The state service.
  47. */
  48. public function __construct(Connection $database, AccountInterface $current_user, EntityManagerInterface $entity_manager, StateInterface $state) {
  49. $this->database = $database;
  50. $this->currentUser = $current_user;
  51. $this->entityManager = $entity_manager;
  52. $this->state = $state;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function read($entities, $entity_type, $accurate = TRUE) {
  58. $options = $accurate ? [] : ['target' => 'replica'];
  59. $stats = $this->database->select('comment_entity_statistics', 'ces', $options)
  60. ->fields('ces')
  61. ->condition('ces.entity_id', array_keys($entities), 'IN')
  62. ->condition('ces.entity_type', $entity_type)
  63. ->execute();
  64. $statistics_records = [];
  65. while ($entry = $stats->fetchObject()) {
  66. $statistics_records[] = $entry;
  67. }
  68. return $statistics_records;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function delete(EntityInterface $entity) {
  74. $this->database->delete('comment_entity_statistics')
  75. ->condition('entity_id', $entity->id())
  76. ->condition('entity_type', $entity->getEntityTypeId())
  77. ->execute();
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function create(FieldableEntityInterface $entity, $fields) {
  83. $query = $this->database->insert('comment_entity_statistics')
  84. ->fields([
  85. 'entity_id',
  86. 'entity_type',
  87. 'field_name',
  88. 'cid',
  89. 'last_comment_timestamp',
  90. 'last_comment_name',
  91. 'last_comment_uid',
  92. 'comment_count',
  93. ]);
  94. foreach ($fields as $field_name => $detail) {
  95. // Skip fields that entity does not have.
  96. if (!$entity->hasField($field_name)) {
  97. continue;
  98. }
  99. // Get the user ID from the entity if it's set, or default to the
  100. // currently logged in user.
  101. $last_comment_uid = 0;
  102. if ($entity instanceof EntityOwnerInterface) {
  103. $last_comment_uid = $entity->getOwnerId();
  104. }
  105. if (!isset($last_comment_uid)) {
  106. // Default to current user when entity does not implement
  107. // EntityOwnerInterface or author is not set.
  108. $last_comment_uid = $this->currentUser->id();
  109. }
  110. // Default to REQUEST_TIME when entity does not have a changed property.
  111. $last_comment_timestamp = REQUEST_TIME;
  112. // @todo Make comment statistics language aware and add some tests. See
  113. // https://www.drupal.org/node/2318875
  114. if ($entity instanceof EntityChangedInterface) {
  115. $last_comment_timestamp = $entity->getChangedTimeAcrossTranslations();
  116. }
  117. $query->values([
  118. 'entity_id' => $entity->id(),
  119. 'entity_type' => $entity->getEntityTypeId(),
  120. 'field_name' => $field_name,
  121. 'cid' => 0,
  122. 'last_comment_timestamp' => $last_comment_timestamp,
  123. 'last_comment_name' => NULL,
  124. 'last_comment_uid' => $last_comment_uid,
  125. 'comment_count' => 0,
  126. ]);
  127. }
  128. $query->execute();
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getMaximumCount($entity_type) {
  134. return $this->database->query('SELECT MAX(comment_count) FROM {comment_entity_statistics} WHERE entity_type = :entity_type', [':entity_type' => $entity_type])->fetchField();
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function getRankingInfo() {
  140. return [
  141. 'comments' => [
  142. 'title' => t('Number of comments'),
  143. 'join' => [
  144. 'type' => 'LEFT',
  145. 'table' => 'comment_entity_statistics',
  146. 'alias' => 'ces',
  147. // Default to comment field as this is the most common use case for
  148. // nodes.
  149. 'on' => "ces.entity_id = i.sid AND ces.entity_type = 'node' AND ces.field_name = 'comment'",
  150. ],
  151. // Inverse law that maps the highest view count on the site to 1 and 0
  152. // to 0. Note that the ROUND here is necessary for PostgreSQL and SQLite
  153. // in order to ensure that the :comment_scale argument is treated as
  154. // a numeric type, because the PostgreSQL PDO driver sometimes puts
  155. // values in as strings instead of numbers in complex expressions like
  156. // this.
  157. 'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * (ROUND(:comment_scale, 4)))',
  158. 'arguments' => [':comment_scale' => \Drupal::state()->get('comment.node_comment_statistics_scale') ?: 0],
  159. ],
  160. ];
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function update(CommentInterface $comment) {
  166. // Allow bulk updates and inserts to temporarily disable the maintenance of
  167. // the {comment_entity_statistics} table.
  168. if (!$this->state->get('comment.maintain_entity_statistics')) {
  169. return;
  170. }
  171. $query = $this->database->select('comment_field_data', 'c');
  172. $query->addExpression('COUNT(cid)');
  173. $count = $query->condition('c.entity_id', $comment->getCommentedEntityId())
  174. ->condition('c.entity_type', $comment->getCommentedEntityTypeId())
  175. ->condition('c.field_name', $comment->getFieldName())
  176. ->condition('c.status', CommentInterface::PUBLISHED)
  177. ->condition('default_langcode', 1)
  178. ->execute()
  179. ->fetchField();
  180. if ($count > 0) {
  181. // Comments exist.
  182. $last_reply = $this->database->select('comment_field_data', 'c')
  183. ->fields('c', ['cid', 'name', 'changed', 'uid'])
  184. ->condition('c.entity_id', $comment->getCommentedEntityId())
  185. ->condition('c.entity_type', $comment->getCommentedEntityTypeId())
  186. ->condition('c.field_name', $comment->getFieldName())
  187. ->condition('c.status', CommentInterface::PUBLISHED)
  188. ->condition('default_langcode', 1)
  189. ->orderBy('c.created', 'DESC')
  190. ->range(0, 1)
  191. ->execute()
  192. ->fetchObject();
  193. // Use merge here because entity could be created before comment field.
  194. $this->database->merge('comment_entity_statistics')
  195. ->fields([
  196. 'cid' => $last_reply->cid,
  197. 'comment_count' => $count,
  198. 'last_comment_timestamp' => $last_reply->changed,
  199. 'last_comment_name' => $last_reply->uid ? '' : $last_reply->name,
  200. 'last_comment_uid' => $last_reply->uid,
  201. ])
  202. ->keys([
  203. 'entity_id' => $comment->getCommentedEntityId(),
  204. 'entity_type' => $comment->getCommentedEntityTypeId(),
  205. 'field_name' => $comment->getFieldName(),
  206. ])
  207. ->execute();
  208. }
  209. else {
  210. // Comments do not exist.
  211. $entity = $comment->getCommentedEntity();
  212. // Get the user ID from the entity if it's set, or default to the
  213. // currently logged in user.
  214. if ($entity instanceof EntityOwnerInterface) {
  215. $last_comment_uid = $entity->getOwnerId();
  216. }
  217. if (!isset($last_comment_uid)) {
  218. // Default to current user when entity does not implement
  219. // EntityOwnerInterface or author is not set.
  220. $last_comment_uid = $this->currentUser->id();
  221. }
  222. $this->database->update('comment_entity_statistics')
  223. ->fields([
  224. 'cid' => 0,
  225. 'comment_count' => 0,
  226. // Use the changed date of the entity if it's set, or default to
  227. // REQUEST_TIME.
  228. 'last_comment_timestamp' => ($entity instanceof EntityChangedInterface) ? $entity->getChangedTimeAcrossTranslations() : REQUEST_TIME,
  229. 'last_comment_name' => '',
  230. 'last_comment_uid' => $last_comment_uid,
  231. ])
  232. ->condition('entity_id', $comment->getCommentedEntityId())
  233. ->condition('entity_type', $comment->getCommentedEntityTypeId())
  234. ->condition('field_name', $comment->getFieldName())
  235. ->execute();
  236. }
  237. // Reset the cache of the commented entity so that when the entity is loaded
  238. // the next time, the statistics will be loaded again.
  239. $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->resetCache([$comment->getCommentedEntityId()]);
  240. }
  241. }