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