|
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
|
|
View Code Duplication |
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 $this->deleted === 1; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.