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