3 class MysqlInstaller
extends InstallerDBType
{
4 protected $globalNames = array(
14 protected $internalDefaults = array(
15 '_MysqlEngine' => 'InnoDB',
16 '_MysqlCharset' => 'binary',
19 var $supportedEngines = array( 'InnoDB', 'MyISAM' );
21 var $minimumVersion = '4.0.14';
23 var $webUserPrivs = array(
28 'CREATE TEMPORARY TABLES',
35 function __construct( $parent ) {
36 parent
::__construct( $parent );
38 if ( $this->parent
->getVar( 'wgDBtype' ) !== $this->getName() ) {
42 if ( $this->parent
->getVar( 'wgDBtype' ) !== $this->getName() ) {
46 # Add our user callback to installSteps, right before the tables are created.
48 debug_print_backtrace();
52 'callback' => array( &$this, 'setupUser' ),
55 $this->parent
->addInstallStepFollowing( "tables", $callback );
58 static function isCompiled() {
59 return self
::checkExtension( 'mysql' );
62 function getGlobalDefaults() {
66 function getConnectForm() {
68 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
69 $this->parent
->getHelpBox( 'config-db-host-help' ) .
70 Xml
::openElement( 'fieldset' ) .
71 Xml
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
72 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
73 $this->parent
->getHelpBox( 'config-db-name-help' ) .
74 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
75 $this->parent
->getHelpBox( 'config-db-prefix-help' ) .
76 Xml
::closeElement( 'fieldset' ) .
77 $this->getInstallUserBox();
80 function submitConnectForm() {
81 // Get variables from the request
82 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
85 $status = Status
::newGood();
86 if ( !strlen( $newValues['wgDBname'] ) ) {
87 $status->fatal( 'config-missing-db-name' );
88 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
89 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
91 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
92 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
94 if ( !$status->isOK() ) {
99 $status = $this->submitInstallUserBox();
100 if ( !$status->isOK() ) {
105 $status = $this->getConnection();
106 if ( !$status->isOK() ) {
109 $conn = $status->value
;
112 $version = $conn->getServerVersion();
113 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
114 return Status
::newFatal( 'config-mysql-old', $this->minimumVersion
, $version );
120 function getConnection() {
121 $status = Status
::newGood();
123 $this->db
= new DatabaseMysql(
124 $this->getVar( 'wgDBserver' ),
125 $this->getVar( '_InstallUser' ),
126 $this->getVar( '_InstallPassword' ),
130 $this->getVar( 'wgDBprefix' )
132 $status->value
= $this->db
;
134 } catch ( DBConnectionError
$e ) {
135 $status->fatal( 'config-connection-error', $e->getMessage() );
140 function doUpgrade() {
141 $status = $this->getConnection();
142 if ( !$status->isOK() ) {
143 $this->parent
->showStatusError( $status );
146 $conn = $status->value
;
148 # Determine existing default character set
149 if ( $conn->tableExists( "revision" ) ) {
150 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
151 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
152 $row = $conn->fetchObject( $res );
154 $this->parent
->showMessage( 'config-show-table-status' );
155 $existingSchema = false;
156 $existingEngine = false;
158 if ( preg_match( '/^latin1/', $row->Collation
) ) {
159 $existingSchema = 'mysql4';
160 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
161 $existingSchema = 'mysql5';
162 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
163 $existingSchema = 'mysql5-binary';
165 $existingSchema = false;
166 $this->parent
->showMessage( 'config-unknown-collation' );
168 if ( isset( $row->Engine
) ) {
169 $existingEngine = $row->Engine
;
171 $existingEngine = $row->Type
;
180 * Get a list of storage engines that are available and supported
182 function getEngines() {
183 $engines = array( 'InnoDB', 'MyISAM' );
184 $status = $this->getConnection();
185 if ( !$status->isOK() ) {
188 $conn = $status->value
;
190 $version = $conn->getServerVersion();
191 if ( version_compare( $version, "4.1.2", "<" ) ) {
192 // No SHOW ENGINES in this version
197 $res = $conn->query( 'SHOW ENGINES' );
198 foreach ( $res as $row ) {
199 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
200 $engines[] = $row->Engine
;
203 $engines = array_intersect( $this->supportedEngines
, $engines );
208 * Get a list of character sets that are available and supported
210 function getCharsets() {
211 $status = $this->getConnection();
212 $mysql5 = array( 'binary', 'utf8' );
213 $mysql4 = array( 'mysql4' );
214 if ( !$status->isOK() ) {
217 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
224 * Return true if the install user can create accounts
226 function canCreateAccounts() {
227 $status = $this->getConnection();
228 if ( !$status->isOK() ) {
231 $conn = $status->value
;
233 // Check version, need INFORMATION_SCHEMA and CREATE USER
234 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
238 // Get current account name
239 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
240 $parts = explode( '@', $currentName );
241 if ( count( $parts ) != 2 ) {
244 $quotedUser = $conn->addQuotes( $parts[0] ) .
245 '@' . $conn->addQuotes( $parts[1] );
247 // The user needs to have INSERT on mysql.* to be able to CREATE USER
248 // The grantee will be double-quoted in this query, as required
249 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
250 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
251 $insertMysql = false;
252 $grantOptions = array_flip( $this->webUserPrivs
);
253 foreach ( $res as $row ) {
254 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
257 if ( $row->IS_GRANTABLE
) {
258 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
262 // Check for DB-specific privs for mysql.*
263 if ( !$insertMysql ) {
264 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
266 'GRANTEE' => $quotedUser,
267 'TABLE_SCHEMA' => 'mysql',
268 'PRIVILEGE_TYPE' => 'INSERT',
275 if ( !$insertMysql ) {
279 // Check for DB-level grant options
280 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
282 'GRANTEE' => $quotedUser,
285 foreach ( $res as $row ) {
286 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
287 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
288 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
291 if ( count( $grantOptions ) ) {
292 // Can't grant everything
298 function getSettingsForm() {
299 if ( $this->canCreateAccounts() ) {
300 $noCreateMsg = false;
302 $noCreateMsg = 'config-db-web-no-create-privs';
304 $s = $this->getWebUserBox( $noCreateMsg );
306 // Do engine selector
307 $engines = $this->getEngines();
308 // If the current default engine is not supported, use an engine that is
309 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
310 $this->setVar( '_MysqlEngine', reset( $engines ) );
312 if ( count( $engines ) >= 2 ) {
313 $s .= $this->getRadioSet( array(
314 'var' => '_MysqlEngine',
315 'label' => 'config-mysql-engine',
316 'itemLabelPrefix' => 'config-mysql-',
319 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
322 // If the current default charset is not supported, use a charset that is
323 $charsets = $this->getCharsets();
324 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
325 $this->setVar( '_MysqlCharset', reset( $charsets ) );
328 // Do charset selector
329 if ( count( $charsets ) >= 2 ) {
330 $s .= $this->getRadioSet( array(
331 'var' => '_MysqlCharset',
332 'label' => 'config-mysql-charset',
333 'itemLabelPrefix' => 'config-mysql-',
334 'values' => $charsets
336 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
342 function submitSettingsForm() {
343 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
344 $status = $this->submitWebUserBox();
345 if ( !$status->isOK() ) {
349 // Validate the create checkbox
350 $canCreate = $this->canCreateAccounts();
352 $this->setVar( '_CreateDBAccount', false );
355 $create = $this->getVar( '_CreateDBAccount' );
359 // Test the web account
361 $webConn = new Database(
362 $this->getVar( 'wgDBserver' ),
363 $this->getVar( 'wgDBuser' ),
364 $this->getVar( 'wgDBpassword' ),
368 $this->getVar( 'wgDBprefix' )
370 } catch ( DBConnectionError
$e ) {
371 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
375 // Validate engines and charsets
376 // This is done pre-submit already so it's just for security
377 $engines = $this->getEngines();
378 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
379 $this->setVar( '_MysqlEngine', reset( $engines ) );
381 $charsets = $this->getCharsets();
382 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
383 $this->setVar( '_MysqlCharset', reset( $charsets ) );
385 return Status
::newGood();
388 function setupDatabase() {
389 $status = $this->getConnection();
390 if ( !$status->isOK() ) {
393 $conn = $status->value
;
394 $dbName = $this->getVar( 'wgDBname' );
395 if( !$conn->selectDB( $dbName ) ) {
396 $conn->query( "CREATE DATABASE `$dbName`" );
397 $conn->selectDB( $dbName );
402 function setupUser() {
405 if ( !$this->getVar( '_CreateDBAccount' ) ) {
406 return Status
::newGood();
409 $status = $this->getConnection();
410 if ( !$status->isOK() ) {
414 $db = $this->getVar( 'wgDBname' );
415 $this->db
->selectDB( $db );
416 $error = $this->db
->sourceFile( "$IP/maintenance/users.sql" );
418 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
424 function createTables() {
426 $status = $this->getConnection();
427 if ( !$status->isOK() ) {
430 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
431 if ( !$this->db
->sourceFile( "$IP/maintenance/tables.sql" ) ) {
434 return Status
::newGood();
437 function getTableOptions() {
438 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
439 'default charset' => $this->getVar( '_MysqlCharset' ) );
442 function getLocalSettings() {
443 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
444 $prefix = $this->getVar( 'wgDBprefix' );
445 $opts = $this->getTableOptions();
446 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
448 "# MySQL specific settings
449 \$wgDBprefix = \"{$prefix}\";
451 # MySQL table options to use during installation or update
452 \$wgDBTableOptions = \"{$tblOpts}\";
454 # Experimental charset support for MySQL 4.1/5.0.
455 \$wgDBmysql5 = {$dbmysql5};";