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