MediaWiki master
CreateSysopTask.php
Go to the documentation of this file.
1<?php
2
4
10
11class CreateSysopTask extends Task {
13 private $userFactory;
14
16 private $userGroupManager;
17
18 public function getName() {
19 return 'sysop';
20 }
21
22 public function getDependencies() {
23 return [ 'services', 'extension-tables' ];
24 }
25
26 public function getProvidedNames() {
27 return [ 'created-user-names' ];
28 }
29
30 public function execute(): Status {
31 $this->initServices( $this->getServices() );
32 $status = $this->createUser( $this->getOption( 'AdminName' ) );
33 $createdUserNames = [];
34 if ( $status->isOK() && $status->value !== null ) {
35 $createdUserNames[] = $status->value;
36 }
37 $this->getContext()->provide( 'created-user-names', $createdUserNames );
38 return $status;
39 }
40
41 private function createUser( $name ) {
42 $user = $this->userFactory->newFromName( $name );
43
44 if ( !$user ) {
45 // We should've validated this earlier anyway!
46 return Status::newFatal( 'config-admin-error-user', $name );
47 }
48
49 if ( $user->idForName() == 0 ) {
50 $user->addToDatabase();
51
52 $password = $this->getOption( 'AdminPassword' );
53 $status = $user->changeAuthenticationData( [
54 'username' => $user->getName(),
55 'password' => $password,
56 'retype' => $password,
57 ] );
58 if ( !$status->isGood() ) {
59 return Status::newFatal( 'config-admin-error-password',
60 $name, $status->getWikiText( false, false, $this->getOption( 'UserLang' ) ) );
61 }
62
63 $this->userGroupManager->addUserToGroup( $user, 'sysop' );
64 $this->userGroupManager->addUserToGroup( $user, 'bureaucrat' );
65 $this->userGroupManager->addUserToGroup( $user, 'interface-admin' );
66 if ( $this->getOption( 'AdminEmail' ) ) {
67 $user->setEmail( $this->getOption( 'AdminEmail' ) );
68 }
69 $user->saveSettings();
70
71 // Update user count
72 $ssUpdate = SiteStatsUpdate::factory( [ 'users' => 1 ] );
73 $ssUpdate->doUpdate();
74
75 return Status::newGood( $user->getName() );
76 }
77
78 return Status::newGood();
79 }
80
81 private function initServices( MediaWikiServices $services ) {
82 $this->userFactory = $services->getUserFactory();
83 $this->userGroupManager = $services->getUserGroupManager();
84 }
85
86}
Class for handling updates to the site_stats table.
getDependencies()
Get a list of names or aliases of tasks that must be done prior to this task.
getProvidedNames()
Get a list of names of objects that this task promises to provide via $this->getContext()->provide().
getName()
Get the symbolic name of the task.
Base class for installer tasks.
Definition Task.php:24
getOption(string $name)
Get an installer option value.
Definition Task.php:192
getServices()
Get the restored services.
Definition Task.php:296
getContext()
Get the execution context.
Definition Task.php:171
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Create User objects.
Manage user group memberships.