fb96e3208e66772de53536b1665d506ac412073a
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',
32 public $supportedEngines = array( 'InnoDB', 'MyISAM' );
34 public $webUserPrivs = array(
39 'CREATE TEMPORARY TABLES',
42 public function getName() {
46 public function __construct( $parent ) {
47 parent
::__construct( $parent );
50 public function isCompiled() {
51 return self
::checkExtension( 'mysql' );
54 public function getGlobalDefaults() {
58 public function getConnectForm() {
60 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent
->getHelpBox( 'config-db-host-help' ) ) .
61 Html
::openElement( 'fieldset' ) .
62 Html
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
63 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent
->getHelpBox( 'config-db-name-help' ) ) .
64 $this->getTextBox( 'wgDBprefix', 'config-db-prefix', array(), $this->parent
->getHelpBox( 'config-db-prefix-help' ) ) .
65 Html
::closeElement( 'fieldset' ) .
66 $this->getInstallUserBox();
69 public function submitConnectForm() {
70 // Get variables from the request.
71 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
74 $status = Status
::newGood();
75 if ( !strlen( $newValues['wgDBserver'] ) ) {
76 $status->fatal( 'config-missing-db-host' );
78 if ( !strlen( $newValues['wgDBname'] ) ) {
79 $status->fatal( 'config-missing-db-name' );
80 } elseif ( !preg_match( '/^[a-z0-9_-]+$/i', $newValues['wgDBname'] ) ) {
81 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
83 if ( !preg_match( '/^[a-z0-9_-]*$/i', $newValues['wgDBprefix'] ) ) {
84 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
86 if ( !$status->isOK() ) {
91 $status = $this->submitInstallUserBox();
92 if ( !$status->isOK() ) {
97 $status = $this->getConnection();
98 if ( !$status->isOK() ) {
101 $conn = $status->value
;
104 $version = $conn->getServerVersion();
105 if ( version_compare( $version, MW_MIN_MYSQL_VERSION
) < 0 ) {
106 return Status
::newFatal( 'config-mysql-old', MW_MIN_MYSQL_VERSION
, $version );
112 public function openConnection() {
113 $status = Status
::newGood();
115 $db = new DatabaseMysql(
116 $this->getVar( 'wgDBserver' ),
117 $this->getVar( '_InstallUser' ),
118 $this->getVar( '_InstallPassword' ),
122 $this->getVar( 'wgDBprefix' )
124 $status->value
= $db;
125 } catch ( DBConnectionError
$e ) {
126 $status->fatal( 'config-connection-error', $e->getMessage() );
131 public function preUpgrade() {
132 global $wgDBuser, $wgDBpassword;
134 $status = $this->getConnection();
135 if ( !$status->isOK() ) {
136 $this->parent
->showStatusError( $status );
139 $conn = $status->value
;
140 $conn->selectDB( $this->getVar( 'wgDBname' ) );
142 # Determine existing default character set
143 if ( $conn->tableExists( "revision" ) ) {
144 $revision = $conn->buildLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
145 $res = $conn->query( "SHOW TABLE STATUS $revision", __METHOD__
);
146 $row = $conn->fetchObject( $res );
148 $this->parent
->showMessage( 'config-show-table-status' );
149 $existingSchema = false;
150 $existingEngine = false;
152 if ( preg_match( '/^latin1/', $row->Collation
) ) {
153 $existingSchema = 'mysql4';
154 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
155 $existingSchema = 'utf8';
156 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
157 $existingSchema = 'binary';
159 $existingSchema = false;
160 $this->parent
->showMessage( 'config-unknown-collation' );
162 if ( isset( $row->Engine
) ) {
163 $existingEngine = $row->Engine
;
165 $existingEngine = $row->Type
;
169 $existingSchema = false;
170 $existingEngine = false;
173 if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) {
174 $this->setVar( '_MysqlCharset', $existingSchema );
176 if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
177 $this->setVar( '_MysqlEngine', $existingEngine );
180 # Normal user and password are selected after this step, so for now
181 # just copy these two
182 $wgDBuser = $this->getVar( '_InstallUser' );
183 $wgDBpassword = $this->getVar( '_InstallPassword' );
187 * Get a list of storage engines that are available and supported
189 public function getEngines() {
190 $engines = array( 'InnoDB', 'MyISAM' );
191 $status = $this->getConnection();
192 if ( !$status->isOK() ) {
195 $conn = $status->value
;
197 $version = $conn->getServerVersion();
198 if ( version_compare( $version, "4.1.2", "<" ) ) {
199 // No SHOW ENGINES in this version
204 $res = $conn->query( 'SHOW ENGINES', __METHOD__
);
205 foreach ( $res as $row ) {
206 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
207 $engines[] = $row->Engine
;
210 $engines = array_intersect( $this->supportedEngines
, $engines );
215 * Get a list of character sets that are available and supported
217 public function getCharsets() {
218 $status = $this->getConnection();
219 $mysql5 = array( 'binary', 'utf8' );
220 $mysql4 = array( 'mysql4' );
221 if ( !$status->isOK() ) {
224 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
231 * Return true if the install user can create accounts
233 public function canCreateAccounts() {
234 $status = $this->getConnection();
235 if ( !$status->isOK() ) {
238 $conn = $status->value
;
240 // Check version, need INFORMATION_SCHEMA and CREATE USER
241 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
245 // Get current account name
246 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
247 $parts = explode( '@', $currentName );
248 if ( count( $parts ) != 2 ) {
251 $quotedUser = $conn->addQuotes( $parts[0] ) .
252 '@' . $conn->addQuotes( $parts[1] );
254 // The user needs to have INSERT on mysql.* to be able to CREATE USER
255 // The grantee will be double-quoted in this query, as required
256 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
257 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
258 $insertMysql = false;
259 $grantOptions = array_flip( $this->webUserPrivs
);
260 foreach ( $res as $row ) {
261 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
264 if ( $row->IS_GRANTABLE
) {
265 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
269 // Check for DB-specific privs for mysql.*
270 if ( !$insertMysql ) {
271 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
273 'GRANTEE' => $quotedUser,
274 'TABLE_SCHEMA' => 'mysql',
275 'PRIVILEGE_TYPE' => 'INSERT',
282 if ( !$insertMysql ) {
286 // Check for DB-level grant options
287 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
289 'GRANTEE' => $quotedUser,
292 foreach ( $res as $row ) {
293 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
294 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
295 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
298 if ( count( $grantOptions ) ) {
299 // Can't grant everything
305 public function getSettingsForm() {
306 if ( $this->canCreateAccounts() ) {
307 $noCreateMsg = false;
309 $noCreateMsg = 'config-db-web-no-create-privs';
311 $s = $this->getWebUserBox( $noCreateMsg );
313 // Do engine selector
314 $engines = $this->getEngines();
315 // If the current default engine is not supported, use an engine that is
316 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
317 $this->setVar( '_MysqlEngine', reset( $engines ) );
319 if ( count( $engines ) >= 2 ) {
320 $s .= $this->getRadioSet( array(
321 'var' => '_MysqlEngine',
322 'label' => 'config-mysql-engine',
323 'itemLabelPrefix' => 'config-mysql-',
326 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
329 // If the current default charset is not supported, use a charset that is
330 $charsets = $this->getCharsets();
331 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
332 $this->setVar( '_MysqlCharset', reset( $charsets ) );
335 // Do charset selector
336 if ( count( $charsets ) >= 2 ) {
337 $s .= $this->getRadioSet( array(
338 'var' => '_MysqlCharset',
339 'label' => 'config-mysql-charset',
340 'itemLabelPrefix' => 'config-mysql-',
341 'values' => $charsets
343 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
349 public function submitSettingsForm() {
350 $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
351 $status = $this->submitWebUserBox();
352 if ( !$status->isOK() ) {
356 // Validate the create checkbox
357 $canCreate = $this->canCreateAccounts();
359 $this->setVar( '_CreateDBAccount', false );
362 $create = $this->getVar( '_CreateDBAccount' );
366 // Test the web account
369 $this->getVar( 'wgDBserver' ),
370 $this->getVar( 'wgDBuser' ),
371 $this->getVar( 'wgDBpassword' ),
375 $this->getVar( 'wgDBprefix' )
377 } catch ( DBConnectionError
$e ) {
378 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
382 // Validate engines and charsets
383 // This is done pre-submit already so it's just for security
384 $engines = $this->getEngines();
385 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
386 $this->setVar( '_MysqlEngine', reset( $engines ) );
388 $charsets = $this->getCharsets();
389 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
390 $this->setVar( '_MysqlCharset', reset( $charsets ) );
392 return Status
::newGood();
395 public function preInstall() {
396 # Add our user callback to installSteps, right before the tables are created.
399 'callback' => array( $this, 'setupUser' ),
401 $this->parent
->addInstallStep( $callback, 'tables' );
404 public function setupDatabase() {
405 $status = $this->getConnection();
406 if ( !$status->isOK() ) {
409 $conn = $status->value
;
410 $dbName = $this->getVar( 'wgDBname' );
411 if( !$conn->selectDB( $dbName ) ) {
412 $conn->query( "CREATE DATABASE " . $conn->addIdentifierQuotes( $dbName ), __METHOD__
);
413 $conn->selectDB( $dbName );
415 $this->setupSchemaVars();
419 public function setupUser() {
420 $dbUser = $this->getVar( 'wgDBuser' );
421 if( $dbUser == $this->getVar( '_InstallUser' ) ) {
422 return Status
::newGood();
424 $status = $this->getConnection();
425 if ( !$status->isOK() ) {
429 $this->setupSchemaVars();
430 $dbName = $this->getVar( 'wgDBname' );
431 $this->db
->selectDB( $dbName );
432 $server = $this->getVar( 'wgDBserver' );
433 $password = $this->getVar( 'wgDBpassword' );
434 $grantableNames = array();
436 if ( $this->getVar( '_CreateDBAccount' ) ) {
437 // Before we blindly try to create a user that already has access,
438 try { // first attempt to connect to the database
446 $this->getVar( 'wgDBprefix' )
448 $grantableNames[] = $this->buildFullUserName( $dbUser, $server );
449 $tryToCreate = false;
450 } catch ( DBConnectionError
$e ) {
454 $grantableNames[] = $this->buildFullUserName( $dbUser, $server );
455 $tryToCreate = false;
459 $createHostList = array($server,
461 'localhost.localdomain',
465 $createHostList = array_unique( $createHostList );
466 $escPass = $this->db
->addQuotes( $password );
468 foreach( $createHostList as $host ) {
469 $fullName = $this->buildFullUserName( $dbUser, $host );
470 if( !$this->userDefinitelyExists( $dbUser, $host ) ) {
473 $this->db
->query( "CREATE USER $fullName IDENTIFIED BY $escPass", __METHOD__
);
475 $grantableNames[] = $fullName;
476 } catch( DBQueryError
$dqe ) {
477 if( $this->db
->lastErrno() == 1396 /* ER_CANNOT_USER */ ) {
478 // User (probably) already exists
479 $this->db
->rollback();
480 $status->warning( 'config-install-user-alreadyexists', $dbUser );
481 $grantableNames[] = $fullName;
484 // If we couldn't create for some bizzare reason and the
485 // user probably doesn't exist, skip the grant
486 $this->db
->rollback();
487 $status->warning( 'config-install-user-create-failed', $dbUser, $dqe->getText() );
491 $status->warning( 'config-install-user-alreadyexists', $dbUser );
492 $grantableNames[] = $fullName;
498 // Try to grant to all the users we know exist or we were able to create
499 $dbAllTables = $this->db
->addIdentifierQuotes( $dbName ) . '.*';
500 foreach( $grantableNames as $name ) {
503 $this->db
->query( "GRANT ALL PRIVILEGES ON $dbAllTables TO $name", __METHOD__
);
505 } catch( DBQueryError
$dqe ) {
506 $this->db
->rollback();
507 $status->fatal( 'config-install-user-grant-failed', $dbUser, $dqe->getText() );
515 * Return a formal 'User'@'Host' username for use in queries
516 * @param $name String Username, quotes will be added
517 * @param $host String Hostname, quotes will be added
520 private function buildFullUserName( $name, $host ) {
521 return $this->db
->addQuotes( $name ) . '@' . $this->db
->addQuotes( $host );
525 * Try to see if the user account exists. Our "superuser" may not have
526 * access to mysql.user, so false means "no" or "maybe"
527 * @param $host String Hostname to check
528 * @param $user String Username to check
531 private function userDefinitelyExists( $host, $user ) {
533 $res = $this->db
->selectRow( 'mysql.user', array( 'Host', 'User' ),
534 array( 'Host' => $host, 'User' => $user ), __METHOD__
);
536 } catch( DBQueryError
$dqe ) {
543 * Return any table options to be applied to all tables that don't
548 protected function getTableOptions() {
550 if ( $this->getVar( '_MysqlEngine' ) !== null ) {
551 $options[] = "ENGINE=" . $this->getVar( '_MysqlEngine' );
553 if ( $this->getVar( '_MysqlCharset' ) !== null ) {
554 $options[] = 'DEFAULT CHARSET=' . $this->getVar( '_MysqlCharset' );
556 return implode( ', ', $options );
560 * Get variables to substitute into tables.sql and the SQL patch files.
562 public function getSchemaVars() {
564 'wgDBTableOptions' => $this->getTableOptions(),
565 'wgDBname' => $this->getVar( 'wgDBname' ),
566 'wgDBuser' => $this->getVar( 'wgDBuser' ),
567 'wgDBpassword' => $this->getVar( 'wgDBpassword' ),
571 public function getLocalSettings() {
572 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
573 $prefix = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgDBprefix' ) );
574 $tblOpts = LocalSettingsGenerator
::escapePhpString( $this->getTableOptions() );
576 "# MySQL specific settings
577 \$wgDBprefix = \"{$prefix}\";
579 # MySQL table options to use during installation or update
580 \$wgDBTableOptions = \"{$tblOpts}\";
582 # Experimental charset support for MySQL 4.1/5.0.
583 \$wgDBmysql5 = {$dbmysql5};";