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 $minimumVersion = '4.0.14';
36 public $webUserPrivs = array(
41 'CREATE TEMPORARY TABLES',
44 public function getName() {
48 public function __construct( $parent ) {
49 parent
::__construct( $parent );
52 public function isCompiled() {
53 return self
::checkExtension( 'mysql' );
56 public function getGlobalDefaults() {
60 public function getConnectForm() {
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();
74 public function submitConnectForm() {
75 // Get variables from the request.
76 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
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'] );
85 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
86 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
88 if ( !$status->isOK() ) {
93 $status = $this->submitInstallUserBox();
94 if ( !$status->isOK() ) {
99 $status = $this->getConnection();
100 if ( !$status->isOK() ) {
103 $conn = $status->value
;
106 $version = $conn->getServerVersion();
107 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
108 return Status
::newFatal( 'config-mysql-old', $this->minimumVersion
, $version );
114 public function getConnection() {
115 $status = Status
::newGood();
117 $this->db
= new DatabaseMysql(
118 $this->getVar( 'wgDBserver' ),
119 $this->getVar( '_InstallUser' ),
120 $this->getVar( '_InstallPassword' ),
124 $this->getVar( 'wgDBprefix' )
126 $status->value
= $this->db
;
127 } catch ( DBConnectionError
$e ) {
128 $status->fatal( 'config-connection-error', $e->getMessage() );
133 public function preUpgrade() {
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->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
145 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
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 = 'mysql5';
156 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
157 $existingSchema = 'mysql5-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->parent
->showMessage( 'config-mysql-charset-mismatch', $this->getVar( '_MysqlCharset' ), $existingSchema );
175 $this->setVar( '_MysqlCharset', $existingSchema );
177 if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
178 $this->parent
->showMessage( 'config-mysql-egine-mismatch', $this->getVar( '_MysqlEngine' ), $existingEngine );
179 $this->setVar( '_MysqlEngine', $existingEngine );
184 * @todo FIXME: this code is just pure crap for compatibility between
187 public function doUpgrade() {
188 global $wgDBuser, $wgDBpassword;
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' );
199 ob_start( array( $this, 'outputHandler' ) );
201 $updater = DatabaseUpdater
::newForDb( $this->db
);
202 $updater->doUpdates();
203 } catch ( MWException
$e ) {
204 echo "\nAn error occured:\n";
213 * Get a list of storage engines that are available and supported
215 public function getEngines() {
216 $engines = array( 'InnoDB', 'MyISAM' );
217 $status = $this->getConnection();
218 if ( !$status->isOK() ) {
221 $conn = $status->value
;
223 $version = $conn->getServerVersion();
224 if ( version_compare( $version, "4.1.2", "<" ) ) {
225 // No SHOW ENGINES in this version
230 $res = $conn->query( 'SHOW ENGINES' );
231 foreach ( $res as $row ) {
232 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
233 $engines[] = $row->Engine
;
236 $engines = array_intersect( $this->supportedEngines
, $engines );
241 * Get a list of character sets that are available and supported
243 public function getCharsets() {
244 $status = $this->getConnection();
245 $mysql5 = array( 'binary', 'utf8' );
246 $mysql4 = array( 'mysql4' );
247 if ( !$status->isOK() ) {
250 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
257 * Return true if the install user can create accounts
259 public function canCreateAccounts() {
260 $status = $this->getConnection();
261 if ( !$status->isOK() ) {
264 $conn = $status->value
;
266 // Check version, need INFORMATION_SCHEMA and CREATE USER
267 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
271 // Get current account name
272 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
273 $parts = explode( '@', $currentName );
274 if ( count( $parts ) != 2 ) {
277 $quotedUser = $conn->addQuotes( $parts[0] ) .
278 '@' . $conn->addQuotes( $parts[1] );
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' ) {
290 if ( $row->IS_GRANTABLE
) {
291 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
295 // Check for DB-specific privs for mysql.*
296 if ( !$insertMysql ) {
297 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
299 'GRANTEE' => $quotedUser,
300 'TABLE_SCHEMA' => 'mysql',
301 'PRIVILEGE_TYPE' => 'INSERT',
308 if ( !$insertMysql ) {
312 // Check for DB-level grant options
313 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
315 'GRANTEE' => $quotedUser,
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
] );
324 if ( count( $grantOptions ) ) {
325 // Can't grant everything
331 public function getSettingsForm() {
332 if ( $this->canCreateAccounts() ) {
333 $noCreateMsg = false;
335 $noCreateMsg = 'config-db-web-no-create-privs';
337 $s = $this->getWebUserBox( $noCreateMsg );
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 ) );
345 if ( count( $engines ) >= 2 ) {
346 $s .= $this->getRadioSet( array(
347 'var' => '_MysqlEngine',
348 'label' => 'config-mysql-engine',
349 'itemLabelPrefix' => 'config-mysql-',
352 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
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 ) );
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
369 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
375 public function submitSettingsForm() {
376 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
377 $status = $this->submitWebUserBox();
378 if ( !$status->isOK() ) {
382 // Validate the create checkbox
383 $canCreate = $this->canCreateAccounts();
385 $this->setVar( '_CreateDBAccount', false );
388 $create = $this->getVar( '_CreateDBAccount' );
392 // Test the web account
394 $webConn = new Database(
395 $this->getVar( 'wgDBserver' ),
396 $this->getVar( 'wgDBuser' ),
397 $this->getVar( 'wgDBpassword' ),
401 $this->getVar( 'wgDBprefix' )
403 } catch ( DBConnectionError
$e ) {
404 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
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 ) );
414 $charsets = $this->getCharsets();
415 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
416 $this->setVar( '_MysqlCharset', reset( $charsets ) );
418 return Status
::newGood();
421 public function preInstall() {
422 # Add our user callback to installSteps, right before the tables are created.
426 'callback' => array( $this, 'setupUser' ),
429 $this->parent
->addInstallStepFollowing( "tables", $callback );
432 public function setupDatabase() {
433 $status = $this->getConnection();
434 if ( !$status->isOK() ) {
437 $conn = $status->value
;
438 $dbName = $this->getVar( 'wgDBname' );
439 if( !$conn->selectDB( $dbName ) ) {
440 $conn->query( "CREATE DATABASE `$dbName`" );
441 $conn->selectDB( $dbName );
446 public function setupUser() {
449 if ( !$this->getVar( '_CreateDBAccount' ) ) {
450 return Status
::newGood();
453 $status = $this->getConnection();
454 if ( !$status->isOK() ) {
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 );
468 public function getTableOptions() {
469 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
470 'default charset' => $this->getVar( '_MysqlCharset' ) );
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'];
479 "# MySQL specific settings
480 \$wgDBprefix = \"{$prefix}\";
482 # MySQL table options to use during installation or update
483 \$wgDBTableOptions = \"{$tblOpts}\";
485 # Experimental charset support for MySQL 4.1/5.0.
486 \$wgDBmysql5 = {$dbmysql5};";