Per ^demon's comment on r71430: moved doUpgrade() to DatabaseInstaller (did not remov...
[mediawiki.git] / includes / installer / MysqlInstaller.php
blob3876d1a8617357e0c9569d623aca27f045d0ff33
1 <?php
2 /**
3 * MySQL-specific installer.
5 * @file
6 * @ingroup Deployment
7 */
9 /**
10 * Class for setting up the MediaWiki database using MySQL.
12 * @ingroup Deployment
13 * @since 1.17
15 class MysqlInstaller extends DatabaseInstaller {
17 protected $globalNames = array(
18 'wgDBserver',
19 'wgDBname',
20 'wgDBuser',
21 'wgDBpassword',
22 'wgDBprefix',
23 'wgDBTableOptions',
24 'wgDBmysql5',
27 protected $internalDefaults = array(
28 '_MysqlEngine' => 'InnoDB',
29 '_MysqlCharset' => 'binary',
32 public $supportedEngines = array( 'InnoDB', 'MyISAM' );
34 public $minimumVersion = '4.0.14';
36 public $webUserPrivs = array(
37 'DELETE',
38 'INSERT',
39 'SELECT',
40 'UPDATE',
41 'CREATE TEMPORARY TABLES',
44 public function getName() {
45 return 'mysql';
48 public function __construct( $parent ) {
49 parent::__construct( $parent );
52 public function isCompiled() {
53 return self::checkExtension( 'mysql' );
56 public function getGlobalDefaults() {
57 return array();
60 public function getConnectForm() {
61 return
62 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
63 $this->parent->getHelpBox( 'config-db-host-help' ) .
64 Xml::openElement( 'fieldset' ) .
65 Xml::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
66 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
67 $this->parent->getHelpBox( 'config-db-name-help' ) .
68 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
69 $this->parent->getHelpBox( 'config-db-prefix-help' ) .
70 Xml::closeElement( 'fieldset' ) .
71 $this->getInstallUserBox();
74 public function submitConnectForm() {
75 // Get variables from the request.
76 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
78 // Validate them.
79 $status = Status::newGood();
80 if ( !strlen( $newValues['wgDBname'] ) ) {
81 $status->fatal( 'config-missing-db-name' );
82 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
83 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
85 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
86 $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
88 if ( !$status->isOK() ) {
89 return $status;
92 // Submit user box
93 $status = $this->submitInstallUserBox();
94 if ( !$status->isOK() ) {
95 return $status;
98 // Try to connect
99 $status = $this->getConnection();
100 if ( !$status->isOK() ) {
101 return $status;
103 $conn = $status->value;
105 // Check version
106 $version = $conn->getServerVersion();
107 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
108 return Status::newFatal( 'config-mysql-old', $this->minimumVersion, $version );
111 return $status;
114 public function getConnection() {
115 $status = Status::newGood();
116 try {
117 $this->db = new DatabaseMysql(
118 $this->getVar( 'wgDBserver' ),
119 $this->getVar( '_InstallUser' ),
120 $this->getVar( '_InstallPassword' ),
121 false,
122 false,
124 $this->getVar( 'wgDBprefix' )
126 $status->value = $this->db;
127 } catch ( DBConnectionError $e ) {
128 $status->fatal( 'config-connection-error', $e->getMessage() );
130 return $status;
133 public function preUpgrade() {
134 global $wgDBuser, $wgDBpassword;
136 $status = $this->getConnection();
137 if ( !$status->isOK() ) {
138 $this->parent->showStatusError( $status );
139 return;
141 $conn = $status->value;
142 $conn->selectDB( $this->getVar( 'wgDBname' ) );
144 # Determine existing default character set
145 if ( $conn->tableExists( "revision" ) ) {
146 $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
147 $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
148 $row = $conn->fetchObject( $res );
149 if ( !$row ) {
150 $this->parent->showMessage( 'config-show-table-status' );
151 $existingSchema = false;
152 $existingEngine = false;
153 } else {
154 if ( preg_match( '/^latin1/', $row->Collation ) ) {
155 $existingSchema = 'mysql4';
156 } elseif ( preg_match( '/^utf8/', $row->Collation ) ) {
157 $existingSchema = 'mysql5';
158 } elseif ( preg_match( '/^binary/', $row->Collation ) ) {
159 $existingSchema = 'mysql5-binary';
160 } else {
161 $existingSchema = false;
162 $this->parent->showMessage( 'config-unknown-collation' );
164 if ( isset( $row->Engine ) ) {
165 $existingEngine = $row->Engine;
166 } else {
167 $existingEngine = $row->Type;
170 } else {
171 $existingSchema = false;
172 $existingEngine = false;
175 if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) {
176 $this->parent->showMessage( 'config-mysql-charset-mismatch', $this->getVar( '_MysqlCharset' ), $existingSchema );
177 $this->setVar( '_MysqlCharset', $existingSchema );
179 if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
180 $this->parent->showMessage( 'config-mysql-egine-mismatch', $this->getVar( '_MysqlEngine' ), $existingEngine );
181 $this->setVar( '_MysqlEngine', $existingEngine );
184 # Normal user and password are selected after this step, so for now
185 # just copy these two
186 $wgDBuser = $this->getVar( '_InstallUser' );
187 $wgDBpassword = $this->getVar( '_InstallPassword' );
191 * Get a list of storage engines that are available and supported
193 public function getEngines() {
194 $engines = array( 'InnoDB', 'MyISAM' );
195 $status = $this->getConnection();
196 if ( !$status->isOK() ) {
197 return $engines;
199 $conn = $status->value;
201 $version = $conn->getServerVersion();
202 if ( version_compare( $version, "4.1.2", "<" ) ) {
203 // No SHOW ENGINES in this version
204 return $engines;
207 $engines = array();
208 $res = $conn->query( 'SHOW ENGINES' );
209 foreach ( $res as $row ) {
210 if ( $row->Support == 'YES' || $row->Support == 'DEFAULT' ) {
211 $engines[] = $row->Engine;
214 $engines = array_intersect( $this->supportedEngines, $engines );
215 return $engines;
219 * Get a list of character sets that are available and supported
221 public function getCharsets() {
222 $status = $this->getConnection();
223 $mysql5 = array( 'binary', 'utf8' );
224 $mysql4 = array( 'mysql4' );
225 if ( !$status->isOK() ) {
226 return $mysql5;
228 if ( version_compare( $status->value->getServerVersion(), '4.1.0', '>=' ) ) {
229 return $mysql5;
231 return $mysql4;
235 * Return true if the install user can create accounts
237 public function canCreateAccounts() {
238 $status = $this->getConnection();
239 if ( !$status->isOK() ) {
240 return false;
242 $conn = $status->value;
244 // Check version, need INFORMATION_SCHEMA and CREATE USER
245 if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
246 return false;
249 // Get current account name
250 $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__ );
251 $parts = explode( '@', $currentName );
252 if ( count( $parts ) != 2 ) {
253 return false;
255 $quotedUser = $conn->addQuotes( $parts[0] ) .
256 '@' . $conn->addQuotes( $parts[1] );
258 // The user needs to have INSERT on mysql.* to be able to CREATE USER
259 // The grantee will be double-quoted in this query, as required
260 $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
261 array( 'GRANTEE' => $quotedUser ), __METHOD__ );
262 $insertMysql = false;
263 $grantOptions = array_flip( $this->webUserPrivs );
264 foreach ( $res as $row ) {
265 if ( $row->PRIVILEGE_TYPE == 'INSERT' ) {
266 $insertMysql = true;
268 if ( $row->IS_GRANTABLE ) {
269 unset( $grantOptions[$row->PRIVILEGE_TYPE] );
273 // Check for DB-specific privs for mysql.*
274 if ( !$insertMysql ) {
275 $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
276 array(
277 'GRANTEE' => $quotedUser,
278 'TABLE_SCHEMA' => 'mysql',
279 'PRIVILEGE_TYPE' => 'INSERT',
280 ), __METHOD__ );
281 if ( $row ) {
282 $insertMysql = true;
286 if ( !$insertMysql ) {
287 return false;
290 // Check for DB-level grant options
291 $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
292 array(
293 'GRANTEE' => $quotedUser,
294 'IS_GRANTABLE' => 1,
295 ), __METHOD__ );
296 foreach ( $res as $row ) {
297 $regex = $conn->likeToRegex( $row->TABLE_SCHEMA );
298 if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
299 unset( $grantOptions[$row->PRIVILEGE_TYPE] );
302 if ( count( $grantOptions ) ) {
303 // Can't grant everything
304 return false;
306 return true;
309 public function getSettingsForm() {
310 if ( $this->canCreateAccounts() ) {
311 $noCreateMsg = false;
312 } else {
313 $noCreateMsg = 'config-db-web-no-create-privs';
315 $s = $this->getWebUserBox( $noCreateMsg );
317 // Do engine selector
318 $engines = $this->getEngines();
319 // If the current default engine is not supported, use an engine that is
320 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
321 $this->setVar( '_MysqlEngine', reset( $engines ) );
323 if ( count( $engines ) >= 2 ) {
324 $s .= $this->getRadioSet( array(
325 'var' => '_MysqlEngine',
326 'label' => 'config-mysql-engine',
327 'itemLabelPrefix' => 'config-mysql-',
328 'values' => $engines
330 $s .= $this->parent->getHelpBox( 'config-mysql-engine-help' );
333 // If the current default charset is not supported, use a charset that is
334 $charsets = $this->getCharsets();
335 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
336 $this->setVar( '_MysqlCharset', reset( $charsets ) );
339 // Do charset selector
340 if ( count( $charsets ) >= 2 ) {
341 $s .= $this->getRadioSet( array(
342 'var' => '_MysqlCharset',
343 'label' => 'config-mysql-charset',
344 'itemLabelPrefix' => 'config-mysql-',
345 'values' => $charsets
347 $s .= $this->parent->getHelpBox( 'config-mysql-charset-help' );
350 return $s;
353 public function submitSettingsForm() {
354 $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
355 $status = $this->submitWebUserBox();
356 if ( !$status->isOK() ) {
357 return $status;
360 // Validate the create checkbox
361 $canCreate = $this->canCreateAccounts();
362 if ( !$canCreate ) {
363 $this->setVar( '_CreateDBAccount', false );
364 $create = false;
365 } else {
366 $create = $this->getVar( '_CreateDBAccount' );
369 if ( !$create ) {
370 // Test the web account
371 try {
372 $webConn = new Database(
373 $this->getVar( 'wgDBserver' ),
374 $this->getVar( 'wgDBuser' ),
375 $this->getVar( 'wgDBpassword' ),
376 false,
377 false,
379 $this->getVar( 'wgDBprefix' )
381 } catch ( DBConnectionError $e ) {
382 return Status::newFatal( 'config-connection-error', $e->getMessage() );
386 // Validate engines and charsets
387 // This is done pre-submit already so it's just for security
388 $engines = $this->getEngines();
389 if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
390 $this->setVar( '_MysqlEngine', reset( $engines ) );
392 $charsets = $this->getCharsets();
393 if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
394 $this->setVar( '_MysqlCharset', reset( $charsets ) );
396 return Status::newGood();
399 public function preInstall() {
400 # Add our user callback to installSteps, right before the tables are created.
401 $callback = array(
402 array(
403 'name' => 'user',
404 'callback' => array( $this, 'setupUser' ),
407 $this->parent->addInstallStepFollowing( "tables", $callback );
410 public function setupDatabase() {
411 $status = $this->getConnection();
412 if ( !$status->isOK() ) {
413 return $status;
415 $conn = $status->value;
416 $dbName = $this->getVar( 'wgDBname' );
417 if( !$conn->selectDB( $dbName ) ) {
418 $conn->query( "CREATE DATABASE `$dbName`" );
419 $conn->selectDB( $dbName );
421 return $status;
424 public function setupUser() {
425 global $IP;
427 if ( !$this->getVar( '_CreateDBAccount' ) ) {
428 return Status::newGood();
431 $status = $this->getConnection();
432 if ( !$status->isOK() ) {
433 return $status;
436 $db = $this->getVar( 'wgDBname' );
437 $this->db->selectDB( $db );
438 $error = $this->db->sourceFile( "$IP/maintenance/users.sql" );
439 if ( $error !== true ) {
440 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
443 return $status;
446 public function getTableOptions() {
447 return array( 'engine' => $this->getVar( '_MysqlEngine' ),
448 'default charset' => $this->getVar( '_MysqlCharset' ) );
451 public function getLocalSettings() {
452 $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
453 $prefix = $this->getVar( 'wgDBprefix' );
454 $opts = $this->getTableOptions();
455 $tblOpts = "ENGINE=" . $opts['engine'] . ', DEFAULT CHARSET=' . $opts['default charset'];
456 return
457 "# MySQL specific settings
458 \$wgDBprefix = \"{$prefix}\";
460 # MySQL table options to use during installation or update
461 \$wgDBTableOptions = \"{$tblOpts}\";
463 # Experimental charset support for MySQL 4.1/5.0.
464 \$wgDBmysql5 = {$dbmysql5};";