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\Background; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use Waca\DataObjects\Request; |
14
|
|
|
use Waca\DataObjects\User; |
15
|
|
|
use Waca\ExceptionHandler; |
16
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
17
|
|
|
use Waca\Helpers\Interfaces\IMediaWikiClient; |
18
|
|
|
use Waca\Helpers\Logger; |
19
|
|
|
use Waca\Helpers\MediaWikiHelper; |
20
|
|
|
use Waca\Helpers\RequestEmailHelper; |
21
|
|
|
use Waca\RequestStatus; |
22
|
|
|
|
23
|
|
|
abstract class CreationTaskBase extends BackgroundTaskBase |
24
|
|
|
{ |
25
|
|
|
/** @var Request */ |
26
|
|
|
private $request; |
27
|
|
|
/** |
28
|
|
|
* @var MediaWikiHelper |
29
|
|
|
* Don't use this directly. |
30
|
|
|
*/ |
31
|
|
|
private $mwHelper = null; |
32
|
|
|
|
33
|
|
|
public function execute() |
34
|
|
|
{ |
35
|
|
|
$this->request = $this->getRequest(); |
36
|
|
|
$user = $this->getTriggerUser(); |
37
|
|
|
$parameters = $this->getParameters(); |
38
|
|
|
|
39
|
|
|
if ($this->request->getStatus() !== RequestStatus::JOBQUEUE) { |
40
|
|
|
$this->markCancelled('Request is not deferred to the job queue'); |
41
|
|
|
|
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($this->request->getEmailSent() != 0 && !isset($parameters->emailText)) { |
46
|
|
|
$this->markFailed('Request has already been sent a templated email'); |
47
|
|
|
|
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($this->request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
52
|
|
|
$this->markFailed('Private data of request has been purged.'); |
53
|
|
|
|
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$emailText = null; |
58
|
|
|
$ccMailingList = null; |
59
|
|
|
$logTarget = null; |
60
|
|
|
|
61
|
|
|
if (isset($parameters->emailText) && isset($parameters->ccMailingList)) { |
62
|
|
|
$emailText = $parameters->emailText; |
63
|
|
|
$ccMailingList = $parameters->ccMailingList; |
64
|
|
|
$logTarget = "custom-y"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->getEmailTemplate() !== null) { |
68
|
|
|
$emailText = $this->getEmailTemplate()->getText(); |
69
|
|
|
$ccMailingList = false; |
70
|
|
|
$logTarget = $this->getEmailTemplate()->getId(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($emailText === null || $ccMailingList === null) { |
74
|
|
|
$this->markFailed('Unable to get closure email text'); |
75
|
|
|
|
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$this->performCreation($user); |
81
|
|
|
|
82
|
|
|
$this->request->setStatus(RequestStatus::CLOSED); |
83
|
|
|
$this->request->setQueue(null); |
84
|
|
|
$this->request->setReserved(null); |
85
|
|
|
$this->request->setEmailSent(true); |
86
|
|
|
$this->request->save(); |
87
|
|
|
|
88
|
|
|
// Log the closure as the user |
89
|
|
|
$logComment = $this->getEmailTemplate() === null ? $emailText : null; |
90
|
|
|
Logger::closeRequest($this->getDatabase(), $this->request, $logTarget, $logComment, $this->getTriggerUser()); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$requestEmailHelper = new RequestEmailHelper($this->getEmailHelper()); |
93
|
|
|
$requestEmailHelper->sendMail($this->request, $emailText, $this->getTriggerUser(), $ccMailingList); |
94
|
|
|
} |
95
|
|
|
catch (Exception $ex) { |
96
|
|
|
if (mb_strlen($ex->getMessage()) > 255) { |
97
|
|
|
ExceptionHandler::logExceptionToDisk($ex, $this->getSiteConfiguration()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->markFailed(substr($ex->getMessage(), 0, 255)); |
101
|
|
|
|
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->markComplete(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return IMediaWikiClient |
110
|
|
|
*/ |
111
|
|
|
protected abstract function getMediaWikiClient(); |
112
|
|
|
|
113
|
|
|
protected function getMediaWikiHelper() |
114
|
|
|
{ |
115
|
|
|
if ($this->mwHelper === null) { |
116
|
|
|
$this->mwHelper = new MediaWikiHelper($this->getMediaWikiClient(), $this->getSiteConfiguration()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->mwHelper; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** @noinspection PhpUnusedParameterInspection */ |
123
|
|
|
protected function getCreationReason(Request $request, User $user) |
124
|
|
|
{ |
125
|
|
|
return 'Requested account at [[WP:ACC]], request #' . $request->getId(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $name |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
protected function checkAccountExists($name) |
134
|
|
|
{ |
135
|
|
|
return $this->getMediaWikiHelper()->checkAccountExists($name); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function markFailed($reason = null, bool $acknowledged = false) |
139
|
|
|
{ |
140
|
|
|
$this->request->setStatus(RequestStatus::HOSPITAL); |
141
|
|
|
$this->request->setQueue(null); |
142
|
|
|
$this->request->save(); |
143
|
|
|
|
144
|
|
|
$this->getNotificationHelper()->requestCreationFailed($this->request, $this->getTriggerUser()); |
145
|
|
|
|
146
|
|
|
Logger::hospitalised($this->getDatabase(), $this->request); |
147
|
|
|
|
148
|
|
|
// auto-acknowledge failed creation tasks, as these land in the hospital queue anyway. |
149
|
|
|
parent::markFailed($reason, true); |
150
|
|
|
Logger::backgroundJobAcknowledged($this->getDatabase(), $this->getJob(), "Auto-acknowledged due to request deferral to hospital queue"); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $user |
155
|
|
|
* |
156
|
|
|
* @throws ApplicationLogicException |
157
|
|
|
*/ |
158
|
|
|
protected function performCreation($user) |
159
|
|
|
{ |
160
|
|
|
$mw = $this->getMediaWikiHelper(); |
161
|
|
|
|
162
|
|
|
$reason = $this->getCreationReason($this->request, $user); |
163
|
|
|
|
164
|
|
|
if ($this->checkAccountExists($this->request->getName())) { |
165
|
|
|
throw new ApplicationLogicException('Account already exists'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$mw->createAccount($this->request->getName(), $this->request->getEmail(), $reason); |
169
|
|
|
|
170
|
|
|
if (!$this->checkAccountExists($this->request->getName())) { |
171
|
|
|
throw new ApplicationLogicException('Account creation appeared to succeed but account does not exist.'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->request->setStatus(RequestStatus::CLOSED); |
175
|
|
|
$this->request->save(); |
176
|
|
|
} |
177
|
|
|
} |