3 * MySQL-specific installer.
10 * Class for setting up the MediaWiki database using MySQL.
15 class MysqlInstaller
extends DatabaseInstaller
{
17 protected $globalNames = array(
27 protected $internalDefaults = array(
28 '_MysqlEngine' => 'InnoDB',
29 '_MysqlCharset' => 'binary',
30 '_InstallUser' => 'root',
33 public $supportedEngines = array( 'InnoDB', 'MyISAM' );
35 public $minimumVersion = '4.0.14';
37 public $webUserPrivs = array(
42 'CREATE TEMPORARY TABLES',
48 public function getName() {
52 public function __construct( $parent ) {
53 parent
::__construct( $parent );
59 public function isCompiled() {
60 return self
::checkExtension( 'mysql' );
66 public function getGlobalDefaults() {
73 public function getConnectForm() {
75 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent
->getHelpBox( 'config-db-host-help' ) ) .
76 Html
::openElement( 'fieldset' ) .
77 Html
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
78 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent
->getHelpBox( 'config-db-name-help' ) ) .
79 $this->getTextBox( 'wgDBprefix', 'config-db-prefix', array(), $this->parent
->getHelpBox( 'config-db-prefix-help' ) ) .
80 Html
::closeElement( 'fieldset' ) .
81 $this->getInstallUserBox();
84 public function submitConnectForm() {
85 // Get variables from the request.
86 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
89 $status = Status
::newGood();
90 if ( !strlen( $newValues['wgDBserver'] ) ) {
91 $status->fatal( 'config-missing-db-host' );
93 if ( !strlen( $newValues['wgDBname'] ) ) {
94 $status->fatal( 'config-missing-db-name' );
95 } elseif ( !preg_match( '/^[a-z0-9_-]+$/i', $newValues['wgDBname'] ) ) {
96 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
98 if ( !preg_match( '/^[a-z0-9_-]*$/i', $newValues['wgDBprefix'] ) ) {
99 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
101 if ( !$status->isOK() ) {
106 $status = $this->submitInstallUserBox();
107 if ( !$status->isOK() ) {
112 $status = $this->getConnection();
113 if ( !$status->isOK() ) {
116 $conn = $status->value
;
119 $version = $conn->getServerVersion();
120 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
121 return Status
::newFatal( 'config-mysql-old', $this->minimumVersion
, $version );
130 public function openConnection() {
131 $status = Status
::newGood();
133 $db = new DatabaseMysql(
134 $this->getVar( 'wgDBserver' ),
135 $this->getVar( '_InstallUser' ),
136 $this->getVar( '_InstallPassword' ),
140 $this->getVar( 'wgDBprefix' )
142 $status->value
= $db;
143 } catch ( DBConnectionError
$e ) {
144 $status->fatal( 'config-connection-error', $e->getMessage() );
149 public function preUpgrade() {
150 global $wgDBuser, $wgDBpassword;
152 $status = $this->getConnection();
153 if ( !$status->isOK() ) {
154 $this->parent
->showStatusError( $status );
157 $conn = $status->value
;
158 $conn->selectDB( $this->getVar( 'wgDBname' ) );
160 # Determine existing default character set
161 if ( $conn->tableExists( "revision" ) ) {
162 $revision = $conn->buildLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
163 $res = $conn->query( "SHOW TABLE STATUS $revision", __METHOD__
);
164 $row = $conn->fetchObject( $res );
166 $this->parent
->showMessage( 'config-show-table-status' );
167 $existingSchema = false;
168 $existingEngine = false;
170 if ( preg_match( '/^latin1/', $row->Collation
) ) {
171 $existingSchema = 'mysql4';
172 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
173 $existingSchema = 'utf8';
174 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
175 $existingSchema = 'binary';
177 $existingSchema = false;
178 $this->parent
->showMessage( 'config-unknown-collation' );
180 if ( isset( $row->Engine
) ) {
181 $existingEngine = $row->Engine
;
183 $existingEngine = $row->Type
;
187 $existingSchema = false;
188 $existingEngine = false;
191 if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) {
192 $this->setVar( '_MysqlCharset', $existingSchema );
194 if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
195 $this->setVar( '_MysqlEngine', $existingEngine );
198 # Normal user and password are selected after this step, so for now
199 # just copy these two
200 $wgDBuser = $this->getVar( '_InstallUser' );
201 $wgDBpassword = $this->getVar( '_InstallPassword' );
205 * Get a list of storage engines that are available and supported
209 public function getEngines() {
210 $engines = array( 'InnoDB', 'MyISAM' );
211 $status = $this->getConnection();
212 if ( !$status->isOK() ) {
215 $conn = $status->value
;
217 $version = $conn->getServerVersion();
218 if ( version_compare( $version, "4.1.2", "<" ) ) {
219 // No SHOW ENGINES in this version
224 $res = $conn->query( 'SHOW ENGINES', __METHOD__
);
225 foreach ( $res as $row ) {
226 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
227 $engines[] = $row->Engine
;
230 $engines = array_intersect( $this->supportedEngines
, $engines );
235 * Get a list of character sets that are available and supported
239 public function getCharsets() {
240 $status = $this->getConnection();
241 $mysql5 = array( 'binary', 'utf8' );
242 $mysql4 = array( 'mysql4' );
243 if ( !$status->isOK() ) {
246 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
253 * Return true if the install user can create accounts
257 public function canCreateAccounts() {
258 $status = $this->getConnection();
259 if ( !$status->isOK() ) {
262 $conn = $status->value
;
264 // Check version, need INFORMATION_SCHEMA and CREATE USER
265 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
269 // Get current account name
270 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
271 $parts = explode( '@', $currentName );
272 if ( count( $parts ) != 2 ) {
275 $quotedUser = $conn->addQuotes( $parts[0] ) .
276 '@' . $conn->addQuotes( $parts[1] );
278 // The user needs to have INSERT on mysql.* to be able to CREATE USER
279 // The grantee will be double-quoted in this query, as required
280 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
281 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
282 $insertMysql = false;
283 $grantOptions = array_flip( $this->webUserPrivs
);
284 foreach ( $res as $row ) {
285 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
288 if ( $row->IS_GRANTABLE
) {
289 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
293 // Check for DB-specific privs for mysql.*
294 if ( !$insertMysql ) {
295 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
297 'GRANTEE' => $quotedUser,
298 'TABLE_SCHEMA' => 'mysql',
299 'PRIVILEGE_TYPE' => 'INSERT',
306 if ( !$insertMysql ) {
310 // Check for DB-level grant options
311 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
313 'GRANTEE' => $quotedUser,
316 foreach ( $res as $row ) {
317 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
318 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
319 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
322 if ( count( $grantOptions ) ) {
323 // Can't grant everything
332 public function getSettingsForm() {
333 if ( $this->canCreateAccounts() ) {
334 $noCreateMsg = false;
336 $noCreateMsg = 'config-db-web-no-create-privs';
338 $s = $this->getWebUserBox( $noCreateMsg );
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 ) );
347 $s .= Xml
::openElement( 'div', array(
348 'id' => 'dbMyisamWarning'
350 $s .= $this->parent
->getWarningBox( wfMsg( 'config-mysql-myisam-dep' ) );
351 $s .= Xml
::closeElement( 'div' );
353 if( $this->getVar( '_MysqlEngine' ) != 'MyISAM' ) {
354 $s .= Xml
::openElement( 'script', array( 'type' => 'text/javascript' ) );
355 $s .= '$(\'#dbMyisamWarning\').hide();';
356 $s .= Xml
::closeElement( 'script' );
359 if ( count( $engines ) >= 2 ) {
360 $s .= $this->getRadioSet( array(
361 'var' => '_MysqlEngine',
362 'label' => 'config-mysql-engine',
363 'itemLabelPrefix' => 'config-mysql-',
364 'values' => $engines,
365 'itemAttribs' => array(
367 'class' => 'showHideRadio',
368 'rel' => 'dbMyisamWarning'),
370 'class' => 'hideShowRadio',
371 'rel' => 'dbMyisamWarning')
373 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
376 // If the current default charset is not supported, use a charset that is
377 $charsets = $this->getCharsets();
378 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
379 $this->setVar( '_MysqlCharset', reset( $charsets ) );
382 // Do charset selector
383 if ( count( $charsets ) >= 2 ) {
384 $s .= $this->getRadioSet( array(
385 'var' => '_MysqlCharset',
386 'label' => 'config-mysql-charset',
387 'itemLabelPrefix' => 'config-mysql-',
388 'values' => $charsets
390 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
399 public function submitSettingsForm() {
400 $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
401 $status = $this->submitWebUserBox();
402 if ( !$status->isOK() ) {
406 // Validate the create checkbox
407 $canCreate = $this->canCreateAccounts();
409 $this->setVar( '_CreateDBAccount', false );
412 $create = $this->getVar( '_CreateDBAccount' );
416 // Test the web account
419 $this->getVar( 'wgDBserver' ),
420 $this->getVar( 'wgDBuser' ),
421 $this->getVar( 'wgDBpassword' ),
425 $this->getVar( 'wgDBprefix' )
427 } catch ( DBConnectionError
$e ) {
428 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
432 // Validate engines and charsets
433 // This is done pre-submit already so it's just for security
434 $engines = $this->getEngines();
435 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
436 $this->setVar( '_MysqlEngine', reset( $engines ) );
438 $charsets = $this->getCharsets();
439 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
440 $this->setVar( '_MysqlCharset', reset( $charsets ) );
442 return Status
::newGood();
445 public function preInstall() {
446 # Add our user callback to installSteps, right before the tables are created.
449 'callback' => array( $this, 'setupUser' ),
451 $this->parent
->addInstallStep( $callback, 'tables' );
457 public function setupDatabase() {
458 $status = $this->getConnection();
459 if ( !$status->isOK() ) {
462 $conn = $status->value
;
463 $dbName = $this->getVar( 'wgDBname' );
464 if( !$conn->selectDB( $dbName ) ) {
465 $conn->query( "CREATE DATABASE " . $conn->addIdentifierQuotes( $dbName ), __METHOD__
);
466 $conn->selectDB( $dbName );
468 $this->setupSchemaVars();
475 public function setupUser() {
476 $dbUser = $this->getVar( 'wgDBuser' );
477 if( $dbUser == $this->getVar( '_InstallUser' ) ) {
478 return Status
::newGood();
480 $status = $this->getConnection();
481 if ( !$status->isOK() ) {
485 $this->setupSchemaVars();
486 $dbName = $this->getVar( 'wgDBname' );
487 $this->db
->selectDB( $dbName );
488 $server = $this->getVar( 'wgDBserver' );
489 $password = $this->getVar( 'wgDBpassword' );
490 $grantableNames = array();
492 if ( $this->getVar( '_CreateDBAccount' ) ) {
493 // Before we blindly try to create a user that already has access,
494 try { // first attempt to connect to the database
502 $this->getVar( 'wgDBprefix' )
504 $grantableNames[] = $this->buildFullUserName( $dbUser, $server );
505 $tryToCreate = false;
506 } catch ( DBConnectionError
$e ) {
510 $grantableNames[] = $this->buildFullUserName( $dbUser, $server );
511 $tryToCreate = false;
515 $createHostList = array($server,
517 'localhost.localdomain',
521 $createHostList = array_unique( $createHostList );
522 $escPass = $this->db
->addQuotes( $password );
524 foreach( $createHostList as $host ) {
525 $fullName = $this->buildFullUserName( $dbUser, $host );
526 if( !$this->userDefinitelyExists( $dbUser, $host ) ) {
529 $this->db
->query( "CREATE USER $fullName IDENTIFIED BY $escPass", __METHOD__
);
531 $grantableNames[] = $fullName;
532 } catch( DBQueryError
$dqe ) {
533 if( $this->db
->lastErrno() == 1396 /* ER_CANNOT_USER */ ) {
534 // User (probably) already exists
535 $this->db
->rollback();
536 $status->warning( 'config-install-user-alreadyexists', $dbUser );
537 $grantableNames[] = $fullName;
540 // If we couldn't create for some bizzare reason and the
541 // user probably doesn't exist, skip the grant
542 $this->db
->rollback();
543 $status->warning( 'config-install-user-create-failed', $dbUser, $dqe->getText() );
547 $status->warning( 'config-install-user-alreadyexists', $dbUser );
548 $grantableNames[] = $fullName;
554 // Try to grant to all the users we know exist or we were able to create
555 $dbAllTables = $this->db
->addIdentifierQuotes( $dbName ) . '.*';
556 foreach( $grantableNames as $name ) {
559 $this->db
->query( "GRANT ALL PRIVILEGES ON $dbAllTables TO $name", __METHOD__
);
561 } catch( DBQueryError
$dqe ) {
562 $this->db
->rollback();
563 $status->fatal( 'config-install-user-grant-failed', $dbUser, $dqe->getText() );
571 * Return a formal 'User'@'Host' username for use in queries
572 * @param $name String Username, quotes will be added
573 * @param $host String Hostname, quotes will be added
576 private function buildFullUserName( $name, $host ) {
577 return $this->db
->addQuotes( $name ) . '@' . $this->db
->addQuotes( $host );
581 * Try to see if the user account exists. Our "superuser" may not have
582 * access to mysql.user, so false means "no" or "maybe"
583 * @param $host String Hostname to check
584 * @param $user String Username to check
587 private function userDefinitelyExists( $host, $user ) {
589 $res = $this->db
->selectRow( 'mysql.user', array( 'Host', 'User' ),
590 array( 'Host' => $host, 'User' => $user ), __METHOD__
);
592 } catch( DBQueryError
$dqe ) {
599 * Return any table options to be applied to all tables that don't
604 protected function getTableOptions() {
606 if ( $this->getVar( '_MysqlEngine' ) !== null ) {
607 $options[] = "ENGINE=" . $this->getVar( '_MysqlEngine' );
609 if ( $this->getVar( '_MysqlCharset' ) !== null ) {
610 $options[] = 'DEFAULT CHARSET=' . $this->getVar( '_MysqlCharset' );
612 return implode( ', ', $options );
616 * Get variables to substitute into tables.sql and the SQL patch files.
620 public function getSchemaVars() {
622 'wgDBTableOptions' => $this->getTableOptions(),
623 'wgDBname' => $this->getVar( 'wgDBname' ),
624 'wgDBuser' => $this->getVar( 'wgDBuser' ),
625 'wgDBpassword' => $this->getVar( 'wgDBpassword' ),
629 public function getLocalSettings() {
630 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
631 $prefix = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgDBprefix' ) );
632 $tblOpts = LocalSettingsGenerator
::escapePhpString( $this->getTableOptions() );
634 "# MySQL specific settings
635 \$wgDBprefix = \"{$prefix}\";
637 # MySQL table options to use during installation or update
638 \$wgDBTableOptions = \"{$tblOpts}\";
640 # Experimental charset support for MySQL 4.1/5.0.
641 \$wgDBmysql5 = {$dbmysql5};";