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