View source
<?php
namespace Drupal\comment;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\EntityOwnerInterface;
class CommentStatistics implements CommentStatisticsInterface {
protected $database;
protected $databaseReplica;
protected $currentUser;
protected $entityTypeManager;
protected $state;
public function __construct(Connection $database, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, Connection $database_replica = NULL) {
$this->database = $database;
$this->databaseReplica = $database_replica ?: $database;
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
$this->state = $state;
}
public function read($entities, $entity_type, $accurate = TRUE) {
$connection = $accurate ? $this->database : $this->databaseReplica;
$stats = $connection
->select('comment_entity_statistics', 'ces')
->fields('ces')
->condition('ces.entity_id', array_keys($entities), 'IN')
->condition('ces.entity_type', $entity_type)
->execute();
$statistics_records = [];
while ($entry = $stats
->fetchObject()) {
$statistics_records[] = $entry;
}
return $statistics_records;
}
public function delete(EntityInterface $entity) {
$this->database
->delete('comment_entity_statistics')
->condition('entity_id', $entity
->id())
->condition('entity_type', $entity
->getEntityTypeId())
->execute();
}
public function create(FieldableEntityInterface $entity, $fields) {
$query = $this->database
->insert('comment_entity_statistics')
->fields([
'entity_id',
'entity_type',
'field_name',
'cid',
'last_comment_timestamp',
'last_comment_name',
'last_comment_uid',
'comment_count',
]);
foreach ($fields as $field_name => $detail) {
if (!$entity
->hasField($field_name)) {
continue;
}
$last_comment_uid = 0;
if ($entity instanceof EntityOwnerInterface) {
$last_comment_uid = $entity
->getOwnerId();
}
if (!isset($last_comment_uid)) {
$last_comment_uid = $this->currentUser
->id();
}
$last_comment_timestamp = REQUEST_TIME;
if ($entity instanceof EntityChangedInterface) {
$last_comment_timestamp = $entity
->getChangedTimeAcrossTranslations();
}
$query
->values([
'entity_id' => $entity
->id(),
'entity_type' => $entity
->getEntityTypeId(),
'field_name' => $field_name,
'cid' => 0,
'last_comment_timestamp' => $last_comment_timestamp,
'last_comment_name' => NULL,
'last_comment_uid' => $last_comment_uid,
'comment_count' => 0,
]);
}
$query
->execute();
}
public function getMaximumCount($entity_type) {
return $this->database
->query('SELECT MAX([comment_count]) FROM {comment_entity_statistics} WHERE [entity_type] = :entity_type', [
':entity_type' => $entity_type,
])
->fetchField();
}
public function getRankingInfo() {
return [
'comments' => [
'title' => t('Number of comments'),
'join' => [
'type' => 'LEFT',
'table' => 'comment_entity_statistics',
'alias' => 'ces',
'on' => "ces.entity_id = i.sid AND ces.entity_type = 'node' AND ces.field_name = 'comment'",
],
'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * (ROUND(:comment_scale, 4)))',
'arguments' => [
':comment_scale' => \Drupal::state()
->get('comment.node_comment_statistics_scale', 0),
],
],
];
}
public function update(CommentInterface $comment) {
if (!$this->state
->get('comment.maintain_entity_statistics')) {
return;
}
$query = $this->database
->select('comment_field_data', 'c');
$query
->addExpression('COUNT([cid])');
$count = $query
->condition('c.entity_id', $comment
->getCommentedEntityId())
->condition('c.entity_type', $comment
->getCommentedEntityTypeId())
->condition('c.field_name', $comment
->getFieldName())
->condition('c.status', CommentInterface::PUBLISHED)
->condition('default_langcode', 1)
->execute()
->fetchField();
if ($count > 0) {
$last_reply = $this->database
->select('comment_field_data', 'c')
->fields('c', [
'cid',
'name',
'changed',
'uid',
])
->condition('c.entity_id', $comment
->getCommentedEntityId())
->condition('c.entity_type', $comment
->getCommentedEntityTypeId())
->condition('c.field_name', $comment
->getFieldName())
->condition('c.status', CommentInterface::PUBLISHED)
->condition('default_langcode', 1)
->orderBy('c.created', 'DESC')
->range(0, 1)
->execute()
->fetchObject();
$this->database
->merge('comment_entity_statistics')
->fields([
'cid' => $last_reply->cid,
'comment_count' => $count,
'last_comment_timestamp' => $last_reply->changed,
'last_comment_name' => $last_reply->uid ? '' : $last_reply->name,
'last_comment_uid' => $last_reply->uid,
])
->keys([
'entity_id' => $comment
->getCommentedEntityId(),
'entity_type' => $comment
->getCommentedEntityTypeId(),
'field_name' => $comment
->getFieldName(),
])
->execute();
}
else {
$entity = $comment
->getCommentedEntity();
if ($entity instanceof EntityOwnerInterface) {
$last_comment_uid = $entity
->getOwnerId();
}
if (!isset($last_comment_uid)) {
$last_comment_uid = $this->currentUser
->id();
}
$this->database
->update('comment_entity_statistics')
->fields([
'cid' => 0,
'comment_count' => 0,
'last_comment_timestamp' => $entity instanceof EntityChangedInterface ? $entity
->getChangedTimeAcrossTranslations() : REQUEST_TIME,
'last_comment_name' => '',
'last_comment_uid' => $last_comment_uid,
])
->condition('entity_id', $comment
->getCommentedEntityId())
->condition('entity_type', $comment
->getCommentedEntityTypeId())
->condition('field_name', $comment
->getFieldName())
->execute();
}
$this->entityTypeManager
->getStorage($comment
->getCommentedEntityTypeId())
->resetCache([
$comment
->getCommentedEntityId(),
]);
}
}