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