Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WebInstallerExistingWiki
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 4
420
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
132
 showKeyForm
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 importVariables
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 handleExistingUpgrade
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3/**
4 * @license GPL-2.0-or-later
5 * @file
6 * @ingroup Installer
7 */
8
9namespace MediaWiki\Installer;
10
11use MediaWiki\Status\Status;
12
13class WebInstallerExistingWiki extends WebInstallerPage {
14
15    /**
16     * @return string
17     */
18    public function execute() {
19        // If there is no LocalSettings.php, continue to the installer welcome page
20        $vars = Installer::getExistingLocalSettings();
21        if ( !$vars ) {
22            return 'skip';
23        }
24
25        // Check if the upgrade key supplied to the user has appeared in LocalSettings.php
26        if ( $vars['wgUpgradeKey'] !== false
27            && $this->getVar( '_UpgradeKeySupplied' )
28            && $this->getVar( 'wgUpgradeKey' ) === $vars['wgUpgradeKey']
29        ) {
30            // It's there, so the user is authorized
31            $status = $this->handleExistingUpgrade( $vars );
32            if ( $status->isOK() ) {
33                return 'skip';
34            } else {
35                $this->startForm();
36                $this->parent->showStatusBox( $status );
37                $this->endForm( 'continue' );
38
39                return 'output';
40            }
41        }
42
43        // If there is no $wgUpgradeKey, tell the user to add one to LocalSettings.php
44        if ( $vars['wgUpgradeKey'] === false ) {
45            $this->setVar( '_UpgradeKeySupplied', true );
46            $this->startForm();
47            $this->addHTML( $this->parent->getInfoBox(
48                wfMessage( 'config-upgrade-key-missing', "<pre dir=\"ltr\">\$wgUpgradeKey = '" .
49                    $this->getVar( 'wgUpgradeKey' ) . "';</pre>" )->plain()
50            ) );
51            $this->endForm( 'continue' );
52
53            return 'output';
54        }
55
56        // If there is an upgrade key, but it wasn't supplied, prompt the user to enter it
57
58        $r = $this->parent->request;
59        if ( $r->wasPosted() ) {
60            $key = $r->getText( 'config_wgUpgradeKey' );
61            if ( !$key || $key !== $vars['wgUpgradeKey'] ) {
62                $this->parent->showError( 'config-localsettings-badkey' );
63                $this->showKeyForm();
64
65                return 'output';
66            }
67            // Key was OK
68            $status = $this->handleExistingUpgrade( $vars );
69            if ( $status->isOK() ) {
70                return 'continue';
71            } else {
72                $this->parent->showStatusBox( $status );
73                $this->showKeyForm();
74
75                return 'output';
76            }
77        } else {
78            $this->showKeyForm();
79
80            return 'output';
81        }
82    }
83
84    /**
85     * Show the "enter key" form
86     */
87    protected function showKeyForm() {
88        $this->startForm();
89        $this->addHTML(
90            $this->parent->getInfoBox( wfMessage( 'config-localsettings-upgrade' )->plain() ) .
91            '<br />' .
92            $this->parent->getTextBox( [
93                'var' => 'wgUpgradeKey',
94                'value' => '',
95                'label' => 'config-localsettings-key',
96                'attribs' => [ 'autocomplete' => 'off' ],
97            ] )
98        );
99        $this->endForm( 'continue' );
100    }
101
102    /**
103     * @param string[] $names
104     * @param mixed[] $vars
105     *
106     * @return Status
107     */
108    protected function importVariables( $names, $vars ) {
109        $status = Status::newGood();
110        foreach ( $names as $name ) {
111            if ( !isset( $vars[$name] ) ) {
112                $status->fatal( 'config-localsettings-incomplete', $name );
113            }
114            $this->setVar( $name, $vars[$name] );
115        }
116
117        return $status;
118    }
119
120    /**
121     * Initiate an upgrade of the existing database
122     *
123     * @param mixed[] $vars Variables from LocalSettings.php
124     *
125     * @return Status
126     */
127    protected function handleExistingUpgrade( $vars ) {
128        // Check $wgDBtype
129        if ( !isset( $vars['wgDBtype'] ) ||
130            !in_array( $vars['wgDBtype'], Installer::getDBTypes() )
131        ) {
132            return Status::newFatal( 'config-localsettings-connection-error', '' );
133        }
134
135        // Set the relevant variables from LocalSettings.php
136        $requiredVars = [ 'wgDBtype' ];
137        $status = $this->importVariables( $requiredVars, $vars );
138        $installer = $this->parent->getDBInstaller();
139        $status->merge( $this->importVariables( $installer->getGlobalNames(), $vars ) );
140        if ( !$status->isOK() ) {
141            return $status;
142        }
143
144        $this->setVar( '_InstallUser', $vars['wgDBadminuser'] ?? $vars['wgDBuser'] );
145        $this->setVar( '_InstallPassword', $vars['wgDBadminpassword'] ?? $vars['wgDBpassword'] );
146
147        // Test the database connection
148        $status = $installer->getConnection( DatabaseInstaller::CONN_CREATE_DATABASE );
149        if ( !$status->isOK() ) {
150            // Adjust the error message to explain things correctly
151            $status->replaceMessage( 'config-connection-error',
152                'config-localsettings-connection-error' );
153
154            return $status;
155        }
156
157        // All good
158        $this->setVar( '_ExistingDBSettings', true );
159
160        // Copy $wgAuthenticationTokenVersion too, if it exists
161        $this->setVar( 'wgAuthenticationTokenVersion',
162            $vars['wgAuthenticationTokenVersion'] ?? null
163        );
164
165        return $status;
166    }
167
168}