Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / forum / src / ForumIndexStorage.php
1 <?php
2
3 namespace Drupal\forum;
4 use Drupal\comment\CommentInterface;
5 use Drupal\Core\Database\Connection;
6 use Drupal\node\NodeInterface;
7
8 /**
9  * Handles CRUD operations to {forum_index} table.
10  */
11 class ForumIndexStorage implements ForumIndexStorageInterface {
12
13   /**
14    * The active database connection.
15    *
16    * @var \Drupal\Core\Database\Connection
17    */
18   protected $database;
19
20   /**
21    * Constructs a ForumIndexStorage object.
22    *
23    * @param \Drupal\Core\Database\Connection $database
24    *   The current database connection.
25    */
26   public function __construct(Connection $database) {
27     $this->database = $database;
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function getOriginalTermId(NodeInterface $node) {
34     return $this->database->queryRange("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, [':nid' => $node->id()])->fetchField();
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function create(NodeInterface $node) {
41     $this->database->insert('forum')
42       ->fields([
43         'tid' => $node->forum_tid,
44         'vid' => $node->getRevisionId(),
45         'nid' => $node->id(),
46       ])
47       ->execute();
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function read(array $vids) {
54     return $this->database->select('forum', 'f')
55       ->fields('f', ['nid', 'tid'])
56       ->condition('f.vid', $vids, 'IN')
57       ->execute();
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function delete(NodeInterface $node) {
64     $this->database->delete('forum')
65       ->condition('nid', $node->id())
66       ->execute();
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function deleteRevision(NodeInterface $node) {
73     $this->database->delete('forum')
74       ->condition('nid', $node->id())
75       ->condition('vid', $node->getRevisionId())
76       ->execute();
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function update(NodeInterface $node) {
83     $this->database->update('forum')
84       ->fields(['tid' => $node->forum_tid])
85       ->condition('vid', $node->getRevisionId())
86       ->execute();
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function updateIndex(NodeInterface $node) {
93     $nid = $node->id();
94     $count = $this->database->query("SELECT COUNT(cid) FROM {comment_field_data} c INNER JOIN {forum_index} i ON c.entity_id = i.nid WHERE c.entity_id = :nid AND c.field_name = 'comment_forum' AND c.entity_type = 'node' AND c.status = :status AND c.default_langcode = 1", [
95       ':nid' => $nid,
96       ':status' => CommentInterface::PUBLISHED,
97     ])->fetchField();
98
99     if ($count > 0) {
100       // Comments exist.
101       $last_reply = $this->database->queryRange("SELECT cid, name, created, uid FROM {comment_field_data} WHERE entity_id = :nid AND field_name = 'comment_forum' AND entity_type = 'node' AND status = :status AND default_langcode = 1 ORDER BY cid DESC", 0, 1, [
102         ':nid' => $nid,
103         ':status' => CommentInterface::PUBLISHED,
104       ])->fetchObject();
105       $this->database->update('forum_index')
106         ->fields( [
107           'comment_count' => $count,
108           'last_comment_timestamp' => $last_reply->created,
109         ])
110         ->condition('nid', $nid)
111         ->execute();
112     }
113     else {
114       // Comments do not exist.
115       // @todo This should be actually filtering on the desired node language
116       $this->database->update('forum_index')
117         ->fields( [
118           'comment_count' => 0,
119           'last_comment_timestamp' => $node->getCreatedTime(),
120         ])
121         ->condition('nid', $nid)
122         ->execute();
123     }
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public function createIndex(NodeInterface $node) {
130     $query = $this->database->insert('forum_index')
131       ->fields(['nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp']);
132     foreach ($node->getTranslationLanguages() as $langcode => $language) {
133       $translation = $node->getTranslation($langcode);
134       foreach ($translation->taxonomy_forums as $item) {
135         $query->values([
136           'nid' => $node->id(),
137           'title' => $translation->label(),
138           'tid' => $item->target_id,
139           'sticky' => (int) $node->isSticky(),
140           'created' => $node->getCreatedTime(),
141           'comment_count' => 0,
142           'last_comment_timestamp' => $node->getCreatedTime(),
143         ]);
144       }
145     }
146     $query->execute();
147     // The logic for determining last_comment_count is fairly complex, so
148     // update the index too.
149     if ($node->isNew()) {
150       $this->updateIndex($node);
151     }
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public function deleteIndex(NodeInterface $node) {
158     $this->database->delete('forum_index')
159       ->condition('nid', $node->id())
160       ->execute();
161   }
162
163 }