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