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