Reverted r75832 per my comments on CR, unanswered for 19 days. Moving all help inform...
[lhc/web/wiklou.git] / includes / installer / MysqlInstaller.php
1 <?php
2 /**
3 * MySQL-specific installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Class for setting up the MediaWiki database using MySQL.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 class MysqlInstaller extends DatabaseInstaller {
16
17 protected $globalNames = array(
18 'wgDBserver',
19 'wgDBname',
20 'wgDBuser',
21 'wgDBpassword',
22 'wgDBprefix',
23 'wgDBTableOptions',
24 'wgDBmysql5',
25 );
26
27 protected $internalDefaults = array(
28 '_MysqlEngine' => 'InnoDB',
29 '_MysqlCharset' => 'binary',
30 );
31
32 public $supportedEngines = array( 'InnoDB', 'MyISAM' );
33
34 public $minimumVersion = '4.0.14';
35
36 public $webUserPrivs = array(
37 'DELETE',
38 'INSERT',
39 'SELECT',
40 'UPDATE',
41 'CREATE TEMPORARY TABLES',
42 );
43
44 public function getName() {
45 return 'mysql';
46 }
47
48 public function __construct( $parent ) {
49 parent::__construct( $parent );
50 }
51
52 public function isCompiled() {
53 return self::checkExtension( 'mysql' );
54 }
55
56 public function getGlobalDefaults() {
57 return array();
58 }
59
60 public function getConnectForm() {
61 return
62 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
63 $this->parent->getHelpBox( 'config-db-host-help' ) .
64 Html::openElement( 'fieldset' ) .
65 Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
66 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
67 $this->parent->getHelpBox( 'config-db-name-help' ) .
68 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
69 $this->parent->getHelpBox( 'config-db-prefix-help' ) .
70 Html::closeElement( 'fieldset' ) .
71 $this->getInstallUserBox();
72 }
73
74 public function submitConnectForm() {
75 // Get variables from the request.
76 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
77
78 // Validate them.
79 $status = Status::newGood();
80 if ( !strlen( $newValues['wgDBname'] ) ) {
81 $status->fatal( 'config-missing-db-name' );
82 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
83 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
84 }
85 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
86 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
87 }
88 if ( !$status->isOK() ) {
89 return $status;
90 }
91
92 // Submit user box
93 $status = $this->submitInstallUserBox();
94 if ( !$status->isOK() ) {
95 return $status;
96 }
97
98 // Try to connect
99 $status = $this->getConnection();
100 if ( !$status->isOK() ) {
101 return $status;
102 }
103 $conn = $status->value;
104
105 // Check version
106 $version = $conn->getServerVersion();
107 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
108 return Status::newFatal( 'config-mysql-old', $this->minimumVersion, $version );
109 }
110
111 return $status;
112 }
113
114 public function getConnection() {
115 $status = Status::newGood();
116 try {
117 $this->db = new DatabaseMysql(
118 $this->getVar( 'wgDBserver' ),
119 $this->getVar( '_InstallUser' ),
120 $this->getVar( '_InstallPassword' ),
121 false,
122 false,
123 0,
124 $this->getVar( 'wgDBprefix' )
125 );
126 $status->value = $this->db;
127 } catch ( DBConnectionError $e ) {
128 $status->fatal( 'config-connection-error', $e->getMessage() );
129 }
130 return $status;
131 }
132
133 public function preUpgrade() {
134 global $wgDBuser, $wgDBpassword;
135
136 $status = $this->getConnection();
137 if ( !$status->isOK() ) {
138 $this->parent->showStatusError( $status );
139 return;
140 }
141 $conn = $status->value;
142 $conn->selectDB( $this->getVar( 'wgDBname' ) );
143
144 # Determine existing default character set
145 if ( $conn->tableExists( "revision" ) ) {
146 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
147 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'", __METHOD__ );
148 $row = $conn->fetchObject( $res );
149 if ( !$row ) {
150 $this->parent->showMessage( 'config-show-table-status' );
151 $existingSchema = false;
152 $existingEngine = false;
153 } else {
154 if ( preg_match( '/^latin1/', $row->Collation ) ) {
155 $existingSchema = 'mysql4';
156 } elseif ( preg_match( '/^utf8/', $row->Collation ) ) {
157 $existingSchema = 'mysql5';
158 } elseif ( preg_match( '/^binary/', $row->Collation ) ) {
159 $existingSchema = 'mysql5-binary';
160 } else {
161 $existingSchema = false;
162 $this->parent->showMessage( 'config-unknown-collation' );
163 }
164 if ( isset( $row->Engine ) ) {
165 $existingEngine = $row->Engine;
166 } else {
167 $existingEngine = $row->Type;
168 }
169 }
170 } else {
171 $existingSchema = false;
172 $existingEngine = false;
173 }
174
175 if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) {
176 $this->parent->showMessage( 'config-mysql-charset-mismatch', $this->getVar( '_MysqlCharset' ), $existingSchema );
177 $this->setVar( '_MysqlCharset', $existingSchema );
178 }
179 if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
180 $this->parent->showMessage( 'config-mysql-egine-mismatch', $this->getVar( '_MysqlEngine' ), $existingEngine );
181 $this->setVar( '_MysqlEngine', $existingEngine );
182 }
183
184 # Normal user and password are selected after this step, so for now
185 # just copy these two
186 $wgDBuser = $this->getVar( '_InstallUser' );
187 $wgDBpassword = $this->getVar( '_InstallPassword' );
188 }
189
190 /**
191 * Get a list of storage engines that are available and supported
192 */
193 public function getEngines() {
194 $engines = array( 'InnoDB', 'MyISAM' );
195 $status = $this->getConnection();
196 if ( !$status->isOK() ) {
197 return $engines;
198 }
199 $conn = $status->value;
200
201 $version = $conn->getServerVersion();
202 if ( version_compare( $version, "4.1.2", "<" ) ) {
203 // No SHOW ENGINES in this version
204 return $engines;
205 }
206
207 $engines = array();
208 $res = $conn->query( 'SHOW ENGINES', __METHOD__ );
209 foreach ( $res as $row ) {
210 if ( $row->Support == 'YES' || $row->Support == 'DEFAULT' ) {
211 $engines[] = $row->Engine;
212 }
213 }
214 $engines = array_intersect( $this->supportedEngines, $engines );
215 return $engines;
216 }
217
218 /**
219 * Get a list of character sets that are available and supported
220 */
221 public function getCharsets() {
222 $status = $this->getConnection();
223 $mysql5 = array( 'binary', 'utf8' );
224 $mysql4 = array( 'mysql4' );
225 if ( !$status->isOK() ) {
226 return $mysql5;
227 }
228 if ( version_compare( $status->value->getServerVersion(), '4.1.0', '>=' ) ) {
229 return $mysql5;
230 }
231 return $mysql4;
232 }
233
234 /**
235 * Return true if the install user can create accounts
236 */
237 public function canCreateAccounts() {
238 $status = $this->getConnection();
239 if ( !$status->isOK() ) {
240 return false;
241 }
242 $conn = $status->value;
243
244 // Check version, need INFORMATION_SCHEMA and CREATE USER
245 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
246 return false;
247 }
248
249 // Get current account name
250 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__ );
251 $parts = explode( '@', $currentName );
252 if ( count( $parts ) != 2 ) {
253 return false;
254 }
255 $quotedUser = $conn->addQuotes( $parts[0] ) .
256 '@' . $conn->addQuotes( $parts[1] );
257
258 // The user needs to have INSERT on mysql.* to be able to CREATE USER
259 // The grantee will be double-quoted in this query, as required
260 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
261 array( 'GRANTEE' => $quotedUser ), __METHOD__ );
262 $insertMysql = false;
263 $grantOptions = array_flip( $this->webUserPrivs );
264 foreach ( $res as $row ) {
265 if ( $row->PRIVILEGE_TYPE == 'INSERT' ) {
266 $insertMysql = true;
267 }
268 if ( $row->IS_GRANTABLE ) {
269 unset( $grantOptions[$row->PRIVILEGE_TYPE] );
270 }
271 }
272
273 // Check for DB-specific privs for mysql.*
274 if ( !$insertMysql ) {
275 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
276 array(
277 'GRANTEE' => $quotedUser,
278 'TABLE_SCHEMA' => 'mysql',
279 'PRIVILEGE_TYPE' => 'INSERT',
280 ), __METHOD__ );
281 if ( $row ) {
282 $insertMysql = true;
283 }
284 }
285
286 if ( !$insertMysql ) {
287 return false;
288 }
289
290 // Check for DB-level grant options
291 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
292 array(
293 'GRANTEE' => $quotedUser,
294 'IS_GRANTABLE' => 1,
295 ), __METHOD__ );
296 foreach ( $res as $row ) {
297 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA );
298 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
299 unset( $grantOptions[$row->PRIVILEGE_TYPE] );
300 }
301 }
302 if ( count( $grantOptions ) ) {
303 // Can't grant everything
304 return false;
305 }
306 return true;
307 }
308
309 public function getSettingsForm() {
310 if ( $this->canCreateAccounts() ) {
311 $noCreateMsg = false;
312 } else {
313 $noCreateMsg = 'config-db-web-no-create-privs';
314 }
315 $s = $this->getWebUserBox( $noCreateMsg );
316
317 // Do engine selector
318 $engines = $this->getEngines();
319 // If the current default engine is not supported, use an engine that is
320 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
321 $this->setVar( '_MysqlEngine', reset( $engines ) );
322 }
323 if ( count( $engines ) >= 2 ) {
324 $s .= $this->getRadioSet( array(
325 'var' => '_MysqlEngine',
326 'label' => 'config-mysql-engine',
327 'itemLabelPrefix' => 'config-mysql-',
328 'values' => $engines
329 ));
330 $s .= $this->parent->getHelpBox( 'config-mysql-engine-help' );
331 }
332
333 // If the current default charset is not supported, use a charset that is
334 $charsets = $this->getCharsets();
335 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
336 $this->setVar( '_MysqlCharset', reset( $charsets ) );
337 }
338
339 // Do charset selector
340 if ( count( $charsets ) >= 2 ) {
341 $s .= $this->getRadioSet( array(
342 'var' => '_MysqlCharset',
343 'label' => 'config-mysql-charset',
344 'itemLabelPrefix' => 'config-mysql-',
345 'values' => $charsets
346 ));
347 $s .= $this->parent->getHelpBox( 'config-mysql-charset-help' );
348 }
349
350 return $s;
351 }
352
353 public function submitSettingsForm() {
354 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
355 $status = $this->submitWebUserBox();
356 if ( !$status->isOK() ) {
357 return $status;
358 }
359
360 // Validate the create checkbox
361 $canCreate = $this->canCreateAccounts();
362 if ( !$canCreate ) {
363 $this->setVar( '_CreateDBAccount', false );
364 $create = false;
365 } else {
366 $create = $this->getVar( '_CreateDBAccount' );
367 }
368
369 if ( !$create ) {
370 // Test the web account
371 try {
372 new Database(
373 $this->getVar( 'wgDBserver' ),
374 $this->getVar( 'wgDBuser' ),
375 $this->getVar( 'wgDBpassword' ),
376 false,
377 false,
378 0,
379 $this->getVar( 'wgDBprefix' )
380 );
381 } catch ( DBConnectionError $e ) {
382 return Status::newFatal( 'config-connection-error', $e->getMessage() );
383 }
384 }
385
386 // Validate engines and charsets
387 // This is done pre-submit already so it's just for security
388 $engines = $this->getEngines();
389 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
390 $this->setVar( '_MysqlEngine', reset( $engines ) );
391 }
392 $charsets = $this->getCharsets();
393 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
394 $this->setVar( '_MysqlCharset', reset( $charsets ) );
395 }
396 return Status::newGood();
397 }
398
399 public function preInstall() {
400 # Add our user callback to installSteps, right before the tables are created.
401 $callback = array(
402 'name' => 'user',
403 'callback' => array( $this, 'setupUser' ),
404 );
405 $this->parent->addInstallStepFollowing( "tables", $callback );
406 }
407
408 public function setupDatabase() {
409 $status = $this->getConnection();
410 if ( !$status->isOK() ) {
411 return $status;
412 }
413 $conn = $status->value;
414 $dbName = $this->getVar( 'wgDBname' );
415 if( !$conn->selectDB( $dbName ) ) {
416 $conn->query( "CREATE DATABASE `$dbName`", __METHOD__ );
417 $conn->selectDB( $dbName );
418 }
419 return $status;
420 }
421
422 public function setupUser() {
423 global $IP;
424
425 if ( !$this->getVar( '_CreateDBAccount' ) ) {
426 return Status::newGood();
427 }
428
429 $status = $this->getConnection();
430 if ( !$status->isOK() ) {
431 return $status;
432 }
433
434 $db = $this->getVar( 'wgDBname' );
435 $this->db->selectDB( $db );
436 $error = $this->db->sourceFile( "$IP/maintenance/users.sql" );
437 if ( $error !== true ) {
438 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
439 }
440
441 return $status;
442 }
443
444 public function getTableOptions() {
445 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
446 'default charset' => $this->getVar( '_MysqlCharset' ) );
447 }
448
449 public function getLocalSettings() {
450 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
451 $prefix = $this->getVar( 'wgDBprefix' );
452 $opts = $this->getTableOptions();
453 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
454 return
455 "# MySQL specific settings
456 \$wgDBprefix = \"{$prefix}\";
457
458 # MySQL table options to use during installation or update
459 \$wgDBTableOptions = \"{$tblOpts}\";
460
461 # Experimental charset support for MySQL 4.1/5.0.
462 \$wgDBmysql5 = {$dbmysql5};";
463 }
464 }