wikimedia /
mediawiki
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * MySQL-specific installer. |
||
| 4 | * |
||
| 5 | * This program is free software; you can redistribute it and/or modify |
||
| 6 | * it under the terms of the GNU General Public License as published by |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 15 | * You should have received a copy of the GNU General Public License along |
||
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
| 18 | * http://www.gnu.org/copyleft/gpl.html |
||
| 19 | * |
||
| 20 | * @file |
||
| 21 | * @ingroup Deployment |
||
| 22 | */ |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class for setting up the MediaWiki database using MySQL. |
||
| 26 | * |
||
| 27 | * @ingroup Deployment |
||
| 28 | * @since 1.17 |
||
| 29 | */ |
||
| 30 | class MysqlInstaller extends DatabaseInstaller { |
||
| 31 | |||
| 32 | protected $globalNames = [ |
||
| 33 | 'wgDBserver', |
||
| 34 | 'wgDBname', |
||
| 35 | 'wgDBuser', |
||
| 36 | 'wgDBpassword', |
||
| 37 | 'wgDBprefix', |
||
| 38 | 'wgDBTableOptions', |
||
| 39 | 'wgDBmysql5', |
||
| 40 | ]; |
||
| 41 | |||
| 42 | protected $internalDefaults = [ |
||
| 43 | '_MysqlEngine' => 'InnoDB', |
||
| 44 | '_MysqlCharset' => 'binary', |
||
| 45 | '_InstallUser' => 'root', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | public $supportedEngines = [ 'InnoDB', 'MyISAM' ]; |
||
| 49 | |||
| 50 | public $minimumVersion = '5.0.3'; |
||
| 51 | |||
| 52 | public $webUserPrivs = [ |
||
| 53 | 'DELETE', |
||
| 54 | 'INSERT', |
||
| 55 | 'SELECT', |
||
| 56 | 'UPDATE', |
||
| 57 | 'CREATE TEMPORARY TABLES', |
||
| 58 | ]; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public function getName() { |
||
| 64 | return 'mysql'; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | public function isCompiled() { |
||
| 71 | return self::checkExtension( 'mysql' ) || self::checkExtension( 'mysqli' ); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function getConnectForm() { |
||
| 78 | return $this->getTextBox( |
||
| 79 | 'wgDBserver', |
||
| 80 | 'config-db-host', |
||
| 81 | [], |
||
| 82 | $this->parent->getHelpBox( 'config-db-host-help' ) |
||
| 83 | ) . |
||
| 84 | Html::openElement( 'fieldset' ) . |
||
| 85 | Html::element( 'legend', [], wfMessage( 'config-db-wiki-settings' )->text() ) . |
||
| 86 | $this->getTextBox( 'wgDBname', 'config-db-name', [ 'dir' => 'ltr' ], |
||
| 87 | $this->parent->getHelpBox( 'config-db-name-help' ) ) . |
||
| 88 | $this->getTextBox( 'wgDBprefix', 'config-db-prefix', [ 'dir' => 'ltr' ], |
||
| 89 | $this->parent->getHelpBox( 'config-db-prefix-help' ) ) . |
||
| 90 | Html::closeElement( 'fieldset' ) . |
||
| 91 | $this->getInstallUserBox(); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function submitConnectForm() { |
||
| 95 | // Get variables from the request. |
||
| 96 | $newValues = $this->setVarsFromRequest( [ 'wgDBserver', 'wgDBname', 'wgDBprefix' ] ); |
||
| 97 | |||
| 98 | // Validate them. |
||
| 99 | $status = Status::newGood(); |
||
| 100 | if ( !strlen( $newValues['wgDBserver'] ) ) { |
||
| 101 | $status->fatal( 'config-missing-db-host' ); |
||
| 102 | } |
||
| 103 | View Code Duplication | if ( !strlen( $newValues['wgDBname'] ) ) { |
|
| 104 | $status->fatal( 'config-missing-db-name' ); |
||
| 105 | } elseif ( !preg_match( '/^[a-z0-9+_-]+$/i', $newValues['wgDBname'] ) ) { |
||
| 106 | $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] ); |
||
| 107 | } |
||
| 108 | if ( !preg_match( '/^[a-z0-9_-]*$/i', $newValues['wgDBprefix'] ) ) { |
||
| 109 | $status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] ); |
||
| 110 | } |
||
| 111 | if ( !$status->isOK() ) { |
||
| 112 | return $status; |
||
| 113 | } |
||
| 114 | |||
| 115 | // Submit user box |
||
| 116 | $status = $this->submitInstallUserBox(); |
||
| 117 | if ( !$status->isOK() ) { |
||
| 118 | return $status; |
||
| 119 | } |
||
| 120 | |||
| 121 | // Try to connect |
||
| 122 | $status = $this->getConnection(); |
||
| 123 | if ( !$status->isOK() ) { |
||
| 124 | return $status; |
||
| 125 | } |
||
| 126 | /** |
||
| 127 | * @var $conn Database |
||
| 128 | */ |
||
| 129 | $conn = $status->value; |
||
| 130 | |||
| 131 | // Check version |
||
| 132 | $version = $conn->getServerVersion(); |
||
| 133 | if ( version_compare( $version, $this->minimumVersion ) < 0 ) { |
||
| 134 | return Status::newFatal( 'config-mysql-old', $this->minimumVersion, $version ); |
||
| 135 | } |
||
| 136 | |||
| 137 | return $status; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return Status |
||
| 142 | */ |
||
| 143 | public function openConnection() { |
||
| 144 | $status = Status::newGood(); |
||
| 145 | try { |
||
| 146 | $db = Database::factory( 'mysql', [ |
||
| 147 | 'host' => $this->getVar( 'wgDBserver' ), |
||
| 148 | 'user' => $this->getVar( '_InstallUser' ), |
||
| 149 | 'password' => $this->getVar( '_InstallPassword' ), |
||
| 150 | 'dbname' => false, |
||
| 151 | 'flags' => 0, |
||
| 152 | 'tablePrefix' => $this->getVar( 'wgDBprefix' ) ] ); |
||
| 153 | $status->value = $db; |
||
| 154 | } catch ( DBConnectionError $e ) { |
||
| 155 | $status->fatal( 'config-connection-error', $e->getMessage() ); |
||
| 156 | } |
||
| 157 | |||
| 158 | return $status; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function preUpgrade() { |
||
| 162 | global $wgDBuser, $wgDBpassword; |
||
| 163 | |||
| 164 | $status = $this->getConnection(); |
||
| 165 | if ( !$status->isOK() ) { |
||
| 166 | $this->parent->showStatusError( $status ); |
||
|
0 ignored issues
–
show
|
|||
| 167 | |||
| 168 | return; |
||
| 169 | } |
||
| 170 | /** |
||
| 171 | * @var $conn Database |
||
| 172 | */ |
||
| 173 | $conn = $status->value; |
||
| 174 | $conn->selectDB( $this->getVar( 'wgDBname' ) ); |
||
| 175 | |||
| 176 | # Determine existing default character set |
||
| 177 | if ( $conn->tableExists( "revision", __METHOD__ ) ) { |
||
| 178 | $revision = $conn->buildLike( $this->getVar( 'wgDBprefix' ) . 'revision' ); |
||
| 179 | $res = $conn->query( "SHOW TABLE STATUS $revision", __METHOD__ ); |
||
| 180 | $row = $conn->fetchObject( $res ); |
||
|
0 ignored issues
–
show
It seems like
$res defined by $conn->query("SHOW TABLE...revision}", __METHOD__) on line 179 can also be of type boolean; however, IDatabase::fetchObject() does only seem to accept object<ResultWrapper>|object<stdClass>, maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. Loading history...
|
|||
| 181 | if ( !$row ) { |
||
| 182 | $this->parent->showMessage( 'config-show-table-status' ); |
||
| 183 | $existingSchema = false; |
||
| 184 | $existingEngine = false; |
||
| 185 | } else { |
||
| 186 | if ( preg_match( '/^latin1/', $row->Collation ) ) { |
||
| 187 | $existingSchema = 'latin1'; |
||
| 188 | } elseif ( preg_match( '/^utf8/', $row->Collation ) ) { |
||
| 189 | $existingSchema = 'utf8'; |
||
| 190 | } elseif ( preg_match( '/^binary/', $row->Collation ) ) { |
||
| 191 | $existingSchema = 'binary'; |
||
| 192 | } else { |
||
| 193 | $existingSchema = false; |
||
| 194 | $this->parent->showMessage( 'config-unknown-collation' ); |
||
| 195 | } |
||
| 196 | if ( isset( $row->Engine ) ) { |
||
| 197 | $existingEngine = $row->Engine; |
||
| 198 | } else { |
||
| 199 | $existingEngine = $row->Type; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } else { |
||
| 203 | $existingSchema = false; |
||
| 204 | $existingEngine = false; |
||
| 205 | } |
||
| 206 | |||
| 207 | if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) { |
||
|
0 ignored issues
–
show
The expression
$existingSchema of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
|
|||
| 208 | $this->setVar( '_MysqlCharset', $existingSchema ); |
||
| 209 | } |
||
| 210 | if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) { |
||
| 211 | $this->setVar( '_MysqlEngine', $existingEngine ); |
||
| 212 | } |
||
| 213 | |||
| 214 | # Normal user and password are selected after this step, so for now |
||
| 215 | # just copy these two |
||
| 216 | $wgDBuser = $this->getVar( '_InstallUser' ); |
||
| 217 | $wgDBpassword = $this->getVar( '_InstallPassword' ); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get a list of storage engines that are available and supported |
||
| 222 | * |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | public function getEngines() { |
||
| 226 | $status = $this->getConnection(); |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @var $conn Database |
||
| 230 | */ |
||
| 231 | $conn = $status->value; |
||
| 232 | |||
| 233 | $engines = []; |
||
| 234 | $res = $conn->query( 'SHOW ENGINES', __METHOD__ ); |
||
| 235 | foreach ( $res as $row ) { |
||
|
0 ignored issues
–
show
The expression
$res of type boolean|object<ResultWrapper> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
| 236 | if ( $row->Support == 'YES' || $row->Support == 'DEFAULT' ) { |
||
| 237 | $engines[] = $row->Engine; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | $engines = array_intersect( $this->supportedEngines, $engines ); |
||
| 241 | |||
| 242 | return $engines; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get a list of character sets that are available and supported |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public function getCharsets() { |
||
| 251 | return [ 'binary', 'utf8' ]; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Return true if the install user can create accounts |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function canCreateAccounts() { |
||
| 260 | $status = $this->getConnection(); |
||
| 261 | if ( !$status->isOK() ) { |
||
| 262 | return false; |
||
| 263 | } |
||
| 264 | /** @var $conn Database */ |
||
| 265 | $conn = $status->value; |
||
| 266 | |||
| 267 | // Get current account name |
||
| 268 | $currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__ ); |
||
| 269 | $parts = explode( '@', $currentName ); |
||
| 270 | if ( count( $parts ) != 2 ) { |
||
| 271 | return false; |
||
| 272 | } |
||
| 273 | $quotedUser = $conn->addQuotes( $parts[0] ) . |
||
| 274 | '@' . $conn->addQuotes( $parts[1] ); |
||
| 275 | |||
| 276 | // The user needs to have INSERT on mysql.* to be able to CREATE USER |
||
| 277 | // The grantee will be double-quoted in this query, as required |
||
| 278 | $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*', |
||
| 279 | [ 'GRANTEE' => $quotedUser ], __METHOD__ ); |
||
| 280 | $insertMysql = false; |
||
| 281 | $grantOptions = array_flip( $this->webUserPrivs ); |
||
| 282 | foreach ( $res as $row ) { |
||
|
0 ignored issues
–
show
The expression
$res of type boolean|object<ResultWrapper> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
| 283 | if ( $row->PRIVILEGE_TYPE == 'INSERT' ) { |
||
| 284 | $insertMysql = true; |
||
| 285 | } |
||
| 286 | if ( $row->IS_GRANTABLE ) { |
||
| 287 | unset( $grantOptions[$row->PRIVILEGE_TYPE] ); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | // Check for DB-specific privs for mysql.* |
||
| 292 | if ( !$insertMysql ) { |
||
| 293 | $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*', |
||
| 294 | [ |
||
| 295 | 'GRANTEE' => $quotedUser, |
||
| 296 | 'TABLE_SCHEMA' => 'mysql', |
||
| 297 | 'PRIVILEGE_TYPE' => 'INSERT', |
||
| 298 | ], __METHOD__ ); |
||
| 299 | if ( $row ) { |
||
| 300 | $insertMysql = true; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | if ( !$insertMysql ) { |
||
| 305 | return false; |
||
| 306 | } |
||
| 307 | |||
| 308 | // Check for DB-level grant options |
||
| 309 | $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*', |
||
| 310 | [ |
||
| 311 | 'GRANTEE' => $quotedUser, |
||
| 312 | 'IS_GRANTABLE' => 1, |
||
| 313 | ], __METHOD__ ); |
||
| 314 | foreach ( $res as $row ) { |
||
|
0 ignored issues
–
show
The expression
$res of type boolean|object<ResultWrapper> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
| 315 | $regex = $conn->likeToRegex( $row->TABLE_SCHEMA ); |
||
|
0 ignored issues
–
show
The method
likeToRegex() does not seem to exist on object<Database>.
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||
| 316 | if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) { |
||
| 317 | unset( $grantOptions[$row->PRIVILEGE_TYPE] ); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | if ( count( $grantOptions ) ) { |
||
| 321 | // Can't grant everything |
||
| 322 | return false; |
||
| 323 | } |
||
| 324 | |||
| 325 | return true; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function getSettingsForm() { |
||
| 332 | if ( $this->canCreateAccounts() ) { |
||
| 333 | $noCreateMsg = false; |
||
| 334 | } else { |
||
| 335 | $noCreateMsg = 'config-db-web-no-create-privs'; |
||
| 336 | } |
||
| 337 | $s = $this->getWebUserBox( $noCreateMsg ); |
||
| 338 | |||
| 339 | // Do engine selector |
||
| 340 | $engines = $this->getEngines(); |
||
| 341 | // If the current default engine is not supported, use an engine that is |
||
| 342 | if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) { |
||
| 343 | $this->setVar( '_MysqlEngine', reset( $engines ) ); |
||
| 344 | } |
||
| 345 | |||
| 346 | $s .= Xml::openElement( 'div', [ |
||
| 347 | 'id' => 'dbMyisamWarning' |
||
| 348 | ] ); |
||
| 349 | $myisamWarning = 'config-mysql-myisam-dep'; |
||
| 350 | if ( count( $engines ) === 1 ) { |
||
| 351 | $myisamWarning = 'config-mysql-only-myisam-dep'; |
||
| 352 | } |
||
| 353 | $s .= $this->parent->getWarningBox( wfMessage( $myisamWarning )->text() ); |
||
| 354 | $s .= Xml::closeElement( 'div' ); |
||
| 355 | |||
| 356 | if ( $this->getVar( '_MysqlEngine' ) != 'MyISAM' ) { |
||
| 357 | $s .= Xml::openElement( 'script' ); |
||
| 358 | $s .= '$(\'#dbMyisamWarning\').hide();'; |
||
| 359 | $s .= Xml::closeElement( 'script' ); |
||
| 360 | } |
||
| 361 | |||
| 362 | if ( count( $engines ) >= 2 ) { |
||
| 363 | // getRadioSet() builds a set of labeled radio buttons. |
||
| 364 | // For grep: The following messages are used as the item labels: |
||
| 365 | // config-mysql-innodb, config-mysql-myisam |
||
| 366 | $s .= $this->getRadioSet( [ |
||
| 367 | 'var' => '_MysqlEngine', |
||
| 368 | 'label' => 'config-mysql-engine', |
||
| 369 | 'itemLabelPrefix' => 'config-mysql-', |
||
| 370 | 'values' => $engines, |
||
| 371 | 'itemAttribs' => [ |
||
| 372 | 'MyISAM' => [ |
||
| 373 | 'class' => 'showHideRadio', |
||
| 374 | 'rel' => 'dbMyisamWarning' |
||
| 375 | ], |
||
| 376 | 'InnoDB' => [ |
||
| 377 | 'class' => 'hideShowRadio', |
||
| 378 | 'rel' => 'dbMyisamWarning' |
||
| 379 | ] |
||
| 380 | ] |
||
| 381 | ] ); |
||
| 382 | $s .= $this->parent->getHelpBox( 'config-mysql-engine-help' ); |
||
| 383 | } |
||
| 384 | |||
| 385 | // If the current default charset is not supported, use a charset that is |
||
| 386 | $charsets = $this->getCharsets(); |
||
| 387 | if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) { |
||
| 388 | $this->setVar( '_MysqlCharset', reset( $charsets ) ); |
||
| 389 | } |
||
| 390 | |||
| 391 | // Do charset selector |
||
| 392 | if ( count( $charsets ) >= 2 ) { |
||
| 393 | // getRadioSet() builds a set of labeled radio buttons. |
||
| 394 | // For grep: The following messages are used as the item labels: |
||
| 395 | // config-mysql-binary, config-mysql-utf8 |
||
| 396 | $s .= $this->getRadioSet( [ |
||
| 397 | 'var' => '_MysqlCharset', |
||
| 398 | 'label' => 'config-mysql-charset', |
||
| 399 | 'itemLabelPrefix' => 'config-mysql-', |
||
| 400 | 'values' => $charsets |
||
| 401 | ] ); |
||
| 402 | $s .= $this->parent->getHelpBox( 'config-mysql-charset-help' ); |
||
| 403 | } |
||
| 404 | |||
| 405 | return $s; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return Status |
||
| 410 | */ |
||
| 411 | public function submitSettingsForm() { |
||
| 412 | $this->setVarsFromRequest( [ '_MysqlEngine', '_MysqlCharset' ] ); |
||
| 413 | $status = $this->submitWebUserBox(); |
||
| 414 | if ( !$status->isOK() ) { |
||
| 415 | return $status; |
||
| 416 | } |
||
| 417 | |||
| 418 | // Validate the create checkbox |
||
| 419 | $canCreate = $this->canCreateAccounts(); |
||
| 420 | View Code Duplication | if ( !$canCreate ) { |
|
| 421 | $this->setVar( '_CreateDBAccount', false ); |
||
| 422 | $create = false; |
||
| 423 | } else { |
||
| 424 | $create = $this->getVar( '_CreateDBAccount' ); |
||
| 425 | } |
||
| 426 | |||
| 427 | if ( !$create ) { |
||
| 428 | // Test the web account |
||
| 429 | try { |
||
| 430 | Database::factory( 'mysql', [ |
||
| 431 | 'host' => $this->getVar( 'wgDBserver' ), |
||
| 432 | 'user' => $this->getVar( 'wgDBuser' ), |
||
| 433 | 'password' => $this->getVar( 'wgDBpassword' ), |
||
| 434 | 'dbname' => false, |
||
| 435 | 'flags' => 0, |
||
| 436 | 'tablePrefix' => $this->getVar( 'wgDBprefix' ) |
||
| 437 | ] ); |
||
| 438 | } catch ( DBConnectionError $e ) { |
||
| 439 | return Status::newFatal( 'config-connection-error', $e->getMessage() ); |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | // Validate engines and charsets |
||
| 444 | // This is done pre-submit already so it's just for security |
||
| 445 | $engines = $this->getEngines(); |
||
| 446 | if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) { |
||
| 447 | $this->setVar( '_MysqlEngine', reset( $engines ) ); |
||
| 448 | } |
||
| 449 | $charsets = $this->getCharsets(); |
||
| 450 | if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) { |
||
| 451 | $this->setVar( '_MysqlCharset', reset( $charsets ) ); |
||
| 452 | } |
||
| 453 | |||
| 454 | return Status::newGood(); |
||
| 455 | } |
||
| 456 | |||
| 457 | View Code Duplication | public function preInstall() { |
|
| 458 | # Add our user callback to installSteps, right before the tables are created. |
||
| 459 | $callback = [ |
||
| 460 | 'name' => 'user', |
||
| 461 | 'callback' => [ $this, 'setupUser' ], |
||
| 462 | ]; |
||
| 463 | $this->parent->addInstallStep( $callback, 'tables' ); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @return Status |
||
| 468 | */ |
||
| 469 | public function setupDatabase() { |
||
| 470 | $status = $this->getConnection(); |
||
| 471 | if ( !$status->isOK() ) { |
||
| 472 | return $status; |
||
| 473 | } |
||
| 474 | /** @var Database $conn */ |
||
| 475 | $conn = $status->value; |
||
| 476 | $dbName = $this->getVar( 'wgDBname' ); |
||
| 477 | if ( !$conn->selectDB( $dbName ) ) { |
||
| 478 | $conn->query( |
||
| 479 | "CREATE DATABASE " . $conn->addIdentifierQuotes( $dbName ) . "CHARACTER SET utf8", |
||
| 480 | __METHOD__ |
||
| 481 | ); |
||
| 482 | $conn->selectDB( $dbName ); |
||
| 483 | } |
||
| 484 | $this->setupSchemaVars(); |
||
| 485 | |||
| 486 | return $status; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return Status |
||
| 491 | */ |
||
| 492 | public function setupUser() { |
||
| 493 | $dbUser = $this->getVar( 'wgDBuser' ); |
||
| 494 | if ( $dbUser == $this->getVar( '_InstallUser' ) ) { |
||
| 495 | return Status::newGood(); |
||
| 496 | } |
||
| 497 | $status = $this->getConnection(); |
||
| 498 | if ( !$status->isOK() ) { |
||
| 499 | return $status; |
||
| 500 | } |
||
| 501 | |||
| 502 | $this->setupSchemaVars(); |
||
| 503 | $dbName = $this->getVar( 'wgDBname' ); |
||
| 504 | $this->db->selectDB( $dbName ); |
||
| 505 | $server = $this->getVar( 'wgDBserver' ); |
||
| 506 | $password = $this->getVar( 'wgDBpassword' ); |
||
| 507 | $grantableNames = []; |
||
| 508 | |||
| 509 | if ( $this->getVar( '_CreateDBAccount' ) ) { |
||
| 510 | // Before we blindly try to create a user that already has access, |
||
| 511 | try { // first attempt to connect to the database |
||
| 512 | Database::factory( 'mysql', [ |
||
| 513 | 'host' => $server, |
||
| 514 | 'user' => $dbUser, |
||
| 515 | 'password' => $password, |
||
| 516 | 'dbname' => false, |
||
| 517 | 'flags' => 0, |
||
| 518 | 'tablePrefix' => $this->getVar( 'wgDBprefix' ) |
||
| 519 | ] ); |
||
| 520 | $grantableNames[] = $this->buildFullUserName( $dbUser, $server ); |
||
| 521 | $tryToCreate = false; |
||
| 522 | } catch ( DBConnectionError $e ) { |
||
| 523 | $tryToCreate = true; |
||
| 524 | } |
||
| 525 | } else { |
||
| 526 | $grantableNames[] = $this->buildFullUserName( $dbUser, $server ); |
||
| 527 | $tryToCreate = false; |
||
| 528 | } |
||
| 529 | |||
| 530 | if ( $tryToCreate ) { |
||
| 531 | $createHostList = [ |
||
| 532 | $server, |
||
| 533 | 'localhost', |
||
| 534 | 'localhost.localdomain', |
||
| 535 | '%' |
||
| 536 | ]; |
||
| 537 | |||
| 538 | $createHostList = array_unique( $createHostList ); |
||
| 539 | $escPass = $this->db->addQuotes( $password ); |
||
| 540 | |||
| 541 | foreach ( $createHostList as $host ) { |
||
| 542 | $fullName = $this->buildFullUserName( $dbUser, $host ); |
||
| 543 | if ( !$this->userDefinitelyExists( $host, $dbUser ) ) { |
||
| 544 | try { |
||
| 545 | $this->db->begin( __METHOD__ ); |
||
| 546 | $this->db->query( "CREATE USER $fullName IDENTIFIED BY $escPass", __METHOD__ ); |
||
| 547 | $this->db->commit( __METHOD__ ); |
||
| 548 | $grantableNames[] = $fullName; |
||
| 549 | } catch ( DBQueryError $dqe ) { |
||
| 550 | if ( $this->db->lastErrno() == 1396 /* ER_CANNOT_USER */ ) { |
||
| 551 | // User (probably) already exists |
||
| 552 | $this->db->rollback( __METHOD__ ); |
||
| 553 | $status->warning( 'config-install-user-alreadyexists', $dbUser ); |
||
| 554 | $grantableNames[] = $fullName; |
||
| 555 | break; |
||
| 556 | } else { |
||
| 557 | // If we couldn't create for some bizzare reason and the |
||
| 558 | // user probably doesn't exist, skip the grant |
||
| 559 | $this->db->rollback( __METHOD__ ); |
||
| 560 | $status->warning( 'config-install-user-create-failed', $dbUser, $dqe->getText() ); |
||
|
0 ignored issues
–
show
The method
getText() does not seem to exist on object<DBQueryError>.
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||
| 561 | } |
||
| 562 | } |
||
| 563 | } else { |
||
| 564 | $status->warning( 'config-install-user-alreadyexists', $dbUser ); |
||
| 565 | $grantableNames[] = $fullName; |
||
| 566 | break; |
||
| 567 | } |
||
| 568 | } |
||
| 569 | } |
||
| 570 | |||
| 571 | // Try to grant to all the users we know exist or we were able to create |
||
| 572 | $dbAllTables = $this->db->addIdentifierQuotes( $dbName ) . '.*'; |
||
| 573 | foreach ( $grantableNames as $name ) { |
||
| 574 | try { |
||
| 575 | $this->db->begin( __METHOD__ ); |
||
| 576 | $this->db->query( "GRANT ALL PRIVILEGES ON $dbAllTables TO $name", __METHOD__ ); |
||
| 577 | $this->db->commit( __METHOD__ ); |
||
| 578 | } catch ( DBQueryError $dqe ) { |
||
| 579 | $this->db->rollback( __METHOD__ ); |
||
| 580 | $status->fatal( 'config-install-user-grant-failed', $dbUser, $dqe->getText() ); |
||
|
0 ignored issues
–
show
The method
getText() does not seem to exist on object<DBQueryError>.
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | return $status; |
||
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Return a formal 'User'@'Host' username for use in queries |
||
| 589 | * @param string $name Username, quotes will be added |
||
| 590 | * @param string $host Hostname, quotes will be added |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | private function buildFullUserName( $name, $host ) { |
||
| 594 | return $this->db->addQuotes( $name ) . '@' . $this->db->addQuotes( $host ); |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Try to see if the user account exists. Our "superuser" may not have |
||
| 599 | * access to mysql.user, so false means "no" or "maybe" |
||
| 600 | * @param string $host Hostname to check |
||
| 601 | * @param string $user Username to check |
||
| 602 | * @return bool |
||
| 603 | */ |
||
| 604 | private function userDefinitelyExists( $host, $user ) { |
||
| 605 | try { |
||
| 606 | $res = $this->db->selectRow( 'mysql.user', [ 'Host', 'User' ], |
||
| 607 | [ 'Host' => $host, 'User' => $user ], __METHOD__ ); |
||
| 608 | |||
| 609 | return (bool)$res; |
||
| 610 | } catch ( DBQueryError $dqe ) { |
||
| 611 | return false; |
||
| 612 | } |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Return any table options to be applied to all tables that don't |
||
| 617 | * override them. |
||
| 618 | * |
||
| 619 | * @return string |
||
| 620 | */ |
||
| 621 | protected function getTableOptions() { |
||
| 622 | $options = []; |
||
| 623 | if ( $this->getVar( '_MysqlEngine' ) !== null ) { |
||
| 624 | $options[] = "ENGINE=" . $this->getVar( '_MysqlEngine' ); |
||
| 625 | } |
||
| 626 | if ( $this->getVar( '_MysqlCharset' ) !== null ) { |
||
| 627 | $options[] = 'DEFAULT CHARSET=' . $this->getVar( '_MysqlCharset' ); |
||
| 628 | } |
||
| 629 | |||
| 630 | return implode( ', ', $options ); |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Get variables to substitute into tables.sql and the SQL patch files. |
||
| 635 | * |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | View Code Duplication | public function getSchemaVars() { |
|
| 639 | return [ |
||
| 640 | 'wgDBTableOptions' => $this->getTableOptions(), |
||
| 641 | 'wgDBname' => $this->getVar( 'wgDBname' ), |
||
| 642 | 'wgDBuser' => $this->getVar( 'wgDBuser' ), |
||
| 643 | 'wgDBpassword' => $this->getVar( 'wgDBpassword' ), |
||
| 644 | ]; |
||
| 645 | } |
||
| 646 | |||
| 647 | public function getLocalSettings() { |
||
| 648 | $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) ); |
||
| 649 | $prefix = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBprefix' ) ); |
||
| 650 | $tblOpts = LocalSettingsGenerator::escapePhpString( $this->getTableOptions() ); |
||
| 651 | |||
| 652 | return "# MySQL specific settings |
||
| 653 | \$wgDBprefix = \"{$prefix}\"; |
||
| 654 | |||
| 655 | # MySQL table options to use during installation or update |
||
| 656 | \$wgDBTableOptions = \"{$tblOpts}\"; |
||
| 657 | |||
| 658 | # Experimental charset support for MySQL 5.0. |
||
| 659 | \$wgDBmysql5 = {$dbmysql5};"; |
||
| 660 | } |
||
| 661 | } |
||
| 662 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.