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\Pages; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use Waca\DataObjects\User; |
13
|
|
|
use Waca\DataObjects\WelcomeTemplate; |
14
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
15
|
|
|
use Waca\Helpers\Logger; |
16
|
|
|
use Waca\SessionAlert; |
17
|
|
|
use Waca\Tasks\InternalPageBase; |
18
|
|
|
use Waca\WebRequest; |
19
|
|
|
|
20
|
|
|
class PageWelcomeTemplateManagement extends InternalPageBase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Main function for this page, when no specific actions are called. |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
protected function main() |
27
|
|
|
{ |
28
|
|
|
$templateList = WelcomeTemplate::getAll($this->getDatabase()); |
29
|
|
|
|
30
|
|
|
$this->assignCSRFToken(); |
31
|
|
|
|
32
|
|
|
$user = User::getCurrent($this->getDatabase()); |
33
|
|
|
$this->assign('canEdit', $this->barrierTest('edit', $user)); |
34
|
|
|
$this->assign('canAdd', $this->barrierTest('add', $user)); |
35
|
|
|
|
36
|
|
|
$this->assign('templateList', $templateList); |
37
|
|
|
$this->setTemplate('welcome-template/list.tpl'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Handles the requests for selecting a template to use. |
42
|
|
|
* |
43
|
|
|
* @throws ApplicationLogicException |
44
|
|
|
*/ |
45
|
|
|
protected function select() |
46
|
|
|
{ |
47
|
|
|
// get rid of GETs |
48
|
|
|
if (!WebRequest::wasPosted()) { |
49
|
|
|
$this->redirect('welcomeTemplates'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->validateCSRFToken(); |
53
|
|
|
|
54
|
|
|
$user = User::getCurrent($this->getDatabase()); |
55
|
|
|
|
56
|
|
|
if (WebRequest::postBoolean('disable')) { |
57
|
|
|
$user->setWelcomeTemplate(null); |
58
|
|
|
$user->save(); |
59
|
|
|
|
60
|
|
|
SessionAlert::success('Disabled automatic user welcoming.'); |
61
|
|
|
$this->redirect('welcomeTemplates'); |
62
|
|
|
|
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$database = $this->getDatabase(); |
67
|
|
|
|
68
|
|
|
$templateId = WebRequest::postInt('template'); |
69
|
|
|
/** @var false|WelcomeTemplate $template */ |
70
|
|
|
$template = WelcomeTemplate::getById($templateId, $database); |
71
|
|
|
|
72
|
|
|
if ($template === false || $template->isDeleted()) { |
73
|
|
|
throw new ApplicationLogicException('Unknown template'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$user->setWelcomeTemplate($template->getId()); |
77
|
|
|
$user->save(); |
78
|
|
|
|
79
|
|
|
SessionAlert::success("Updated selected welcome template for automatic welcoming."); |
80
|
|
|
|
81
|
|
|
$this->redirect('welcomeTemplates'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Handles the requests for viewing a template. |
86
|
|
|
* |
87
|
|
|
* @throws ApplicationLogicException |
88
|
|
|
*/ |
89
|
|
|
protected function view() |
90
|
|
|
{ |
91
|
|
|
$database = $this->getDatabase(); |
92
|
|
|
|
93
|
|
|
$templateId = WebRequest::getInt('template'); |
94
|
|
|
|
95
|
|
|
/** @var WelcomeTemplate $template */ |
96
|
|
|
$template = WelcomeTemplate::getById($templateId, $database); |
97
|
|
|
|
98
|
|
|
if ($template === false) { |
99
|
|
|
throw new ApplicationLogicException('Cannot find requested template'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$templateHtml = $this->getWikiTextHelper()->getHtmlForWikiText($template->getBotCode()); |
103
|
|
|
|
104
|
|
|
$this->assign('templateHtml', $templateHtml); |
105
|
|
|
$this->assign('template', $template); |
106
|
|
|
$this->setTemplate('welcome-template/view.tpl'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Handler for the add action to create a new welcome template |
111
|
|
|
* |
112
|
|
|
* @throws Exception |
113
|
|
|
*/ |
114
|
|
|
protected function add() |
115
|
|
|
{ |
116
|
|
|
if (WebRequest::wasPosted()) { |
117
|
|
|
$this->validateCSRFToken(); |
118
|
|
|
$database = $this->getDatabase(); |
119
|
|
|
|
120
|
|
|
$userCode = WebRequest::postString('usercode'); |
121
|
|
|
$botCode = WebRequest::postString('botcode'); |
122
|
|
|
|
123
|
|
|
$this->validate($userCode, $botCode); |
124
|
|
|
|
125
|
|
|
$template = new WelcomeTemplate(); |
126
|
|
|
$template->setDatabase($database); |
127
|
|
|
$template->setUserCode($userCode); |
128
|
|
|
$template->setBotCode($botCode); |
129
|
|
|
$template->save(); |
130
|
|
|
|
131
|
|
|
Logger::welcomeTemplateCreated($database, $template); |
132
|
|
|
|
133
|
|
|
$this->getNotificationHelper()->welcomeTemplateCreated($template); |
134
|
|
|
|
135
|
|
|
SessionAlert::success("Template successfully created."); |
136
|
|
|
|
137
|
|
|
$this->redirect('welcomeTemplates'); |
138
|
|
|
} |
139
|
|
|
else { |
140
|
|
|
$this->assignCSRFToken(); |
141
|
|
|
$this->setTemplate("welcome-template/add.tpl"); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Hander for editing templates |
147
|
|
|
*/ |
148
|
|
|
protected function edit() |
149
|
|
|
{ |
150
|
|
|
$database = $this->getDatabase(); |
151
|
|
|
|
152
|
|
|
$templateId = WebRequest::getInt('template'); |
153
|
|
|
|
154
|
|
|
/** @var WelcomeTemplate $template */ |
155
|
|
|
$template = WelcomeTemplate::getById($templateId, $database); |
156
|
|
|
|
157
|
|
|
if ($template === false) { |
158
|
|
|
throw new ApplicationLogicException('Cannot find requested template'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($template->isDeleted()) { |
162
|
|
|
throw new ApplicationLogicException('The specified template has been deleted'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (WebRequest::wasPosted()) { |
166
|
|
|
$this->validateCSRFToken(); |
167
|
|
|
|
168
|
|
|
$userCode = WebRequest::postString('usercode'); |
169
|
|
|
$botCode = WebRequest::postString('botcode'); |
170
|
|
|
|
171
|
|
|
$this->validate($userCode, $botCode); |
172
|
|
|
|
173
|
|
|
$template->setUserCode($userCode); |
174
|
|
|
$template->setBotCode($botCode); |
175
|
|
|
$template->setUpdateVersion(WebRequest::postInt('updateversion')); |
176
|
|
|
$template->save(); |
177
|
|
|
|
178
|
|
|
Logger::welcomeTemplateEdited($database, $template); |
179
|
|
|
|
180
|
|
|
SessionAlert::success("Template updated."); |
181
|
|
|
|
182
|
|
|
$this->getNotificationHelper()->welcomeTemplateEdited($template); |
183
|
|
|
|
184
|
|
|
$this->redirect('welcomeTemplates'); |
185
|
|
|
} |
186
|
|
|
else { |
187
|
|
|
$this->assignCSRFToken(); |
188
|
|
|
$this->assign('template', $template); |
189
|
|
|
$this->setTemplate('welcome-template/edit.tpl'); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function delete() |
194
|
|
|
{ |
195
|
|
|
$this->redirect('welcomeTemplates'); |
196
|
|
|
|
197
|
|
|
if (!WebRequest::wasPosted()) { |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$this->validateCSRFToken(); |
202
|
|
|
|
203
|
|
|
$database = $this->getDatabase(); |
204
|
|
|
|
205
|
|
|
$templateId = WebRequest::postInt('template'); |
206
|
|
|
$updateVersion = WebRequest::postInt('updateversion'); |
207
|
|
|
|
208
|
|
|
/** @var WelcomeTemplate $template */ |
209
|
|
|
$template = WelcomeTemplate::getById($templateId, $database); |
210
|
|
|
|
211
|
|
|
if ($template === false || $template->isDeleted()) { |
212
|
|
|
throw new ApplicationLogicException('Cannot find requested template'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// set the update version to the version sent by the client (optimisticly lock from initial page load) |
216
|
|
|
$template->setUpdateVersion($updateVersion); |
217
|
|
|
|
218
|
|
|
$database |
219
|
|
|
->prepare("UPDATE user SET welcome_template = NULL WHERE welcome_template = :id;") |
220
|
|
|
->execute(array(":id" => $templateId)); |
221
|
|
|
|
222
|
|
|
Logger::welcomeTemplateDeleted($database, $template); |
223
|
|
|
|
224
|
|
|
$template->delete(); |
225
|
|
|
|
226
|
|
|
SessionAlert::success( |
227
|
|
|
"Template deleted. Any users who were using this template have had automatic welcoming disabled."); |
228
|
|
|
$this->getNotificationHelper()->welcomeTemplateDeleted($templateId); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
private function validate($userCode, $botCode) |
232
|
|
|
{ |
233
|
|
|
if ($userCode === null) { |
234
|
|
|
throw new ApplicationLogicException('User code cannot be null'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if ($botCode === null) { |
238
|
|
|
throw new ApplicationLogicException('Bot code cannot be null'); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|