|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
|
4
|
|
|
* * |
|
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
|
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
|
7
|
|
|
******************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Waca\DataObjects; |
|
10
|
|
|
|
|
11
|
|
|
use DateTimeImmutable; |
|
12
|
|
|
use Exception; |
|
13
|
|
|
use PDO; |
|
14
|
|
|
use Waca\DataObject; |
|
15
|
|
|
use Waca\Exceptions\OptimisticLockFailedException; |
|
16
|
|
|
use Waca\PdoDatabase; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Comment data object |
|
20
|
|
|
*/ |
|
21
|
|
|
class Comment extends DataObject |
|
22
|
|
|
{ |
|
23
|
|
|
private $time; |
|
24
|
|
|
private $user; |
|
25
|
|
|
private $comment; |
|
26
|
|
|
private $visibility = "user"; |
|
27
|
|
|
private $request; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Retrieves all comments for a request, optionally filtered |
|
31
|
|
|
* |
|
32
|
|
|
* @param integer $id Request ID to search by |
|
33
|
|
|
* @param PdoDatabase $database |
|
34
|
|
|
* @param bool $showAll True to show all comments, False to show only unprotected comments, and protected |
|
35
|
|
|
* comments visible to $userId |
|
36
|
|
|
* @param null|int $userId User to filter by |
|
37
|
|
|
* |
|
38
|
|
|
* @return Comment[] |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function getForRequest($id, PdoDatabase $database, $showAll = false, $userId = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($showAll) { |
|
43
|
|
|
$statement = $database->prepare('SELECT * FROM comment WHERE request = :target;'); |
|
44
|
|
|
} |
|
45
|
|
|
else { |
|
46
|
|
|
$statement = $database->prepare(<<<SQL |
|
47
|
|
|
SELECT * FROM comment |
|
48
|
|
|
WHERE request = :target AND (visibility = 'user' OR user = :userid); |
|
49
|
|
|
SQL |
|
50
|
|
|
); |
|
51
|
|
|
$statement->bindValue(':userid', $userId); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$statement->bindValue(':target', $id); |
|
55
|
|
|
|
|
56
|
|
|
$statement->execute(); |
|
57
|
|
|
|
|
58
|
|
|
$result = array(); |
|
59
|
|
|
/** @var Comment $v */ |
|
60
|
|
|
foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
|
61
|
|
|
$v->setDatabase($database); |
|
62
|
|
|
$result[] = $v; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $result; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @throws Exception |
|
70
|
|
|
*/ |
|
71
|
|
|
public function save() |
|
72
|
|
|
{ |
|
73
|
|
|
if ($this->isNew()) { |
|
74
|
|
|
// insert |
|
75
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
|
76
|
|
|
INSERT INTO comment ( time, user, comment, visibility, request ) |
|
77
|
|
|
VALUES ( CURRENT_TIMESTAMP(), :user, :comment, :visibility, :request ); |
|
78
|
|
|
SQL |
|
79
|
|
|
); |
|
80
|
|
|
$statement->bindValue(":user", $this->user); |
|
81
|
|
|
$statement->bindValue(":comment", $this->comment); |
|
82
|
|
|
$statement->bindValue(":visibility", $this->visibility); |
|
83
|
|
|
$statement->bindValue(":request", $this->request); |
|
84
|
|
|
|
|
85
|
|
|
if ($statement->execute()) { |
|
86
|
|
|
$this->id = (int)$this->dbObject->lastInsertId(); |
|
87
|
|
|
} |
|
88
|
|
|
else { |
|
89
|
|
|
throw new Exception($statement->errorInfo()); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
else { |
|
93
|
|
|
// update |
|
94
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
|
95
|
|
|
UPDATE comment |
|
96
|
|
|
SET comment = :comment, visibility = :visibility, updateversion = updateversion + 1 |
|
97
|
|
|
WHERE id = :id AND updateversion = :updateversion; |
|
98
|
|
|
SQL |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$statement->bindValue(':id', $this->id); |
|
102
|
|
|
$statement->bindValue(':updateversion', $this->updateversion); |
|
103
|
|
|
|
|
104
|
|
|
$statement->bindValue(':comment', $this->comment); |
|
105
|
|
|
$statement->bindValue(':visibility', $this->visibility); |
|
106
|
|
|
|
|
107
|
|
|
if (!$statement->execute()) { |
|
108
|
|
|
throw new Exception($statement->errorInfo()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ($statement->rowCount() !== 1) { |
|
112
|
|
|
throw new OptimisticLockFailedException(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->updateversion++; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return DateTimeImmutable |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getTime() |
|
123
|
|
|
{ |
|
124
|
|
|
return new DateTimeImmutable($this->time); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return int |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getUser() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->user; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param int $user |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setUser($user) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->user = $user; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getComment() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->comment; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $comment |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setComment($comment) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->comment = $comment; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getVisibility() |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->visibility; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $visibility |
|
169
|
|
|
*/ |
|
170
|
|
|
public function setVisibility($visibility) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->visibility = $visibility; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return int |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getRequest() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->request; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param int $request |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setRequest($request) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->request = $request; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|