| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
namespace MediaWiki\Installer; |
| 26 |
|
| 27 |
use MediaWiki\Status\Status; |
| 28 |
use RuntimeException; |
| 29 |
use Wikimedia\AtEase\AtEase; |
| 30 |
use Wikimedia\Rdbms\Database; |
| 31 |
use Wikimedia\Rdbms\DatabaseDomain; |
| 32 |
use Wikimedia\Rdbms\DBQueryError; |
| 33 |
use Wikimedia\Rdbms\IDatabase; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
abstract class DatabaseInstaller { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public const CONN_CREATE_DATABASE = 'create-database'; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public const CONN_CREATE_SCHEMA = 'create-schema'; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public const CONN_CREATE_TABLES = 'create-tables'; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public const CONN_DONT_KNOW = 'dont-know'; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public $parent; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static $minimumVersion; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected static $notMinimumVersionMessage; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public $db = null; |
| 90 |
|
| 91 |
|
| 92 |
private $cachedConn; |
| 93 |
|
| 94 |
private $cachedConnType; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
protected $internalDefaults = []; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
protected $globalNames = []; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static function meetsMinimumRequirement( IDatabase $conn ) { |
| 118 |
$serverVersion = $conn->getServerVersion(); |
| 119 |
if ( version_compare( $serverVersion, static::$minimumVersion ) < 0 ) { |
| 120 |
return Status::newFatal( |
| 121 |
static::$notMinimumVersionMessage, static::$minimumVersion, $serverVersion |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
return Status::newGood(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
abstract public function getName(); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
abstract public function isCompiled(); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function checkPrerequisites() { |
| 144 |
return Status::newGood(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
abstract protected function openConnection( string $type ); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
abstract public function setupDatabase(); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function getConnection( $type = self::CONN_DONT_KNOW ) { |
| 180 |
if ( $type === self::CONN_DONT_KNOW ) { |
| 181 |
if ( $this->cachedConnType ) { |
| 182 |
$type = $this->cachedConnType; |
| 183 |
} else { |
| 184 |
$type = self::CONN_CREATE_DATABASE; |
| 185 |
} |
| 186 |
} |
| 187 |
if ( $this->cachedConn ) { |
| 188 |
if ( $this->cachedConnType === $type ) { |
| 189 |
return new ConnectionStatus( $this->cachedConn ); |
| 190 |
} else { |
| 191 |
return $this->changeConnType( $this->cachedConn, $this->cachedConnType, $type ); |
| 192 |
} |
| 193 |
} |
| 194 |
$status = $this->openConnection( $type ); |
| 195 |
if ( $status->isOK() ) { |
| 196 |
$this->cachedConn = $status->getDB(); |
| 197 |
$this->cachedConnType = $type; |
| 198 |
|
| 199 |
$this->db = $this->cachedConn; |
| 200 |
|
| 201 |
if ( $type === self::CONN_CREATE_SCHEMA || $type === self::CONN_CREATE_TABLES ) { |
| 202 |
$this->cachedConn->setSchemaVars( $this->getSchemaVars() ); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return $status; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function definitelyGetConnection( string $type ): Database { |
| 217 |
$status = $this->getConnection( $type ); |
| 218 |
if ( !$status->isOK() ) { |
| 219 |
throw new RuntimeException( __METHOD__ . ': unexpected DB connection error' ); |
| 220 |
} |
| 221 |
return $status->getDB(); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
protected function changeConnType( Database $conn, &$storedType, $newType ) { |
| 248 |
|
| 249 |
if ( $storedType === self::CONN_CREATE_DATABASE ) { |
| 250 |
if ( $newType === self::CONN_CREATE_SCHEMA || $newType === self::CONN_CREATE_TABLES ) { |
| 251 |
|
| 252 |
$conn->selectDomain( new DatabaseDomain( |
| 253 |
$this->getVar( 'wgDBname' ), |
| 254 |
$this->getVar( 'wgDBmwschema' ), |
| 255 |
$this->getVar( 'wgDBprefix' ) ?? '' |
| 256 |
) ); |
| 257 |
$conn->setSchemaVars( $this->getSchemaVars() ); |
| 258 |
$storedType = self::CONN_CREATE_SCHEMA; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if ( $newType === self::CONN_CREATE_TABLES && $storedType === self::CONN_CREATE_SCHEMA ) { |
| 263 |
$status = $this->changeConnTypeFromSchemaToTables( $conn ); |
| 264 |
if ( $status->isOK() ) { |
| 265 |
$storedType = self::CONN_CREATE_TABLES; |
| 266 |
} |
| 267 |
return $status; |
| 268 |
} |
| 269 |
return new ConnectionStatus( $conn ); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
protected function changeConnTypeFromSchemaToTables( Database $conn ) { |
| 280 |
return new ConnectionStatus( $conn ); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
private function applySourceFile( $conn, $sqlFile ) { |
| 291 |
$status = Status::newGood(); |
| 292 |
try { |
| 293 |
$conn->doAtomicSection( __METHOD__, |
| 294 |
static function ( $conn ) use ( $sqlFile ) { |
| 295 |
$conn->sourceFile( $sqlFile ); |
| 296 |
}, |
| 297 |
IDatabase::ATOMIC_CANCELABLE |
| 298 |
); |
| 299 |
} catch ( DBQueryError $e ) { |
| 300 |
$status->fatal( "config-install-tables-failed", $e->getMessage() ); |
| 301 |
} |
| 302 |
return $status; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public function createTables() { |
| 311 |
$status = $this->getConnection( self::CONN_CREATE_TABLES ); |
| 312 |
if ( !$status->isOK() ) { |
| 313 |
return $status; |
| 314 |
} |
| 315 |
$conn = $status->getDB(); |
| 316 |
if ( $conn->tableExists( 'archive', __METHOD__ ) ) { |
| 317 |
$status->warning( "config-install-tables-exist" ); |
| 318 |
return $status; |
| 319 |
} |
| 320 |
$status = $this->applySourceFile( $conn, |
| 321 |
$this->getSqlFilePath( 'tables-generated.sql' ) ); |
| 322 |
if ( !$status->isOK() ) { |
| 323 |
return $status; |
| 324 |
} |
| 325 |
$status->merge( $this->applySourceFile( $conn, |
| 326 |
$this->getSqlFilePath( 'tables.sql' ) ) ); |
| 327 |
return $status; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public function insertUpdateKeys() { |
| 336 |
$updater = DatabaseUpdater::newForDB( |
| 337 |
$this->definitelyGetConnection( self::CONN_CREATE_TABLES ) ); |
| 338 |
$updater->insertInitialUpdateKeys(); |
| 339 |
return Status::newGood(); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
private function getSqlFilePath( string $filename ) { |
| 350 |
global $IP; |
| 351 |
|
| 352 |
$dbmsSpecificFilePath = "$IP/maintenance/" . $this->getName() . "/$filename"; |
| 353 |
if ( file_exists( $dbmsSpecificFilePath ) ) { |
| 354 |
return $dbmsSpecificFilePath; |
| 355 |
} else { |
| 356 |
return "$IP/maintenance/$filename"; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public function createExtensionTables() { |
| 365 |
$status = $this->getConnection( self::CONN_CREATE_TABLES ); |
| 366 |
if ( !$status->isOK() ) { |
| 367 |
return $status; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
$updater = DatabaseUpdater::newForDB( $status->getDB() ); |
| 372 |
$updater->setAutoExtensionHookContainer( $this->parent->getAutoExtensionHookContainer() ); |
| 373 |
$updater->doUpdates( [ 'extensions' ] ); |
| 374 |
|
| 375 |
return $status; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
abstract public function getLocalSettings(); |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public function getSchemaVars() { |
| 391 |
return []; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public function enableLB() { |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public function preInstall() { |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public function preUpgrade() { |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public function getGlobalNames() { |
| 419 |
return $this->globalNames; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public function __construct( $parent ) { |
| 428 |
$this->parent = $parent; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
protected static function checkExtension( $name ) { |
| 439 |
return extension_loaded( $name ); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
public function getReadableName() { |
| 447 |
|
| 448 |
return wfMessage( 'config-type-' . $this->getName() )->text(); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public function getGlobalDefaults() { |
| 457 |
$defaults = []; |
| 458 |
foreach ( $this->getGlobalNames() as $var ) { |
| 459 |
if ( isset( $GLOBALS[$var] ) ) { |
| 460 |
$defaults[$var] = $GLOBALS[$var]; |
| 461 |
} |
| 462 |
} |
| 463 |
return $defaults; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public function getInternalDefaults() { |
| 471 |
return $this->internalDefaults; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
public function getVar( $var, $default = null ) { |
| 481 |
$defaults = $this->getGlobalDefaults(); |
| 482 |
$internal = $this->getInternalDefaults(); |
| 483 |
if ( isset( $defaults[$var] ) ) { |
| 484 |
$default = $defaults[$var]; |
| 485 |
} elseif ( isset( $internal[$var] ) ) { |
| 486 |
$default = $internal[$var]; |
| 487 |
} |
| 488 |
|
| 489 |
return $this->parent->getVar( $var, $default ); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public function setVar( $name, $value ) { |
| 498 |
$this->parent->setVar( $name, $value ); |
| 499 |
} |
| 500 |
|
| 501 |
abstract public function getConnectForm( WebInstaller $webInstaller ): DatabaseConnectForm; |
| 502 |
|
| 503 |
abstract public function getSettingsForm( WebInstaller $webInstaller ): DatabaseSettingsForm; |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public function needsUpgrade() { |
| 516 |
$status = $this->getConnection( self::CONN_CREATE_SCHEMA ); |
| 517 |
if ( !$status->isOK() ) { |
| 518 |
return false; |
| 519 |
} |
| 520 |
$db = $status->getDB(); |
| 521 |
return $db->tableExists( 'cur', __METHOD__ ) || |
| 522 |
$db->tableExists( 'revision', __METHOD__ ); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public function populateInterwikiTable() { |
| 531 |
$status = $this->getConnection( self::CONN_CREATE_TABLES ); |
| 532 |
if ( !$status->isOK() ) { |
| 533 |
return $status; |
| 534 |
} |
| 535 |
$conn = $status->getDB(); |
| 536 |
|
| 537 |
$row = $conn->newSelectQueryBuilder() |
| 538 |
->select( '1' ) |
| 539 |
->from( 'interwiki' ) |
| 540 |
->caller( __METHOD__ )->fetchRow(); |
| 541 |
if ( $row ) { |
| 542 |
$status->warning( 'config-install-interwiki-exists' ); |
| 543 |
|
| 544 |
return $status; |
| 545 |
} |
| 546 |
global $IP; |
| 547 |
AtEase::suppressWarnings(); |
| 548 |
$rows = file( "$IP/maintenance/interwiki.list", |
| 549 |
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ); |
| 550 |
AtEase::restoreWarnings(); |
| 551 |
if ( !$rows ) { |
| 552 |
return Status::newFatal( 'config-install-interwiki-list' ); |
| 553 |
} |
| 554 |
$insert = $conn->newInsertQueryBuilder() |
| 555 |
->insertInto( 'interwiki' ); |
| 556 |
foreach ( $rows as $row ) { |
| 557 |
$row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); |
| 558 |
if ( $row == "" ) { |
| 559 |
continue; |
| 560 |
} |
| 561 |
$row .= "|"; |
| 562 |
$insert->row( |
| 563 |
array_combine( |
| 564 |
[ 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ], |
| 565 |
explode( '|', $row ) |
| 566 |
) |
| 567 |
); |
| 568 |
} |
| 569 |
$insert->caller( __METHOD__ )->execute(); |
| 570 |
|
| 571 |
return Status::newGood(); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
protected function selectDatabase( Database $conn, string $database ) { |
| 581 |
$schema = $conn->dbSchema(); |
| 582 |
$prefix = $conn->tablePrefix(); |
| 583 |
|
| 584 |
$conn->selectDomain( new DatabaseDomain( |
| 585 |
$database, |
| 586 |
|
| 587 |
( $schema !== '' ) ? $schema : null, |
| 588 |
$prefix |
| 589 |
) ); |
| 590 |
|
| 591 |
return true; |
| 592 |
} |
| 593 |
} |