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['wgDBname'] ) ) {
78 $status->fatal( 'config-missing-db-name' );
79 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
80 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
82 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
83 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
85 if ( !$status->isOK() ) {
90 $status = $this->submitInstallUserBox();
91 if ( !$status->isOK() ) {
96 $status = $this->getConnection();
97 if ( !$status->isOK() ) {
100 $conn = $status->value
;
103 $version = $conn->getServerVersion();
104 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
105 return Status
::newFatal( 'config-mysql-old', $this->minimumVersion
, $version );
111 public function getConnection() {
112 $status = Status
::newGood();
114 $this->db
= new DatabaseMysql(
115 $this->getVar( 'wgDBserver' ),
116 $this->getVar( '_InstallUser' ),
117 $this->getVar( '_InstallPassword' ),
121 $this->getVar( 'wgDBprefix' )
123 $status->value
= $this->db
;
124 } catch ( DBConnectionError
$e ) {
125 $status->fatal( 'config-connection-error', $e->getMessage() );
130 public function preUpgrade() {
131 global $wgDBuser, $wgDBpassword;
133 $status = $this->getConnection();
134 if ( !$status->isOK() ) {
135 $this->parent
->showStatusError( $status );
138 $conn = $status->value
;
139 $conn->selectDB( $this->getVar( 'wgDBname' ) );
141 # Determine existing default character set
142 if ( $conn->tableExists( "revision" ) ) {
143 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
144 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'", __METHOD__
);
145 $row = $conn->fetchObject( $res );
147 $this->parent
->showMessage( 'config-show-table-status' );
148 $existingSchema = false;
149 $existingEngine = false;
151 if ( preg_match( '/^latin1/', $row->Collation
) ) {
152 $existingSchema = 'mysql4';
153 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
154 $existingSchema = 'mysql5';
155 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
156 $existingSchema = 'mysql5-binary';
158 $existingSchema = false;
159 $this->parent
->showMessage( 'config-unknown-collation' );
161 if ( isset( $row->Engine
) ) {
162 $existingEngine = $row->Engine
;
164 $existingEngine = $row->Type
;
168 $existingSchema = false;
169 $existingEngine = false;
172 if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) {
173 $this->parent
->showMessage( 'config-mysql-charset-mismatch', $this->getVar( '_MysqlCharset' ), $existingSchema );
174 $this->setVar( '_MysqlCharset', $existingSchema );
176 if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
177 $this->parent
->showMessage( 'config-mysql-egine-mismatch', $this->getVar( '_MysqlEngine' ), $existingEngine );
178 $this->setVar( '_MysqlEngine', $existingEngine );
181 # Normal user and password are selected after this step, so for now
182 # just copy these two
183 $wgDBuser = $this->getVar( '_InstallUser' );
184 $wgDBpassword = $this->getVar( '_InstallPassword' );
188 * Get a list of storage engines that are available and supported
190 public function getEngines() {
191 $engines = array( 'InnoDB', 'MyISAM' );
192 $status = $this->getConnection();
193 if ( !$status->isOK() ) {
196 $conn = $status->value
;
198 $version = $conn->getServerVersion();
199 if ( version_compare( $version, "4.1.2", "<" ) ) {
200 // No SHOW ENGINES in this version
205 $res = $conn->query( 'SHOW ENGINES', __METHOD__
);
206 foreach ( $res as $row ) {
207 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
208 $engines[] = $row->Engine
;
211 $engines = array_intersect( $this->supportedEngines
, $engines );
216 * Get a list of character sets that are available and supported
218 public function getCharsets() {
219 $status = $this->getConnection();
220 $mysql5 = array( 'binary', 'utf8' );
221 $mysql4 = array( 'mysql4' );
222 if ( !$status->isOK() ) {
225 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
232 * Return true if the install user can create accounts
234 public function canCreateAccounts() {
235 $status = $this->getConnection();
236 if ( !$status->isOK() ) {
239 $conn = $status->value
;
241 // Check version, need INFORMATION_SCHEMA and CREATE USER
242 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
246 // Get current account name
247 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
248 $parts = explode( '@', $currentName );
249 if ( count( $parts ) != 2 ) {
252 $quotedUser = $conn->addQuotes( $parts[0] ) .
253 '@' . $conn->addQuotes( $parts[1] );
255 // The user needs to have INSERT on mysql.* to be able to CREATE USER
256 // The grantee will be double-quoted in this query, as required
257 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
258 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
259 $insertMysql = false;
260 $grantOptions = array_flip( $this->webUserPrivs
);
261 foreach ( $res as $row ) {
262 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
265 if ( $row->IS_GRANTABLE
) {
266 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
270 // Check for DB-specific privs for mysql.*
271 if ( !$insertMysql ) {
272 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
274 'GRANTEE' => $quotedUser,
275 'TABLE_SCHEMA' => 'mysql',
276 'PRIVILEGE_TYPE' => 'INSERT',
283 if ( !$insertMysql ) {
287 // Check for DB-level grant options
288 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
290 'GRANTEE' => $quotedUser,
293 foreach ( $res as $row ) {
294 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
295 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
296 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
299 if ( count( $grantOptions ) ) {
300 // Can't grant everything
306 public function getSettingsForm() {
307 if ( $this->canCreateAccounts() ) {
308 $noCreateMsg = false;
310 $noCreateMsg = 'config-db-web-no-create-privs';
312 $s = $this->getWebUserBox( $noCreateMsg );
314 // Do engine selector
315 $engines = $this->getEngines();
316 // If the current default engine is not supported, use an engine that is
317 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
318 $this->setVar( '_MysqlEngine', reset( $engines ) );
320 if ( count( $engines ) >= 2 ) {
321 $s .= $this->getRadioSet( array(
322 'var' => '_MysqlEngine',
323 'label' => 'config-mysql-engine',
324 'itemLabelPrefix' => 'config-mysql-',
327 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
330 // If the current default charset is not supported, use a charset that is
331 $charsets = $this->getCharsets();
332 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
333 $this->setVar( '_MysqlCharset', reset( $charsets ) );
336 // Do charset selector
337 if ( count( $charsets ) >= 2 ) {
338 $s .= $this->getRadioSet( array(
339 'var' => '_MysqlCharset',
340 'label' => 'config-mysql-charset',
341 'itemLabelPrefix' => 'config-mysql-',
342 'values' => $charsets
344 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
350 public function submitSettingsForm() {
351 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
352 $status = $this->submitWebUserBox();
353 if ( !$status->isOK() ) {
357 // Validate the create checkbox
358 $canCreate = $this->canCreateAccounts();
360 $this->setVar( '_CreateDBAccount', false );
363 $create = $this->getVar( '_CreateDBAccount' );
367 // Test the web account
370 $this->getVar( 'wgDBserver' ),
371 $this->getVar( 'wgDBuser' ),
372 $this->getVar( 'wgDBpassword' ),
376 $this->getVar( 'wgDBprefix' )
378 } catch ( DBConnectionError
$e ) {
379 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
383 // Validate engines and charsets
384 // This is done pre-submit already so it's just for security
385 $engines = $this->getEngines();
386 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
387 $this->setVar( '_MysqlEngine', reset( $engines ) );
389 $charsets = $this->getCharsets();
390 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
391 $this->setVar( '_MysqlCharset', reset( $charsets ) );
393 return Status
::newGood();
396 public function preInstall() {
397 # Add our user callback to installSteps, right before the tables are created.
400 'callback' => array( $this, 'setupUser' ),
402 $this->parent
->addInstallStepFollowing( "tables", $callback );
405 public function setupDatabase() {
406 $status = $this->getConnection();
407 if ( !$status->isOK() ) {
410 $conn = $status->value
;
411 $dbName = $this->getVar( 'wgDBname' );
412 if( !$conn->selectDB( $dbName ) ) {
413 $conn->query( "CREATE DATABASE `$dbName`", __METHOD__
);
414 $conn->selectDB( $dbName );
419 public function setupUser() {
422 if ( !$this->getVar( '_CreateDBAccount' ) ) {
423 return Status
::newGood();
426 $status = $this->getConnection();
427 if ( !$status->isOK() ) {
431 $db = $this->getVar( 'wgDBname' );
432 $this->db
->selectDB( $db );
433 $error = $this->db
->sourceFile( "$IP/maintenance/users.sql" );
434 if ( $error !== true ) {
435 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
441 public function getTableOptions() {
442 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
443 'default charset' => $this->getVar( '_MysqlCharset' ) );
446 public function getLocalSettings() {
447 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
448 $prefix = $this->getVar( 'wgDBprefix' );
449 $opts = $this->getTableOptions();
450 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
452 "# MySQL specific settings
453 \$wgDBprefix = \"{$prefix}\";
455 # MySQL table options to use during installation or update
456 \$wgDBTableOptions = \"{$tblOpts}\";
458 # Experimental charset support for MySQL 4.1/5.0.
459 \$wgDBmysql5 = {$dbmysql5};";