Localisation updates for core and extension messages from translatewiki.net (2010...
[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 Xml::openElement( 'fieldset' ) .
65 Xml::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 Xml::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 $status = $this->getConnection();
135 if ( !$status->isOK() ) {
136 $this->parent->showStatusError( $status );
137 return;
138 }
139 $conn = $status->value;
140 $conn->selectDB( $this->getVar( 'wgDBname' ) );
141
142 # Determine existing default character set
143 if ( $conn->tableExists( "revision" ) ) {
144 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
145 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
146 $row = $conn->fetchObject( $res );
147 if ( !$row ) {
148 $this->parent->showMessage( 'config-show-table-status' );
149 $existingSchema = false;
150 $existingEngine = false;
151 } else {
152 if ( preg_match( '/^latin1/', $row->Collation ) ) {
153 $existingSchema = 'mysql4';
154 } elseif ( preg_match( '/^utf8/', $row->Collation ) ) {
155 $existingSchema = 'mysql5';
156 } elseif ( preg_match( '/^binary/', $row->Collation ) ) {
157 $existingSchema = 'mysql5-binary';
158 } else {
159 $existingSchema = false;
160 $this->parent->showMessage( 'config-unknown-collation' );
161 }
162 if ( isset( $row->Engine ) ) {
163 $existingEngine = $row->Engine;
164 } else {
165 $existingEngine = $row->Type;
166 }
167 }
168 } else {
169 $existingSchema = false;
170 $existingEngine = false;
171 }
172
173 if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) {
174 $this->parent->showMessage( 'config-mysql-charset-mismatch', $this->getVar( '_MysqlCharset' ), $existingSchema );
175 $this->setVar( '_MysqlCharset', $existingSchema );
176 }
177 if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
178 $this->parent->showMessage( 'config-mysql-egine-mismatch', $this->getVar( '_MysqlEngine' ), $existingEngine );
179 $this->setVar( '_MysqlEngine', $existingEngine );
180 }
181 }
182
183 /**
184 * @todo FIXME: this code is just pure crap for compatibility between
185 * old and new code.
186 */
187 public function doUpgrade() {
188 global $wgDatabase, $wgDBuser, $wgDBpassword;
189
190 # Some maintenance scripts like wfGetDB()
191 LBFactory::enableBackend();
192 # For do_all_updates()
193 $wgDatabase = $this->db;
194 # Normal user and password are selected after this step, so for now
195 # just copy these two
196 $wgDBuser = $this->getVar( '_InstallUser' );
197 $wgDBpassword = $this->getVar( '_InstallPassword' );
198
199 $ret = true;
200
201 ob_start( array( $this, 'outputHandler' ) );
202 try {
203 do_all_updates( false, true );
204 } catch ( MWException $e ) {
205 echo "\nAn error occured:\n";
206 echo $e->getText();
207 $ret = false;
208 }
209 ob_end_flush();
210 return $ret;
211 }
212
213 /**
214 * Get a list of storage engines that are available and supported
215 */
216 public function getEngines() {
217 $engines = array( 'InnoDB', 'MyISAM' );
218 $status = $this->getConnection();
219 if ( !$status->isOK() ) {
220 return $engines;
221 }
222 $conn = $status->value;
223
224 $version = $conn->getServerVersion();
225 if ( version_compare( $version, "4.1.2", "<" ) ) {
226 // No SHOW ENGINES in this version
227 return $engines;
228 }
229
230 $engines = array();
231 $res = $conn->query( 'SHOW ENGINES' );
232 foreach ( $res as $row ) {
233 if ( $row->Support == 'YES' || $row->Support == 'DEFAULT' ) {
234 $engines[] = $row->Engine;
235 }
236 }
237 $engines = array_intersect( $this->supportedEngines, $engines );
238 return $engines;
239 }
240
241 /**
242 * Get a list of character sets that are available and supported
243 */
244 public function getCharsets() {
245 $status = $this->getConnection();
246 $mysql5 = array( 'binary', 'utf8' );
247 $mysql4 = array( 'mysql4' );
248 if ( !$status->isOK() ) {
249 return $mysql5;
250 }
251 if ( version_compare( $status->value->getServerVersion(), '4.1.0', '>=' ) ) {
252 return $mysql5;
253 }
254 return $mysql4;
255 }
256
257 /**
258 * Return true if the install user can create accounts
259 */
260 public function canCreateAccounts() {
261 $status = $this->getConnection();
262 if ( !$status->isOK() ) {
263 return false;
264 }
265 $conn = $status->value;
266
267 // Check version, need INFORMATION_SCHEMA and CREATE USER
268 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
269 return false;
270 }
271
272 // Get current account name
273 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__ );
274 $parts = explode( '@', $currentName );
275 if ( count( $parts ) != 2 ) {
276 return false;
277 }
278 $quotedUser = $conn->addQuotes( $parts[0] ) .
279 '@' . $conn->addQuotes( $parts[1] );
280
281 // The user needs to have INSERT on mysql.* to be able to CREATE USER
282 // The grantee will be double-quoted in this query, as required
283 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
284 array( 'GRANTEE' => $quotedUser ), __METHOD__ );
285 $insertMysql = false;
286 $grantOptions = array_flip( $this->webUserPrivs );
287 foreach ( $res as $row ) {
288 if ( $row->PRIVILEGE_TYPE == 'INSERT' ) {
289 $insertMysql = true;
290 }
291 if ( $row->IS_GRANTABLE ) {
292 unset( $grantOptions[$row->PRIVILEGE_TYPE] );
293 }
294 }
295
296 // Check for DB-specific privs for mysql.*
297 if ( !$insertMysql ) {
298 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
299 array(
300 'GRANTEE' => $quotedUser,
301 'TABLE_SCHEMA' => 'mysql',
302 'PRIVILEGE_TYPE' => 'INSERT',
303 ), __METHOD__ );
304 if ( $row ) {
305 $insertMysql = true;
306 }
307 }
308
309 if ( !$insertMysql ) {
310 return false;
311 }
312
313 // Check for DB-level grant options
314 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
315 array(
316 'GRANTEE' => $quotedUser,
317 'IS_GRANTABLE' => 1,
318 ), __METHOD__ );
319 foreach ( $res as $row ) {
320 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA );
321 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
322 unset( $grantOptions[$row->PRIVILEGE_TYPE] );
323 }
324 }
325 if ( count( $grantOptions ) ) {
326 // Can't grant everything
327 return false;
328 }
329 return true;
330 }
331
332 public function getSettingsForm() {
333 if ( $this->canCreateAccounts() ) {
334 $noCreateMsg = false;
335 } else {
336 $noCreateMsg = 'config-db-web-no-create-privs';
337 }
338 $s = $this->getWebUserBox( $noCreateMsg );
339
340 // Do engine selector
341 $engines = $this->getEngines();
342 // If the current default engine is not supported, use an engine that is
343 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
344 $this->setVar( '_MysqlEngine', reset( $engines ) );
345 }
346 if ( count( $engines ) >= 2 ) {
347 $s .= $this->getRadioSet( array(
348 'var' => '_MysqlEngine',
349 'label' => 'config-mysql-engine',
350 'itemLabelPrefix' => 'config-mysql-',
351 'values' => $engines
352 ));
353 $s .= $this->parent->getHelpBox( 'config-mysql-engine-help' );
354 }
355
356 // If the current default charset is not supported, use a charset that is
357 $charsets = $this->getCharsets();
358 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
359 $this->setVar( '_MysqlCharset', reset( $charsets ) );
360 }
361
362 // Do charset selector
363 if ( count( $charsets ) >= 2 ) {
364 $s .= $this->getRadioSet( array(
365 'var' => '_MysqlCharset',
366 'label' => 'config-mysql-charset',
367 'itemLabelPrefix' => 'config-mysql-',
368 'values' => $charsets
369 ));
370 $s .= $this->parent->getHelpBox( 'config-mysql-charset-help' );
371 }
372
373 return $s;
374 }
375
376 public function submitSettingsForm() {
377 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
378 $status = $this->submitWebUserBox();
379 if ( !$status->isOK() ) {
380 return $status;
381 }
382
383 // Validate the create checkbox
384 $canCreate = $this->canCreateAccounts();
385 if ( !$canCreate ) {
386 $this->setVar( '_CreateDBAccount', false );
387 $create = false;
388 } else {
389 $create = $this->getVar( '_CreateDBAccount' );
390 }
391
392 if ( !$create ) {
393 // Test the web account
394 try {
395 $webConn = new Database(
396 $this->getVar( 'wgDBserver' ),
397 $this->getVar( 'wgDBuser' ),
398 $this->getVar( 'wgDBpassword' ),
399 false,
400 false,
401 0,
402 $this->getVar( 'wgDBprefix' )
403 );
404 } catch ( DBConnectionError $e ) {
405 return Status::newFatal( 'config-connection-error', $e->getMessage() );
406 }
407 }
408
409 // Validate engines and charsets
410 // This is done pre-submit already so it's just for security
411 $engines = $this->getEngines();
412 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
413 $this->setVar( '_MysqlEngine', reset( $engines ) );
414 }
415 $charsets = $this->getCharsets();
416 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
417 $this->setVar( '_MysqlCharset', reset( $charsets ) );
418 }
419 return Status::newGood();
420 }
421
422 public function preInstall() {
423 # Add our user callback to installSteps, right before the tables are created.
424 $callback = array(
425 array(
426 'name' => 'user',
427 'callback' => array( $this, 'setupUser' ),
428 )
429 );
430 $this->parent->addInstallStepFollowing( "tables", $callback );
431 }
432
433 public function setupDatabase() {
434 $status = $this->getConnection();
435 if ( !$status->isOK() ) {
436 return $status;
437 }
438 $conn = $status->value;
439 $dbName = $this->getVar( 'wgDBname' );
440 if( !$conn->selectDB( $dbName ) ) {
441 $conn->query( "CREATE DATABASE `$dbName`" );
442 $conn->selectDB( $dbName );
443 }
444 return $status;
445 }
446
447 public function setupUser() {
448 global $IP;
449
450 if ( !$this->getVar( '_CreateDBAccount' ) ) {
451 return Status::newGood();
452 }
453
454 $status = $this->getConnection();
455 if ( !$status->isOK() ) {
456 return $status;
457 }
458
459 $db = $this->getVar( 'wgDBname' );
460 $this->db->selectDB( $db );
461 $error = $this->db->sourceFile( "$IP/maintenance/users.sql" );
462 if ( $error !== true ) {
463 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
464 }
465
466 return $status;
467 }
468
469 public function getTableOptions() {
470 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
471 'default charset' => $this->getVar( '_MysqlCharset' ) );
472 }
473
474 public function getLocalSettings() {
475 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
476 $prefix = $this->getVar( 'wgDBprefix' );
477 $opts = $this->getTableOptions();
478 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
479 return
480 "# MySQL specific settings
481 \$wgDBprefix = \"{$prefix}\";
482
483 # MySQL table options to use during installation or update
484 \$wgDBTableOptions = \"{$tblOpts}\";
485
486 # Experimental charset support for MySQL 4.1/5.0.
487 \$wgDBmysql5 = {$dbmysql5};";
488 }
489 }