comparison core/modules/comment/src/CommentStorage.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php 1 <?php
2 2
3 namespace Drupal\comment; 3 namespace Drupal\comment;
4 4
5 use Drupal\Core\Cache\CacheBackendInterface; 5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
6 use Drupal\Core\Database\Connection; 7 use Drupal\Core\Database\Connection;
7 use Drupal\Core\Entity\EntityManagerInterface; 8 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Entity\EntityTypeInterface; 9 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Entity\EntityInterface; 10 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Entity\FieldableEntityInterface; 11 use Drupal\Core\Entity\FieldableEntityInterface;
41 * The current user. 42 * The current user.
42 * @param \Drupal\Core\Cache\CacheBackendInterface $cache 43 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
43 * Cache backend instance to use. 44 * Cache backend instance to use.
44 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager 45 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
45 * The language manager. 46 * The language manager.
46 */ 47 * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
47 public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) { 48 * The memory cache.
48 parent::__construct($entity_info, $database, $entity_manager, $cache, $language_manager); 49 */
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);
49 $this->currentUser = $current_user; 52 $this->currentUser = $current_user;
50 } 53 }
51 54
52 /** 55 /**
53 * {@inheritdoc} 56 * {@inheritdoc}
57 $entity_info, 60 $entity_info,
58 $container->get('database'), 61 $container->get('database'),
59 $container->get('entity.manager'), 62 $container->get('entity.manager'),
60 $container->get('current_user'), 63 $container->get('current_user'),
61 $container->get('cache.entity'), 64 $container->get('cache.entity'),
62 $container->get('language_manager') 65 $container->get('language_manager'),
66 $container->get('entity.memory_cache')
63 ); 67 );
64 } 68 }
65 69
66 /** 70 /**
67 * {@inheritdoc} 71 * {@inheritdoc}
68 */ 72 */
69 public function getMaxThread(CommentInterface $comment) { 73 public function getMaxThread(CommentInterface $comment) {
70 $query = $this->database->select('comment_field_data', 'c') 74 $query = $this->database->select($this->getDataTable(), 'c')
71 ->condition('entity_id', $comment->getCommentedEntityId()) 75 ->condition('entity_id', $comment->getCommentedEntityId())
72 ->condition('field_name', $comment->getFieldName()) 76 ->condition('field_name', $comment->getFieldName())
73 ->condition('entity_type', $comment->getCommentedEntityTypeId()) 77 ->condition('entity_type', $comment->getCommentedEntityTypeId())
74 ->condition('default_langcode', 1); 78 ->condition('default_langcode', 1);
75 $query->addExpression('MAX(thread)', 'thread'); 79 $query->addExpression('MAX(thread)', 'thread');
79 83
80 /** 84 /**
81 * {@inheritdoc} 85 * {@inheritdoc}
82 */ 86 */
83 public function getMaxThreadPerThread(CommentInterface $comment) { 87 public function getMaxThreadPerThread(CommentInterface $comment) {
84 $query = $this->database->select('comment_field_data', 'c') 88 $query = $this->database->select($this->getDataTable(), 'c')
85 ->condition('entity_id', $comment->getCommentedEntityId()) 89 ->condition('entity_id', $comment->getCommentedEntityId())
86 ->condition('field_name', $comment->getFieldName()) 90 ->condition('field_name', $comment->getFieldName())
87 ->condition('entity_type', $comment->getCommentedEntityTypeId()) 91 ->condition('entity_type', $comment->getCommentedEntityTypeId())
88 ->condition('thread', $comment->getParentComment()->getThread() . '.%', 'LIKE') 92 ->condition('thread', $comment->getParentComment()->getThread() . '.%', 'LIKE')
89 ->condition('default_langcode', 1); 93 ->condition('default_langcode', 1);
96 * {@inheritdoc} 100 * {@inheritdoc}
97 */ 101 */
98 public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) { 102 public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) {
99 // Count how many comments (c1) are before $comment (c2) in display order. 103 // Count how many comments (c1) are before $comment (c2) in display order.
100 // This is the 0-based display ordinal. 104 // This is the 0-based display ordinal.
101 $query = $this->database->select('comment_field_data', 'c1'); 105 $data_table = $this->getDataTable();
102 $query->innerJoin('comment_field_data', 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name'); 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');
103 $query->addExpression('COUNT(*)', 'count'); 108 $query->addExpression('COUNT(*)', 'count');
104 $query->condition('c2.cid', $comment->id()); 109 $query->condition('c2.cid', $comment->id());
105 if (!$this->currentUser->hasPermission('administer comments')) { 110 if (!$this->currentUser->hasPermission('administer comments')) {
106 $query->condition('c1.status', CommentInterface::PUBLISHED); 111 $query->condition('c1.status', CommentInterface::PUBLISHED);
107 } 112 }
131 * {@inheritdoc} 136 * {@inheritdoc}
132 */ 137 */
133 public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) { 138 public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) {
134 $field = $entity->getFieldDefinition($field_name); 139 $field = $entity->getFieldDefinition($field_name);
135 $comments_per_page = $field->getSetting('per_page'); 140 $comments_per_page = $field->getSetting('per_page');
141 $data_table = $this->getDataTable();
136 142
137 if ($total_comments <= $comments_per_page) { 143 if ($total_comments <= $comments_per_page) {
138 // Only one page of comments. 144 // Only one page of comments.
139 $count = 0; 145 $count = 0;
140 } 146 }
144 } 150 }
145 else { 151 else {
146 // Threaded comments. 152 // Threaded comments.
147 153
148 // 1. Find all the threads with a new comment. 154 // 1. Find all the threads with a new comment.
149 $unread_threads_query = $this->database->select('comment_field_data', 'comment') 155 $unread_threads_query = $this->database->select($data_table, 'comment')
150 ->fields('comment', ['thread']) 156 ->fields('comment', ['thread'])
151 ->condition('entity_id', $entity->id()) 157 ->condition('entity_id', $entity->id())
152 ->condition('entity_type', $entity->getEntityTypeId()) 158 ->condition('entity_type', $entity->getEntityTypeId())
153 ->condition('field_name', $field_name) 159 ->condition('field_name', $field_name)
154 ->condition('status', CommentInterface::PUBLISHED) 160 ->condition('status', CommentInterface::PUBLISHED)
169 175
170 // Remove the final '/'. 176 // Remove the final '/'.
171 $first_thread = substr($first_thread, 0, -1); 177 $first_thread = substr($first_thread, 0, -1);
172 178
173 // Find the number of the first comment of the first unread thread. 179 // Find the number of the first comment of the first unread thread.
174 $count = $this->database->query('SELECT COUNT(*) FROM {comment_field_data} WHERE entity_id = :entity_id 180 $count = $this->database->query('SELECT COUNT(*) FROM {' . $data_table . '} WHERE entity_id = :entity_id
175 AND entity_type = :entity_type 181 AND entity_type = :entity_type
176 AND field_name = :field_name 182 AND field_name = :field_name
177 AND status = :status 183 AND status = :status
178 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread 184 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread
179 AND default_langcode = 1', [ 185 AND default_langcode = 1', [
190 196
191 /** 197 /**
192 * {@inheritdoc} 198 * {@inheritdoc}
193 */ 199 */
194 public function getChildCids(array $comments) { 200 public function getChildCids(array $comments) {
195 return $this->database->select('comment_field_data', 'c') 201 return $this->database->select($this->getDataTable(), 'c')
196 ->fields('c', ['cid']) 202 ->fields('c', ['cid'])
197 ->condition('pid', array_keys($comments), 'IN') 203 ->condition('pid', array_keys($comments), 'IN')
198 ->condition('default_langcode', 1) 204 ->condition('default_langcode', 1)
199 ->execute() 205 ->execute()
200 ->fetchCol(); 206 ->fetchCol();
256 * now we "ORDER BY thread DESC" we get the correct order. However this would 262 * now we "ORDER BY thread DESC" we get the correct order. However this would
257 * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need 263 * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need
258 * to consider the trailing "/" so we use a substring only. 264 * to consider the trailing "/" so we use a substring only.
259 */ 265 */
260 public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) { 266 public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) {
261 $query = $this->database->select('comment_field_data', 'c'); 267 $data_table = $this->getDataTable();
268 $query = $this->database->select($data_table, 'c');
262 $query->addField('c', 'cid'); 269 $query->addField('c', 'cid');
263 $query 270 $query
264 ->condition('c.entity_id', $entity->id()) 271 ->condition('c.entity_id', $entity->id())
265 ->condition('c.entity_type', $entity->getEntityTypeId()) 272 ->condition('c.entity_type', $entity->getEntityTypeId())
266 ->condition('c.field_name', $field_name) 273 ->condition('c.field_name', $field_name)
276 ->limit($comments_per_page); 283 ->limit($comments_per_page);
277 if ($pager_id) { 284 if ($pager_id) {
278 $query->element($pager_id); 285 $query->element($pager_id);
279 } 286 }
280 287
281 $count_query = $this->database->select('comment_field_data', 'c'); 288 $count_query = $this->database->select($data_table, 'c');
282 $count_query->addExpression('COUNT(*)'); 289 $count_query->addExpression('COUNT(*)');
283 $count_query 290 $count_query
284 ->condition('c.entity_id', $entity->id()) 291 ->condition('c.entity_id', $entity->id())
285 ->condition('c.entity_type', $entity->getEntityTypeId()) 292 ->condition('c.entity_type', $entity->getEntityTypeId())
286 ->condition('c.field_name', $field_name) 293 ->condition('c.field_name', $field_name)
322 329
323 /** 330 /**
324 * {@inheritdoc} 331 * {@inheritdoc}
325 */ 332 */
326 public function getUnapprovedCount() { 333 public function getUnapprovedCount() {
327 return $this->database->select('comment_field_data', 'c') 334 return $this->database->select($this->getDataTable(), 'c')
328 ->condition('status', CommentInterface::NOT_PUBLISHED, '=') 335 ->condition('status', CommentInterface::NOT_PUBLISHED, '=')
329 ->condition('default_langcode', 1) 336 ->condition('default_langcode', 1)
330 ->countQuery() 337 ->countQuery()
331 ->execute() 338 ->execute()
332 ->fetchField(); 339 ->fetchField();