d2d6bdf685b921054c1ee4cca70a0b9cc4e598f8
4 * Class for setting up the MediaWiki database using MySQL.
9 class MysqlInstaller
extends DatabaseInstaller
{
11 protected $globalNames = array(
21 protected $internalDefaults = array(
22 '_MysqlEngine' => 'InnoDB',
23 '_MysqlCharset' => 'binary',
26 public $supportedEngines = array( 'InnoDB', 'MyISAM' );
28 public $minimumVersion = '4.0.14';
30 public $webUserPrivs = array(
35 'CREATE TEMPORARY TABLES',
38 public function getName() {
42 public function __construct( $parent ) {
43 parent
::__construct( $parent );
46 public function isCompiled() {
47 return self
::checkExtension( 'mysql' );
50 public function getGlobalDefaults() {
54 public function getConnectForm() {
56 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
57 $this->parent
->getHelpBox( 'config-db-host-help' ) .
58 Xml
::openElement( 'fieldset' ) .
59 Xml
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
60 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
61 $this->parent
->getHelpBox( 'config-db-name-help' ) .
62 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
63 $this->parent
->getHelpBox( 'config-db-prefix-help' ) .
64 Xml
::closeElement( 'fieldset' ) .
65 $this->getInstallUserBox();
68 public function submitConnectForm() {
69 // Get variables from the request.
70 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
73 $status = Status
::newGood();
74 if ( !strlen( $newValues['wgDBname'] ) ) {
75 $status->fatal( 'config-missing-db-name' );
76 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
77 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
79 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
80 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
82 if ( !$status->isOK() ) {
87 $status = $this->submitInstallUserBox();
88 if ( !$status->isOK() ) {
93 $status = $this->getConnection();
94 if ( !$status->isOK() ) {
97 $conn = $status->value
;
100 $version = $conn->getServerVersion();
101 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
102 return Status
::newFatal( 'config-mysql-old', $this->minimumVersion
, $version );
108 public function getConnection() {
109 $status = Status
::newGood();
111 $this->db
= new DatabaseMysql(
112 $this->getVar( 'wgDBserver' ),
113 $this->getVar( '_InstallUser' ),
114 $this->getVar( '_InstallPassword' ),
118 $this->getVar( 'wgDBprefix' )
120 $status->value
= $this->db
;
122 } catch ( DBConnectionError
$e ) {
123 $status->fatal( 'config-connection-error', $e->getMessage() );
128 public function doUpgrade() {
129 $status = $this->getConnection();
130 if ( !$status->isOK() ) {
131 $this->parent
->showStatusError( $status );
134 $conn = $status->value
;
136 # Determine existing default character set
137 if ( $conn->tableExists( "revision" ) ) {
138 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
139 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
140 $row = $conn->fetchObject( $res );
142 $this->parent
->showMessage( 'config-show-table-status' );
143 $existingSchema = false;
144 $existingEngine = false;
146 if ( preg_match( '/^latin1/', $row->Collation
) ) {
147 $existingSchema = 'mysql4';
148 } elseif ( preg_match( '/^utf8/', $row->Collation
) ) {
149 $existingSchema = 'mysql5';
150 } elseif ( preg_match( '/^binary/', $row->Collation
) ) {
151 $existingSchema = 'mysql5-binary';
153 $existingSchema = false;
154 $this->parent
->showMessage( 'config-unknown-collation' );
156 if ( isset( $row->Engine
) ) {
157 $existingEngine = $row->Engine
;
159 $existingEngine = $row->Type
;
168 * Get a list of storage engines that are available and supported
170 public function getEngines() {
171 $engines = array( 'InnoDB', 'MyISAM' );
172 $status = $this->getConnection();
173 if ( !$status->isOK() ) {
176 $conn = $status->value
;
178 $version = $conn->getServerVersion();
179 if ( version_compare( $version, "4.1.2", "<" ) ) {
180 // No SHOW ENGINES in this version
185 $res = $conn->query( 'SHOW ENGINES' );
186 foreach ( $res as $row ) {
187 if ( $row->Support
== 'YES' ||
$row->Support
== 'DEFAULT' ) {
188 $engines[] = $row->Engine
;
191 $engines = array_intersect( $this->supportedEngines
, $engines );
196 * Get a list of character sets that are available and supported
198 public function getCharsets() {
199 $status = $this->getConnection();
200 $mysql5 = array( 'binary', 'utf8' );
201 $mysql4 = array( 'mysql4' );
202 if ( !$status->isOK() ) {
205 if ( version_compare( $status->value
->getServerVersion(), '4.1.0', '>=' ) ) {
212 * Return true if the install user can create accounts
214 public function canCreateAccounts() {
215 $status = $this->getConnection();
216 if ( !$status->isOK() ) {
219 $conn = $status->value
;
221 // Check version, need INFORMATION_SCHEMA and CREATE USER
222 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
226 // Get current account name
227 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__
);
228 $parts = explode( '@', $currentName );
229 if ( count( $parts ) != 2 ) {
232 $quotedUser = $conn->addQuotes( $parts[0] ) .
233 '@' . $conn->addQuotes( $parts[1] );
235 // The user needs to have INSERT on mysql.* to be able to CREATE USER
236 // The grantee will be double-quoted in this query, as required
237 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
238 array( 'GRANTEE' => $quotedUser ), __METHOD__
);
239 $insertMysql = false;
240 $grantOptions = array_flip( $this->webUserPrivs
);
241 foreach ( $res as $row ) {
242 if ( $row->PRIVILEGE_TYPE
== 'INSERT' ) {
245 if ( $row->IS_GRANTABLE
) {
246 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
250 // Check for DB-specific privs for mysql.*
251 if ( !$insertMysql ) {
252 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
254 'GRANTEE' => $quotedUser,
255 'TABLE_SCHEMA' => 'mysql',
256 'PRIVILEGE_TYPE' => 'INSERT',
263 if ( !$insertMysql ) {
267 // Check for DB-level grant options
268 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
270 'GRANTEE' => $quotedUser,
273 foreach ( $res as $row ) {
274 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA
);
275 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
276 unset( $grantOptions[$row->PRIVILEGE_TYPE
] );
279 if ( count( $grantOptions ) ) {
280 // Can't grant everything
286 public function getSettingsForm() {
287 if ( $this->canCreateAccounts() ) {
288 $noCreateMsg = false;
290 $noCreateMsg = 'config-db-web-no-create-privs';
292 $s = $this->getWebUserBox( $noCreateMsg );
294 // Do engine selector
295 $engines = $this->getEngines();
296 // If the current default engine is not supported, use an engine that is
297 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
298 $this->setVar( '_MysqlEngine', reset( $engines ) );
300 if ( count( $engines ) >= 2 ) {
301 $s .= $this->getRadioSet( array(
302 'var' => '_MysqlEngine',
303 'label' => 'config-mysql-engine',
304 'itemLabelPrefix' => 'config-mysql-',
307 $s .= $this->parent
->getHelpBox( 'config-mysql-engine-help' );
310 // If the current default charset is not supported, use a charset that is
311 $charsets = $this->getCharsets();
312 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
313 $this->setVar( '_MysqlCharset', reset( $charsets ) );
316 // Do charset selector
317 if ( count( $charsets ) >= 2 ) {
318 $s .= $this->getRadioSet( array(
319 'var' => '_MysqlCharset',
320 'label' => 'config-mysql-charset',
321 'itemLabelPrefix' => 'config-mysql-',
322 'values' => $charsets
324 $s .= $this->parent
->getHelpBox( 'config-mysql-charset-help' );
330 public function submitSettingsForm() {
331 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
332 $status = $this->submitWebUserBox();
333 if ( !$status->isOK() ) {
337 // Validate the create checkbox
338 $canCreate = $this->canCreateAccounts();
340 $this->setVar( '_CreateDBAccount', false );
343 $create = $this->getVar( '_CreateDBAccount' );
347 // Test the web account
349 $webConn = new Database(
350 $this->getVar( 'wgDBserver' ),
351 $this->getVar( 'wgDBuser' ),
352 $this->getVar( 'wgDBpassword' ),
356 $this->getVar( 'wgDBprefix' )
358 } catch ( DBConnectionError
$e ) {
359 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
363 // Validate engines and charsets
364 // This is done pre-submit already so it's just for security
365 $engines = $this->getEngines();
366 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
367 $this->setVar( '_MysqlEngine', reset( $engines ) );
369 $charsets = $this->getCharsets();
370 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
371 $this->setVar( '_MysqlCharset', reset( $charsets ) );
373 return Status
::newGood();
376 public function preInstall() {
377 # Add our user callback to installSteps, right before the tables are created.
381 'callback' => array( $this, 'setupUser' ),
384 $this->parent
->addInstallStepFollowing( "tables", $callback );
387 public function setupDatabase() {
388 $status = $this->getConnection();
389 if ( !$status->isOK() ) {
392 $conn = $status->value
;
393 $dbName = $this->getVar( 'wgDBname' );
394 if( !$conn->selectDB( $dbName ) ) {
395 $conn->query( "CREATE DATABASE `$dbName`" );
396 $conn->selectDB( $dbName );
401 public function setupUser() {
404 if ( !$this->getVar( '_CreateDBAccount' ) ) {
405 return Status
::newGood();
408 $status = $this->getConnection();
409 if ( !$status->isOK() ) {
413 $db = $this->getVar( 'wgDBname' );
414 $this->db
->selectDB( $db );
415 $error = $this->db
->sourceFile( "$IP/maintenance/users.sql" );
416 if ( $error !== true ) {
417 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
423 public function createTables() {
425 $status = $this->getConnection();
426 if ( !$status->isOK() ) {
429 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
431 if( $this->db
->tableExists( 'user' ) ) {
432 $status->warning( 'config-install-tables-exist' );
436 $error = $this->db
->sourceFile( "$IP/maintenance/tables.sql" );
437 if( $error !== true ) {
438 $status->fatal( 'config-install-tables-failed', $error );
443 public function getTableOptions() {
444 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
445 'default charset' => $this->getVar( '_MysqlCharset' ) );
448 public function getLocalSettings() {
449 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
450 $prefix = $this->getVar( 'wgDBprefix' );
451 $opts = $this->getTableOptions();
452 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
454 "# MySQL specific settings
455 \$wgDBprefix = \"{$prefix}\";
457 # MySQL table options to use during installation or update
458 \$wgDBTableOptions = \"{$tblOpts}\";
460 # Experimental charset support for MySQL 4.1/5.0.
461 \$wgDBmysql5 = {$dbmysql5};";