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