1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
|
<?php
/**
* DBMS-specific installation helper.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Installer
*/
namespace MediaWiki\Installer;
use MediaWiki\Status\Status;
use RuntimeException;
use Wikimedia\AtEase\AtEase;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\DatabaseDomain;
use Wikimedia\Rdbms\DBQueryError;
use Wikimedia\Rdbms\IDatabase;
/**
* Base class for DBMS-specific installation helper classes.
*
* @ingroup Installer
* @since 1.17
*/
abstract class DatabaseInstaller {
/**
* A connection for creating DBs, suitable for pre-installation.
*/
public const CONN_CREATE_DATABASE = 'create-database';
/**
* A connection to the new DB, for creating schemas and other similar
* objects in the new DB.
*/
public const CONN_CREATE_SCHEMA = 'create-schema';
/**
* A connection with a role suitable for creating tables.
*/
public const CONN_CREATE_TABLES = 'create-tables';
/**
* Legacy default connection type. Before MW 1.43, getConnection() with no
* parameters would return the cached connection. The state (especially the
* selected domain) would depend on the previously executed install steps.
* Using this constant tries to reproduce this behaviour.
*
* @deprecated since 1.43
*/
public const CONN_DONT_KNOW = 'dont-know';
/**
* The Installer object.
*
* @var Installer
*/
public $parent;
/**
* @var string Set by subclasses
*/
public static $minimumVersion;
/**
* @var string Set by subclasses
*/
protected static $notMinimumVersionMessage;
/**
* @deprecated since 1.43 -- use definitelyGetConnection()
* @var Database
*/
public $db = null;
/** @var Database|null */
private $cachedConn;
/** @var string|null */
private $cachedConnType;
/**
* Internal variables for installation.
*
* @var array
*/
protected $internalDefaults = [];
/**
* Array of MW configuration globals this class uses.
*
* @var array
*/
protected $globalNames = [];
/**
* Whether the provided version meets the necessary requirements for this type
*
* @param IDatabase $conn
* @return Status
* @since 1.30
*/
public static function meetsMinimumRequirement( IDatabase $conn ) {
$serverVersion = $conn->getServerVersion();
if ( version_compare( $serverVersion, static::$minimumVersion ) < 0 ) {
return Status::newFatal(
static::$notMinimumVersionMessage, static::$minimumVersion, $serverVersion
);
}
return Status::newGood();
}
/**
* Return the internal name, e.g. 'mysql', or 'sqlite'.
*/
abstract public function getName();
/**
* @return bool Returns true if the client library is compiled in.
*/
abstract public function isCompiled();
/**
* Checks for installation prerequisites other than those checked by isCompiled()
* @since 1.19
* @return Status
*/
public function checkPrerequisites() {
return Status::newGood();
}
/**
* Open a connection to the database using the administrative user/password
* currently defined in the session, without any caching. Returns a status
* object. On success, the status object will contain a Database object in
* its value member.
*
* The database should not be implicitly created.
*
* @param string $type One of the self::CONN_* constants, except CONN_DONT_KNOW
* @return ConnectionStatus
*/
abstract protected function openConnection( string $type );
/**
* Create the database and return a Status object indicating success or
* failure.
*
* @return Status
*/
abstract public function setupDatabase();
/**
* Connect to the database using the administrative user/password currently
* defined in the session. Returns a status object. On success, the status
* object will contain a Database object in its value member.
*
* This will return a cached connection if one is available.
*
* @param string $type One of the self::CONN_* constants. Using CONN_DONT_KNOW
* is deprecated and will cause an exception to be thrown in a future release.
* @return ConnectionStatus
*/
public function getConnection( $type = self::CONN_DONT_KNOW ) {
if ( $type === self::CONN_DONT_KNOW ) {
if ( $this->cachedConnType ) {
$type = $this->cachedConnType;
} else {
$type = self::CONN_CREATE_DATABASE;
}
}
if ( $this->cachedConn ) {
if ( $this->cachedConnType === $type ) {
return new ConnectionStatus( $this->cachedConn );
} else {
return $this->changeConnType( $this->cachedConn, $this->cachedConnType, $type );
}
}
$status = $this->openConnection( $type );
if ( $status->isOK() ) {
$this->cachedConn = $status->getDB();
$this->cachedConnType = $type;
// Assign to $this->db for b/c
$this->db = $this->cachedConn;
if ( $type === self::CONN_CREATE_SCHEMA || $type === self::CONN_CREATE_TABLES ) {
$this->cachedConn->setSchemaVars( $this->getSchemaVars() );
}
}
return $status;
}
/**
* Get a connection and unwrap it from its Status object, throwing an
* exception on failure.
*
* @param string $type
* @return Database
*/
public function definitelyGetConnection( string $type ): Database {
$status = $this->getConnection( $type );
if ( !$status->isOK() ) {
throw new RuntimeException( __METHOD__ . ': unexpected DB connection error' );
}
return $status->getDB();
}
/**
* Change the type of a connection.
*
* CONN_CREATE_DATABASE means the domain is indeterminate and irrelevant,
* so converting from this type can be done by selecting the domain, and
* converting to it is a no-op.
*
* CONN_CREATE_SCHEMA means the domain is correct but tables created by
* PostgreSQL will have the incorrect role. So to convert from this to
* CONN_CREATE_TABLES, we set the role.
*
* CONN_CREATE_TABLES means a fully-configured connection, suitable for
* most tasks, so converting from it is a no-op.
*
* @param Database $conn
* @param string &$storedType One of the self::CONN_* constants. An in/out
* parameter, set to the new type on success. It is set to the "real" new
* type, reflecting the highest configuration level reached, to avoid
* unnecessary selectDomain() calls when we need to temporarily give an
* unconfigured connection.
* @param string $newType One of the self::CONN_* constants
* @return ConnectionStatus
*/
protected function changeConnType( Database $conn, &$storedType, $newType ) {
// Change type from database to schema, if requested
if ( $storedType === self::CONN_CREATE_DATABASE ) {
if ( $newType === self::CONN_CREATE_SCHEMA || $newType === self::CONN_CREATE_TABLES ) {
// TODO: catch exceptions from selectDomain and report as a Status
$conn->selectDomain( new DatabaseDomain(
$this->getVar( 'wgDBname' ),
$this->getVar( 'wgDBmwschema' ),
$this->getVar( 'wgDBprefix' ) ?? ''
) );
$conn->setSchemaVars( $this->getSchemaVars() );
$storedType = self::CONN_CREATE_SCHEMA;
}
}
// Change type from schema to tables, if requested
if ( $newType === self::CONN_CREATE_TABLES && $storedType === self::CONN_CREATE_SCHEMA ) {
$status = $this->changeConnTypeFromSchemaToTables( $conn );
if ( $status->isOK() ) {
$storedType = self::CONN_CREATE_TABLES;
}
return $status;
}
return new ConnectionStatus( $conn );
}
/**
* Change the type of a connection from CONN_CREATE_SCHEMA to CONN_CREATE_TABLES.
* Postgres overrides this.
*
* @param Database $conn
* @return ConnectionStatus
*/
protected function changeConnTypeFromSchemaToTables( Database $conn ) {
return new ConnectionStatus( $conn );
}
/**
* Apply a SQL source file to the database as part of running an installation step.
*
* @param Database $conn
* @param string $sqlFile
* @return Status
*/
private function applySourceFile( $conn, $sqlFile ) {
$status = Status::newGood();
try {
$conn->doAtomicSection( __METHOD__,
static function ( $conn ) use ( $sqlFile ) {
$conn->sourceFile( $sqlFile );
},
IDatabase::ATOMIC_CANCELABLE
);
} catch ( DBQueryError $e ) {
$status->fatal( "config-install-tables-failed", $e->getMessage() );
}
return $status;
}
/**
* Create database tables from scratch from the automatically generated file
*
* @return Status
*/
public function createTables() {
$status = $this->getConnection( self::CONN_CREATE_TABLES );
if ( !$status->isOK() ) {
return $status;
}
$conn = $status->getDB();
if ( $conn->tableExists( 'archive', __METHOD__ ) ) {
$status->warning( "config-install-tables-exist" );
return $status;
}
$status = $this->applySourceFile( $conn,
$this->getSqlFilePath( 'tables-generated.sql' ) );
if ( !$status->isOK() ) {
return $status;
}
$status->merge( $this->applySourceFile( $conn,
$this->getSqlFilePath( 'tables.sql' ) ) );
return $status;
}
/**
* Insert update keys into table to prevent running unneeded updates.
*
* @return Status
*/
public function insertUpdateKeys() {
$updater = DatabaseUpdater::newForDB(
$this->definitelyGetConnection( self::CONN_CREATE_TABLES ) );
$updater->insertInitialUpdateKeys();
return Status::newGood();
}
/**
* Return a path to the DBMS-specific SQL file if it exists,
* otherwise default SQL file
*
* @param string $filename
* @return string
*/
private function getSqlFilePath( string $filename ) {
global $IP;
$dbmsSpecificFilePath = "$IP/maintenance/" . $this->getName() . "/$filename";
if ( file_exists( $dbmsSpecificFilePath ) ) {
return $dbmsSpecificFilePath;
} else {
return "$IP/maintenance/$filename";
}
}
/**
* Create the tables for each extension the user enabled
* @return Status
*/
public function createExtensionTables() {
$status = $this->getConnection( self::CONN_CREATE_TABLES );
if ( !$status->isOK() ) {
return $status;
}
// Now run updates to create tables for old extensions
$updater = DatabaseUpdater::newForDB( $status->getDB() );
$updater->setAutoExtensionHookContainer( $this->parent->getAutoExtensionHookContainer() );
$updater->doUpdates( [ 'extensions' ] );
return $status;
}
/**
* Get the DBMS-specific options for LocalSettings.php generation.
*
* @return string
*/
abstract public function getLocalSettings();
/**
* Override this to provide DBMS-specific schema variables, to be
* substituted into tables.sql and other schema files.
* @return array
*/
public function getSchemaVars() {
return [];
}
/**
* @deprecated since 1.43
*/
public function enableLB() {
}
/**
* Allow DB installers a chance to make last-minute changes before installation
* occurs. This happens before setupDatabase() or createTables() is called, but
* long after the constructor. Helpful for things like modifying setup steps :)
*/
public function preInstall() {
}
/**
* Allow DB installers a chance to make checks before upgrade.
*/
public function preUpgrade() {
}
/**
* Get an array of MW configuration globals that will be configured by this class.
* @return array
*/
public function getGlobalNames() {
return $this->globalNames;
}
/**
* Construct and initialise parent.
* This is typically only called from Installer::getDBInstaller()
* @param Installer $parent
*/
public function __construct( $parent ) {
$this->parent = $parent;
}
/**
* Convenience function.
* Check if a named extension is present.
*
* @param string $name
* @return bool
*/
protected static function checkExtension( $name ) {
return extension_loaded( $name );
}
/**
* Get the internationalised name for this DBMS.
* @return string
*/
public function getReadableName() {
// Messages: config-type-mysql, config-type-postgres, config-type-sqlite
return wfMessage( 'config-type-' . $this->getName() )->text();
}
/**
* Get a name=>value map of MW configuration globals for the default values.
* @return array
* @return-taint none
*/
public function getGlobalDefaults() {
$defaults = [];
foreach ( $this->getGlobalNames() as $var ) {
if ( isset( $GLOBALS[$var] ) ) {
$defaults[$var] = $GLOBALS[$var];
}
}
return $defaults;
}
/**
* Get a name=>value map of internal variables used during installation.
* @return array
*/
public function getInternalDefaults() {
return $this->internalDefaults;
}
/**
* Get a variable, taking local defaults into account.
* @param string $var
* @param mixed|null $default
* @return mixed
*/
public function getVar( $var, $default = null ) {
$defaults = $this->getGlobalDefaults();
$internal = $this->getInternalDefaults();
if ( isset( $defaults[$var] ) ) {
$default = $defaults[$var];
} elseif ( isset( $internal[$var] ) ) {
$default = $internal[$var];
}
return $this->parent->getVar( $var, $default );
}
/**
* Convenience alias for $this->parent->setVar()
* @param string $name
* @param mixed $value
*/
public function setVar( $name, $value ) {
$this->parent->setVar( $name, $value );
}
abstract public function getConnectForm( WebInstaller $webInstaller ): DatabaseConnectForm;
abstract public function getSettingsForm( WebInstaller $webInstaller ): DatabaseSettingsForm;
/**
* Determine whether an existing installation of MediaWiki is present in
* the configured administrative connection. Returns true if there is
* such a wiki, false if the database doesn't exist.
*
* Traditionally, this is done by testing for the existence of either
* the revision table or the cur table.
*
* @return bool
*/
public function needsUpgrade() {
$status = $this->getConnection( self::CONN_CREATE_SCHEMA );
if ( !$status->isOK() ) {
return false;
}
$db = $status->getDB();
return $db->tableExists( 'cur', __METHOD__ ) ||
$db->tableExists( 'revision', __METHOD__ );
}
/**
* Common function for databases that don't understand the MySQLish syntax of interwiki.list.
*
* @return Status
*/
public function populateInterwikiTable() {
$status = $this->getConnection( self::CONN_CREATE_TABLES );
if ( !$status->isOK() ) {
return $status;
}
$conn = $status->getDB();
$row = $conn->newSelectQueryBuilder()
->select( '1' )
->from( 'interwiki' )
->caller( __METHOD__ )->fetchRow();
if ( $row ) {
$status->warning( 'config-install-interwiki-exists' );
return $status;
}
global $IP;
AtEase::suppressWarnings();
$rows = file( "$IP/maintenance/interwiki.list",
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
AtEase::restoreWarnings();
if ( !$rows ) {
return Status::newFatal( 'config-install-interwiki-list' );
}
$insert = $conn->newInsertQueryBuilder()
->insertInto( 'interwiki' );
foreach ( $rows as $row ) {
$row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
if ( $row == "" ) {
continue;
}
$row .= "|";
$insert->row(
array_combine(
[ 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ],
explode( '|', $row )
)
);
}
$insert->caller( __METHOD__ )->execute();
return Status::newGood();
}
/**
* @param Database $conn
* @param string $database
* @return bool
* @since 1.39
*/
protected function selectDatabase( Database $conn, string $database ) {
$schema = $conn->dbSchema();
$prefix = $conn->tablePrefix();
$conn->selectDomain( new DatabaseDomain(
$database,
// DatabaseDomain uses null for unspecified schemas
( $schema !== '' ) ? $schema : null,
$prefix
) );
return true;
}
}
|