3 class MysqlInstaller
extends DatabaseInstaller
{
5 protected $globalNames = array(
15 protected $internalDefaults = array(
16 '_MysqlEngine' => 'InnoDB',
17 '_MysqlCharset' => 'binary',
20 public $supportedEngines = array( 'InnoDB', 'MyISAM' );
22 public $minimumVersion = '4.0.14';
24 public $webUserPrivs = array(
29 'CREATE TEMPORARY TABLES',
32 public function getName() {
36 public function __construct( $parent ) {
37 parent
::__construct( $parent );
40 public function isCompiled() {
41 return self
::checkExtension( 'mysql' );
44 public function getGlobalDefaults() {
48 public function getConnectForm() {
50 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
51 $this->parent
->getHelpBox( 'config-db-host-help' ) .
52 Xml
::openElement( 'fieldset' ) .
53 Xml
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
54 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
55 $this->parent
->getHelpBox( 'config-db-name-help' ) .
56 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
57 $this->parent
->getHelpBox( 'config-db-prefix-help' ) .
58 Xml
::closeElement( 'fieldset' ) .
59 $this->getInstallUserBox();
62 public function submitConnectForm() {
63 // Get variables from the request
64 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
67 $status = Status
::newGood();
68 if ( !strlen( $newValues['wgDBname'] ) ) {
69 $status->fatal( 'config-missing-db-name' );
70 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
71 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
73 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
74 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
76 if ( !$status->isOK() ) {
81 $status = $this->submitInstallUserBox();
82 if ( !$status->isOK() ) {
87 $status = $this->getConnection();
88 if ( !$status->isOK() ) {
91 $conn = $status->value
;
94 $version = $conn->getServerVersion();
95 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
96 return Status
::newFatal( 'config-mysql-old', $this->minimumVersion
, $version );
102 public function getConnection() {
103 $status = Status
::newGood();
105 $this->db
= new DatabaseMysql(
106 $this->getVar( 'wgDBserver' ),
107 $this->getVar( '_InstallUser' ),
108 $this->getVar( '_InstallPassword' ),
112 $this->getVar( 'wgDBprefix' )
114 $status->value
= $this->db
;
116 } catch ( DBConnectionError
$e ) {
117 $status->fatal( 'config-connection-error', $e->getMessage() );
122 public function doUpgrade() {
123 $status = $this->getConnection();
124 if ( !$status->isOK() ) {
125 $this->parent
->showStatusError( $status );
128 $conn = $status->value
;
130 # Determine existing default character set
131 if ( $conn->tableExists( "revision" ) ) {
132 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
133 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
134 $row = $conn->fetchObject( $res );
136 $this->parent
->showMessage( 'config-show-table-status' );
137 $existingSchema = false;
138 $existingEngine = false;
140 if ( preg_match( '/^latin1/', $row->Collation
) ) {
141 $existingSchema = 'mysql4';
142 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
143 $existingSchema = 'mysql5';
144 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
145 $existingSchema = 'mysql5-binary';
147 $existingSchema = false;
148 $this->parent
->showMessage( 'config-unknown-collation' );
150 if ( isset( $row->Engine
) ) {
151 $existingEngine = $row->Engine
;
153 $existingEngine = $row->Type
;
162 * Get a list of storage engines that are available and supported
164 public function getEngines() {
165 $engines = array( 'InnoDB', 'MyISAM' );
166 $status = $this->getConnection();
167 if ( !$status->isOK() ) {
170 $conn = $status->value
;
172 $version = $conn->getServerVersion();
173 if ( version_compare( $version, "4.1.2", "<" ) ) {
174 // No SHOW ENGINES in this version
179 $res = $conn->query( 'SHOW ENGINES' );
180 foreach ( $res as $row ) {
181 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
182 $engines[] = $row->Engine
;
185 $engines = array_intersect( $this->supportedEngines
, $engines );
190 * Get a list of character sets that are available and supported
192 public function getCharsets() {
193 $status = $this->getConnection();
194 $mysql5 = array( 'binary', 'utf8' );
195 $mysql4 = array( 'mysql4' );
196 if ( !$status->isOK() ) {
199 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
206 * Return true if the install user can create accounts
208 public function canCreateAccounts() {
209 $status = $this->getConnection();
210 if ( !$status->isOK() ) {
213 $conn = $status->value
;
215 // Check version, need INFORMATION_SCHEMA and CREATE USER
216 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
220 // Get current account name
221 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
222 $parts = explode( '@', $currentName );
223 if ( count( $parts ) != 2 ) {
226 $quotedUser = $conn->addQuotes( $parts[0] ) .
227 '@' . $conn->addQuotes( $parts[1] );
229 // The user needs to have INSERT on mysql.* to be able to CREATE USER
230 // The grantee will be double-quoted in this query, as required
231 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
232 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
233 $insertMysql = false;
234 $grantOptions = array_flip( $this->webUserPrivs
);
235 foreach ( $res as $row ) {
236 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
239 if ( $row->IS_GRANTABLE
) {
240 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
244 // Check for DB-specific privs for mysql.*
245 if ( !$insertMysql ) {
246 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
248 'GRANTEE' => $quotedUser,
249 'TABLE_SCHEMA' => 'mysql',
250 'PRIVILEGE_TYPE' => 'INSERT',
257 if ( !$insertMysql ) {
261 // Check for DB-level grant options
262 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
264 'GRANTEE' => $quotedUser,
267 foreach ( $res as $row ) {
268 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
269 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
270 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
273 if ( count( $grantOptions ) ) {
274 // Can't grant everything
280 public function getSettingsForm() {
281 if ( $this->canCreateAccounts() ) {
282 $noCreateMsg = false;
284 $noCreateMsg = 'config-db-web-no-create-privs';
286 $s = $this->getWebUserBox( $noCreateMsg );
288 // Do engine selector
289 $engines = $this->getEngines();
290 // If the current default engine is not supported, use an engine that is
291 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
292 $this->setVar( '_MysqlEngine', reset( $engines ) );
294 if ( count( $engines ) >= 2 ) {
295 $s .= $this->getRadioSet( array(
296 'var' => '_MysqlEngine',
297 'label' => 'config-mysql-engine',
298 'itemLabelPrefix' => 'config-mysql-',
301 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
304 // If the current default charset is not supported, use a charset that is
305 $charsets = $this->getCharsets();
306 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
307 $this->setVar( '_MysqlCharset', reset( $charsets ) );
310 // Do charset selector
311 if ( count( $charsets ) >= 2 ) {
312 $s .= $this->getRadioSet( array(
313 'var' => '_MysqlCharset',
314 'label' => 'config-mysql-charset',
315 'itemLabelPrefix' => 'config-mysql-',
316 'values' => $charsets
318 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
324 public function submitSettingsForm() {
325 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
326 $status = $this->submitWebUserBox();
327 if ( !$status->isOK() ) {
331 // Validate the create checkbox
332 $canCreate = $this->canCreateAccounts();
334 $this->setVar( '_CreateDBAccount', false );
337 $create = $this->getVar( '_CreateDBAccount' );
341 // Test the web account
343 $webConn = new Database(
344 $this->getVar( 'wgDBserver' ),
345 $this->getVar( 'wgDBuser' ),
346 $this->getVar( 'wgDBpassword' ),
350 $this->getVar( 'wgDBprefix' )
352 } catch ( DBConnectionError
$e ) {
353 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
357 // Validate engines and charsets
358 // This is done pre-submit already so it's just for security
359 $engines = $this->getEngines();
360 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
361 $this->setVar( '_MysqlEngine', reset( $engines ) );
363 $charsets = $this->getCharsets();
364 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
365 $this->setVar( '_MysqlCharset', reset( $charsets ) );
367 return Status
::newGood();
370 public function preInstall() {
371 # Add our user callback to installSteps, right before the tables are created.
375 'callback' => array( $this, 'setupUser' ),
378 $this->parent
->addInstallStepFollowing( "tables", $callback );
381 public function setupDatabase() {
382 $status = $this->getConnection();
383 if ( !$status->isOK() ) {
386 $conn = $status->value
;
387 $dbName = $this->getVar( 'wgDBname' );
388 if( !$conn->selectDB( $dbName ) ) {
389 $conn->query( "CREATE DATABASE `$dbName`" );
390 $conn->selectDB( $dbName );
395 public function setupUser() {
398 if ( !$this->getVar( '_CreateDBAccount' ) ) {
399 return Status
::newGood();
402 $status = $this->getConnection();
403 if ( !$status->isOK() ) {
407 $db = $this->getVar( 'wgDBname' );
408 $this->db
->selectDB( $db );
409 $error = $this->db
->sourceFile( "$IP/maintenance/users.sql" );
410 if ( $error !== true ) {
411 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
417 public function createTables() {
419 $status = $this->getConnection();
420 if ( !$status->isOK() ) {
423 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
425 if( $this->db
->tableExists( 'user' ) ) {
426 $status->warning( 'config-install-tables-exist' );
430 $error = $this->db
->sourceFile( "$IP/maintenance/tables.sql" );
431 if( $error !== true ) {
432 $status->fatal( 'config-install-tables-failed', $error );
437 public function getTableOptions() {
438 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
439 'default charset' => $this->getVar( '_MysqlCharset' ) );
442 public 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};";