enwikipedia-acc /
waca
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 Exception; |
||
| 12 | use Waca\DataObjects\Request; |
||
| 13 | use Waca\DataObjects\User; |
||
| 14 | use Waca\Exceptions\ApplicationLogicException; |
||
| 15 | use Waca\Helpers\Interfaces\IMediaWikiClient; |
||
| 16 | use Waca\Helpers\Logger; |
||
| 17 | use Waca\Helpers\MediaWikiHelper; |
||
| 18 | use Waca\Helpers\RequestEmailHelper; |
||
| 19 | use Waca\RequestStatus; |
||
| 20 | |||
| 21 | abstract class CreationTaskBase extends BackgroundTaskBase |
||
| 22 | { |
||
| 23 | /** @var Request */ |
||
| 24 | private $request; |
||
|
0 ignored issues
–
show
Comprehensibility
introduced
by
Loading history...
|
|||
| 25 | /** |
||
| 26 | * @var MediaWikiHelper |
||
| 27 | * Don't use this directly. |
||
| 28 | */ |
||
| 29 | private $mwHelper = null; |
||
| 30 | |||
| 31 | public function execute() |
||
| 32 | { |
||
| 33 | $this->request = $this->getRequest(); |
||
| 34 | $user = $this->getTriggerUser(); |
||
| 35 | |||
| 36 | if ($this->request->getStatus() !== RequestStatus::JOBQUEUE) { |
||
| 37 | $this->markCancelled('Request is not deferred to the job queue'); |
||
| 38 | |||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | if ($this->request->getEmailSent() != 0) { |
||
| 43 | $this->markFailed('Request has already been sent an email'); |
||
| 44 | |||
| 45 | return; |
||
| 46 | } |
||
| 47 | |||
| 48 | if ($this->getEmailTemplate() === null) { |
||
| 49 | $this->markFailed('No email template specified'); |
||
| 50 | |||
| 51 | return; |
||
| 52 | } |
||
| 53 | |||
| 54 | try { |
||
| 55 | $this->performCreation($user); |
||
| 56 | |||
| 57 | $this->request->setStatus(RequestStatus::CLOSED); |
||
| 58 | $this->request->setReserved(null); |
||
| 59 | $this->request->save(); |
||
| 60 | |||
| 61 | // Log the closure as the user |
||
| 62 | Logger::closeRequest($this->getDatabase(), $this->request, $this->getEmailTemplate()->getId(), null, |
||
| 63 | $this->getTriggerUser()); |
||
| 64 | |||
| 65 | $requestEmailHelper = new RequestEmailHelper($this->getEmailHelper()); |
||
| 66 | $requestEmailHelper->sendMail($this->request, $this->getEmailTemplate()->getText(), $this->getTriggerUser(), |
||
| 67 | false); |
||
| 68 | |||
| 69 | $this->getNotificationHelper()->requestClosed($this->request, $this->getEmailTemplate()->getName()); |
||
| 70 | } |
||
| 71 | catch (Exception $ex) { |
||
| 72 | $this->markFailed($ex->getMessage()); |
||
| 73 | |||
| 74 | return; |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->markComplete(); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return IMediaWikiClient |
||
| 82 | */ |
||
| 83 | protected abstract function getMediaWikiClient(); |
||
| 84 | |||
| 85 | protected function getMediaWikiHelper(){ |
||
| 86 | if($this->mwHelper === null) { |
||
| 87 | $this->mwHelper = new MediaWikiHelper($this->getMediaWikiClient(), $this->getSiteConfiguration()); |
||
| 88 | } |
||
| 89 | |||
| 90 | return $this->mwHelper; |
||
| 91 | } |
||
| 92 | |||
| 93 | protected function getCreationReason(Request $request, User $user) |
||
| 94 | { |
||
| 95 | return 'Requested account at [[WP:ACC]], request #' . $request->getId(); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $name |
||
| 100 | * |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | protected function checkAccountExists($name) |
||
| 104 | { |
||
| 105 | return $this->getMediaWikiHelper()->checkAccountExists($name); |
||
| 106 | } |
||
| 107 | |||
| 108 | protected function markFailed($reason = null) |
||
| 109 | { |
||
| 110 | $this->request->setStatus(RequestStatus::HOSPITAL); |
||
| 111 | $this->request->save(); |
||
| 112 | |||
| 113 | Logger::hospitalised($this->getDatabase(), $this->request); |
||
| 114 | |||
| 115 | parent::markFailed($reason); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $user |
||
| 120 | * |
||
| 121 | * @throws ApplicationLogicException |
||
| 122 | */ |
||
| 123 | protected function performCreation($user) |
||
| 124 | { |
||
| 125 | $mw = $this->getMediaWikiHelper(); |
||
| 126 | |||
| 127 | $reason = $this->getCreationReason($this->request, $user); |
||
| 128 | |||
| 129 | if ($this->checkAccountExists($this->request->getName())) { |
||
| 130 | throw new ApplicationLogicException('Account already exists'); |
||
| 131 | } |
||
| 132 | |||
| 133 | $mw->createAccount($this->request->getName(), $this->request->getEmail(), $reason); |
||
| 134 | |||
| 135 | if (!$this->checkAccountExists($this->request->getName())) { |
||
| 136 | throw new ApplicationLogicException('Account creation appeared to succeed but account does not exist.'); |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->request->setStatus(RequestStatus::CLOSED); |
||
| 140 | $this->request->save(); |
||
| 141 | } |
||
| 142 | } |