Merge "docs: Fix typo"
[mediawiki.git] / includes / installer / MysqlSettingsForm.php
blobaa78abf40d99e2820c8559a8bc8d4c6c8590b044
1 <?php
3 namespace MediaWiki\Installer;
5 use MediaWiki\MediaWikiServices;
6 use MediaWiki\Status\Status;
7 use Wikimedia\Rdbms\DBConnectionError;
9 /**
10 * @internal
12 class MysqlSettingsForm extends DatabaseSettingsForm {
14 /**
15 * @return string
17 public function getHtml() {
18 if ( $this->getMysqlInstaller()->canCreateAccounts() ) {
19 $noCreateMsg = false;
20 } else {
21 $noCreateMsg = 'config-db-web-no-create-privs';
23 $s = $this->getWebUserBox( $noCreateMsg );
25 // Do engine selector
26 $engines = $this->getMysqlInstaller()->getEngines();
27 // If the current default engine is not supported, use an engine that is
28 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
29 $this->setVar( '_MysqlEngine', reset( $engines ) );
32 // If the current default charset is not supported, use a charset that is
33 $charsets = $this->getMysqlInstaller()->getCharsets();
34 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
35 $this->setVar( '_MysqlCharset', reset( $charsets ) );
38 return $s;
41 /**
42 * @return Status
44 public function submit() {
45 $this->setVarsFromRequest( [ '_MysqlEngine', '_MysqlCharset' ] );
46 $status = $this->submitWebUserBox();
47 if ( !$status->isOK() ) {
48 return $status;
51 // Validate the create checkbox
52 $canCreate = $this->getMysqlInstaller()->canCreateAccounts();
53 if ( !$canCreate ) {
54 $this->setVar( '_CreateDBAccount', false );
55 $create = false;
56 } else {
57 $create = $this->getVar( '_CreateDBAccount' );
60 if ( !$create ) {
61 // Test the web account
62 try {
63 MediaWikiServices::getInstance()->getDatabaseFactory()->create( 'mysql', [
64 'host' => $this->getVar( 'wgDBserver' ),
65 'user' => $this->getVar( 'wgDBuser' ),
66 'password' => $this->getVar( 'wgDBpassword' ),
67 'ssl' => $this->getVar( 'wgDBssl' ),
68 'dbname' => null,
69 'flags' => 0,
70 'tablePrefix' => $this->getVar( 'wgDBprefix' )
71 ] );
72 } catch ( DBConnectionError $e ) {
73 return Status::newFatal( 'config-connection-error', $e->getMessage() );
77 // Validate engines and charsets
78 // This is done pre-submit already, so it's just for security
79 $engines = $this->getMysqlInstaller()->getEngines();
80 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
81 $this->setVar( '_MysqlEngine', reset( $engines ) );
83 $charsets = $this->getMysqlInstaller()->getCharsets();
84 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
85 $this->setVar( '_MysqlCharset', reset( $charsets ) );
88 return Status::newGood();
91 /**
92 * Downcast the DatabaseInstaller
94 private function getMysqlInstaller(): MysqlInstaller {
95 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
96 return $this->dbInstaller;