1 | <?php |
||
2 | /****************************************************************************** |
||
3 | * Wikipedia Account Creation Assistance tool * |
||
4 | * ACC Development Team. Please see team.json for a list of contributors. * |
||
5 | * * |
||
6 | * This is free and unencumbered software released into the public domain. * |
||
7 | * Please see LICENSE.md for the full licencing statement. * |
||
8 | ******************************************************************************/ |
||
9 | |||
10 | namespace Waca\DataObjects; |
||
11 | |||
12 | use DateTimeImmutable; |
||
13 | use Exception; |
||
14 | use PDO; |
||
15 | use Waca\DataObject; |
||
16 | use Waca\Exceptions\OptimisticLockFailedException; |
||
17 | use Waca\PdoDatabase; |
||
18 | |||
19 | /** |
||
20 | * Comment data object |
||
21 | */ |
||
22 | class Comment extends DataObject |
||
23 | { |
||
24 | private $time; |
||
25 | private $user; |
||
26 | private $comment; |
||
27 | private $visibility = "user"; |
||
28 | private $request; |
||
29 | private $flagged = 0; |
||
30 | private $edited; |
||
31 | |||
32 | /** |
||
33 | * Retrieves all comments for a request, optionally filtered |
||
34 | * |
||
35 | * @param integer $id Request ID to search by |
||
36 | * @param PdoDatabase $database |
||
37 | * @param bool $showRestricted True to show all comments, False to show only unprotected comments, and protected |
||
38 | * comments visible to $userId |
||
39 | * @param bool $showCheckuser |
||
40 | * @param null|int $userId User to filter by |
||
41 | * |
||
42 | * @return Comment[] |
||
43 | */ |
||
44 | public static function getForRequest($id, PdoDatabase $database, $showRestricted = false, $showCheckuser = false, $userId = null) |
||
45 | { |
||
46 | $parameters = ['requester', 'user']; |
||
47 | if ($showCheckuser) { |
||
48 | $parameters[] = 'checkuser'; |
||
49 | } |
||
50 | if ($showRestricted) { |
||
51 | $parameters[] = 'admin'; |
||
52 | } |
||
53 | |||
54 | $visibilityPlaceholders = str_repeat('?,', count($parameters) - 1) . '?'; |
||
55 | |||
56 | $statement = $database->prepare(<<<SQL |
||
57 | SELECT * FROM comment |
||
58 | WHERE (visibility in (${visibilityPlaceholders}) OR user = ?) AND request = ?; |
||
59 | SQL |
||
60 | ); |
||
61 | |||
62 | $parameters[] = $userId; |
||
63 | $parameters[] = $id; |
||
64 | |||
65 | $statement->execute($parameters); |
||
66 | |||
67 | $result = array(); |
||
68 | /** @var Comment $v */ |
||
69 | foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
||
70 | $v->setDatabase($database); |
||
71 | $result[] = $v; |
||
72 | } |
||
73 | |||
74 | return $result; |
||
75 | } |
||
76 | |||
77 | public static function getFlaggedComments(PdoDatabase $database, int $domain) |
||
78 | { |
||
79 | $statement = $database->prepare('SELECT c.* FROM comment c INNER JOIN request r ON c.request = r.id WHERE c.flagged = 1 AND r.domain = :domain;'); |
||
80 | $statement->execute([':domain' => $domain]); |
||
81 | |||
82 | $result = array(); |
||
83 | /** @var Comment $v */ |
||
84 | foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
||
85 | $v->setDatabase($database); |
||
86 | $result[] = $v; |
||
87 | } |
||
88 | |||
89 | return $result; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @throws Exception |
||
94 | */ |
||
95 | public function save() |
||
96 | { |
||
97 | if ($this->isNew()) { |
||
98 | // insert |
||
99 | $statement = $this->dbObject->prepare(<<<SQL |
||
100 | INSERT INTO comment ( time, user, comment, visibility, request, flagged ) |
||
101 | VALUES ( CURRENT_TIMESTAMP(), :user, :comment, :visibility, :request, :flagged ); |
||
102 | SQL |
||
103 | ); |
||
104 | $statement->bindValue(":user", $this->user); |
||
105 | $statement->bindValue(":comment", $this->comment); |
||
106 | $statement->bindValue(":visibility", $this->visibility); |
||
107 | $statement->bindValue(":request", $this->request); |
||
108 | $statement->bindValue(":flagged", $this->flagged); |
||
109 | |||
110 | if ($statement->execute()) { |
||
111 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
112 | } |
||
113 | else { |
||
114 | throw new Exception($statement->errorInfo()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
115 | } |
||
116 | } |
||
117 | else { |
||
118 | // update |
||
119 | $statement = $this->dbObject->prepare(<<<SQL |
||
120 | UPDATE comment |
||
121 | SET comment = :comment, visibility = :visibility, flagged = :flagged, edited = :edited, updateversion = updateversion + 1 |
||
122 | WHERE id = :id AND updateversion = :updateversion; |
||
123 | SQL |
||
124 | ); |
||
125 | |||
126 | $statement->bindValue(':id', $this->id); |
||
127 | $statement->bindValue(':updateversion', $this->updateversion); |
||
128 | |||
129 | $statement->bindValue(':comment', $this->comment); |
||
130 | $statement->bindValue(':visibility', $this->visibility); |
||
131 | $statement->bindValue(":flagged", $this->flagged); |
||
132 | $statement->bindValue(":edited", $this->edited); |
||
133 | |||
134 | if (!$statement->execute()) { |
||
135 | throw new Exception($statement->errorInfo()); |
||
136 | } |
||
137 | |||
138 | if ($statement->rowCount() !== 1) { |
||
139 | throw new OptimisticLockFailedException(); |
||
140 | } |
||
141 | |||
142 | $this->updateversion++; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return DateTimeImmutable |
||
148 | */ |
||
149 | public function getTime() |
||
150 | { |
||
151 | return new DateTimeImmutable($this->time); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return int |
||
156 | */ |
||
157 | public function getUser() |
||
158 | { |
||
159 | return $this->user; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param int $user |
||
164 | */ |
||
165 | public function setUser($user) |
||
166 | { |
||
167 | $this->user = $user; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getComment() |
||
174 | { |
||
175 | return $this->comment; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $comment |
||
180 | */ |
||
181 | public function setComment($comment) |
||
182 | { |
||
183 | $this->comment = $comment; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getVisibility() |
||
190 | { |
||
191 | return $this->visibility; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param string $visibility |
||
196 | */ |
||
197 | public function setVisibility($visibility) |
||
198 | { |
||
199 | $this->visibility = $visibility; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @return int |
||
204 | */ |
||
205 | public function getRequest() |
||
206 | { |
||
207 | return $this->request; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param int $request |
||
212 | */ |
||
213 | public function setRequest($request) |
||
214 | { |
||
215 | $this->request = $request; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function getFlagged() : bool |
||
222 | { |
||
223 | return $this->flagged == 1; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param bool $flagged |
||
228 | */ |
||
229 | public function setFlagged(bool $flagged): void |
||
230 | { |
||
231 | $this->flagged = $flagged ? 1 : 0; |
||
232 | } |
||
233 | |||
234 | public function touchEdited() : void |
||
235 | { |
||
236 | $dateTimeImmutable = new DateTimeImmutable("now"); |
||
237 | $this->edited = $dateTimeImmutable->format('Y-m-d H:i:s'); |
||
238 | } |
||
239 | |||
240 | public function getEdited() : ?DateTimeImmutable |
||
241 | { |
||
242 | if ($this->edited === null) { |
||
243 | return null; |
||
244 | } |
||
245 | |||
246 | return new DateTimeImmutable($this->edited); |
||
247 | } |
||
248 | } |
||
249 |