3 namespace Drupal\comment;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
7 use Drupal\Core\Database\Connection;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Entity\EntityInterface;
11 use Drupal\Core\Entity\FieldableEntityInterface;
12 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
13 use Drupal\Core\Session\AccountInterface;
14 use Drupal\Core\Language\LanguageManagerInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * Defines the storage handler class for comments.
20 * This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class,
21 * adding required special handling for comment entities.
23 class CommentStorage extends SqlContentEntityStorage implements CommentStorageInterface {
28 * @var \Drupal\Core\Session\AccountInterface
30 protected $currentUser;
33 * Constructs a CommentStorage object.
35 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info
36 * An array of entity info for the entity type.
37 * @param \Drupal\Core\Database\Connection $database
38 * The database connection to be used.
39 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
41 * @param \Drupal\Core\Session\AccountInterface $current_user
43 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
44 * Cache backend instance to use.
45 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
46 * The language manager.
47 * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
50 public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache) {
51 parent::__construct($entity_info, $database, $entity_manager, $cache, $language_manager, $memory_cache);
52 $this->currentUser = $current_user;
58 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
61 $container->get('database'),
62 $container->get('entity.manager'),
63 $container->get('current_user'),
64 $container->get('cache.entity'),
65 $container->get('language_manager'),
66 $container->get('entity.memory_cache')
73 public function getMaxThread(CommentInterface $comment) {
74 $query = $this->database->select($this->getDataTable(), 'c')
75 ->condition('entity_id', $comment->getCommentedEntityId())
76 ->condition('field_name', $comment->getFieldName())
77 ->condition('entity_type', $comment->getCommentedEntityTypeId())
78 ->condition('default_langcode', 1);
79 $query->addExpression('MAX(thread)', 'thread');
80 return $query->execute()
87 public function getMaxThreadPerThread(CommentInterface $comment) {
88 $query = $this->database->select($this->getDataTable(), 'c')
89 ->condition('entity_id', $comment->getCommentedEntityId())
90 ->condition('field_name', $comment->getFieldName())
91 ->condition('entity_type', $comment->getCommentedEntityTypeId())
92 ->condition('thread', $comment->getParentComment()->getThread() . '.%', 'LIKE')
93 ->condition('default_langcode', 1);
94 $query->addExpression('MAX(thread)', 'thread');
95 return $query->execute()
102 public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) {
103 // Count how many comments (c1) are before $comment (c2) in display order.
104 // This is the 0-based display ordinal.
105 $data_table = $this->getDataTable();
106 $query = $this->database->select($data_table, 'c1');
107 $query->innerJoin($data_table, 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name');
108 $query->addExpression('COUNT(*)', 'count');
109 $query->condition('c2.cid', $comment->id());
110 if (!$this->currentUser->hasPermission('administer comments')) {
111 $query->condition('c1.status', CommentInterface::PUBLISHED);
114 if ($comment_mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
115 // For rendering flat comments, cid is used for ordering comments due to
116 // unpredictable behavior with timestamp, so we make the same assumption
118 $query->condition('c1.cid', $comment->id(), '<');
121 // For threaded comments, the c.thread column is used for ordering. We can
122 // use the sorting code for comparison, but must remove the trailing
124 $query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) - 1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) - 1))');
127 $query->condition('c1.default_langcode', 1);
128 $query->condition('c2.default_langcode', 1);
130 $ordinal = $query->execute()->fetchField();
132 return ($divisor > 1) ? floor($ordinal / $divisor) : $ordinal;
138 public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) {
139 $field = $entity->getFieldDefinition($field_name);
140 $comments_per_page = $field->getSetting('per_page');
141 $data_table = $this->getDataTable();
143 if ($total_comments <= $comments_per_page) {
144 // Only one page of comments.
147 elseif ($field->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) {
149 $count = $total_comments - $new_comments;
152 // Threaded comments.
154 // 1. Find all the threads with a new comment.
155 $unread_threads_query = $this->database->select($data_table, 'comment')
156 ->fields('comment', ['thread'])
157 ->condition('entity_id', $entity->id())
158 ->condition('entity_type', $entity->getEntityTypeId())
159 ->condition('field_name', $field_name)
160 ->condition('status', CommentInterface::PUBLISHED)
161 ->condition('default_langcode', 1)
162 ->orderBy('created', 'DESC')
163 ->orderBy('cid', 'DESC')
164 ->range(0, $new_comments);
166 // 2. Find the first thread.
167 $first_thread_query = $this->database->select($unread_threads_query, 'thread');
168 $first_thread_query->addExpression('SUBSTRING(thread, 1, (LENGTH(thread) - 1))', 'torder');
169 $first_thread = $first_thread_query
170 ->fields('thread', ['thread'])
176 // Remove the final '/'.
177 $first_thread = substr($first_thread, 0, -1);
179 // Find the number of the first comment of the first unread thread.
180 $count = $this->database->query('SELECT COUNT(*) FROM {' . $data_table . '} WHERE entity_id = :entity_id
181 AND entity_type = :entity_type
182 AND field_name = :field_name
184 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread
185 AND default_langcode = 1', [
186 ':status' => CommentInterface::PUBLISHED,
187 ':entity_id' => $entity->id(),
188 ':field_name' => $field_name,
189 ':entity_type' => $entity->getEntityTypeId(),
190 ':thread' => $first_thread,
194 return $comments_per_page > 0 ? (int) ($count / $comments_per_page) : 0;
200 public function getChildCids(array $comments) {
201 return $this->database->select($this->getDataTable(), 'c')
202 ->fields('c', ['cid'])
203 ->condition('pid', array_keys($comments), 'IN')
204 ->condition('default_langcode', 1)
212 * To display threaded comments in the correct order we keep a 'thread' field
213 * and order by that value. This field keeps this data in
214 * a way which is easy to update and convenient to use.
216 * A "thread" value starts at "1". If we add a child (A) to this comment,
217 * we assign it a "thread" = "1.1". A child of (A) will have "1.1.1". Next
218 * brother of (A) will get "1.2". Next brother of the parent of (A) will get
221 * First of all note that the thread field stores the depth of the comment:
222 * depth 0 will be "X", depth 1 "X.X", depth 2 "X.X.X", etc.
224 * Now to get the ordering right, consider this example:
232 * If we "ORDER BY thread ASC" we get the above result, and this is the
233 * natural order sorted by time. However, if we "ORDER BY thread DESC"
242 * Clearly, this is not a natural way to see a thread, and users will get
243 * confused. The natural order to show a thread by time desc would be:
251 * which is what we already did before the standard pager patch. To achieve
252 * this we simply add a "/" at the end of each "thread" value. This way, the
253 * thread fields will look like this:
261 * we add "/" since this char is, in ASCII, higher than every number, so if
262 * now we "ORDER BY thread DESC" we get the correct order. However this would
263 * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need
264 * to consider the trailing "/" so we use a substring only.
266 public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) {
267 $data_table = $this->getDataTable();
268 $query = $this->database->select($data_table, 'c');
269 $query->addField('c', 'cid');
271 ->condition('c.entity_id', $entity->id())
272 ->condition('c.entity_type', $entity->getEntityTypeId())
273 ->condition('c.field_name', $field_name)
274 ->condition('c.default_langcode', 1)
275 ->addTag('entity_access')
276 ->addTag('comment_filter')
277 ->addMetaData('base_table', 'comment')
278 ->addMetaData('entity', $entity)
279 ->addMetaData('field_name', $field_name);
281 if ($comments_per_page) {
282 $query = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')
283 ->limit($comments_per_page);
285 $query->element($pager_id);
288 $count_query = $this->database->select($data_table, 'c');
289 $count_query->addExpression('COUNT(*)');
291 ->condition('c.entity_id', $entity->id())
292 ->condition('c.entity_type', $entity->getEntityTypeId())
293 ->condition('c.field_name', $field_name)
294 ->condition('c.default_langcode', 1)
295 ->addTag('entity_access')
296 ->addTag('comment_filter')
297 ->addMetaData('base_table', 'comment')
298 ->addMetaData('entity', $entity)
299 ->addMetaData('field_name', $field_name);
300 $query->setCountQuery($count_query);
303 if (!$this->currentUser->hasPermission('administer comments')) {
304 $query->condition('c.status', CommentInterface::PUBLISHED);
305 if ($comments_per_page) {
306 $count_query->condition('c.status', CommentInterface::PUBLISHED);
309 if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
310 $query->orderBy('c.cid', 'ASC');
313 // See comment above. Analysis reveals that this doesn't cost too
314 // much. It scales much much better than having the whole comment
316 $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
317 $query->orderBy('torder', 'ASC');
320 $cids = $query->execute()->fetchCol();
324 $comments = $this->loadMultiple($cids);
333 public function getUnapprovedCount() {
334 return $this->database->select($this->getDataTable(), 'c')
335 ->condition('status', CommentInterface::NOT_PUBLISHED, '=')
336 ->condition('default_langcode', 1)