Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / comment / src / CommentStorage.php
1 <?php
2
3 namespace Drupal\comment;
4
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;
16
17 /**
18  * Defines the storage handler class for comments.
19  *
20  * This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class,
21  * adding required special handling for comment entities.
22  */
23 class CommentStorage extends SqlContentEntityStorage implements CommentStorageInterface {
24
25   /**
26    * The current user.
27    *
28    * @var \Drupal\Core\Session\AccountInterface
29    */
30   protected $currentUser;
31
32   /**
33    * Constructs a CommentStorage object.
34    *
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
40    *   The entity manager.
41    * @param \Drupal\Core\Session\AccountInterface $current_user
42    *   The 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
48    *   The memory cache.
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);
52     $this->currentUser = $current_user;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
59     return new static(
60       $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')
67     );
68   }
69
70   /**
71    * {@inheritdoc}
72    */
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()
81       ->fetchField();
82   }
83
84   /**
85    * {@inheritdoc}
86    */
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()
96       ->fetchField();
97   }
98
99   /**
100    * {@inheritdoc}
101    */
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);
112     }
113
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
117       // here.
118       $query->condition('c1.cid', $comment->id(), '<');
119     }
120     else {
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
123       // slash.
124       $query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) - 1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) - 1))');
125     }
126
127     $query->condition('c1.default_langcode', 1);
128     $query->condition('c2.default_langcode', 1);
129
130     $ordinal = $query->execute()->fetchField();
131
132     return ($divisor > 1) ? floor($ordinal / $divisor) : $ordinal;
133   }
134
135   /**
136    * {@inheritdoc}
137    */
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();
142
143     if ($total_comments <= $comments_per_page) {
144       // Only one page of comments.
145       $count = 0;
146     }
147     elseif ($field->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) {
148       // Flat comments.
149       $count = $total_comments - $new_comments;
150     }
151     else {
152       // Threaded comments.
153
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);
165
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'])
171         ->orderBy('torder')
172         ->range(0, 1)
173         ->execute()
174         ->fetchField();
175
176       // Remove the final '/'.
177       $first_thread = substr($first_thread, 0, -1);
178
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
183                         AND status = :status
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,
191       ])->fetchField();
192     }
193
194     return $comments_per_page > 0 ? (int) ($count / $comments_per_page) : 0;
195   }
196
197   /**
198    * {@inheritdoc}
199    */
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)
205       ->execute()
206       ->fetchCol();
207   }
208
209   /**
210    * {@inheritdoc}
211    *
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.
215    *
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
219    * "2" and so on.
220    *
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.
223    *
224    * Now to get the ordering right, consider this example:
225    *
226    * 1
227    * 1.1
228    * 1.1.1
229    * 1.2
230    * 2
231    *
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"
234    * we get:
235    *
236    * 2
237    * 1.2
238    * 1.1.1
239    * 1.1
240    * 1
241    *
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:
244    *
245    * 2
246    * 1
247    * 1.2
248    * 1.1
249    * 1.1.1
250    *
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:
254    *
255    * 1/
256    * 1.1/
257    * 1.1.1/
258    * 1.2/
259    * 2/
260    *
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.
265    */
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');
270     $query
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);
280
281     if ($comments_per_page) {
282       $query = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')
283         ->limit($comments_per_page);
284       if ($pager_id) {
285         $query->element($pager_id);
286       }
287
288       $count_query = $this->database->select($data_table, 'c');
289       $count_query->addExpression('COUNT(*)');
290       $count_query
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);
301     }
302
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);
307       }
308     }
309     if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
310       $query->orderBy('c.cid', 'ASC');
311     }
312     else {
313       // See comment above. Analysis reveals that this doesn't cost too
314       // much. It scales much much better than having the whole comment
315       // structure.
316       $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
317       $query->orderBy('torder', 'ASC');
318     }
319
320     $cids = $query->execute()->fetchCol();
321
322     $comments = [];
323     if ($cids) {
324       $comments = $this->loadMultiple($cids);
325     }
326
327     return $comments;
328   }
329
330   /**
331    * {@inheritdoc}
332    */
333   public function getUnapprovedCount() {
334     return $this->database->select($this->getDataTable(), 'c')
335       ->condition('status', CommentInterface::NOT_PUBLISHED, '=')
336       ->condition('default_langcode', 1)
337       ->countQuery()
338       ->execute()
339       ->fetchField();
340   }
341
342 }