4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Installer
;
25 use MediaWiki\Status\Status
;
27 class WebInstallerExistingWiki
extends WebInstallerPage
{
32 public function execute() {
33 // If there is no LocalSettings.php, continue to the installer welcome page
34 $vars = Installer
::getExistingLocalSettings();
39 // Check if the upgrade key supplied to the user has appeared in LocalSettings.php
40 if ( $vars['wgUpgradeKey'] !== false
41 && $this->getVar( '_UpgradeKeySupplied' )
42 && $this->getVar( 'wgUpgradeKey' ) === $vars['wgUpgradeKey']
44 // It's there, so the user is authorized
45 $status = $this->handleExistingUpgrade( $vars );
46 if ( $status->isOK() ) {
50 $this->parent
->showStatusBox( $status );
51 $this->endForm( 'continue' );
57 // If there is no $wgUpgradeKey, tell the user to add one to LocalSettings.php
58 if ( $vars['wgUpgradeKey'] === false ) {
59 $this->setVar( '_UpgradeKeySupplied', true );
61 $this->addHTML( $this->parent
->getInfoBox(
62 wfMessage( 'config-upgrade-key-missing', "<pre dir=\"ltr\">\$wgUpgradeKey = '" .
63 $this->getVar( 'wgUpgradeKey' ) . "';</pre>" )->plain()
65 $this->endForm( 'continue' );
70 // If there is an upgrade key, but it wasn't supplied, prompt the user to enter it
72 $r = $this->parent
->request
;
73 if ( $r->wasPosted() ) {
74 $key = $r->getText( 'config_wgUpgradeKey' );
75 if ( !$key ||
$key !== $vars['wgUpgradeKey'] ) {
76 $this->parent
->showError( 'config-localsettings-badkey' );
82 $status = $this->handleExistingUpgrade( $vars );
83 if ( $status->isOK() ) {
86 $this->parent
->showStatusBox( $status );
99 * Show the "enter key" form
101 protected function showKeyForm() {
104 $this->parent
->getInfoBox( wfMessage( 'config-localsettings-upgrade' )->plain() ) .
106 $this->parent
->getTextBox( [
107 'var' => 'wgUpgradeKey',
109 'label' => 'config-localsettings-key',
110 'attribs' => [ 'autocomplete' => 'off' ],
113 $this->endForm( 'continue' );
117 * @param string[] $names
118 * @param mixed[] $vars
122 protected function importVariables( $names, $vars ) {
123 $status = Status
::newGood();
124 foreach ( $names as $name ) {
125 if ( !isset( $vars[$name] ) ) {
126 $status->fatal( 'config-localsettings-incomplete', $name );
128 $this->setVar( $name, $vars[$name] );
135 * Initiate an upgrade of the existing database
137 * @param mixed[] $vars Variables from LocalSettings.php
141 protected function handleExistingUpgrade( $vars ) {
143 if ( !isset( $vars['wgDBtype'] ) ||
144 !in_array( $vars['wgDBtype'], Installer
::getDBTypes() )
146 return Status
::newFatal( 'config-localsettings-connection-error', '' );
149 // Set the relevant variables from LocalSettings.php
150 $requiredVars = [ 'wgDBtype' ];
151 $status = $this->importVariables( $requiredVars, $vars );
152 $installer = $this->parent
->getDBInstaller();
153 $status->merge( $this->importVariables( $installer->getGlobalNames(), $vars ) );
154 if ( !$status->isOK() ) {
158 $this->setVar( '_InstallUser', $vars['wgDBadminuser'] ??
$vars['wgDBuser'] );
159 $this->setVar( '_InstallPassword', $vars['wgDBadminpassword'] ??
$vars['wgDBpassword'] );
161 // Test the database connection
162 $status = $installer->getConnection( DatabaseInstaller
::CONN_CREATE_DATABASE
);
163 if ( !$status->isOK() ) {
164 // Adjust the error message to explain things correctly
165 $status->replaceMessage( 'config-connection-error',
166 'config-localsettings-connection-error' );
172 $this->setVar( '_ExistingDBSettings', true );
174 // Copy $wgAuthenticationTokenVersion too, if it exists
175 $this->setVar( 'wgAuthenticationTokenVersion',
176 $vars['wgAuthenticationTokenVersion'] ??
null