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