1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* ACC Development Team. Please see team.json for a list of contributors. * |
5
|
|
|
* * |
6
|
|
|
* This is free and unencumbered software released into the public domain. * |
7
|
|
|
* Please see LICENSE.md for the full licencing statement. * |
8
|
|
|
******************************************************************************/ |
9
|
|
|
|
10
|
|
|
namespace Waca\Pages; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use Waca\DataObjects\User; |
14
|
|
|
use Waca\DataObjects\WelcomeTemplate; |
15
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
16
|
|
|
use Waca\Helpers\Logger; |
17
|
|
|
use Waca\Helpers\MediaWikiHelper; |
18
|
|
|
use Waca\Helpers\OAuthUserHelper; |
19
|
|
|
use Waca\Helpers\PreferenceManager; |
20
|
|
|
use Waca\SessionAlert; |
21
|
|
|
use Waca\Tasks\InternalPageBase; |
22
|
|
|
use Waca\WebRequest; |
23
|
|
|
|
24
|
|
|
class PageWelcomeTemplateManagement extends InternalPageBase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Main function for this page, when no specific actions are called. |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
protected function main() |
31
|
|
|
{ |
32
|
|
|
$database = $this->getDatabase(); |
33
|
|
|
$templateList = WelcomeTemplate::getAll($database, 1); // FIXME: domains |
34
|
|
|
$preferenceManager = PreferenceManager::getForCurrent($database); |
35
|
|
|
|
36
|
|
|
$this->setHtmlTitle('Welcome Templates'); |
37
|
|
|
|
38
|
|
|
$this->assignCSRFToken(); |
39
|
|
|
|
40
|
|
|
$user = User::getCurrent($database); |
41
|
|
|
|
42
|
|
|
$currentTemplate = $preferenceManager->getPreference(PreferenceManager::PREF_WELCOMETEMPLATE); |
43
|
|
|
$this->assign('currentTemplate', $currentTemplate); |
44
|
|
|
|
45
|
|
|
$this->assign('canEdit', $this->barrierTest('edit', $user)); |
46
|
|
|
$this->assign('canAdd', $this->barrierTest('add', $user)); |
47
|
|
|
$this->assign('canSelect', $this->barrierTest('select', $user)); |
48
|
|
|
|
49
|
|
|
$this->assign('templateList', $templateList); |
50
|
|
|
$this->setTemplate('welcome-template/list.tpl'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Handles the requests for selecting a template to use. |
55
|
|
|
* |
56
|
|
|
* @throws ApplicationLogicException |
57
|
|
|
*/ |
58
|
|
|
protected function select() |
59
|
|
|
{ |
60
|
|
|
// get rid of GETs |
61
|
|
|
if (!WebRequest::wasPosted()) { |
62
|
|
|
$this->redirect('welcomeTemplates'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->validateCSRFToken(); |
66
|
|
|
|
67
|
|
|
$database = $this->getDatabase(); |
68
|
|
|
$user = User::getCurrent($database); |
|
|
|
|
69
|
|
|
$preferenceManager = PreferenceManager::getForCurrent($database); |
70
|
|
|
|
71
|
|
|
if (WebRequest::postBoolean('disable')) { |
72
|
|
|
$preferenceManager->setLocalPreference(PreferenceManager::PREF_WELCOMETEMPLATE, null); |
73
|
|
|
|
74
|
|
|
SessionAlert::success('Disabled automatic user welcoming.'); |
75
|
|
|
$this->redirect('welcomeTemplates'); |
76
|
|
|
|
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$templateId = WebRequest::postInt('template'); |
81
|
|
|
/** @var false|WelcomeTemplate $template */ |
82
|
|
|
$template = WelcomeTemplate::getById($templateId, $database); |
83
|
|
|
|
84
|
|
|
if ($template === false || $template->isDeleted()) { |
85
|
|
|
throw new ApplicationLogicException('Unknown template'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$preferenceManager->setLocalPreference(PreferenceManager::PREF_WELCOMETEMPLATE, $template->getId()); |
89
|
|
|
|
90
|
|
|
SessionAlert::success("Updated selected welcome template for automatic welcoming."); |
91
|
|
|
|
92
|
|
|
$this->redirect('welcomeTemplates'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Handles the requests for viewing a template. |
97
|
|
|
* |
98
|
|
|
* @throws ApplicationLogicException |
99
|
|
|
*/ |
100
|
|
|
protected function view() |
101
|
|
|
{ |
102
|
|
|
$this->setHtmlTitle('View Welcome Template'); |
103
|
|
|
|
104
|
|
|
$database = $this->getDatabase(); |
105
|
|
|
|
106
|
|
|
$templateId = WebRequest::getInt('template'); |
107
|
|
|
|
108
|
|
|
/** @var false|WelcomeTemplate $template */ |
109
|
|
|
$template = WelcomeTemplate::getById($templateId, $database); |
110
|
|
|
|
111
|
|
|
if ($template === false) { |
|
|
|
|
112
|
|
|
throw new ApplicationLogicException('Cannot find requested template'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$currentUser = User::getCurrent($database); |
116
|
|
|
|
117
|
|
|
// This includes a section header, because we use the "new section" API call. |
118
|
|
|
$wikiText = "== " . $template->getSectionHeader() . "==\n" . $template->getBotCodeForWikiSave('Example User', $currentUser->getOnWikiName()); |
119
|
|
|
|
120
|
|
|
$oauth = new OAuthUserHelper($currentUser, $database, $this->getOauthProtocolHelper(), |
121
|
|
|
$this->getSiteConfiguration()); |
122
|
|
|
$mediaWikiHelper = new MediaWikiHelper($oauth, $this->getSiteConfiguration()); |
123
|
|
|
|
124
|
|
|
$templateHtml = $mediaWikiHelper->getHtmlForWikiText($wikiText); |
125
|
|
|
|
126
|
|
|
// Add site to relevant links, since the MediaWiki parser returns, eg, `/wiki/Help:Introduction` |
127
|
|
|
// and we want to link to <https://en.wikipedia.org/wiki/Help:Introduction> rather than |
128
|
|
|
// <https://accounts.wmflabs.org/wiki/Help:Introduction> |
129
|
|
|
// The code currently assumes that the template was parsed for enwiki, and will need to be |
130
|
|
|
// updated once other wikis are supported. |
131
|
|
|
$templateHtml = preg_replace('/(<a href=")(\/wiki\/)/', '$1//en.wikipedia.org$2', $templateHtml); |
132
|
|
|
|
133
|
|
|
$this->assign('templateHtml', $templateHtml); |
134
|
|
|
$this->assign('template', $template); |
135
|
|
|
$this->setTemplate('welcome-template/view.tpl'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Handler for the add action to create a new welcome template |
140
|
|
|
* |
141
|
|
|
* @throws Exception |
142
|
|
|
*/ |
143
|
|
|
protected function add() |
144
|
|
|
{ |
145
|
|
|
$this->assign('createmode', true); |
146
|
|
|
|
147
|
|
|
if (WebRequest::wasPosted()) { |
148
|
|
|
$this->validateCSRFToken(); |
149
|
|
|
$database = $this->getDatabase(); |
150
|
|
|
|
151
|
|
|
$userCode = WebRequest::postString('usercode'); |
152
|
|
|
$botCode = WebRequest::postString('botcode'); |
153
|
|
|
|
154
|
|
|
$this->validate($userCode, $botCode); |
155
|
|
|
|
156
|
|
|
$template = new WelcomeTemplate(); |
157
|
|
|
$template->setDatabase($database); |
158
|
|
|
$template->setUserCode($userCode); |
159
|
|
|
$template->setBotCode($botCode); |
160
|
|
|
$template->setDomain(1); // FIXME: domains! |
161
|
|
|
$template->save(); |
162
|
|
|
|
163
|
|
|
Logger::welcomeTemplateCreated($database, $template); |
164
|
|
|
|
165
|
|
|
$this->getNotificationHelper()->welcomeTemplateCreated($template); |
166
|
|
|
|
167
|
|
|
SessionAlert::success("Template successfully created."); |
168
|
|
|
|
169
|
|
|
$this->redirect('welcomeTemplates'); |
170
|
|
|
} |
171
|
|
|
else { |
172
|
|
|
$this->assignCSRFToken(); |
173
|
|
|
$this->assign('template', new WelcomeTemplate()); |
174
|
|
|
$this->setTemplate("welcome-template/edit.tpl"); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Handler for editing templates |
180
|
|
|
*/ |
181
|
|
|
protected function edit() |
182
|
|
|
{ |
183
|
|
|
$database = $this->getDatabase(); |
184
|
|
|
|
185
|
|
|
$templateId = WebRequest::getInt('template'); |
186
|
|
|
|
187
|
|
|
/** @var false|WelcomeTemplate $template */ |
188
|
|
|
$template = WelcomeTemplate::getById($templateId, $database); |
189
|
|
|
|
190
|
|
|
if ($template === false) { |
|
|
|
|
191
|
|
|
throw new ApplicationLogicException('Cannot find requested template'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($template->isDeleted()) { |
195
|
|
|
throw new ApplicationLogicException('The specified template has been deleted'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$this->assign('createmode', false); |
199
|
|
|
|
200
|
|
|
if (WebRequest::wasPosted()) { |
201
|
|
|
$this->validateCSRFToken(); |
202
|
|
|
|
203
|
|
|
$userCode = WebRequest::postString('usercode'); |
204
|
|
|
$botCode = WebRequest::postString('botcode'); |
205
|
|
|
|
206
|
|
|
$this->validate($userCode, $botCode); |
207
|
|
|
|
208
|
|
|
$template->setUserCode($userCode); |
209
|
|
|
$template->setBotCode($botCode); |
210
|
|
|
$template->setUpdateVersion(WebRequest::postInt('updateversion')); |
211
|
|
|
$template->save(); |
212
|
|
|
|
213
|
|
|
Logger::welcomeTemplateEdited($database, $template); |
214
|
|
|
|
215
|
|
|
SessionAlert::success("Template updated."); |
216
|
|
|
|
217
|
|
|
$this->getNotificationHelper()->welcomeTemplateEdited($template); |
218
|
|
|
|
219
|
|
|
$this->redirect('welcomeTemplates'); |
220
|
|
|
} |
221
|
|
|
else { |
222
|
|
|
$this->assignCSRFToken(); |
223
|
|
|
$this->assign('template', $template); |
224
|
|
|
$this->setTemplate('welcome-template/edit.tpl'); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
protected function delete() |
229
|
|
|
{ |
230
|
|
|
if (!WebRequest::wasPosted()) { |
231
|
|
|
$this->redirect('welcomeTemplates'); |
232
|
|
|
return; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$this->validateCSRFToken(); |
236
|
|
|
|
237
|
|
|
$database = $this->getDatabase(); |
238
|
|
|
|
239
|
|
|
$templateId = WebRequest::postInt('template'); |
240
|
|
|
$updateVersion = WebRequest::postInt('updateversion'); |
241
|
|
|
|
242
|
|
|
/** @var false|WelcomeTemplate $template */ |
243
|
|
|
$template = WelcomeTemplate::getById($templateId, $database); |
244
|
|
|
|
245
|
|
|
if ($template === false || $template->isDeleted()) { |
246
|
|
|
throw new ApplicationLogicException('Cannot find requested template'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// set the update version to the version sent by the client (optimisticly lock from initial page load) |
250
|
|
|
$template->setUpdateVersion($updateVersion); |
251
|
|
|
|
252
|
|
|
$database |
253
|
|
|
->prepare("UPDATE userpreference SET value = NULL, updateversion = updateversion + 1 WHERE preference = :pref and value = :id;") |
254
|
|
|
->execute([ |
255
|
|
|
':id' => $templateId, |
256
|
|
|
':pref' => PreferenceManager::PREF_WELCOMETEMPLATE |
257
|
|
|
]); |
258
|
|
|
|
259
|
|
|
Logger::welcomeTemplateDeleted($database, $template); |
260
|
|
|
|
261
|
|
|
$template->delete(); |
262
|
|
|
|
263
|
|
|
$this->redirect('welcomeTemplates'); |
264
|
|
|
|
265
|
|
|
SessionAlert::success( |
266
|
|
|
"Template deleted. Any users who were using this template have had automatic welcoming disabled."); |
267
|
|
|
$this->getNotificationHelper()->welcomeTemplateDeleted($templateId); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
private function validate($userCode, $botCode) |
271
|
|
|
{ |
272
|
|
|
if ($userCode === null) { |
273
|
|
|
throw new ApplicationLogicException('User code cannot be null'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if ($botCode === null) { |
277
|
|
|
throw new ApplicationLogicException('Bot code cannot be null'); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|