|
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
|
|
|
LIMIT 1; |
|
99
|
|
|
SQL |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$statement->bindValue(':id', $this->id); |
|
103
|
|
|
$statement->bindValue(':updateversion', $this->updateversion); |
|
104
|
|
|
|
|
105
|
|
|
$statement->bindValue(':comment', $this->comment); |
|
106
|
|
|
$statement->bindValue(':visibility', $this->visibility); |
|
107
|
|
|
|
|
108
|
|
|
if (!$statement->execute()) { |
|
109
|
|
|
throw new Exception($statement->errorInfo()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if ($statement->rowCount() !== 1) { |
|
113
|
|
|
throw new OptimisticLockFailedException(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$this->updateversion++; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return DateTimeImmutable |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getTime() |
|
124
|
|
|
{ |
|
125
|
|
|
return new DateTimeImmutable($this->time); |
|
126
|
|
|
} |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return int |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getUser() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->user; |
|
134
|
|
|
} |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param int $user |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setUser($user) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->user = $user; |
|
142
|
|
|
} |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getComment() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->comment; |
|
150
|
|
|
} |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string $comment |
|
154
|
|
|
*/ |
|
155
|
|
|
public function setComment($comment) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->comment = $comment; |
|
158
|
|
|
} |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getVisibility() |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->visibility; |
|
166
|
|
|
} |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $visibility |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setVisibility($visibility) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->visibility = $visibility; |
|
174
|
|
|
} |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return int |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getRequest() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->request; |
|
182
|
|
|
} |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param int $request |
|
186
|
|
|
*/ |
|
187
|
|
|
public function setRequest($request) |
|
188
|
|
|
{ |
|
189
|
|
|
$this->request = $request; |
|
190
|
|
|
} |
|
|
|
|
|
|
191
|
|
|
} |
|
|
|
|
|
|
192
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'or with double quotes"literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\') and the backslash (\\). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is ValueIf your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.