3 namespace MediaWiki\Installer
;
5 use MediaWiki\MediaWikiServices
;
6 use MediaWiki\Status\Status
;
7 use Wikimedia\Rdbms\DBConnectionError
;
12 class MysqlSettingsForm
extends DatabaseSettingsForm
{
17 public function getHtml() {
18 if ( $this->getMysqlInstaller()->canCreateAccounts() ) {
21 $noCreateMsg = 'config-db-web-no-create-privs';
23 $s = $this->getWebUserBox( $noCreateMsg );
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 ) );
44 public function submit() {
45 $this->setVarsFromRequest( [ '_MysqlEngine', '_MysqlCharset' ] );
46 $status = $this->submitWebUserBox();
47 if ( !$status->isOK() ) {
51 // Validate the create checkbox
52 $canCreate = $this->getMysqlInstaller()->canCreateAccounts();
54 $this->setVar( '_CreateDBAccount', false );
57 $create = $this->getVar( '_CreateDBAccount' );
61 // Test the web account
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' ),
70 'tablePrefix' => $this->getVar( 'wgDBprefix' )
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();
92 * Downcast the DatabaseInstaller
94 private function getMysqlInstaller(): MysqlInstaller
{
95 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
96 return $this->dbInstaller
;