MediaWiki master
WebInstallerExistingWiki.php
Go to the documentation of this file.
1<?php
2
9namespace MediaWiki\Installer;
10
12
14
18 public function execute() {
19 // If there is no LocalSettings.php, continue to the installer welcome page
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
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
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
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}
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:44
const CONN_CREATE_DATABASE
A connection for creating DBs, suitable for pre-installation.