Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PostgresSettingsForm
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
342
0.00% covered (danger)
0.00%
0 / 1
 getHtml
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 submit
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
210
 getPostgresInstaller
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPostgresUtils
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Installer;
4
5use MediaWiki\Installer\Task\ITaskContext;
6use MediaWiki\Installer\Task\PostgresUtils;
7use MediaWiki\Status\Status;
8use Wikimedia\Rdbms\DatabasePostgres;
9
10/**
11 * @internal
12 */
13class PostgresSettingsForm extends DatabaseSettingsForm {
14
15    /** @inheritDoc */
16    public function getHtml() {
17        if ( $this->getPostgresUtils()->canCreateAccounts() ) {
18            $noCreateMsg = false;
19        } else {
20            $noCreateMsg = 'config-db-web-no-create-privs';
21        }
22        $s = $this->getWebUserBox( $noCreateMsg );
23
24        return $s;
25    }
26
27    /** @inheritDoc */
28    public function submit() {
29        $status = $this->submitWebUserBox();
30        if ( !$status->isOK() ) {
31            return $status;
32        }
33
34        $same = $this->getVar( 'wgDBuser' ) === $this->getVar( '_InstallUser' );
35
36        if ( $same ) {
37            $exists = true;
38        } else {
39            // Check if the web user exists
40            // Connect to the database with the install user
41            $status = $this->dbInstaller->getConnection( ITaskContext::CONN_CREATE_DATABASE );
42            if ( !$status->isOK() ) {
43                return $status;
44            }
45            $conn = $status->getDB();
46            '@phan-var DatabasePostgres $conn'; /** @var DatabasePostgres $conn */
47            $exists = $conn->roleExists( $this->getVar( 'wgDBuser' ) );
48        }
49
50        // Validate the create checkbox
51        if ( $this->getPostgresUtils()->canCreateAccounts() && !$same && !$exists ) {
52            $create = $this->getVar( '_CreateDBAccount' );
53        } else {
54            $this->setVar( '_CreateDBAccount', false );
55            $create = false;
56        }
57
58        if ( !$create && !$exists ) {
59            if ( $this->getPostgresUtils()->canCreateAccounts() ) {
60                $msg = 'config-install-user-missing-create';
61            } else {
62                $msg = 'config-install-user-missing';
63            }
64
65            return Status::newFatal( $msg, $this->getVar( 'wgDBuser' ) );
66        }
67
68        if ( !$exists ) {
69            // No more checks to do
70            return Status::newGood();
71        }
72
73        // Existing web account. Test the connection.
74        $status = $this->getPostgresInstaller()->openConnectionToAnyDB(
75            $this->getVar( 'wgDBuser' ),
76            $this->getVar( 'wgDBpassword' ) );
77        if ( !$status->isOK() ) {
78            return $status;
79        }
80
81        // The web user is conventionally the table owner in PostgreSQL
82        // installations. Make sure the install user is able to create
83        // objects on behalf of the web user.
84        if ( $same || $this->getPostgresUtils()->canCreateObjectsForWebUser() ) {
85            return Status::newGood();
86        } else {
87            return Status::newFatal( 'config-pg-not-in-role' );
88        }
89    }
90
91    /**
92     * Downcast the DatabaseInstaller
93     */
94    private function getPostgresInstaller(): PostgresInstaller {
95        // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
96        return $this->dbInstaller;
97    }
98
99    private function getPostgresUtils(): PostgresUtils {
100        return new PostgresUtils( $this->dbInstaller );
101    }
102
103}