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 $wgDatabase, $wgDBuser, $wgDBpassword;
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' );
201 ob_start( array( $this, 'outputHandler' ) );
203 do_all_updates( false, true );
204 } catch ( MWException
$e ) {
205 echo "\nAn error occured:\n";
214 * Get a list of storage engines that are available and supported
216 public function getEngines() {
217 $engines = array( 'InnoDB', 'MyISAM' );
218 $status = $this->getConnection();
219 if ( !$status->isOK() ) {
222 $conn = $status->value
;
224 $version = $conn->getServerVersion();
225 if ( version_compare( $version, "4.1.2", "<" ) ) {
226 // No SHOW ENGINES in this version
231 $res = $conn->query( 'SHOW ENGINES' );
232 foreach ( $res as $row ) {
233 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
234 $engines[] = $row->Engine
;
237 $engines = array_intersect( $this->supportedEngines
, $engines );
242 * Get a list of character sets that are available and supported
244 public function getCharsets() {
245 $status = $this->getConnection();
246 $mysql5 = array( 'binary', 'utf8' );
247 $mysql4 = array( 'mysql4' );
248 if ( !$status->isOK() ) {
251 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
258 * Return true if the install user can create accounts
260 public function canCreateAccounts() {
261 $status = $this->getConnection();
262 if ( !$status->isOK() ) {
265 $conn = $status->value
;
267 // Check version, need INFORMATION_SCHEMA and CREATE USER
268 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
272 // Get current account name
273 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
274 $parts = explode( '@', $currentName );
275 if ( count( $parts ) != 2 ) {
278 $quotedUser = $conn->addQuotes( $parts[0] ) .
279 '@' . $conn->addQuotes( $parts[1] );
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' ) {
291 if ( $row->IS_GRANTABLE
) {
292 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
296 // Check for DB-specific privs for mysql.*
297 if ( !$insertMysql ) {
298 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
300 'GRANTEE' => $quotedUser,
301 'TABLE_SCHEMA' => 'mysql',
302 'PRIVILEGE_TYPE' => 'INSERT',
309 if ( !$insertMysql ) {
313 // Check for DB-level grant options
314 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
316 'GRANTEE' => $quotedUser,
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
] );
325 if ( count( $grantOptions ) ) {
326 // 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 ) );
346 if ( count( $engines ) >= 2 ) {
347 $s .= $this->getRadioSet( array(
348 'var' => '_MysqlEngine',
349 'label' => 'config-mysql-engine',
350 'itemLabelPrefix' => 'config-mysql-',
353 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
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 ) );
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
370 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
376 public function submitSettingsForm() {
377 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
378 $status = $this->submitWebUserBox();
379 if ( !$status->isOK() ) {
383 // Validate the create checkbox
384 $canCreate = $this->canCreateAccounts();
386 $this->setVar( '_CreateDBAccount', false );
389 $create = $this->getVar( '_CreateDBAccount' );
393 // Test the web account
395 $webConn = new Database(
396 $this->getVar( 'wgDBserver' ),
397 $this->getVar( 'wgDBuser' ),
398 $this->getVar( 'wgDBpassword' ),
402 $this->getVar( 'wgDBprefix' )
404 } catch ( DBConnectionError
$e ) {
405 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
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 ) );
415 $charsets = $this->getCharsets();
416 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
417 $this->setVar( '_MysqlCharset', reset( $charsets ) );
419 return Status
::newGood();
422 public function preInstall() {
423 # Add our user callback to installSteps, right before the tables are created.
427 'callback' => array( $this, 'setupUser' ),
430 $this->parent
->addInstallStepFollowing( "tables", $callback );
433 public function setupDatabase() {
434 $status = $this->getConnection();
435 if ( !$status->isOK() ) {
438 $conn = $status->value
;
439 $dbName = $this->getVar( 'wgDBname' );
440 if( !$conn->selectDB( $dbName ) ) {
441 $conn->query( "CREATE DATABASE `$dbName`" );
442 $conn->selectDB( $dbName );
447 public function setupUser() {
450 if ( !$this->getVar( '_CreateDBAccount' ) ) {
451 return Status
::newGood();
454 $status = $this->getConnection();
455 if ( !$status->isOK() ) {
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 );
469 public function createTables() {
471 $status = $this->getConnection();
472 if ( !$status->isOK() ) {
475 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
477 if( $this->db
->tableExists( 'user' ) ) {
478 $status->warning( 'config-install-tables-exist' );
482 $error = $this->db
->sourceFile( "$IP/maintenance/tables.sql" );
483 if( $error !== true ) {
484 $status->fatal( 'config-install-tables-failed', $error );
489 public function getTableOptions() {
490 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
491 'default charset' => $this->getVar( '_MysqlCharset' ) );
494 public function getLocalSettings() {
495 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
496 $prefix = $this->getVar( 'wgDBprefix' );
497 $opts = $this->getTableOptions();
498 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
500 "# MySQL specific settings
501 \$wgDBprefix = \"{$prefix}\";
503 # MySQL table options to use during installation or update
504 \$wgDBTableOptions = \"{$tblOpts}\";
506 # Experimental charset support for MySQL 4.1/5.0.
507 \$wgDBmysql5 = {$dbmysql5};";