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 Waca\DataObjects\EmailTemplate; |
13
|
|
|
use Waca\DataObjects\JobQueue; |
14
|
|
|
use Waca\DataObjects\Request; |
15
|
|
|
use Waca\DataObjects\User; |
16
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
17
|
|
|
use Waca\Helpers\HttpHelper; |
18
|
|
|
use Waca\Helpers\Interfaces\IEmailHelper; |
19
|
|
|
use Waca\Helpers\Interfaces\IOAuthProtocolHelper; |
20
|
|
|
use Waca\Helpers\IrcNotificationHelper; |
21
|
|
|
use Waca\Helpers\Logger; |
22
|
|
|
use Waca\PdoDatabase; |
23
|
|
|
use Waca\SiteConfiguration; |
24
|
|
|
|
25
|
|
|
abstract class BackgroundTaskBase |
26
|
|
|
{ |
27
|
|
|
/** @var JobQueue */ |
28
|
|
|
private $job; |
29
|
|
|
/** @var PdoDatabase */ |
30
|
|
|
private $database; |
31
|
|
|
/** @var IOAuthProtocolHelper */ |
32
|
|
|
private $oauthProtocolHelper; |
33
|
|
|
/** @var SiteConfiguration */ |
34
|
|
|
private $siteConfiguration; |
35
|
|
|
/** @var IEmailHelper */ |
36
|
|
|
private $emailHelper; |
37
|
|
|
/** @var HttpHelper */ |
38
|
|
|
private $httpHelper; |
39
|
|
|
/** @var IrcNotificationHelper */ |
40
|
|
|
private $notificationHelper; |
41
|
|
|
/** @var User */ |
42
|
|
|
private $triggerUser; |
43
|
|
|
/** @var Request */ |
44
|
|
|
private $request; |
45
|
|
|
/** @var EmailTemplate */ |
46
|
|
|
private $emailTemplate = null; |
47
|
|
|
/** @var mixed */ |
48
|
|
|
private $parameters; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return JobQueue |
52
|
|
|
*/ |
53
|
|
|
public function getJob() |
54
|
|
|
{ |
55
|
|
|
return $this->job; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param JobQueue $job |
60
|
|
|
*/ |
61
|
|
|
public function setJob(JobQueue $job) |
62
|
|
|
{ |
63
|
|
|
$this->job = $job; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return PdoDatabase |
68
|
|
|
*/ |
69
|
|
|
public function getDatabase() |
70
|
|
|
{ |
71
|
|
|
return $this->database; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param PdoDatabase $database |
76
|
|
|
*/ |
77
|
|
|
public function setDatabase(PdoDatabase $database) |
78
|
|
|
{ |
79
|
|
|
$this->database = $database; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return IOAuthProtocolHelper |
84
|
|
|
*/ |
85
|
|
|
public function getOauthProtocolHelper() |
86
|
|
|
{ |
87
|
|
|
return $this->oauthProtocolHelper; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param IOAuthProtocolHelper $oauthProtocolHelper |
92
|
|
|
*/ |
93
|
|
|
public function setOauthProtocolHelper(IOAuthProtocolHelper $oauthProtocolHelper) |
94
|
|
|
{ |
95
|
|
|
$this->oauthProtocolHelper = $oauthProtocolHelper; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return SiteConfiguration |
100
|
|
|
*/ |
101
|
|
|
public function getSiteConfiguration() |
102
|
|
|
{ |
103
|
|
|
return $this->siteConfiguration; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param SiteConfiguration $siteConfiguration |
108
|
|
|
*/ |
109
|
|
|
public function setSiteConfiguration(SiteConfiguration $siteConfiguration) |
110
|
|
|
{ |
111
|
|
|
$this->siteConfiguration = $siteConfiguration; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return HttpHelper |
116
|
|
|
*/ |
117
|
|
|
public function getHttpHelper() |
118
|
|
|
{ |
119
|
|
|
return $this->httpHelper; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param HttpHelper $httpHelper |
124
|
|
|
*/ |
125
|
|
|
public function setHttpHelper(HttpHelper $httpHelper) |
126
|
|
|
{ |
127
|
|
|
$this->httpHelper = $httpHelper; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return IEmailHelper |
132
|
|
|
*/ |
133
|
|
|
public function getEmailHelper() |
134
|
|
|
{ |
135
|
|
|
return $this->emailHelper; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param IEmailHelper $emailHelper |
140
|
|
|
*/ |
141
|
|
|
public function setEmailHelper(IEmailHelper $emailHelper) |
142
|
|
|
{ |
143
|
|
|
$this->emailHelper = $emailHelper; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return IrcNotificationHelper |
148
|
|
|
*/ |
149
|
|
|
public function getNotificationHelper() |
150
|
|
|
{ |
151
|
|
|
return $this->notificationHelper; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param IrcNotificationHelper $notificationHelper |
156
|
|
|
*/ |
157
|
|
|
public function setNotificationHelper($notificationHelper) |
158
|
|
|
{ |
159
|
|
|
$this->notificationHelper = $notificationHelper; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
protected abstract function execute(); |
166
|
|
|
|
167
|
|
|
public function run() |
168
|
|
|
{ |
169
|
|
|
$this->triggerUser = User::getById($this->job->getTriggerUserId(), $this->getDatabase()); |
170
|
|
|
|
171
|
|
|
if ($this->triggerUser === false) { |
|
|
|
|
172
|
|
|
throw new ApplicationLogicException('Cannot locate trigger user'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$this->request = Request::getById($this->job->getRequest(), $this->getDatabase()); |
|
|
|
|
176
|
|
|
|
177
|
|
|
if ($this->request === false) { |
178
|
|
|
throw new ApplicationLogicException('Cannot locate request'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($this->job->getEmailTemplate() !== null) { |
|
|
|
|
182
|
|
|
$this->emailTemplate = EmailTemplate::getById($this->job->getEmailTemplate(), $this->getDatabase()); |
|
|
|
|
183
|
|
|
|
184
|
|
|
if ($this->emailTemplate === false) { |
185
|
|
|
throw new ApplicationLogicException('Cannot locate email template'); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($this->job->getParameters() !== null) { |
|
|
|
|
190
|
|
|
$this->parameters = json_decode($this->job->getParameters()); |
191
|
|
|
|
192
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
193
|
|
|
throw new ApplicationLogicException('JSON decode: ' . json_last_error_msg()); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Should we wait for a parent job? |
198
|
|
|
if ($this->job->getParent() !== null) { |
|
|
|
|
199
|
|
|
/** @var JobQueue $parentJob */ |
200
|
|
|
$parentJob = JobQueue::getById($this->job->getParent(), $this->getDatabase()); |
201
|
|
|
|
202
|
|
|
if ($parentJob === false) { |
|
|
|
|
203
|
|
|
$this->markFailed("Parent job could not be found"); |
204
|
|
|
return; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
switch ($parentJob->getStatus()) { |
208
|
|
|
case JobQueue::STATUS_CANCELLED: |
209
|
|
|
case JobQueue::STATUS_FAILED: |
210
|
|
|
$this->markCancelled('Parent job failed/cancelled'); |
211
|
|
|
return; |
212
|
|
|
case JobQueue::STATUS_WAITING: |
213
|
|
|
case JobQueue::STATUS_READY: |
214
|
|
|
case JobQueue::STATUS_QUEUED: |
215
|
|
|
case JobQueue::STATUS_RUNNING: |
216
|
|
|
case JobQueue::STATUS_HELD: |
217
|
|
|
// Defer to next execution |
218
|
|
|
$this->job->setStatus(JobQueue::STATUS_READY); |
219
|
|
|
$this->job->save(); |
220
|
|
|
return; |
221
|
|
|
case JobQueue::STATUS_COMPLETE: |
222
|
|
|
// do nothing |
223
|
|
|
break; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$this->execute(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
protected function markComplete() |
231
|
|
|
{ |
232
|
|
|
$this->job->setStatus(JobQueue::STATUS_COMPLETE); |
233
|
|
|
$this->job->setError(null); |
234
|
|
|
$this->job->setAcknowledged(null); |
235
|
|
|
$this->job->save(); |
236
|
|
|
|
237
|
|
|
Logger::backgroundJobComplete($this->getDatabase(), $this->getJob()); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
protected function markCancelled($reason = null) |
241
|
|
|
{ |
242
|
|
|
$this->job->setStatus(JobQueue::STATUS_CANCELLED); |
243
|
|
|
$this->job->setError($reason); |
244
|
|
|
$this->job->setAcknowledged(null); |
245
|
|
|
$this->job->save(); |
246
|
|
|
|
247
|
|
|
Logger::backgroundJobIssue($this->getDatabase(), $this->getJob()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
protected function markFailed($reason = null, bool $acknowledged = false) |
251
|
|
|
{ |
252
|
|
|
$this->job->setStatus(JobQueue::STATUS_FAILED); |
253
|
|
|
$this->job->setError($reason); |
254
|
|
|
$this->job->setAcknowledged($acknowledged ? 1 : 0); |
255
|
|
|
$this->job->save(); |
256
|
|
|
|
257
|
|
|
Logger::backgroundJobIssue($this->getDatabase(), $this->getJob()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return User |
262
|
|
|
*/ |
263
|
|
|
public function getTriggerUser() |
264
|
|
|
{ |
265
|
|
|
return $this->triggerUser; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return Request |
270
|
|
|
*/ |
271
|
|
|
public function getRequest() |
272
|
|
|
{ |
273
|
|
|
return $this->request; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return EmailTemplate |
278
|
|
|
*/ |
279
|
|
|
public function getEmailTemplate() |
280
|
|
|
{ |
281
|
|
|
return $this->emailTemplate; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return mixed |
286
|
|
|
*/ |
287
|
|
|
public function getParameters() |
288
|
|
|
{ |
289
|
|
|
return $this->parameters; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|