diff options
author | Tim Starling <tstarling@wikimedia.org> | 2024-03-04 16:27:34 +1100 |
---|---|---|
committer | Tim Starling <tstarling@wikimedia.org> | 2024-03-13 13:04:13 +1100 |
commit | 3f852f7ddc20d3d126b155cfcdc543af6a5181ab (patch) | |
tree | 1229b4a69b613b548392bfcfd4c1a6ec6ae974eb /includes/installer/MysqlInstaller.php | |
parent | 2d9190700d5187182b97eb5b9e691d824328ef86 (diff) | |
download | mediawikicore-3f852f7ddc20d3d126b155cfcdc543af6a5181ab.tar.gz mediawikicore-3f852f7ddc20d3d126b155cfcdc543af6a5181ab.zip |
Split web-specific code out of DatabaseInstaller
DatabaseInstaller had some LSP violations, such as calling undeclared
methods of the supplied Installer object, assuming that it was a
WebInstaller. It was also large.
So, split the web form parts of DatabaseInstaller into separate classes.
We have a class hierarchy for the connect forms, and a class hierarchy
for the settings forms, with a base class DatabaseForm mostly as a place
to put protected helper methods.
Also, have DatabaseInstaller::getConnection() return a special subclass
of Status, so that we can remove many Phan type overrides.
Change-Id: Ie84025f8f70b895fa6882848b9a21ba1750d60e2
Diffstat (limited to 'includes/installer/MysqlInstaller.php')
-rw-r--r-- | includes/installer/MysqlInstaller.php | 171 |
1 files changed, 11 insertions, 160 deletions
diff --git a/includes/installer/MysqlInstaller.php b/includes/installer/MysqlInstaller.php index 71f80a2c9f3a..ca6af464b1b5 100644 --- a/includes/installer/MysqlInstaller.php +++ b/includes/installer/MysqlInstaller.php @@ -24,10 +24,7 @@ namespace MediaWiki\Installer; -use MediaWiki\Html\Html; -use MediaWiki\MediaWikiServices; use MediaWiki\Status\Status; -use Wikimedia\Rdbms\Database; use Wikimedia\Rdbms\DatabaseFactory; use Wikimedia\Rdbms\DatabaseMySQL; use Wikimedia\Rdbms\DBConnectionError; @@ -89,72 +86,12 @@ class MysqlInstaller extends DatabaseInstaller { return self::checkExtension( 'mysqli' ); } - /** - * @return string - */ - public function getConnectForm() { - return $this->getTextBox( - 'wgDBserver', - 'config-db-host', - [], - $this->parent->getHelpBox( 'config-db-host-help' ) - ) . - $this->getCheckBox( 'wgDBssl', 'config-db-ssl' ) . - "<span class=\"cdx-card\"><span class=\"cdx-card__text\">" . - Html::element( - 'span', - [ 'class' => 'cdx-card__text__title' ], - wfMessage( 'config-db-wiki-settings' )->text() - ) . - "<span class=\"cdx-card__text__description\">" . - $this->getTextBox( 'wgDBname', 'config-db-name', [ 'dir' => 'ltr' ], - $this->parent->getHelpBox( 'config-db-name-help' ) ) . - $this->getTextBox( 'wgDBprefix', 'config-db-prefix', [ 'dir' => 'ltr' ], - $this->parent->getHelpBox( 'config-db-prefix-help' ) ) . - "</span></span></span>" . - $this->getInstallUserBox(); + public function getConnectForm( WebInstaller $webInstaller ): DatabaseConnectForm { + return new MysqlConnectForm( $webInstaller, $this ); } - public function submitConnectForm() { - // Get variables from the request. - $newValues = $this->setVarsFromRequest( [ 'wgDBserver', 'wgDBname', 'wgDBprefix', 'wgDBssl' ] ); - - // Validate them. - $status = Status::newGood(); - if ( !strlen( $newValues['wgDBserver'] ) ) { - $status->fatal( 'config-missing-db-host' ); - } - if ( !strlen( $newValues['wgDBname'] ) ) { - $status->fatal( 'config-missing-db-name' ); - } elseif ( !preg_match( '/^[a-z0-9+_-]+$/i', $newValues['wgDBname'] ) ) { - $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] ); - } - if ( !preg_match( '/^[a-z0-9_-]*$/i', $newValues['wgDBprefix'] ) ) { - $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] ); - } - if ( !$status->isOK() ) { - return $status; - } - - // Submit user box - $status = $this->submitInstallUserBox(); - if ( !$status->isOK() ) { - return $status; - } - - // Try to connect - $status = $this->getConnection(); - if ( !$status->isOK() ) { - return $status; - } - /** - * @var Database $conn - */ - $conn = $status->value; - '@phan-var Database $conn'; - - // Check version - return static::meetsMinimumRequirement( $conn ); + public function getSettingsForm( WebInstaller $webInstaller ): DatabaseSettingsForm { + return new MysqlSettingsForm( $webInstaller, $this ); } public static function meetsMinimumRequirement( IDatabase $conn ) { @@ -166,10 +103,10 @@ class MysqlInstaller extends DatabaseInstaller { } /** - * @return Status + * @return ConnectionStatus */ public function openConnection() { - $status = Status::newGood(); + $status = new ConnectionStatus; try { /** @var DatabaseMySQL $db */ $db = ( new DatabaseFactory() )->create( 'mysql', [ @@ -180,7 +117,7 @@ class MysqlInstaller extends DatabaseInstaller { 'dbname' => false, 'flags' => 0, 'tablePrefix' => $this->getVar( 'wgDBprefix' ) ] ); - $status->value = $db; + $status->setDB( $db ); } catch ( DBConnectionError $e ) { $status->fatal( 'config-connection-error', $e->getMessage() ); } @@ -197,10 +134,7 @@ class MysqlInstaller extends DatabaseInstaller { return; } - /** - * @var Database $conn - */ - $conn = $status->value; + $conn = $status->getDB(); $this->selectDatabase( $conn, $this->getVar( 'wgDBname' ) ); # Determine existing default character set if ( $conn->tableExists( "revision", __METHOD__ ) ) { @@ -260,11 +194,7 @@ class MysqlInstaller extends DatabaseInstaller { */ public function getEngines() { $status = $this->getConnection(); - - /** - * @var Database $conn - */ - $conn = $status->value; + $conn = $status->getDB(); $engines = []; $res = $conn->query( 'SHOW ENGINES', __METHOD__ ); @@ -297,8 +227,7 @@ class MysqlInstaller extends DatabaseInstaller { if ( !$status->isOK() ) { return false; } - /** @var Database $conn */ - $conn = $status->value; + $conn = $status->getDB(); // Get current account name $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__ ); @@ -376,83 +305,6 @@ class MysqlInstaller extends DatabaseInstaller { return "/$r/s"; } - /** - * @return string - */ - public function getSettingsForm() { - if ( $this->canCreateAccounts() ) { - $noCreateMsg = false; - } else { - $noCreateMsg = 'config-db-web-no-create-privs'; - } - $s = $this->getWebUserBox( $noCreateMsg ); - - // Do engine selector - $engines = $this->getEngines(); - // If the current default engine is not supported, use an engine that is - if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) { - $this->setVar( '_MysqlEngine', reset( $engines ) ); - } - - // If the current default charset is not supported, use a charset that is - $charsets = $this->getCharsets(); - if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) { - $this->setVar( '_MysqlCharset', reset( $charsets ) ); - } - - return $s; - } - - /** - * @return Status - */ - public function submitSettingsForm() { - $this->setVarsFromRequest( [ '_MysqlEngine', '_MysqlCharset' ] ); - $status = $this->submitWebUserBox(); - if ( !$status->isOK() ) { - return $status; - } - - // Validate the create checkbox - $canCreate = $this->canCreateAccounts(); - if ( !$canCreate ) { - $this->setVar( '_CreateDBAccount', false ); - $create = false; - } else { - $create = $this->getVar( '_CreateDBAccount' ); - } - - if ( !$create ) { - // Test the web account - try { - MediaWikiServices::getInstance()->getDatabaseFactory()->create( 'mysql', [ - 'host' => $this->getVar( 'wgDBserver' ), - 'user' => $this->getVar( 'wgDBuser' ), - 'password' => $this->getVar( 'wgDBpassword' ), - 'ssl' => $this->getVar( 'wgDBssl' ), - 'dbname' => false, - 'flags' => 0, - 'tablePrefix' => $this->getVar( 'wgDBprefix' ) - ] ); - } catch ( DBConnectionError $e ) { - return Status::newFatal( 'config-connection-error', $e->getMessage() ); - } - } - - // Validate engines and charsets - // This is done pre-submit already, so it's just for security - $engines = $this->getEngines(); - if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) { - $this->setVar( '_MysqlEngine', reset( $engines ) ); - } - $charsets = $this->getCharsets(); - if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) { - $this->setVar( '_MysqlCharset', reset( $charsets ) ); - } - - return Status::newGood(); - } - public function preInstall() { # Add our user callback to installSteps, right before the tables are created. $callback = [ @@ -470,8 +322,7 @@ class MysqlInstaller extends DatabaseInstaller { if ( !$status->isOK() ) { return $status; } - /** @var Database $conn */ - $conn = $status->value; + $conn = $status->getDB(); $dbName = $this->getVar( 'wgDBname' ); if ( !$this->databaseExists( $dbName ) ) { $conn->query( |