|
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 Exception; |
|
12
|
|
|
use PDO; |
|
13
|
|
|
use Waca\DataObject; |
|
14
|
|
|
use Waca\Exceptions\OptimisticLockFailedException; |
|
15
|
|
|
use Waca\PdoDatabase; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Welcome template data object |
|
19
|
|
|
*/ |
|
20
|
|
|
class WelcomeTemplate extends DataObject |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $usercode; |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
private $botcode; |
|
26
|
|
|
private $usageCache; |
|
27
|
|
|
private $deleted = 0; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Summary of getAll |
|
31
|
|
|
* |
|
32
|
|
|
* @param PdoDatabase $database |
|
33
|
|
|
* |
|
34
|
|
|
* @return WelcomeTemplate[] |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function getAll(PdoDatabase $database) |
|
37
|
|
|
{ |
|
38
|
|
|
$statement = $database->prepare("SELECT * FROM welcometemplate WHERE deleted = 0;"); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
$statement->execute(); |
|
41
|
|
|
|
|
42
|
|
|
$result = array(); |
|
43
|
|
|
/** @var WelcomeTemplate $v */ |
|
44
|
|
|
foreach ($statement->fetchAll(PDO::FETCH_CLASS, self::class) as $v) { |
|
45
|
|
|
$v->setDatabase($database); |
|
46
|
|
|
$result[] = $v; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $result; |
|
50
|
|
|
} |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @throws Exception |
|
54
|
|
|
*/ |
|
|
|
|
|
|
55
|
|
|
public function save() |
|
56
|
|
|
{ |
|
57
|
|
|
if ($this->isNew()) { |
|
58
|
|
|
// insert |
|
59
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
|
60
|
|
|
INSERT INTO welcometemplate (usercode, botcode) VALUES (:usercode, :botcode); |
|
61
|
|
|
SQL |
|
62
|
|
|
); |
|
63
|
|
|
$statement->bindValue(":usercode", $this->usercode); |
|
|
|
|
|
|
64
|
|
|
$statement->bindValue(":botcode", $this->botcode); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
if ($statement->execute()) { |
|
67
|
|
|
$this->id = (int)$this->dbObject->lastInsertId(); |
|
68
|
|
|
} |
|
69
|
|
|
else { |
|
70
|
|
|
throw new Exception($statement->errorInfo()); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
else { |
|
74
|
|
|
// update |
|
75
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
|
76
|
|
|
UPDATE `welcometemplate` |
|
77
|
|
|
SET usercode = :usercode, botcode = :botcode, updateversion = updateversion + 1 |
|
78
|
|
|
WHERE id = :id AND updateversion = :updateversion |
|
79
|
|
|
LIMIT 1; |
|
80
|
|
|
SQL |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$statement->bindValue(':id', $this->id); |
|
84
|
|
|
$statement->bindValue(':updateversion', $this->updateversion); |
|
85
|
|
|
|
|
86
|
|
|
$statement->bindValue(':usercode', $this->usercode); |
|
87
|
|
|
$statement->bindValue(':botcode', $this->botcode); |
|
88
|
|
|
|
|
89
|
|
|
if (!$statement->execute()) { |
|
90
|
|
|
throw new Exception($statement->errorInfo()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($statement->rowCount() !== 1) { |
|
94
|
|
|
throw new OptimisticLockFailedException(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->updateversion++; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getUserCode() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->usercode; |
|
107
|
|
|
} |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $usercode |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setUserCode($usercode) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->usercode = $usercode; |
|
115
|
|
|
} |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getBotCode() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->botcode; |
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $botcode |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setBotCode($botcode) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->botcode = $botcode; |
|
131
|
|
|
} |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return User[] |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getUsersUsingTemplate() |
|
137
|
|
|
{ |
|
138
|
|
|
if ($this->usageCache === null) { |
|
139
|
|
|
$statement = $this->dbObject->prepare("SELECT * FROM user WHERE welcome_template = :id;"); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
$statement->execute(array(":id" => $this->id)); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
$result = array(); |
|
144
|
|
|
/** @var WelcomeTemplate $v */ |
|
145
|
|
|
foreach ($statement->fetchAll(PDO::FETCH_CLASS, User::class) as $v) { |
|
146
|
|
|
$v->setDatabase($this->dbObject); |
|
147
|
|
|
$result[] = $v; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$this->usageCache = $result; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $this->usageCache; |
|
154
|
|
|
} |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Deletes the object from the database |
|
158
|
|
|
*/ |
|
|
|
|
|
|
159
|
|
|
public function delete() |
|
160
|
|
|
{ |
|
161
|
|
|
if ($this->id === null) { |
|
162
|
|
|
// wtf? |
|
163
|
|
|
return; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$deleteQuery = "UPDATE welcometemplate SET deleted = 1 WHERE id = :id AND updateversion = :updateversion;"; |
|
|
|
|
|
|
167
|
|
|
$statement = $this->dbObject->prepare($deleteQuery); |
|
168
|
|
|
|
|
169
|
|
|
$statement->bindValue(":id", $this->id); |
|
|
|
|
|
|
170
|
|
|
$statement->bindValue(":updateversion", $this->updateversion); |
|
|
|
|
|
|
171
|
|
|
$statement->execute(); |
|
172
|
|
|
|
|
173
|
|
|
if ($statement->rowCount() !== 1) { |
|
174
|
|
|
throw new OptimisticLockFailedException(); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
|
|
public function isDeleted() |
|
182
|
|
|
{ |
|
183
|
|
|
return ((int)$this->deleted) === 1; |
|
184
|
|
|
} |
|
|
|
|
|
|
185
|
|
|
} |
|
|
|
|
|
|
186
|
|
|
|
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.