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', array(), $this->parent
->getHelpBox( 'config-db-host-help' ) ) .
63 Html
::openElement( 'fieldset' ) .
64 Html
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
65 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent
->getHelpBox( 'config-db-name-help' ) ) .
66 $this->getTextBox( 'wgDBprefix', 'config-db-prefix', array(), $this->parent
->getHelpBox( 'config-db-prefix-help' ) ) .
67 Html
::closeElement( 'fieldset' ) .
68 $this->getInstallUserBox();
71 public function submitConnectForm() {
72 // Get variables from the request.
73 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
76 $status = Status
::newGood();
77 if ( !strlen( $newValues['wgDBserver'] ) ) {
78 $status->fatal( 'config-missing-db-host' );
80 if ( !strlen( $newValues['wgDBname'] ) ) {
81 $status->fatal( 'config-missing-db-name' );
82 } elseif ( !preg_match( '/^[a-z0-9_-]+$/i', $newValues['wgDBname'] ) ) {
83 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
85 if ( !preg_match( '/^[a-z0-9_-]*$/i', $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 openConnection() {
115 $status = Status
::newGood();
117 $db = new DatabaseMysql(
118 $this->getVar( 'wgDBserver' ),
119 $this->getVar( '_InstallUser' ),
120 $this->getVar( '_InstallPassword' ),
124 $this->getVar( 'wgDBprefix' )
126 $status->value
= $db;
127 } catch ( DBConnectionError
$e ) {
128 $status->fatal( 'config-connection-error', $e->getMessage() );
133 public function preUpgrade() {
134 global $wgDBuser, $wgDBpassword;
136 $status = $this->getConnection();
137 if ( !$status->isOK() ) {
138 $this->parent
->showStatusError( $status );
141 $conn = $status->value
;
142 $conn->selectDB( $this->getVar( 'wgDBname' ) );
144 # Determine existing default character set
145 if ( $conn->tableExists( "revision" ) ) {
146 $revision = $conn->buildLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
147 $res = $conn->query( "SHOW TABLE STATUS $revision", __METHOD__
);
148 $row = $conn->fetchObject( $res );
150 $this->parent
->showMessage( 'config-show-table-status' );
151 $existingSchema = false;
152 $existingEngine = false;
154 if ( preg_match( '/^latin1/', $row->Collation
) ) {
155 $existingSchema = 'mysql4';
156 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
157 $existingSchema = 'utf8';
158 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
159 $existingSchema = 'binary';
161 $existingSchema = false;
162 $this->parent
->showMessage( 'config-unknown-collation' );
164 if ( isset( $row->Engine
) ) {
165 $existingEngine = $row->Engine
;
167 $existingEngine = $row->Type
;
171 $existingSchema = false;
172 $existingEngine = false;
175 if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) {
176 $this->setVar( '_MysqlCharset', $existingSchema );
178 if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
179 $this->setVar( '_MysqlEngine', $existingEngine );
182 # Normal user and password are selected after this step, so for now
183 # just copy these two
184 $wgDBuser = $this->getVar( '_InstallUser' );
185 $wgDBpassword = $this->getVar( '_InstallPassword' );
189 * Get a list of storage engines that are available and supported
191 public function getEngines() {
192 $engines = array( 'InnoDB', 'MyISAM' );
193 $status = $this->getConnection();
194 if ( !$status->isOK() ) {
197 $conn = $status->value
;
199 $version = $conn->getServerVersion();
200 if ( version_compare( $version, "4.1.2", "<" ) ) {
201 // No SHOW ENGINES in this version
206 $res = $conn->query( 'SHOW ENGINES', __METHOD__
);
207 foreach ( $res as $row ) {
208 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
209 $engines[] = $row->Engine
;
212 $engines = array_intersect( $this->supportedEngines
, $engines );
217 * Get a list of character sets that are available and supported
219 public function getCharsets() {
220 $status = $this->getConnection();
221 $mysql5 = array( 'binary', 'utf8' );
222 $mysql4 = array( 'mysql4' );
223 if ( !$status->isOK() ) {
226 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
233 * Return true if the install user can create accounts
235 public function canCreateAccounts() {
236 $status = $this->getConnection();
237 if ( !$status->isOK() ) {
240 $conn = $status->value
;
242 // Check version, need INFORMATION_SCHEMA and CREATE USER
243 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
247 // Get current account name
248 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
249 $parts = explode( '@', $currentName );
250 if ( count( $parts ) != 2 ) {
253 $quotedUser = $conn->addQuotes( $parts[0] ) .
254 '@' . $conn->addQuotes( $parts[1] );
256 // The user needs to have INSERT on mysql.* to be able to CREATE USER
257 // The grantee will be double-quoted in this query, as required
258 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
259 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
260 $insertMysql = false;
261 $grantOptions = array_flip( $this->webUserPrivs
);
262 foreach ( $res as $row ) {
263 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
266 if ( $row->IS_GRANTABLE
) {
267 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
271 // Check for DB-specific privs for mysql.*
272 if ( !$insertMysql ) {
273 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
275 'GRANTEE' => $quotedUser,
276 'TABLE_SCHEMA' => 'mysql',
277 'PRIVILEGE_TYPE' => 'INSERT',
284 if ( !$insertMysql ) {
288 // Check for DB-level grant options
289 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
291 'GRANTEE' => $quotedUser,
294 foreach ( $res as $row ) {
295 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
296 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
297 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
300 if ( count( $grantOptions ) ) {
301 // Can't grant everything
307 public function getSettingsForm() {
308 if ( $this->canCreateAccounts() ) {
309 $noCreateMsg = false;
311 $noCreateMsg = 'config-db-web-no-create-privs';
313 $s = $this->getWebUserBox( $noCreateMsg );
315 // Do engine selector
316 $engines = $this->getEngines();
317 // If the current default engine is not supported, use an engine that is
318 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
319 $this->setVar( '_MysqlEngine', reset( $engines ) );
321 if ( count( $engines ) >= 2 ) {
322 $s .= $this->getRadioSet( array(
323 'var' => '_MysqlEngine',
324 'label' => 'config-mysql-engine',
325 'itemLabelPrefix' => 'config-mysql-',
328 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
331 // If the current default charset is not supported, use a charset that is
332 $charsets = $this->getCharsets();
333 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
334 $this->setVar( '_MysqlCharset', reset( $charsets ) );
337 // Do charset selector
338 if ( count( $charsets ) >= 2 ) {
339 $s .= $this->getRadioSet( array(
340 'var' => '_MysqlCharset',
341 'label' => 'config-mysql-charset',
342 'itemLabelPrefix' => 'config-mysql-',
343 'values' => $charsets
345 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
351 public function submitSettingsForm() {
352 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
353 $status = $this->submitWebUserBox();
354 if ( !$status->isOK() ) {
358 // Validate the create checkbox
359 $canCreate = $this->canCreateAccounts();
361 $this->setVar( '_CreateDBAccount', false );
364 $create = $this->getVar( '_CreateDBAccount' );
368 // Test the web account
371 $this->getVar( 'wgDBserver' ),
372 $this->getVar( 'wgDBuser' ),
373 $this->getVar( 'wgDBpassword' ),
377 $this->getVar( 'wgDBprefix' )
379 } catch ( DBConnectionError
$e ) {
380 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
384 // Validate engines and charsets
385 // This is done pre-submit already so it's just for security
386 $engines = $this->getEngines();
387 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
388 $this->setVar( '_MysqlEngine', reset( $engines ) );
390 $charsets = $this->getCharsets();
391 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
392 $this->setVar( '_MysqlCharset', reset( $charsets ) );
394 return Status
::newGood();
397 public function preInstall() {
398 # Add our user callback to installSteps, right before the tables are created.
401 'callback' => array( $this, 'setupUser' ),
403 $this->parent
->addInstallStep( $callback, 'tables' );
406 public function setupDatabase() {
407 $status = $this->getConnection();
408 if ( !$status->isOK() ) {
411 $conn = $status->value
;
412 $dbName = $this->getVar( 'wgDBname' );
413 if( !$conn->selectDB( $dbName ) ) {
414 $conn->query( "CREATE DATABASE " . $conn->addIdentifierQuotes( $dbName ), __METHOD__
);
415 $conn->selectDB( $dbName );
417 $this->setupSchemaVars();
421 public function setupUser() {
424 if ( !$this->getVar( '_CreateDBAccount' ) ) {
425 return Status
::newGood();
428 $status = $this->getConnection();
429 if ( !$status->isOK() ) {
433 $db = $this->getVar( 'wgDBname' );
434 $this->db
->selectDB( $db );
435 $error = $this->db
->sourceFile( "$IP/maintenance/users.sql" );
436 if ( $error !== true ) {
437 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
444 * Return any table options to be applied to all tables that don't
449 protected function getTableOptions() {
451 if ( $this->getVar( '_MysqlEngine' ) !== null ) {
452 $options[] = "ENGINE=" . $this->getVar( '_MysqlEngine' );
454 if ( $this->getVar( '_MysqlCharset' ) !== null ) {
455 $options[] = 'DEFAULT CHARSET=' . $this->getVar( '_MysqlCharset' );
457 return implode( ', ', $options );
461 * Get variables to substitute into tables.sql and the SQL patch files.
463 public function getSchemaVars() {
465 'wgDBTableOptions' => $this->getTableOptions(),
469 public function getLocalSettings() {
470 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
471 $prefix = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgDBprefix' ) );
472 $tblOpts = LocalSettingsGenerator
::escapePhpString( $this->getTableOptions() );
474 "# MySQL specific settings
475 \$wgDBprefix = \"{$prefix}\";
477 # MySQL table options to use during installation or update
478 \$wgDBTableOptions = \"{$tblOpts}\";
480 # Experimental charset support for MySQL 4.1/5.0.
481 \$wgDBmysql5 = {$dbmysql5};";