MediaWiki master
WebInstallerExistingWiki.php
Go to the documentation of this file.
1<?php
2
23namespace MediaWiki\Installer;
24
26
28
32 public function execute() {
33 // If there is no LocalSettings.php, continue to the installer welcome page
35 if ( !$vars ) {
36 return 'skip';
37 }
38
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']
43 ) {
44 // It's there, so the user is authorized
45 $status = $this->handleExistingUpgrade( $vars );
46 if ( $status->isOK() ) {
47 return 'skip';
48 } else {
49 $this->startForm();
50 $this->parent->showStatusBox( $status );
51 $this->endForm( 'continue' );
52
53 return 'output';
54 }
55 }
56
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 );
60 $this->startForm();
61 $this->addHTML( $this->parent->getInfoBox(
62 wfMessage( 'config-upgrade-key-missing', "<pre dir=\"ltr\">\$wgUpgradeKey = '" .
63 $this->getVar( 'wgUpgradeKey' ) . "';</pre>" )->plain()
64 ) );
65 $this->endForm( 'continue' );
66
67 return 'output';
68 }
69
70 // If there is an upgrade key, but it wasn't supplied, prompt the user to enter it
71
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' );
77 $this->showKeyForm();
78
79 return 'output';
80 }
81 // Key was OK
82 $status = $this->handleExistingUpgrade( $vars );
83 if ( $status->isOK() ) {
84 return 'continue';
85 } else {
86 $this->parent->showStatusBox( $status );
87 $this->showKeyForm();
88
89 return 'output';
90 }
91 } else {
92 $this->showKeyForm();
93
94 return 'output';
95 }
96 }
97
101 protected function showKeyForm() {
102 $this->startForm();
103 $this->addHTML(
104 $this->parent->getInfoBox( wfMessage( 'config-localsettings-upgrade' )->plain() ) .
105 '<br />' .
106 $this->parent->getTextBox( [
107 'var' => 'wgUpgradeKey',
108 'value' => '',
109 'label' => 'config-localsettings-key',
110 'attribs' => [ 'autocomplete' => 'off' ],
111 ] )
112 );
113 $this->endForm( 'continue' );
114 }
115
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 );
127 }
128 $this->setVar( $name, $vars[$name] );
129 }
130
131 return $status;
132 }
133
141 protected function handleExistingUpgrade( $vars ) {
142 // Check $wgDBtype
143 if ( !isset( $vars['wgDBtype'] ) ||
144 !in_array( $vars['wgDBtype'], Installer::getDBTypes() )
145 ) {
146 return Status::newFatal( 'config-localsettings-connection-error', '' );
147 }
148
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() ) {
155 return $status;
156 }
157
158 $this->setVar( '_InstallUser', $vars['wgDBadminuser'] ?? $vars['wgDBuser'] );
159 $this->setVar( '_InstallPassword', $vars['wgDBadminpassword'] ?? $vars['wgDBpassword'] );
160
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' );
167
168 return $status;
169 }
170
171 // All good
172 $this->setVar( '_ExistingDBSettings', true );
173
174 // Copy $wgAuthenticationTokenVersion too, if it exists
175 $this->setVar( 'wgAuthenticationTokenVersion',
176 $vars['wgAuthenticationTokenVersion'] ?? null
177 );
178
179 return $status;
180 }
181
182}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static getDBTypes()
Get a list of known DB types.
static getExistingLocalSettings()
Determine if LocalSettings.php exists.
handleExistingUpgrade( $vars)
Initiate an upgrade of the existing database.
Abstract class to define pages for the web installer.
endForm( $continue='continue', $back='back')
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
const CONN_CREATE_DATABASE
A connection for creating DBs, suitable for pre-installation.