Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
AddWikiTaskContext | |
0.00% |
0 / 38 |
|
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 / 15 |
|
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 | public function getConfigVar( string $name ) { |
40 | return $this->configOverrides[$name] ?? $this->config->get( $name ); |
41 | } |
42 | |
43 | public function getOption( string $name ) { |
44 | return $this->options[$name] ?? null; |
45 | } |
46 | |
47 | public function getConnection( $type = self::CONN_DONT_KNOW ): ConnectionStatus { |
48 | $localDomain = $this->getDomain(); |
49 | $lb = $this->lbFactory->getLoadBalancer( $localDomain ); |
50 | if ( $type === self::CONN_CREATE_DATABASE || $type === self::CONN_CREATE_SCHEMA ) { |
51 | $connectDomain = ''; |
52 | } else { |
53 | $connectDomain = $localDomain->getId(); |
54 | } |
55 | |
56 | $status = new ConnectionStatus; |
57 | try { |
58 | $conn = $lb->getConnection( DB_PRIMARY, [], $connectDomain ); |
59 | if ( $conn instanceof IMaintainableDatabase ) { |
60 | $status->setDB( $conn ); |
61 | } else { |
62 | throw new \RuntimeException( 'Invalid DB connection class' ); |
63 | } |
64 | $conn->setSchemaVars( $this->getSchemaVars() ); |
65 | } catch ( DBConnectionError $e ) { |
66 | $status->fatal( 'config-connection-error', $e->getMessage() ); |
67 | } |
68 | |
69 | return $status; |
70 | } |
71 | |
72 | private function getDomain(): DatabaseDomain { |
73 | return new DatabaseDomain( |
74 | $this->getConfigVar( MainConfigNames::DBname ), |
75 | $this->getConfigVar( MainConfigNames::DBmwschema ), |
76 | $this->getConfigVar( MainConfigNames::DBprefix ) ?? '' |
77 | ); |
78 | } |
79 | |
80 | /** |
81 | * Get schema variable replacements to be applied to SQL files |
82 | * |
83 | * @return array |
84 | */ |
85 | public function getSchemaVars() { |
86 | $tableOptions = $this->getConfigVar( MainConfigNames::DBTableOptions ); |
87 | if ( str_contains( $tableOptions, 'TYPE=' ) ) { |
88 | throw new \RuntimeException( '$wgDBTableOptions contains obsolete TYPE option, ' . |
89 | 'replace it with ENGINE' ); |
90 | } |
91 | if ( str_contains( $tableOptions, 'CHARSET=mysql4' ) ) { |
92 | throw new \RuntimeException( '$wgDBTableOptions contains invalid CHARSET option' ); |
93 | } |
94 | return [ 'wgDBTableOptions' => $tableOptions ]; |
95 | } |
96 | |
97 | public function getDbType(): string { |
98 | return $this->getConfigVar( MainConfigNames::DBtype ); |
99 | } |
100 | |
101 | public function provide( string $name, $value ) { |
102 | $this->provisions[$name] = $value; |
103 | } |
104 | |
105 | public function getProvision( string $name ) { |
106 | if ( isset( $this->provisions[$name] ) ) { |
107 | return $this->provisions[$name]; |
108 | } else { |
109 | throw new \RuntimeException( "Can't find provided data \"$name\"" ); |
110 | } |
111 | } |
112 | |
113 | /** |
114 | * Override a configuration variable |
115 | * |
116 | * @param string $name |
117 | * @param mixed $value |
118 | */ |
119 | public function setConfigVar( string $name, $value ) { |
120 | $this->configOverrides[$name] = $value; |
121 | } |
122 | |
123 | /** |
124 | * Set an installer option |
125 | * |
126 | * @param string $name |
127 | * @param mixed $value |
128 | */ |
129 | public function setOption( string $name, $value ) { |
130 | $this->options[$name] = $value; |
131 | } |
132 | |
133 | } |