wiki.techinc.nl/includes/installer/PostgresSettingsForm.php
Tim Starling 3f852f7ddc 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
2024-03-13 13:04:13 +11:00

96 lines
2.4 KiB
PHP

<?php
namespace MediaWiki\Installer;
use MediaWiki\Status\Status;
use Wikimedia\Rdbms\DatabasePostgres;
/**
* @internal
*/
class PostgresSettingsForm extends DatabaseSettingsForm {
public function getHtml() {
if ( $this->getPostgresInstaller()->canCreateAccounts() ) {
$noCreateMsg = false;
} else {
$noCreateMsg = 'config-db-web-no-create-privs';
}
$s = $this->getWebUserBox( $noCreateMsg );
return $s;
}
public function submit() {
$status = $this->submitWebUserBox();
if ( !$status->isOK() ) {
return $status;
}
$same = $this->getVar( 'wgDBuser' ) === $this->getVar( '_InstallUser' );
if ( $same ) {
$exists = true;
} else {
// Check if the web user exists
// Connect to the database with the install user
$status = $this->getPostgresInstaller()->getPgConnection( 'create-db' );
if ( !$status->isOK() ) {
return $status;
}
$conn = $status->getDB();
'@phan-var DatabasePostgres $conn'; /** @var DatabasePostgres $conn */
$exists = $conn->roleExists( $this->getVar( 'wgDBuser' ) );
}
// Validate the create checkbox
if ( $this->getPostgresInstaller()->canCreateAccounts() && !$same && !$exists ) {
$create = $this->getVar( '_CreateDBAccount' );
} else {
$this->setVar( '_CreateDBAccount', false );
$create = false;
}
if ( !$create && !$exists ) {
if ( $this->getPostgresInstaller()->canCreateAccounts() ) {
$msg = 'config-install-user-missing-create';
} else {
$msg = 'config-install-user-missing';
}
return Status::newFatal( $msg, $this->getVar( 'wgDBuser' ) );
}
if ( !$exists ) {
// No more checks to do
return Status::newGood();
}
// Existing web account. Test the connection.
$status = $this->getPostgresInstaller()->openConnectionToAnyDB(
$this->getVar( 'wgDBuser' ),
$this->getVar( 'wgDBpassword' ) );
if ( !$status->isOK() ) {
return $status;
}
// The web user is conventionally the table owner in PostgreSQL
// installations. Make sure the install user is able to create
// objects on behalf of the web user.
if ( $same || $this->getPostgresInstaller()->canCreateObjectsForWebUser() ) {
return Status::newGood();
} else {
return Status::newFatal( 'config-pg-not-in-role' );
}
}
/**
* Downcast the DatabaseInstaller
* @return PostgresInstaller
*/
private function getPostgresInstaller(): PostgresInstaller {
// @phan-suppress-next-line PhanTypeMismatchReturnSuperType
return $this->dbInstaller;
}
}