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