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.
50 'callback' => array( &$this, 'setupUser' ),
53 $this->parent
->addInstallStepFollowing( "tables", $callback );
56 static function isCompiled() {
57 return self
::checkExtension( 'mysql' );
60 function getGlobalDefaults() {
64 function getConnectForm() {
66 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
67 $this->parent
->getHelpBox( 'config-db-host-help' ) .
68 Xml
::openElement( 'fieldset' ) .
69 Xml
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
70 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
71 $this->parent
->getHelpBox( 'config-db-name-help' ) .
72 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
73 $this->parent
->getHelpBox( 'config-db-prefix-help' ) .
74 Xml
::closeElement( 'fieldset' ) .
75 $this->getInstallUserBox();
78 function submitConnectForm() {
79 // Get variables from the request
80 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
83 $status = Status
::newGood();
84 if ( !strlen( $newValues['wgDBname'] ) ) {
85 $status->fatal( 'config-missing-db-name' );
86 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
87 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
89 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
90 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
92 if ( !$status->isOK() ) {
97 $status = $this->submitInstallUserBox();
98 if ( !$status->isOK() ) {
103 $status = $this->getConnection();
104 if ( !$status->isOK() ) {
107 $conn = $status->value
;
110 $version = $conn->getServerVersion();
111 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
112 return Status
::newFatal( 'config-mysql-old', $this->minimumVersion
, $version );
118 function getConnection() {
119 $status = Status
::newGood();
121 $this->db
= new DatabaseMysql(
122 $this->getVar( 'wgDBserver' ),
123 $this->getVar( '_InstallUser' ),
124 $this->getVar( '_InstallPassword' ),
128 $this->getVar( 'wgDBprefix' )
130 $status->value
= $this->db
;
132 } catch ( DBConnectionError
$e ) {
133 $status->fatal( 'config-connection-error', $e->getMessage() );
138 function doUpgrade() {
139 $status = $this->getConnection();
140 if ( !$status->isOK() ) {
141 $this->parent
->showStatusError( $status );
144 $conn = $status->value
;
146 # Determine existing default character set
147 if ( $conn->tableExists( "revision" ) ) {
148 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
149 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
150 $row = $conn->fetchObject( $res );
152 $this->parent
->showMessage( 'config-show-table-status' );
153 $existingSchema = false;
154 $existingEngine = false;
156 if ( preg_match( '/^latin1/', $row->Collation
) ) {
157 $existingSchema = 'mysql4';
158 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
159 $existingSchema = 'mysql5';
160 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
161 $existingSchema = 'mysql5-binary';
163 $existingSchema = false;
164 $this->parent
->showMessage( 'config-unknown-collation' );
166 if ( isset( $row->Engine
) ) {
167 $existingEngine = $row->Engine
;
169 $existingEngine = $row->Type
;
178 * Get a list of storage engines that are available and supported
180 function getEngines() {
181 $engines = array( 'InnoDB', 'MyISAM' );
182 $status = $this->getConnection();
183 if ( !$status->isOK() ) {
186 $conn = $status->value
;
188 $version = $conn->getServerVersion();
189 if ( version_compare( $version, "4.1.2", "<" ) ) {
190 // No SHOW ENGINES in this version
195 $res = $conn->query( 'SHOW ENGINES' );
196 foreach ( $res as $row ) {
197 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
198 $engines[] = $row->Engine
;
201 $engines = array_intersect( $this->supportedEngines
, $engines );
206 * Get a list of character sets that are available and supported
208 function getCharsets() {
209 $status = $this->getConnection();
210 $mysql5 = array( 'binary', 'utf8' );
211 $mysql4 = array( 'mysql4' );
212 if ( !$status->isOK() ) {
215 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
222 * Return true if the install user can create accounts
224 function canCreateAccounts() {
225 $status = $this->getConnection();
226 if ( !$status->isOK() ) {
229 $conn = $status->value
;
231 // Check version, need INFORMATION_SCHEMA and CREATE USER
232 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
236 // Get current account name
237 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
238 $parts = explode( '@', $currentName );
239 if ( count( $parts ) != 2 ) {
242 $quotedUser = $conn->addQuotes( $parts[0] ) .
243 '@' . $conn->addQuotes( $parts[1] );
245 // The user needs to have INSERT on mysql.* to be able to CREATE USER
246 // The grantee will be double-quoted in this query, as required
247 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
248 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
249 $insertMysql = false;
250 $grantOptions = array_flip( $this->webUserPrivs
);
251 foreach ( $res as $row ) {
252 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
255 if ( $row->IS_GRANTABLE
) {
256 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
260 // Check for DB-specific privs for mysql.*
261 if ( !$insertMysql ) {
262 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
264 'GRANTEE' => $quotedUser,
265 'TABLE_SCHEMA' => 'mysql',
266 'PRIVILEGE_TYPE' => 'INSERT',
273 if ( !$insertMysql ) {
277 // Check for DB-level grant options
278 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
280 'GRANTEE' => $quotedUser,
283 foreach ( $res as $row ) {
284 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
285 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
286 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
289 if ( count( $grantOptions ) ) {
290 // Can't grant everything
296 function getSettingsForm() {
297 if ( $this->canCreateAccounts() ) {
298 $noCreateMsg = false;
300 $noCreateMsg = 'config-db-web-no-create-privs';
302 $s = $this->getWebUserBox( $noCreateMsg );
304 // Do engine selector
305 $engines = $this->getEngines();
306 // If the current default engine is not supported, use an engine that is
307 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
308 $this->setVar( '_MysqlEngine', reset( $engines ) );
310 if ( count( $engines ) >= 2 ) {
311 $s .= $this->getRadioSet( array(
312 'var' => '_MysqlEngine',
313 'label' => 'config-mysql-engine',
314 'itemLabelPrefix' => 'config-mysql-',
317 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
320 // If the current default charset is not supported, use a charset that is
321 $charsets = $this->getCharsets();
322 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
323 $this->setVar( '_MysqlCharset', reset( $charsets ) );
326 // Do charset selector
327 if ( count( $charsets ) >= 2 ) {
328 $s .= $this->getRadioSet( array(
329 'var' => '_MysqlCharset',
330 'label' => 'config-mysql-charset',
331 'itemLabelPrefix' => 'config-mysql-',
332 'values' => $charsets
334 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
340 function submitSettingsForm() {
341 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
342 $status = $this->submitWebUserBox();
343 if ( !$status->isOK() ) {
347 // Validate the create checkbox
348 $canCreate = $this->canCreateAccounts();
350 $this->setVar( '_CreateDBAccount', false );
353 $create = $this->getVar( '_CreateDBAccount' );
357 // Test the web account
359 $webConn = new Database(
360 $this->getVar( 'wgDBserver' ),
361 $this->getVar( 'wgDBuser' ),
362 $this->getVar( 'wgDBpassword' ),
366 $this->getVar( 'wgDBprefix' )
368 } catch ( DBConnectionError
$e ) {
369 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
373 // Validate engines and charsets
374 // This is done pre-submit already so it's just for security
375 $engines = $this->getEngines();
376 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
377 $this->setVar( '_MysqlEngine', reset( $engines ) );
379 $charsets = $this->getCharsets();
380 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
381 $this->setVar( '_MysqlCharset', reset( $charsets ) );
383 return Status
::newGood();
386 function setupDatabase() {
387 $status = $this->getConnection();
388 if ( !$status->isOK() ) {
391 $conn = $status->value
;
392 $dbName = $this->getVar( 'wgDBname' );
393 if( !$conn->selectDB( $dbName ) ) {
394 $conn->query( "CREATE DATABASE `$dbName`" );
395 $conn->selectDB( $dbName );
400 function setupUser() {
403 if ( !$this->getVar( '_CreateDBAccount' ) ) {
404 return Status
::newGood();
407 $status = $this->getConnection();
408 if ( !$status->isOK() ) {
412 $db = $this->getVar( 'wgDBname' );
413 $this->db
->selectDB( $db );
414 $error = $this->db
->sourceFile( "$IP/maintenance/users.sql" );
416 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
422 function createTables() {
424 $status = $this->getConnection();
425 if ( !$status->isOK() ) {
428 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
429 if ( !$this->db
->sourceFile( "$IP/maintenance/tables.sql" ) ) {
432 return Status
::newGood();
435 function getTableOptions() {
436 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
437 'default charset' => $this->getVar( '_MysqlCharset' ) );
440 function getLocalSettings() {
441 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
442 $prefix = $this->getVar( 'wgDBprefix' );
443 $opts = $this->getTableOptions();
444 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
446 "# MySQL specific settings
447 \$wgDBprefix = \"{$prefix}\";
449 # MySQL table options to use during installation or update
450 \$wgDBTableOptions = \"{$tblOpts}\";
452 # Experimental charset support for MySQL 4.1/5.0.
453 \$wgDBmysql5 = {$dbmysql5};";