CommentStorage.php 4.75 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\comment\CommentStorage.
6 7
 */

8
namespace Drupal\comment;
9

10
use Drupal\Core\Database\Connection;
11
use Drupal\Core\Entity\EntityInterface;
12
use Drupal\Core\Entity\EntityManagerInterface;
13
use Drupal\Core\Entity\EntityTypeInterface;
14
use Drupal\Core\Entity\ContentEntityDatabaseStorage;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16 17 18 19

/**
 * Defines the controller class for comments.
 *
20 21
 * This extends the Drupal\Core\Entity\ContentEntityDatabaseStorage class,
 * adding required special handling for comment entities.
22
 */
23
class CommentStorage extends ContentEntityDatabaseStorage implements CommentStorageInterface {
24

25 26 27 28 29 30 31 32
  /**
   * The comment statistics service.
   *
   * @var \Drupal\comment\CommentStatisticsInterface
   */
  protected $statistics;

  /**
33
   * Constructs a CommentStorage object.
34 35 36 37 38
   *
   * @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.
39 40
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
41 42 43
   * @param \Drupal\comment\CommentStatisticsInterface $comment_statistics
   *   The comment statistics service.
   */
44 45
  public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, CommentStatisticsInterface $comment_statistics) {
    parent::__construct($entity_info, $database, $entity_manager);
46 47 48 49 50 51 52 53 54 55
    $this->statistics = $comment_statistics;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
    return new static(
      $entity_info,
      $container->get('database'),
56
      $container->get('entity.manager'),
57 58 59 60
      $container->get('comment.statistics')
    );
  }

61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function updateEntityStatistics(CommentInterface $comment) {
65
    $this->statistics->update($comment);
66
  }
67

68 69 70 71
  /**
   * {@inheritdoc}
   */
  public function getMaxThread(EntityInterface $comment) {
72
    $query = $this->database->select('comment', 'c')
73
      ->condition('entity_id', $comment->getCommentedEntityId())
74
      ->condition('field_name', $comment->getFieldName())
75
      ->condition('entity_type', $comment->getCommentedEntityTypeId());
76 77 78
    $query->addExpression('MAX(thread)', 'thread');
    return $query->execute()
      ->fetchField();
79 80 81 82 83 84
  }

  /**
   * {@inheritdoc}
   */
  public function getMaxThreadPerThread(EntityInterface $comment) {
85
    $query = $this->database->select('comment', 'c')
86
      ->condition('entity_id', $comment->getCommentedEntityId())
87
      ->condition('field_name', $comment->getFieldName())
88 89
      ->condition('entity_type', $comment->getCommentedEntityTypeId())
      ->condition('thread', $comment->getParentComment()->getThread() . '.%', 'LIKE');
90 91 92
    $query->addExpression('MAX(thread)', 'thread');
    return $query->execute()
      ->fetchField();
93 94 95 96 97 98 99 100 101 102 103 104
  }

  /**
   * {@inheritdoc}
   */
  public function getChildCids(array $comments) {
    return $this->database->select('comment', 'c')
      ->fields('c', array('cid'))
      ->condition('pid', array_keys($comments))
      ->execute()
      ->fetchCol();
  }
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  /**
   * {@inheritdoc}
   */
  public function getSchema() {
    $schema = parent::getSchema();

    // Marking the respective fields as NOT NULL makes the indexes more
    // performant.
    $schema['comment']['fields']['pid']['not null'] = TRUE;
    $schema['comment']['fields']['status']['not null'] = TRUE;
    $schema['comment']['fields']['entity_id']['not null'] = TRUE;
    $schema['comment']['fields']['created']['not null'] = TRUE;
    $schema['comment']['fields']['thread']['not null'] = TRUE;

    unset($schema['comment']['indexes']['field__pid']);
    unset($schema['comment']['indexes']['field__entity_id']);
    $schema['comment']['indexes'] += array(
      'comment__status_pid' => array('pid', 'status'),
      'comment__num_new' => array(
        'entity_id',
        array('entity_type', 32),
127
        'comment_type',
128 129 130 131 132 133 134 135
        'status',
        'created',
        'cid',
        'thread',
      ),
      'comment__entity_langcode' => array(
        'entity_id',
        array('entity_type', 32),
136
        'comment_type',
137 138 139 140 141 142 143 144 145 146 147 148 149 150
        'langcode',
      ),
      'comment__created' => array('created'),
    );
    $schema['comment']['foreign keys'] += array(
      'comment__author' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
    );

    return $schema;
  }

151 152 153 154 155 156 157 158 159 160 161
  /**
   * {@inheritdoc}
   */
  public function getUnapprovedCount() {
    return  $this->database->select('comment', 'c')
      ->condition('status', CommentInterface::NOT_PUBLISHED, '=')
      ->countQuery()
      ->execute()
      ->fetchField();
  }

162
}