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 isCompiled() {
36 return $this->checkExtension( 'mysql' );
39 function getGlobalDefaults() {
43 function getConnectForm() {
45 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
46 $this->parent
->getHelpBox( 'config-db-host-help' ) .
47 Xml
::openElement( 'fieldset' ) .
48 Xml
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
49 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
50 $this->parent
->getHelpBox( 'config-db-name-help' ) .
51 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
52 $this->parent
->getHelpBox( 'config-db-prefix-help' ) .
53 Xml
::closeElement( 'fieldset' ) .
54 $this->getInstallUserBox();
57 function submitConnectForm() {
58 // Get variables from the request
59 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
62 $status = Status
::newGood();
63 if ( !strlen( $newValues['wgDBname'] ) ) {
64 $status->fatal( 'config-missing-db-name' );
65 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
66 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
68 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
69 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
71 if ( !$status->isOK() ) {
76 $status = $this->submitInstallUserBox();
77 if ( !$status->isOK() ) {
82 $status = $this->getConnection();
83 if ( !$status->isOK() ) {
86 $conn = $status->value
;
89 $version = $conn->getServerVersion();
90 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
91 return Status
::newFatal( 'config-mysql-old', $this->minimumVersion
, $version );
97 function getConnection() {
98 $status = Status
::newGood();
100 $this->db
= new DatabaseMysql(
101 $this->getVar( 'wgDBserver' ),
102 $this->getVar( '_InstallUser' ),
103 $this->getVar( '_InstallPassword' ),
107 $this->getVar( 'wgDBprefix' )
109 $status->value
= $this->db
;
111 } catch ( DBConnectionError
$e ) {
112 $status->fatal( 'config-connection-error', $e->getMessage() );
117 function doUpgrade() {
118 $status = $this->getConnection();
119 if ( !$status->isOK() ) {
120 $this->parent
->showStatusError( $status );
123 $conn = $status->value
;
125 # Determine existing default character set
126 if ( $conn->tableExists( "revision" ) ) {
127 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
128 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
129 $row = $conn->fetchObject( $res );
131 $this->parent
->showMessage( 'config-show-table-status' );
132 $existingSchema = false;
133 $existingEngine = false;
135 if ( preg_match( '/^latin1/', $row->Collation
) ) {
136 $existingSchema = 'mysql4';
137 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
138 $existingSchema = 'mysql5';
139 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
140 $existingSchema = 'mysql5-binary';
142 $existingSchema = false;
143 $this->parent
->showMessage( 'config-unknown-collation' );
145 if ( isset( $row->Engine
) ) {
146 $existingEngine = $row->Engine
;
148 $existingEngine = $row->Type
;
157 * Get a list of storage engines that are available and supported
159 function getEngines() {
160 $engines = array( 'InnoDB', 'MyISAM' );
161 $status = $this->getConnection();
162 if ( !$status->isOK() ) {
165 $conn = $status->value
;
167 $version = $conn->getServerVersion();
168 if ( version_compare( $version, "4.1.2", "<" ) ) {
169 // No SHOW ENGINES in this version
174 $res = $conn->query( 'SHOW ENGINES' );
175 foreach ( $res as $row ) {
176 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
177 $engines[] = $row->Engine
;
180 $engines = array_intersect( $this->supportedEngines
, $engines );
185 * Get a list of character sets that are available and supported
187 function getCharsets() {
188 $status = $this->getConnection();
189 $mysql5 = array( 'binary', 'utf8' );
190 $mysql4 = array( 'mysql4' );
191 if ( !$status->isOK() ) {
194 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
201 * Return true if the install user can create accounts
203 function canCreateAccounts() {
204 $status = $this->getConnection();
205 if ( !$status->isOK() ) {
208 $conn = $status->value
;
210 // Check version, need INFORMATION_SCHEMA and CREATE USER
211 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
215 // Get current account name
216 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
217 $parts = explode( '@', $currentName );
218 if ( count( $parts ) != 2 ) {
221 $quotedUser = $conn->addQuotes( $parts[0] ) .
222 '@' . $conn->addQuotes( $parts[1] );
224 // The user needs to have INSERT on mysql.* to be able to CREATE USER
225 // The grantee will be double-quoted in this query, as required
226 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
227 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
228 $insertMysql = false;
229 $grantOptions = array_flip( $this->webUserPrivs
);
230 foreach ( $res as $row ) {
231 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
234 if ( $row->IS_GRANTABLE
) {
235 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
239 // Check for DB-specific privs for mysql.*
240 if ( !$insertMysql ) {
241 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
243 'GRANTEE' => $quotedUser,
244 'TABLE_SCHEMA' => 'mysql',
245 'PRIVILEGE_TYPE' => 'INSERT',
252 if ( !$insertMysql ) {
256 // Check for DB-level grant options
257 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
259 'GRANTEE' => $quotedUser,
262 foreach ( $res as $row ) {
263 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
264 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
265 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
268 if ( count( $grantOptions ) ) {
269 // Can't grant everything
275 function getSettingsForm() {
276 if ( $this->canCreateAccounts() ) {
277 $noCreateMsg = false;
279 $noCreateMsg = 'config-db-web-no-create-privs';
281 $s = $this->getWebUserBox( $noCreateMsg );
283 // Do engine selector
284 $engines = $this->getEngines();
285 // If the current default engine is not supported, use an engine that is
286 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
287 $this->setVar( '_MysqlEngine', reset( $engines ) );
289 if ( count( $engines ) >= 2 ) {
290 $s .= $this->getRadioSet( array(
291 'var' => '_MysqlEngine',
292 'label' => 'config-mysql-engine',
293 'itemLabelPrefix' => 'config-mysql-',
296 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
299 // If the current default charset is not supported, use a charset that is
300 $charsets = $this->getCharsets();
301 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
302 $this->setVar( '_MysqlCharset', reset( $charsets ) );
305 // Do charset selector
306 if ( count( $charsets ) >= 2 ) {
307 $s .= $this->getRadioSet( array(
308 'var' => '_MysqlCharset',
309 'label' => 'config-mysql-charset',
310 'itemLabelPrefix' => 'config-mysql-',
311 'values' => $charsets
313 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
319 function submitSettingsForm() {
320 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
321 $status = $this->submitWebUserBox();
322 if ( !$status->isOK() ) {
326 // Validate the create checkbox
327 $canCreate = $this->canCreateAccounts();
329 $this->setVar( '_CreateDBAccount', false );
332 $create = $this->getVar( '_CreateDBAccount' );
336 // Test the web account
338 $webConn = new Database(
339 $this->getVar( 'wgDBserver' ),
340 $this->getVar( 'wgDBuser' ),
341 $this->getVar( 'wgDBpassword' ),
345 $this->getVar( 'wgDBprefix' )
347 } catch ( DBConnectionError
$e ) {
348 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
352 // Validate engines and charsets
353 // This is done pre-submit already so it's just for security
354 $engines = $this->getEngines();
355 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
356 $this->setVar( '_MysqlEngine', reset( $engines ) );
358 $charsets = $this->getCharsets();
359 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
360 $this->setVar( '_MysqlCharset', reset( $charsets ) );
362 return Status
::newGood();
365 function setupDatabase() {
366 $status = $this->getConnection();
367 if ( !$status->isOK() ) {
370 $conn = $status->value
;
371 $dbName = $this->getVar( 'wgDBname' );
372 if( !$conn->selectDB( $dbName ) ) {
373 $conn->query( "CREATE DATABASE `$dbName`" );
374 $conn->selectDB( $dbName );
379 function createTables() {
381 $status = $this->getConnection();
382 if ( !$status->isOK() ) {
385 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
386 if ( !$this->db
->sourceFile( "$IP/maintenance/tables.sql" ) ) {
389 return Status
::newGood();
392 function getTableOptions() {
393 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
394 'default charset' => $this->getVar( '_MysqlCharset' ) );
397 function getLocalSettings() {
398 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
399 $prefix = $this->getVar( 'wgDBprefix' );
400 $opts = $this->getTableOptions();
401 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
403 "# MySQL specific settings
404 \$wgDBprefix = \"{$prefix}\";
406 # MySQL table options to use during installation or update
407 \$wgDBTableOptions = \"{$tblOpts}\";
409 # Experimental charset support for MySQL 4.1/5.0.
410 \$wgDBmysql5 = {$dbmysql5};";