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
19 public function getName() {
20 return 'sysop';
21 }
22
24 public function getDependencies() {
25 return [ 'services', 'extension-tables' ];
26 }
27
29 public function getProvidedNames() {
30 return [ 'created-user-names' ];
31 }
32
33 public function execute(): Status {
34 $this->initServices( $this->getServices() );
35 $status = $this->createUser( $this->getOption( 'AdminName' ) );
36 $createdUserNames = [];
37 if ( $status->isOK() && $status->value !== null ) {
38 $createdUserNames[] = $status->value;
39 }
40 $this->getContext()->provide( 'created-user-names', $createdUserNames );
41 return $status;
42 }
43
44 private function createUser( string $name ): Status {
45 $user = $this->userFactory->newFromName( $name );
46
47 if ( !$user ) {
48 // We should've validated this earlier anyway!
49 return Status::newFatal( 'config-admin-error-user', $name );
50 }
51
52 if ( $user->idForName() == 0 ) {
53 $user->addToDatabase();
54
55 $password = $this->getOption( 'AdminPassword' );
56 $status = $user->changeAuthenticationData( [
57 'username' => $user->getName(),
58 'password' => $password,
59 'retype' => $password,
60 ] );
61 if ( !$status->isGood() ) {
62 return Status::newFatal( 'config-admin-error-password',
63 $name, $status->getWikiText( false, false, $this->getOption( 'UserLang' ) ) );
64 }
65
66 $this->userGroupManager->addUserToGroup( $user, 'sysop' );
67 $this->userGroupManager->addUserToGroup( $user, 'bureaucrat' );
68 $this->userGroupManager->addUserToGroup( $user, 'interface-admin' );
69 if ( $this->getOption( 'AdminEmail' ) ) {
70 $user->setEmail( $this->getOption( 'AdminEmail' ) );
71 }
72 $user->saveSettings();
73
74 // Update user count
75 $ssUpdate = SiteStatsUpdate::factory( [ 'users' => 1 ] );
76 $ssUpdate->doUpdate();
77
78 return Status::newGood( $user->getName() );
79 }
80
81 return Status::newGood();
82 }
83
84 private function initServices( MediaWikiServices $services ) {
85 $this->userFactory = $services->getUserFactory();
86 $this->userGroupManager = $services->getUserGroupManager();
87 }
88
89}
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.to override string|strin...
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.string
Base class for installer tasks.
Definition Task.php:24
getOption(string $name)
Get an installer option value.
Definition Task.php:190
getServices()
Get the restored services.
Definition Task.php:292
getContext()
Get the execution context.
Definition Task.php:169
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Create User objects.
Manage user group memberships.