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 );
39 public function isCompiled() {
40 return self
::checkExtension( 'mysql' );
43 function getGlobalDefaults() {
47 function getConnectForm() {
49 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
50 $this->parent
->getHelpBox( 'config-db-host-help' ) .
51 Xml
::openElement( 'fieldset' ) .
52 Xml
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
53 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
54 $this->parent
->getHelpBox( 'config-db-name-help' ) .
55 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
56 $this->parent
->getHelpBox( 'config-db-prefix-help' ) .
57 Xml
::closeElement( 'fieldset' ) .
58 $this->getInstallUserBox();
61 function submitConnectForm() {
62 // Get variables from the request
63 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
66 $status = Status
::newGood();
67 if ( !strlen( $newValues['wgDBname'] ) ) {
68 $status->fatal( 'config-missing-db-name' );
69 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
70 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
72 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
73 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
75 if ( !$status->isOK() ) {
80 $status = $this->submitInstallUserBox();
81 if ( !$status->isOK() ) {
86 $status = $this->getConnection();
87 if ( !$status->isOK() ) {
90 $conn = $status->value
;
93 $version = $conn->getServerVersion();
94 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
95 return Status
::newFatal( 'config-mysql-old', $this->minimumVersion
, $version );
101 function getConnection() {
102 $status = Status
::newGood();
104 $this->db
= new DatabaseMysql(
105 $this->getVar( 'wgDBserver' ),
106 $this->getVar( '_InstallUser' ),
107 $this->getVar( '_InstallPassword' ),
111 $this->getVar( 'wgDBprefix' )
113 $status->value
= $this->db
;
115 } catch ( DBConnectionError
$e ) {
116 $status->fatal( 'config-connection-error', $e->getMessage() );
121 function doUpgrade() {
122 $status = $this->getConnection();
123 if ( !$status->isOK() ) {
124 $this->parent
->showStatusError( $status );
127 $conn = $status->value
;
129 # Determine existing default character set
130 if ( $conn->tableExists( "revision" ) ) {
131 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
132 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
133 $row = $conn->fetchObject( $res );
135 $this->parent
->showMessage( 'config-show-table-status' );
136 $existingSchema = false;
137 $existingEngine = false;
139 if ( preg_match( '/^latin1/', $row->Collation
) ) {
140 $existingSchema = 'mysql4';
141 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
142 $existingSchema = 'mysql5';
143 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
144 $existingSchema = 'mysql5-binary';
146 $existingSchema = false;
147 $this->parent
->showMessage( 'config-unknown-collation' );
149 if ( isset( $row->Engine
) ) {
150 $existingEngine = $row->Engine
;
152 $existingEngine = $row->Type
;
161 * Get a list of storage engines that are available and supported
163 function getEngines() {
164 $engines = array( 'InnoDB', 'MyISAM' );
165 $status = $this->getConnection();
166 if ( !$status->isOK() ) {
169 $conn = $status->value
;
171 $version = $conn->getServerVersion();
172 if ( version_compare( $version, "4.1.2", "<" ) ) {
173 // No SHOW ENGINES in this version
178 $res = $conn->query( 'SHOW ENGINES' );
179 foreach ( $res as $row ) {
180 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
181 $engines[] = $row->Engine
;
184 $engines = array_intersect( $this->supportedEngines
, $engines );
189 * Get a list of character sets that are available and supported
191 function getCharsets() {
192 $status = $this->getConnection();
193 $mysql5 = array( 'binary', 'utf8' );
194 $mysql4 = array( 'mysql4' );
195 if ( !$status->isOK() ) {
198 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
205 * Return true if the install user can create accounts
207 function canCreateAccounts() {
208 $status = $this->getConnection();
209 if ( !$status->isOK() ) {
212 $conn = $status->value
;
214 // Check version, need INFORMATION_SCHEMA and CREATE USER
215 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
219 // Get current account name
220 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
221 $parts = explode( '@', $currentName );
222 if ( count( $parts ) != 2 ) {
225 $quotedUser = $conn->addQuotes( $parts[0] ) .
226 '@' . $conn->addQuotes( $parts[1] );
228 // The user needs to have INSERT on mysql.* to be able to CREATE USER
229 // The grantee will be double-quoted in this query, as required
230 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
231 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
232 $insertMysql = false;
233 $grantOptions = array_flip( $this->webUserPrivs
);
234 foreach ( $res as $row ) {
235 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
238 if ( $row->IS_GRANTABLE
) {
239 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
243 // Check for DB-specific privs for mysql.*
244 if ( !$insertMysql ) {
245 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
247 'GRANTEE' => $quotedUser,
248 'TABLE_SCHEMA' => 'mysql',
249 'PRIVILEGE_TYPE' => 'INSERT',
256 if ( !$insertMysql ) {
260 // Check for DB-level grant options
261 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
263 'GRANTEE' => $quotedUser,
266 foreach ( $res as $row ) {
267 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
268 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
269 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
272 if ( count( $grantOptions ) ) {
273 // Can't grant everything
279 function getSettingsForm() {
280 if ( $this->canCreateAccounts() ) {
281 $noCreateMsg = false;
283 $noCreateMsg = 'config-db-web-no-create-privs';
285 $s = $this->getWebUserBox( $noCreateMsg );
287 // Do engine selector
288 $engines = $this->getEngines();
289 // If the current default engine is not supported, use an engine that is
290 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
291 $this->setVar( '_MysqlEngine', reset( $engines ) );
293 if ( count( $engines ) >= 2 ) {
294 $s .= $this->getRadioSet( array(
295 'var' => '_MysqlEngine',
296 'label' => 'config-mysql-engine',
297 'itemLabelPrefix' => 'config-mysql-',
300 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
303 // If the current default charset is not supported, use a charset that is
304 $charsets = $this->getCharsets();
305 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
306 $this->setVar( '_MysqlCharset', reset( $charsets ) );
309 // Do charset selector
310 if ( count( $charsets ) >= 2 ) {
311 $s .= $this->getRadioSet( array(
312 'var' => '_MysqlCharset',
313 'label' => 'config-mysql-charset',
314 'itemLabelPrefix' => 'config-mysql-',
315 'values' => $charsets
317 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
323 function submitSettingsForm() {
324 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
325 $status = $this->submitWebUserBox();
326 if ( !$status->isOK() ) {
330 // Validate the create checkbox
331 $canCreate = $this->canCreateAccounts();
333 $this->setVar( '_CreateDBAccount', false );
336 $create = $this->getVar( '_CreateDBAccount' );
340 // Test the web account
342 $webConn = new Database(
343 $this->getVar( 'wgDBserver' ),
344 $this->getVar( 'wgDBuser' ),
345 $this->getVar( 'wgDBpassword' ),
349 $this->getVar( 'wgDBprefix' )
351 } catch ( DBConnectionError
$e ) {
352 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
356 // Validate engines and charsets
357 // This is done pre-submit already so it's just for security
358 $engines = $this->getEngines();
359 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
360 $this->setVar( '_MysqlEngine', reset( $engines ) );
362 $charsets = $this->getCharsets();
363 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
364 $this->setVar( '_MysqlCharset', reset( $charsets ) );
366 return Status
::newGood();
369 public function preInstall() {
370 # Add our user callback to installSteps, right before the tables are created.
374 'callback' => array( $this, 'setupUser' ),
377 $this->parent
->addInstallStepFollowing( "tables", $callback );
380 function setupDatabase() {
381 $status = $this->getConnection();
382 if ( !$status->isOK() ) {
385 $conn = $status->value
;
386 $dbName = $this->getVar( 'wgDBname' );
387 if( !$conn->selectDB( $dbName ) ) {
388 $conn->query( "CREATE DATABASE `$dbName`" );
389 $conn->selectDB( $dbName );
394 function setupUser() {
397 if ( !$this->getVar( '_CreateDBAccount' ) ) {
398 return Status
::newGood();
401 $status = $this->getConnection();
402 if ( !$status->isOK() ) {
406 $db = $this->getVar( 'wgDBname' );
407 $this->db
->selectDB( $db );
408 $error = $this->db
->sourceFile( "$IP/maintenance/users.sql" );
410 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
416 function createTables() {
418 $status = $this->getConnection();
419 if ( !$status->isOK() ) {
422 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
423 if ( !$this->db
->sourceFile( "$IP/maintenance/tables.sql" ) ) {
426 return Status
::newGood();
429 function getTableOptions() {
430 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
431 'default charset' => $this->getVar( '_MysqlCharset' ) );
434 function getLocalSettings() {
435 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
436 $prefix = $this->getVar( 'wgDBprefix' );
437 $opts = $this->getTableOptions();
438 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
440 "# MySQL specific settings
441 \$wgDBprefix = \"{$prefix}\";
443 # MySQL table options to use during installation or update
444 \$wgDBTableOptions = \"{$tblOpts}\";
446 # Experimental charset support for MySQL 4.1/5.0.
447 \$wgDBmysql5 = {$dbmysql5};";