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