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 # Add our user callback to installSteps, right before the tables are created.
39 $where_tables = array_search( "tables", $this->parent
->installSteps
);
43 'callback' => array( &$this, 'setupUser' ),
46 array_splice( $this->parent
->installSteps
, $where_tables, 0, $callback );
49 function isCompiled() {
50 return $this->checkExtension( 'mysql' );
53 function getGlobalDefaults() {
57 function getConnectForm() {
59 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
60 $this->parent
->getHelpBox( 'config-db-host-help' ) .
61 Xml
::openElement( 'fieldset' ) .
62 Xml
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
63 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
64 $this->parent
->getHelpBox( 'config-db-name-help' ) .
65 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
66 $this->parent
->getHelpBox( 'config-db-prefix-help' ) .
67 Xml
::closeElement( 'fieldset' ) .
68 $this->getInstallUserBox();
71 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 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
;
125 } catch ( DBConnectionError
$e ) {
126 $status->fatal( 'config-connection-error', $e->getMessage() );
131 function doUpgrade() {
132 $status = $this->getConnection();
133 if ( !$status->isOK() ) {
134 $this->parent
->showStatusError( $status );
137 $conn = $status->value
;
139 # Determine existing default character set
140 if ( $conn->tableExists( "revision" ) ) {
141 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
142 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
143 $row = $conn->fetchObject( $res );
145 $this->parent
->showMessage( 'config-show-table-status' );
146 $existingSchema = false;
147 $existingEngine = false;
149 if ( preg_match( '/^latin1/', $row->Collation
) ) {
150 $existingSchema = 'mysql4';
151 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
152 $existingSchema = 'mysql5';
153 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
154 $existingSchema = 'mysql5-binary';
156 $existingSchema = false;
157 $this->parent
->showMessage( 'config-unknown-collation' );
159 if ( isset( $row->Engine
) ) {
160 $existingEngine = $row->Engine
;
162 $existingEngine = $row->Type
;
171 * Get a list of storage engines that are available and supported
173 function getEngines() {
174 $engines = array( 'InnoDB', 'MyISAM' );
175 $status = $this->getConnection();
176 if ( !$status->isOK() ) {
179 $conn = $status->value
;
181 $version = $conn->getServerVersion();
182 if ( version_compare( $version, "4.1.2", "<" ) ) {
183 // No SHOW ENGINES in this version
188 $res = $conn->query( 'SHOW ENGINES' );
189 foreach ( $res as $row ) {
190 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
191 $engines[] = $row->Engine
;
194 $engines = array_intersect( $this->supportedEngines
, $engines );
199 * Get a list of character sets that are available and supported
201 function getCharsets() {
202 $status = $this->getConnection();
203 $mysql5 = array( 'binary', 'utf8' );
204 $mysql4 = array( 'mysql4' );
205 if ( !$status->isOK() ) {
208 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
215 * Return true if the install user can create accounts
217 function canCreateAccounts() {
218 $status = $this->getConnection();
219 if ( !$status->isOK() ) {
222 $conn = $status->value
;
224 // Check version, need INFORMATION_SCHEMA and CREATE USER
225 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
229 // Get current account name
230 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
231 $parts = explode( '@', $currentName );
232 if ( count( $parts ) != 2 ) {
235 $quotedUser = $conn->addQuotes( $parts[0] ) .
236 '@' . $conn->addQuotes( $parts[1] );
238 // The user needs to have INSERT on mysql.* to be able to CREATE USER
239 // The grantee will be double-quoted in this query, as required
240 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
241 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
242 $insertMysql = false;
243 $grantOptions = array_flip( $this->webUserPrivs
);
244 foreach ( $res as $row ) {
245 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
248 if ( $row->IS_GRANTABLE
) {
249 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
253 // Check for DB-specific privs for mysql.*
254 if ( !$insertMysql ) {
255 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
257 'GRANTEE' => $quotedUser,
258 'TABLE_SCHEMA' => 'mysql',
259 'PRIVILEGE_TYPE' => 'INSERT',
266 if ( !$insertMysql ) {
270 // Check for DB-level grant options
271 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
273 'GRANTEE' => $quotedUser,
276 foreach ( $res as $row ) {
277 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
278 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
279 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
282 if ( count( $grantOptions ) ) {
283 // Can't grant everything
289 function getSettingsForm() {
290 if ( $this->canCreateAccounts() ) {
291 $noCreateMsg = false;
293 $noCreateMsg = 'config-db-web-no-create-privs';
295 $s = $this->getWebUserBox( $noCreateMsg );
297 // Do engine selector
298 $engines = $this->getEngines();
299 // If the current default engine is not supported, use an engine that is
300 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
301 $this->setVar( '_MysqlEngine', reset( $engines ) );
303 if ( count( $engines ) >= 2 ) {
304 $s .= $this->getRadioSet( array(
305 'var' => '_MysqlEngine',
306 'label' => 'config-mysql-engine',
307 'itemLabelPrefix' => 'config-mysql-',
310 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
313 // If the current default charset is not supported, use a charset that is
314 $charsets = $this->getCharsets();
315 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
316 $this->setVar( '_MysqlCharset', reset( $charsets ) );
319 // Do charset selector
320 if ( count( $charsets ) >= 2 ) {
321 $s .= $this->getRadioSet( array(
322 'var' => '_MysqlCharset',
323 'label' => 'config-mysql-charset',
324 'itemLabelPrefix' => 'config-mysql-',
325 'values' => $charsets
327 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
333 function submitSettingsForm() {
334 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
335 $status = $this->submitWebUserBox();
336 if ( !$status->isOK() ) {
340 // Validate the create checkbox
341 $canCreate = $this->canCreateAccounts();
343 $this->setVar( '_CreateDBAccount', false );
346 $create = $this->getVar( '_CreateDBAccount' );
350 // Test the web account
352 $webConn = new Database(
353 $this->getVar( 'wgDBserver' ),
354 $this->getVar( 'wgDBuser' ),
355 $this->getVar( 'wgDBpassword' ),
359 $this->getVar( 'wgDBprefix' )
361 } catch ( DBConnectionError
$e ) {
362 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
366 // Validate engines and charsets
367 // This is done pre-submit already so it's just for security
368 $engines = $this->getEngines();
369 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
370 $this->setVar( '_MysqlEngine', reset( $engines ) );
372 $charsets = $this->getCharsets();
373 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
374 $this->setVar( '_MysqlCharset', reset( $charsets ) );
376 return Status
::newGood();
379 function setupDatabase() {
380 $status = $this->getConnection();
381 if ( !$status->isOK() ) {
384 $conn = $status->value
;
385 $dbName = $this->getVar( 'wgDBname' );
386 if( !$conn->selectDB( $dbName ) ) {
387 $conn->query( "CREATE DATABASE `$dbName`" );
388 $conn->selectDB( $dbName );
393 function setupUser() {
396 if ( !$this->getVar( '_CreateDBAccount' ) ) {
397 return Status
::newGood();
400 $status = $this->getConnection();
401 if ( !$status->isOK() ) {
405 $db = $this->getVar( 'wgDBname' );
406 $this->db
->selectDB( $db );
407 $error = $this->db
->sourceFile( "$IP/maintenance/users.sql" );
409 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
415 function createTables() {
417 $status = $this->getConnection();
418 if ( !$status->isOK() ) {
421 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
422 if ( !$this->db
->sourceFile( "$IP/maintenance/tables.sql" ) ) {
425 return Status
::newGood();
428 function getTableOptions() {
429 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
430 'default charset' => $this->getVar( '_MysqlCharset' ) );
433 function getLocalSettings() {
434 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
435 $prefix = $this->getVar( 'wgDBprefix' );
436 $opts = $this->getTableOptions();
437 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
439 "# MySQL specific settings
440 \$wgDBprefix = \"{$prefix}\";
442 # MySQL table options to use during installation or update
443 \$wgDBTableOptions = \"{$tblOpts}\";
445 # Experimental charset support for MySQL 4.1/5.0.
446 \$wgDBmysql5 = {$dbmysql5};";