CommentStatistics.php 8.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * @file
 * Contains \Drupal\comment\CommentStatistics.
 */

namespace Drupal\comment;


use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
15
use Drupal\Core\State\StateInterface;
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
use Drupal\Core\Session\AccountInterface;
use Drupal\user\EntityOwnerInterface;

class CommentStatistics implements CommentStatisticsInterface {

  /**
   * The current database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The current logged in user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The entity manager service.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The state service.
   *
45
   * @var \Drupal\Core\State\StateInterface
46 47 48 49 50 51 52 53 54 55 56 57
   */
  protected $state;

  /**
   * Constructs the CommentStatistics service.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The active database connection.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current logged in user.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
58
   * @param \Drupal\Core\State\StateInterface $state
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
   *   The state service.
   */
  public function __construct(Connection $database, AccountInterface $current_user, EntityManagerInterface $entity_manager, StateInterface $state) {
    $this->database = $database;
    $this->currentUser = $current_user;
    $this->entityManager = $entity_manager;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public function read($entities, $entity_type) {
    return $this->database->select('comment_entity_statistics', 'ces')
      ->fields('ces')
      ->condition('ces.entity_id', array_keys($entities))
      ->condition('ces.entity_type', $entity_type)
76 77
      ->execute()
      ->fetchAllAssoc('entity_id');
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  }

  /**
   * {@inheritdoc}
   */
  public function delete(EntityInterface $entity) {
    $this->database->delete('comment_entity_statistics')
      ->condition('entity_id', $entity->id())
      ->condition('entity_type', $entity->getEntityTypeId())
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function create(ContentEntityInterface $entity, $fields) {
    $query = $this->database->insert('comment_entity_statistics')
      ->fields(array(
        'entity_id',
        'entity_type',
98
        'field_name',
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        'cid',
        'last_comment_timestamp',
        'last_comment_name',
        'last_comment_uid',
        'comment_count',
      ));
    foreach ($fields as $field_name => $detail) {
      // Skip fields that entity does not have.
      if (!$entity->hasField($field_name)) {
        continue;
      }
      // Get the user ID from the entity if it's set, or default to the
      // currently logged in user.
      $last_comment_uid = 0;
      if ($entity instanceof EntityOwnerInterface) {
        $last_comment_uid = $entity->getOwnerId();
      }
      if (!isset($last_comment_uid)) {
        // Default to current user when entity does not implement
        // EntityOwnerInterface or author is not set.
        $last_comment_uid = $this->currentUser->id();
      }
      // Default to REQUEST_TIME when entity does not have a changed property.
      $last_comment_timestamp = REQUEST_TIME;
      if ($entity instanceof EntityChangedInterface) {
        $last_comment_timestamp = $entity->getChangedTime();
      }
      $query->values(array(
        'entity_id' => $entity->id(),
        'entity_type' => $entity->getEntityTypeId(),
129
        'field_name' => $field_name,
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        'cid' => 0,
        'last_comment_timestamp' => $last_comment_timestamp,
        'last_comment_name' => NULL,
        'last_comment_uid' => $last_comment_uid,
        'comment_count' => 0,
      ));
    }
    $query->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function getMaximumCount($entity_type) {
    return $this->database->query('SELECT MAX(comment_count) FROM {comment_entity_statistics} WHERE entity_type = :entity_type', array(':entity_type' => $entity_type))->fetchField();
  }

  /**
   * {@inheritdoc}
   */
  public function getRankingInfo() {
    return array(
      'comments' => array(
        'title' => t('Number of comments'),
        'join' => array(
          'type' => 'LEFT',
          'table' => 'comment_entity_statistics',
          'alias' => 'ces',
          // Default to comment field as this is the most common use case for
          // nodes.
160
          'on' => "ces.entity_id = i.sid AND ces.entity_type = 'node' AND ces.field_name = 'comment'",
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        ),
        // Inverse law that maps the highest reply count on the site to 1 and 0
        // to 0. Note that the CAST here is necessary for PostgreSQL, because the
        // PostgreSQL PDO driver sometimes puts values in as strings instead of
        // numbers in complex expressions like this.
        'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * (CAST (:comment_scale AS DECIMAL(10, 4))))',
        'arguments' => array(':comment_scale' => \Drupal::state()->get('comment.node_comment_statistics_scale') ?: 0),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function update(CommentInterface $comment) {
    // Allow bulk updates and inserts to temporarily disable the maintenance of
    // the {comment_entity_statistics} table.
    if (!$this->state->get('comment.maintain_entity_statistics')) {
      return;
    }

    $query = $this->database->select('comment', 'c');
    $query->addExpression('COUNT(cid)');
    $count = $query->condition('c.entity_id', $comment->getCommentedEntityId())
      ->condition('c.entity_type', $comment->getCommentedEntityTypeId())
186
      ->condition('c.field_name', $comment->getFieldName())
187 188 189 190 191 192 193 194 195 196
      ->condition('c.status', CommentInterface::PUBLISHED)
      ->execute()
      ->fetchField();

    if ($count > 0) {
      // Comments exist.
      $last_reply = $this->database->select('comment', 'c')
        ->fields('c', array('cid', 'name', 'changed', 'uid'))
        ->condition('c.entity_id', $comment->getCommentedEntityId())
        ->condition('c.entity_type', $comment->getCommentedEntityTypeId())
197
        ->condition('c.field_name', $comment->getFieldName())
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        ->condition('c.status', CommentInterface::PUBLISHED)
        ->orderBy('c.created', 'DESC')
        ->range(0, 1)
        ->execute()
        ->fetchObject();
      // Use merge here because entity could be created before comment field.
      $this->database->merge('comment_entity_statistics')
        ->fields(array(
          '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(array(
          'entity_id' => $comment->getCommentedEntityId(),
          'entity_type' => $comment->getCommentedEntityTypeId(),
215
          'field_name' => $comment->getFieldName(),
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        ))
        ->execute();
    }
    else {
      // Comments do not exist.
      $entity = $comment->getCommentedEntity();
      // Get the user ID from the entity if it's set, or default to the
      // currently logged in user.
      if ($entity instanceof EntityOwnerInterface) {
        $last_comment_uid = $entity->getOwnerId();
      }
      if (!isset($last_comment_uid)) {
        // Default to current user when entity does not implement
        // EntityOwnerInterface or author is not set.
        $last_comment_uid = $this->currentUser->id();
      }
      $this->database->update('comment_entity_statistics')
        ->fields(array(
          'cid' => 0,
          'comment_count' => 0,
          // Use the created date of the entity if it's set, or default to
          // REQUEST_TIME.
          'last_comment_timestamp' => ($entity instanceof EntityChangedInterface) ? $entity->getChangedTime() : REQUEST_TIME,
          'last_comment_name' => '',
          'last_comment_uid' => $last_comment_uid,
        ))
        ->condition('entity_id', $comment->getCommentedEntityId())
        ->condition('entity_type', $comment->getCommentedEntityTypeId())
244
        ->condition('field_name', $comment->getFieldName())
245 246 247 248 249
        ->execute();
    }
  }

}