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
|
|
|
SQL |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$statement->bindValue(':id', $this->id); |
83
|
|
|
$statement->bindValue(':updateversion', $this->updateversion); |
84
|
|
|
|
85
|
|
|
$statement->bindValue(':usercode', $this->usercode); |
86
|
|
|
$statement->bindValue(':botcode', $this->botcode); |
87
|
|
|
|
88
|
|
|
if (!$statement->execute()) { |
89
|
|
|
throw new Exception($statement->errorInfo()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($statement->rowCount() !== 1) { |
93
|
|
|
throw new OptimisticLockFailedException(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->updateversion++; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getUserCode() |
104
|
|
|
{ |
105
|
|
|
return $this->usercode; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $usercode |
110
|
|
|
*/ |
111
|
|
|
public function setUserCode($usercode) |
112
|
|
|
{ |
113
|
|
|
$this->usercode = $usercode; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getBotCode() |
120
|
|
|
{ |
121
|
|
|
return $this->botcode; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $botcode |
126
|
|
|
*/ |
127
|
|
|
public function setBotCode($botcode) |
128
|
|
|
{ |
129
|
|
|
$this->botcode = $botcode; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return User[] |
134
|
|
|
*/ |
135
|
|
|
public function getUsersUsingTemplate() |
136
|
|
|
{ |
137
|
|
|
if ($this->usageCache === null) { |
138
|
|
|
$statement = $this->dbObject->prepare("SELECT * FROM user WHERE welcome_template = :id;"); |
139
|
|
|
|
140
|
|
|
$statement->execute(array(":id" => $this->id)); |
141
|
|
|
|
142
|
|
|
$result = array(); |
143
|
|
|
/** @var WelcomeTemplate $v */ |
144
|
|
|
foreach ($statement->fetchAll(PDO::FETCH_CLASS, User::class) as $v) { |
145
|
|
|
$v->setDatabase($this->dbObject); |
146
|
|
|
$result[] = $v; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->usageCache = $result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->usageCache; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Deletes the object from the database |
157
|
|
|
*/ |
158
|
|
|
public function delete() |
159
|
|
|
{ |
160
|
|
|
if ($this->id === null) { |
161
|
|
|
// wtf? |
162
|
|
|
return; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$deleteQuery = "UPDATE welcometemplate SET deleted = 1 WHERE id = :id AND updateversion = :updateversion;"; |
166
|
|
|
$statement = $this->dbObject->prepare($deleteQuery); |
167
|
|
|
|
168
|
|
|
$statement->bindValue(":id", $this->id); |
169
|
|
|
$statement->bindValue(":updateversion", $this->updateversion); |
170
|
|
|
$statement->execute(); |
171
|
|
|
|
172
|
|
|
if ($statement->rowCount() !== 1) { |
173
|
|
|
throw new OptimisticLockFailedException(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function isDeleted() |
181
|
|
|
{ |
182
|
|
|
return ((int)$this->deleted) === 1; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|