Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| AddWikiTaskContext | |
0.00% |
0 / 37 |
|
0.00% |
0 / 11 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getConfigVar | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOption | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getConnection | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
| getDomain | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getSchemaVars | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getDbType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| provide | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getProvision | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setConfigVar | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setOption | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Installer\Task; |
| 4 | |
| 5 | use MediaWiki\Config\Config; |
| 6 | use MediaWiki\Installer\ConnectionStatus; |
| 7 | use MediaWiki\MainConfigNames; |
| 8 | use Wikimedia\Rdbms\DatabaseDomain; |
| 9 | use Wikimedia\Rdbms\DBConnectionError; |
| 10 | use Wikimedia\Rdbms\IMaintainableDatabase; |
| 11 | use Wikimedia\Rdbms\LBFactory; |
| 12 | |
| 13 | /** |
| 14 | * A task context for use in installPreConfigured.php |
| 15 | * |
| 16 | * @internal |
| 17 | */ |
| 18 | class AddWikiTaskContext implements ITaskContext { |
| 19 | /** @var Config */ |
| 20 | private $config; |
| 21 | |
| 22 | /** @var LBFactory */ |
| 23 | private $lbFactory; |
| 24 | |
| 25 | /** @var array */ |
| 26 | private $configOverrides = []; |
| 27 | |
| 28 | /** @var array */ |
| 29 | private $options = []; |
| 30 | |
| 31 | /** @var array */ |
| 32 | private $provisions = []; |
| 33 | |
| 34 | public function __construct( Config $config, LBFactory $lbFactory ) { |
| 35 | $this->config = $config; |
| 36 | $this->lbFactory = $lbFactory; |
| 37 | } |
| 38 | |
| 39 | /** @inheritDoc */ |
| 40 | public function getConfigVar( string $name ) { |
| 41 | return $this->configOverrides[$name] ?? $this->config->get( $name ); |
| 42 | } |
| 43 | |
| 44 | /** @inheritDoc */ |
| 45 | public function getOption( string $name ) { |
| 46 | return $this->options[$name] ?? null; |
| 47 | } |
| 48 | |
| 49 | /** @inheritDoc */ |
| 50 | public function getConnection( $type = self::CONN_DONT_KNOW ): ConnectionStatus { |
| 51 | $localDomainID = $this->getDomain()->getId(); |
| 52 | $lb = $this->lbFactory->getLoadBalancer( $localDomainID ); |
| 53 | if ( $type === self::CONN_CREATE_DATABASE || $type === self::CONN_CREATE_SCHEMA ) { |
| 54 | $connectDomain = ''; |
| 55 | } else { |
| 56 | $connectDomain = $localDomainID; |
| 57 | } |
| 58 | |
| 59 | $status = new ConnectionStatus; |
| 60 | try { |
| 61 | $conn = $lb->getConnection( DB_PRIMARY, [], $connectDomain ); |
| 62 | if ( $conn instanceof IMaintainableDatabase ) { |
| 63 | $status->setDB( $conn ); |
| 64 | } else { |
| 65 | throw new \RuntimeException( 'Invalid DB connection class' ); |
| 66 | } |
| 67 | $conn->setSchemaVars( $this->getSchemaVars() ); |
| 68 | } catch ( DBConnectionError $e ) { |
| 69 | $status->fatal( 'config-connection-error', $e->getMessage() ); |
| 70 | } |
| 71 | |
| 72 | return $status; |
| 73 | } |
| 74 | |
| 75 | private function getDomain(): DatabaseDomain { |
| 76 | return new DatabaseDomain( |
| 77 | $this->getConfigVar( MainConfigNames::DBname ), |
| 78 | $this->getConfigVar( MainConfigNames::DBmwschema ), |
| 79 | $this->getConfigVar( MainConfigNames::DBprefix ) ?? '' |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get schema variable replacements to be applied to SQL files |
| 85 | * |
| 86 | * @return array |
| 87 | */ |
| 88 | public function getSchemaVars() { |
| 89 | $tableOptions = $this->getConfigVar( MainConfigNames::DBTableOptions ); |
| 90 | if ( str_contains( $tableOptions, 'TYPE=' ) ) { |
| 91 | throw new \RuntimeException( '$wgDBTableOptions contains obsolete TYPE option, ' . |
| 92 | 'replace it with ENGINE' ); |
| 93 | } |
| 94 | if ( str_contains( $tableOptions, 'CHARSET=mysql4' ) ) { |
| 95 | throw new \RuntimeException( '$wgDBTableOptions contains invalid CHARSET option' ); |
| 96 | } |
| 97 | return [ 'wgDBTableOptions' => $tableOptions ]; |
| 98 | } |
| 99 | |
| 100 | public function getDbType(): string { |
| 101 | return $this->getConfigVar( MainConfigNames::DBtype ); |
| 102 | } |
| 103 | |
| 104 | /** @inheritDoc */ |
| 105 | public function provide( string $name, $value ) { |
| 106 | $this->provisions[$name] = $value; |
| 107 | } |
| 108 | |
| 109 | /** @inheritDoc */ |
| 110 | public function getProvision( string $name ) { |
| 111 | if ( isset( $this->provisions[$name] ) ) { |
| 112 | return $this->provisions[$name]; |
| 113 | } else { |
| 114 | throw new \RuntimeException( "Can't find provided data \"$name\"" ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Override a configuration variable |
| 120 | * |
| 121 | * @param string $name |
| 122 | * @param mixed $value |
| 123 | */ |
| 124 | public function setConfigVar( string $name, $value ) { |
| 125 | $this->configOverrides[$name] = $value; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Set an installer option |
| 130 | * |
| 131 | * @param string $name |
| 132 | * @param mixed $value |
| 133 | */ |
| 134 | public function setOption( string $name, $value ) { |
| 135 | $this->options[$name] = $value; |
| 136 | } |
| 137 | |
| 138 | } |