enwikipedia-acc /
waca
| 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\Task; |
||
| 11 | |||
| 12 | use Waca\Background\BackgroundTaskBase; |
||
| 13 | use Waca\DataObjects\Request; |
||
| 14 | use Waca\DataObjects\User; |
||
| 15 | use Waca\DataObjects\WelcomeTemplate; |
||
| 16 | use Waca\Helpers\MediaWikiHelper; |
||
| 17 | use Waca\Helpers\OAuthUserHelper; |
||
| 18 | use Waca\Helpers\PreferenceManager; |
||
| 19 | use Waca\RequestStatus; |
||
| 20 | |||
| 21 | class WelcomeUserTask extends BackgroundTaskBase |
||
| 22 | { |
||
| 23 | /** @var Request */ |
||
| 24 | private $request; |
||
| 25 | |||
| 26 | public function execute() |
||
| 27 | { |
||
| 28 | $database = $this->getDatabase(); |
||
| 29 | $this->request = $this->getRequest(); |
||
| 30 | $user = $this->getTriggerUser(); |
||
| 31 | //FIXME: domains |
||
| 32 | $userPrefs = new PreferenceManager($database, $user->getId(), 1); |
||
| 33 | |||
| 34 | $welcomeTemplate = $userPrefs->getPreference(PreferenceManager::PREF_WELCOMETEMPLATE); |
||
| 35 | |||
| 36 | if ($welcomeTemplate === null) { |
||
| 37 | $this->markFailed('Welcome template not specified'); |
||
| 38 | |||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** @var WelcomeTemplate $template */ |
||
| 43 | $template = WelcomeTemplate::getById($welcomeTemplate, $database); |
||
| 44 | |||
| 45 | if ($template === false) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 46 | $this->markFailed('Welcome template missing'); |
||
| 47 | |||
| 48 | return; |
||
| 49 | } |
||
| 50 | |||
| 51 | $oauth = new OAuthUserHelper($user, $database, $this->getOauthProtocolHelper(), |
||
| 52 | $this->getSiteConfiguration()); |
||
| 53 | $mediaWikiHelper = new MediaWikiHelper($oauth, $this->getSiteConfiguration()); |
||
| 54 | |||
| 55 | if ($this->request->getStatus() !== RequestStatus::CLOSED) { |
||
| 56 | $this->markFailed('Request is currently open'); |
||
| 57 | |||
| 58 | return; |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!$mediaWikiHelper->checkAccountExists($this->request->getName())) { |
||
| 62 | $this->markFailed('Account does not exist!'); |
||
| 63 | |||
| 64 | return; |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->performWelcome($template, $this->request, $user, $mediaWikiHelper); |
||
| 68 | $this->markComplete(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Performs the welcome |
||
| 73 | * |
||
| 74 | * @param WelcomeTemplate $template |
||
| 75 | * @param Request $request |
||
| 76 | * @param User $user The user who triggered the job |
||
| 77 | * @param MediaWikiHelper $mediaWikiHelper |
||
| 78 | */ |
||
| 79 | private function performWelcome( |
||
| 80 | WelcomeTemplate $template, |
||
| 81 | Request $request, |
||
| 82 | User $user, |
||
| 83 | MediaWikiHelper $mediaWikiHelper |
||
| 84 | ) { |
||
| 85 | $templateText = $template->getBotCodeForWikiSave($request->getName(), $user->getOnWikiName()); |
||
| 86 | |||
| 87 | $mediaWikiHelper->addTalkPageMessage($request->getName(), $template->getSectionHeader(), 'Welcoming user created through [[WP:ACC]]', $templateText); |
||
| 88 | } |
||
| 89 | |||
| 90 | protected function markFailed($reason = null, bool $acknowledged = false) |
||
| 91 | { |
||
| 92 | $this->getNotificationHelper()->requestWelcomeFailed($this->request, $this->getTriggerUser()); |
||
| 93 | |||
| 94 | parent::markFailed($reason, $acknowledged); |
||
| 95 | } |
||
| 96 | } |