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