annotate core/modules/comment/src/CommentStorage.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\comment;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 6 use Drupal\Core\Database\Connection;
Chris@0 7 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 8 use Drupal\Core\Entity\EntityTypeInterface;
Chris@0 9 use Drupal\Core\Entity\EntityInterface;
Chris@0 10 use Drupal\Core\Entity\FieldableEntityInterface;
Chris@0 11 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
Chris@0 12 use Drupal\Core\Session\AccountInterface;
Chris@0 13 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 14 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Defines the storage handler class for comments.
Chris@0 18 *
Chris@0 19 * This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class,
Chris@0 20 * adding required special handling for comment entities.
Chris@0 21 */
Chris@0 22 class CommentStorage extends SqlContentEntityStorage implements CommentStorageInterface {
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The current user.
Chris@0 26 *
Chris@0 27 * @var \Drupal\Core\Session\AccountInterface
Chris@0 28 */
Chris@0 29 protected $currentUser;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Constructs a CommentStorage object.
Chris@0 33 *
Chris@0 34 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info
Chris@0 35 * An array of entity info for the entity type.
Chris@0 36 * @param \Drupal\Core\Database\Connection $database
Chris@0 37 * The database connection to be used.
Chris@0 38 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 39 * The entity manager.
Chris@0 40 * @param \Drupal\Core\Session\AccountInterface $current_user
Chris@0 41 * The current user.
Chris@0 42 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
Chris@0 43 * Cache backend instance to use.
Chris@0 44 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 45 * The language manager.
Chris@0 46 */
Chris@0 47 public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) {
Chris@0 48 parent::__construct($entity_info, $database, $entity_manager, $cache, $language_manager);
Chris@0 49 $this->currentUser = $current_user;
Chris@0 50 }
Chris@0 51
Chris@0 52 /**
Chris@0 53 * {@inheritdoc}
Chris@0 54 */
Chris@0 55 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
Chris@0 56 return new static(
Chris@0 57 $entity_info,
Chris@0 58 $container->get('database'),
Chris@0 59 $container->get('entity.manager'),
Chris@0 60 $container->get('current_user'),
Chris@0 61 $container->get('cache.entity'),
Chris@0 62 $container->get('language_manager')
Chris@0 63 );
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * {@inheritdoc}
Chris@0 68 */
Chris@0 69 public function getMaxThread(CommentInterface $comment) {
Chris@0 70 $query = $this->database->select('comment_field_data', 'c')
Chris@0 71 ->condition('entity_id', $comment->getCommentedEntityId())
Chris@0 72 ->condition('field_name', $comment->getFieldName())
Chris@0 73 ->condition('entity_type', $comment->getCommentedEntityTypeId())
Chris@0 74 ->condition('default_langcode', 1);
Chris@0 75 $query->addExpression('MAX(thread)', 'thread');
Chris@0 76 return $query->execute()
Chris@0 77 ->fetchField();
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * {@inheritdoc}
Chris@0 82 */
Chris@0 83 public function getMaxThreadPerThread(CommentInterface $comment) {
Chris@0 84 $query = $this->database->select('comment_field_data', 'c')
Chris@0 85 ->condition('entity_id', $comment->getCommentedEntityId())
Chris@0 86 ->condition('field_name', $comment->getFieldName())
Chris@0 87 ->condition('entity_type', $comment->getCommentedEntityTypeId())
Chris@0 88 ->condition('thread', $comment->getParentComment()->getThread() . '.%', 'LIKE')
Chris@0 89 ->condition('default_langcode', 1);
Chris@0 90 $query->addExpression('MAX(thread)', 'thread');
Chris@0 91 return $query->execute()
Chris@0 92 ->fetchField();
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * {@inheritdoc}
Chris@0 97 */
Chris@0 98 public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) {
Chris@0 99 // Count how many comments (c1) are before $comment (c2) in display order.
Chris@0 100 // This is the 0-based display ordinal.
Chris@0 101 $query = $this->database->select('comment_field_data', 'c1');
Chris@0 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');
Chris@0 103 $query->addExpression('COUNT(*)', 'count');
Chris@0 104 $query->condition('c2.cid', $comment->id());
Chris@0 105 if (!$this->currentUser->hasPermission('administer comments')) {
Chris@0 106 $query->condition('c1.status', CommentInterface::PUBLISHED);
Chris@0 107 }
Chris@0 108
Chris@0 109 if ($comment_mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
Chris@0 110 // For rendering flat comments, cid is used for ordering comments due to
Chris@0 111 // unpredictable behavior with timestamp, so we make the same assumption
Chris@0 112 // here.
Chris@0 113 $query->condition('c1.cid', $comment->id(), '<');
Chris@0 114 }
Chris@0 115 else {
Chris@0 116 // For threaded comments, the c.thread column is used for ordering. We can
Chris@0 117 // use the sorting code for comparison, but must remove the trailing
Chris@0 118 // slash.
Chris@0 119 $query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) - 1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) - 1))');
Chris@0 120 }
Chris@0 121
Chris@0 122 $query->condition('c1.default_langcode', 1);
Chris@0 123 $query->condition('c2.default_langcode', 1);
Chris@0 124
Chris@0 125 $ordinal = $query->execute()->fetchField();
Chris@0 126
Chris@0 127 return ($divisor > 1) ? floor($ordinal / $divisor) : $ordinal;
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * {@inheritdoc}
Chris@0 132 */
Chris@0 133 public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) {
Chris@0 134 $field = $entity->getFieldDefinition($field_name);
Chris@0 135 $comments_per_page = $field->getSetting('per_page');
Chris@0 136
Chris@0 137 if ($total_comments <= $comments_per_page) {
Chris@0 138 // Only one page of comments.
Chris@0 139 $count = 0;
Chris@0 140 }
Chris@0 141 elseif ($field->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) {
Chris@0 142 // Flat comments.
Chris@0 143 $count = $total_comments - $new_comments;
Chris@0 144 }
Chris@0 145 else {
Chris@0 146 // Threaded comments.
Chris@0 147
Chris@0 148 // 1. Find all the threads with a new comment.
Chris@0 149 $unread_threads_query = $this->database->select('comment_field_data', 'comment')
Chris@0 150 ->fields('comment', ['thread'])
Chris@0 151 ->condition('entity_id', $entity->id())
Chris@0 152 ->condition('entity_type', $entity->getEntityTypeId())
Chris@0 153 ->condition('field_name', $field_name)
Chris@0 154 ->condition('status', CommentInterface::PUBLISHED)
Chris@0 155 ->condition('default_langcode', 1)
Chris@0 156 ->orderBy('created', 'DESC')
Chris@0 157 ->orderBy('cid', 'DESC')
Chris@0 158 ->range(0, $new_comments);
Chris@0 159
Chris@0 160 // 2. Find the first thread.
Chris@0 161 $first_thread_query = $this->database->select($unread_threads_query, 'thread');
Chris@0 162 $first_thread_query->addExpression('SUBSTRING(thread, 1, (LENGTH(thread) - 1))', 'torder');
Chris@0 163 $first_thread = $first_thread_query
Chris@0 164 ->fields('thread', ['thread'])
Chris@0 165 ->orderBy('torder')
Chris@0 166 ->range(0, 1)
Chris@0 167 ->execute()
Chris@0 168 ->fetchField();
Chris@0 169
Chris@0 170 // Remove the final '/'.
Chris@0 171 $first_thread = substr($first_thread, 0, -1);
Chris@0 172
Chris@0 173 // Find the number of the first comment of the first unread thread.
Chris@0 174 $count = $this->database->query('SELECT COUNT(*) FROM {comment_field_data} WHERE entity_id = :entity_id
Chris@0 175 AND entity_type = :entity_type
Chris@0 176 AND field_name = :field_name
Chris@0 177 AND status = :status
Chris@0 178 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread
Chris@0 179 AND default_langcode = 1', [
Chris@0 180 ':status' => CommentInterface::PUBLISHED,
Chris@0 181 ':entity_id' => $entity->id(),
Chris@0 182 ':field_name' => $field_name,
Chris@0 183 ':entity_type' => $entity->getEntityTypeId(),
Chris@0 184 ':thread' => $first_thread,
Chris@0 185 ])->fetchField();
Chris@0 186 }
Chris@0 187
Chris@0 188 return $comments_per_page > 0 ? (int) ($count / $comments_per_page) : 0;
Chris@0 189 }
Chris@0 190
Chris@0 191 /**
Chris@0 192 * {@inheritdoc}
Chris@0 193 */
Chris@0 194 public function getChildCids(array $comments) {
Chris@0 195 return $this->database->select('comment_field_data', 'c')
Chris@0 196 ->fields('c', ['cid'])
Chris@0 197 ->condition('pid', array_keys($comments), 'IN')
Chris@0 198 ->condition('default_langcode', 1)
Chris@0 199 ->execute()
Chris@0 200 ->fetchCol();
Chris@0 201 }
Chris@0 202
Chris@0 203 /**
Chris@0 204 * {@inheritdoc}
Chris@0 205 *
Chris@0 206 * To display threaded comments in the correct order we keep a 'thread' field
Chris@0 207 * and order by that value. This field keeps this data in
Chris@0 208 * a way which is easy to update and convenient to use.
Chris@0 209 *
Chris@0 210 * A "thread" value starts at "1". If we add a child (A) to this comment,
Chris@0 211 * we assign it a "thread" = "1.1". A child of (A) will have "1.1.1". Next
Chris@0 212 * brother of (A) will get "1.2". Next brother of the parent of (A) will get
Chris@0 213 * "2" and so on.
Chris@0 214 *
Chris@0 215 * First of all note that the thread field stores the depth of the comment:
Chris@0 216 * depth 0 will be "X", depth 1 "X.X", depth 2 "X.X.X", etc.
Chris@0 217 *
Chris@0 218 * Now to get the ordering right, consider this example:
Chris@0 219 *
Chris@0 220 * 1
Chris@0 221 * 1.1
Chris@0 222 * 1.1.1
Chris@0 223 * 1.2
Chris@0 224 * 2
Chris@0 225 *
Chris@0 226 * If we "ORDER BY thread ASC" we get the above result, and this is the
Chris@0 227 * natural order sorted by time. However, if we "ORDER BY thread DESC"
Chris@0 228 * we get:
Chris@0 229 *
Chris@0 230 * 2
Chris@0 231 * 1.2
Chris@0 232 * 1.1.1
Chris@0 233 * 1.1
Chris@0 234 * 1
Chris@0 235 *
Chris@0 236 * Clearly, this is not a natural way to see a thread, and users will get
Chris@0 237 * confused. The natural order to show a thread by time desc would be:
Chris@0 238 *
Chris@0 239 * 2
Chris@0 240 * 1
Chris@0 241 * 1.2
Chris@0 242 * 1.1
Chris@0 243 * 1.1.1
Chris@0 244 *
Chris@0 245 * which is what we already did before the standard pager patch. To achieve
Chris@0 246 * this we simply add a "/" at the end of each "thread" value. This way, the
Chris@0 247 * thread fields will look like this:
Chris@0 248 *
Chris@0 249 * 1/
Chris@0 250 * 1.1/
Chris@0 251 * 1.1.1/
Chris@0 252 * 1.2/
Chris@0 253 * 2/
Chris@0 254 *
Chris@0 255 * we add "/" since this char is, in ASCII, higher than every number, so if
Chris@0 256 * now we "ORDER BY thread DESC" we get the correct order. However this would
Chris@0 257 * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need
Chris@0 258 * to consider the trailing "/" so we use a substring only.
Chris@0 259 */
Chris@0 260 public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) {
Chris@0 261 $query = $this->database->select('comment_field_data', 'c');
Chris@0 262 $query->addField('c', 'cid');
Chris@0 263 $query
Chris@0 264 ->condition('c.entity_id', $entity->id())
Chris@0 265 ->condition('c.entity_type', $entity->getEntityTypeId())
Chris@0 266 ->condition('c.field_name', $field_name)
Chris@0 267 ->condition('c.default_langcode', 1)
Chris@0 268 ->addTag('entity_access')
Chris@0 269 ->addTag('comment_filter')
Chris@0 270 ->addMetaData('base_table', 'comment')
Chris@0 271 ->addMetaData('entity', $entity)
Chris@0 272 ->addMetaData('field_name', $field_name);
Chris@0 273
Chris@0 274 if ($comments_per_page) {
Chris@0 275 $query = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')
Chris@0 276 ->limit($comments_per_page);
Chris@0 277 if ($pager_id) {
Chris@0 278 $query->element($pager_id);
Chris@0 279 }
Chris@0 280
Chris@0 281 $count_query = $this->database->select('comment_field_data', 'c');
Chris@0 282 $count_query->addExpression('COUNT(*)');
Chris@0 283 $count_query
Chris@0 284 ->condition('c.entity_id', $entity->id())
Chris@0 285 ->condition('c.entity_type', $entity->getEntityTypeId())
Chris@0 286 ->condition('c.field_name', $field_name)
Chris@0 287 ->condition('c.default_langcode', 1)
Chris@0 288 ->addTag('entity_access')
Chris@0 289 ->addTag('comment_filter')
Chris@0 290 ->addMetaData('base_table', 'comment')
Chris@0 291 ->addMetaData('entity', $entity)
Chris@0 292 ->addMetaData('field_name', $field_name);
Chris@0 293 $query->setCountQuery($count_query);
Chris@0 294 }
Chris@0 295
Chris@0 296 if (!$this->currentUser->hasPermission('administer comments')) {
Chris@0 297 $query->condition('c.status', CommentInterface::PUBLISHED);
Chris@0 298 if ($comments_per_page) {
Chris@0 299 $count_query->condition('c.status', CommentInterface::PUBLISHED);
Chris@0 300 }
Chris@0 301 }
Chris@0 302 if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
Chris@0 303 $query->orderBy('c.cid', 'ASC');
Chris@0 304 }
Chris@0 305 else {
Chris@0 306 // See comment above. Analysis reveals that this doesn't cost too
Chris@0 307 // much. It scales much much better than having the whole comment
Chris@0 308 // structure.
Chris@0 309 $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
Chris@0 310 $query->orderBy('torder', 'ASC');
Chris@0 311 }
Chris@0 312
Chris@0 313 $cids = $query->execute()->fetchCol();
Chris@0 314
Chris@0 315 $comments = [];
Chris@0 316 if ($cids) {
Chris@0 317 $comments = $this->loadMultiple($cids);
Chris@0 318 }
Chris@0 319
Chris@0 320 return $comments;
Chris@0 321 }
Chris@0 322
Chris@0 323 /**
Chris@0 324 * {@inheritdoc}
Chris@0 325 */
Chris@0 326 public function getUnapprovedCount() {
Chris@0 327 return $this->database->select('comment_field_data', 'c')
Chris@0 328 ->condition('status', CommentInterface::NOT_PUBLISHED, '=')
Chris@0 329 ->condition('default_langcode', 1)
Chris@0 330 ->countQuery()
Chris@0 331 ->execute()
Chris@0 332 ->fetchField();
Chris@0 333 }
Chris@0 334
Chris@0 335 }