Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreateSysopTask
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 6
156
0.00% covered (danger)
0.00%
0 / 1
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDependencies
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getProvidedNames
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 createUser
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
30
 initServices
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Installer\Task;
4
5use MediaWiki\Deferred\SiteStatsUpdate;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Status\Status;
8use MediaWiki\User\UserFactory;
9use MediaWiki\User\UserGroupManager;
10
11class CreateSysopTask extends Task {
12    /** @var UserFactory */
13    private $userFactory;
14
15    /** @var UserGroupManager */
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}