Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 127 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
CliInstaller | |
0.00% |
0 / 127 |
|
0.00% |
0 / 14 |
1892 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 74 |
|
0.00% |
0 / 1 |
380 | |||
validateExtensions | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
execute | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
writeConfigurationFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
startStage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
endStage | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
showMessage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
showError | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getMessageText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatMessage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
showStatusMessage | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
envCheckPath | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
envGetDefaultServer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
dirIsExecutable | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * http://www.gnu.org/copyleft/gpl.html |
18 | * |
19 | * @file |
20 | * @ingroup Installer |
21 | */ |
22 | |
23 | namespace MediaWiki\Installer; |
24 | |
25 | use MediaWiki\Context\RequestContext; |
26 | use MediaWiki\Installer\Task\Task; |
27 | use MediaWiki\MediaWikiServices; |
28 | use MediaWiki\Message\Message; |
29 | use MediaWiki\Parser\Sanitizer; |
30 | use MediaWiki\Password\UserPasswordPolicy; |
31 | use MediaWiki\Status\Status; |
32 | use MediaWiki\User\User; |
33 | use Wikimedia\Message\MessageSpecifier; |
34 | |
35 | /** |
36 | * Class for the core installer command line interface. |
37 | * |
38 | * @ingroup Installer |
39 | * @since 1.17 |
40 | */ |
41 | class CliInstaller extends Installer { |
42 | /** @var bool */ |
43 | private $specifiedScriptPath = false; |
44 | |
45 | private const OPTION_MAP = [ |
46 | 'dbtype' => 'wgDBtype', |
47 | 'dbserver' => 'wgDBserver', |
48 | 'dbname' => 'wgDBname', |
49 | 'dbuser' => 'wgDBuser', |
50 | 'dbpass' => 'wgDBpassword', |
51 | 'dbprefix' => 'wgDBprefix', |
52 | 'dbtableoptions' => 'wgDBTableOptions', |
53 | 'dbport' => 'wgDBport', |
54 | 'dbssl' => 'wgDBssl', |
55 | 'dbschema' => 'wgDBmwschema', |
56 | 'dbpath' => 'wgSQLiteDataDir', |
57 | 'server' => 'wgServer', |
58 | 'scriptpath' => 'wgScriptPath', |
59 | ]; |
60 | |
61 | /** |
62 | * @param string $siteName |
63 | * @param string|null $admin |
64 | * @param array $options |
65 | * @throws InstallException |
66 | */ |
67 | public function __construct( $siteName, $admin = null, array $options = [] ) { |
68 | global $wgPasswordPolicy; |
69 | |
70 | parent::__construct(); |
71 | |
72 | if ( isset( $options['scriptpath'] ) ) { |
73 | $this->specifiedScriptPath = true; |
74 | } |
75 | |
76 | foreach ( self::OPTION_MAP as $opt => $global ) { |
77 | if ( isset( $options[$opt] ) ) { |
78 | $GLOBALS[$global] = $options[$opt]; |
79 | $this->setVar( $global, $options[$opt] ); |
80 | } |
81 | } |
82 | |
83 | if ( isset( $options['lang'] ) ) { |
84 | global $wgLang, $wgLanguageCode; |
85 | $this->setVar( '_UserLang', $options['lang'] ); |
86 | $wgLanguageCode = $options['lang']; |
87 | $this->setVar( 'wgLanguageCode', $wgLanguageCode ); |
88 | $wgLang = MediaWikiServices::getInstance()->getLanguageFactory() |
89 | ->getLanguage( $options['lang'] ); |
90 | RequestContext::getMain()->setLanguage( $wgLang ); |
91 | } |
92 | |
93 | $this->setVar( 'wgSitename', $siteName ); |
94 | |
95 | $contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
96 | $metaNS = $contLang->ucfirst( str_replace( ' ', '_', $siteName ) ); |
97 | if ( $metaNS == 'MediaWiki' ) { |
98 | $metaNS = 'Project'; |
99 | } |
100 | $this->setVar( 'wgMetaNamespace', $metaNS ); |
101 | |
102 | if ( !isset( $options['installdbuser'] ) ) { |
103 | $this->setVar( '_InstallUser', |
104 | $this->getVar( 'wgDBuser' ) ); |
105 | $this->setVar( '_InstallPassword', |
106 | $this->getVar( 'wgDBpassword' ) ); |
107 | } else { |
108 | $this->setVar( '_InstallUser', |
109 | $options['installdbuser'] ); |
110 | $this->setVar( '_InstallPassword', |
111 | $options['installdbpass'] ?? "" ); |
112 | |
113 | // Assume that if we're given the installer user, we'll create the account. |
114 | $this->setVar( '_CreateDBAccount', true ); |
115 | } |
116 | |
117 | if ( $admin ) { |
118 | $this->setVar( '_AdminName', $admin ); |
119 | if ( isset( $options['pass'] ) ) { |
120 | $adminUser = User::newFromName( $admin ); |
121 | if ( !$adminUser ) { |
122 | throw new InstallException( Status::newFatal( 'config-admin-name-invalid' ) ); |
123 | } |
124 | $upp = new UserPasswordPolicy( |
125 | $wgPasswordPolicy['policies'], |
126 | $wgPasswordPolicy['checks'] |
127 | ); |
128 | $status = $upp->checkUserPasswordForGroups( $adminUser, $options['pass'], |
129 | [ 'bureaucrat', 'sysop', 'interface-admin' ] ); // per Installer::createSysop() |
130 | if ( !$status->isGood() ) { |
131 | throw new InstallException( Status::newFatal( |
132 | $status->getMessage( 'config-admin-error-password-invalid' ) ) ); |
133 | } |
134 | $this->setVar( '_AdminPassword', $options['pass'] ); |
135 | } |
136 | } |
137 | |
138 | // Detect and inject any extension found |
139 | if ( isset( $options['extensions'] ) ) { |
140 | $status = $this->validateExtensions( |
141 | 'extension', 'extensions', $options['extensions'] ); |
142 | if ( !$status->isOK() ) { |
143 | throw new InstallException( $status ); |
144 | } |
145 | $this->setVar( '_Extensions', $status->value ); |
146 | } elseif ( isset( $options['with-extensions'] ) ) { |
147 | $status = $this->findExtensions(); |
148 | if ( !$status->isOK() ) { |
149 | throw new InstallException( $status ); |
150 | } |
151 | $this->setVar( '_Extensions', array_keys( $status->value ) ); |
152 | } |
153 | |
154 | // Set up the default skins |
155 | if ( isset( $options['skins'] ) ) { |
156 | $status = $this->validateExtensions( 'skin', 'skins', $options['skins'] ); |
157 | if ( !$status->isOK() ) { |
158 | throw new InstallException( $status ); |
159 | } |
160 | $skins = $status->value; |
161 | } else { |
162 | $status = $this->findExtensions( 'skins' ); |
163 | if ( !$status->isOK() ) { |
164 | throw new InstallException( $status ); |
165 | } |
166 | $skins = array_keys( $status->value ); |
167 | } |
168 | $this->setVar( '_Skins', $skins ); |
169 | |
170 | if ( $skins ) { |
171 | $skinNames = array_map( 'strtolower', $skins ); |
172 | $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) ); |
173 | } |
174 | |
175 | $this->setVar( '_WithDevelopmentSettings', isset( $options['with-developmentsettings'] ) ); |
176 | } |
177 | |
178 | private function validateExtensions( $type, $directory, $nameLists ) { |
179 | $extensions = []; |
180 | $status = new Status; |
181 | foreach ( (array)$nameLists as $nameList ) { |
182 | foreach ( explode( ',', $nameList ) as $name ) { |
183 | $name = trim( $name ); |
184 | if ( $name === '' ) { |
185 | continue; |
186 | } |
187 | $extStatus = $this->getExtensionInfo( $type, $directory, $name ); |
188 | if ( $extStatus->isOK() ) { |
189 | $extensions[] = $name; |
190 | } else { |
191 | $status->merge( $extStatus ); |
192 | } |
193 | } |
194 | } |
195 | $extensions = array_unique( $extensions ); |
196 | $status->value = $extensions; |
197 | return $status; |
198 | } |
199 | |
200 | /** |
201 | * Main entry point. |
202 | * @return Status |
203 | */ |
204 | public function execute() { |
205 | // If APC is available, use that as the MainCacheType, instead of nothing. |
206 | // This is hacky and should be consolidated with WebInstallerOptions. |
207 | // This is here instead of in __construct(), because it should run after |
208 | // doEnvironmentChecks(), which populates '_Caches'. |
209 | if ( count( $this->getVar( '_Caches' ) ) ) { |
210 | // We detected a CACHE_ACCEL implementation, use it. |
211 | $this->setVar( '_MainCacheType', 'accel' ); |
212 | } |
213 | |
214 | $vars = Installer::getExistingLocalSettings(); |
215 | if ( $vars ) { |
216 | $status = Status::newFatal( "config-localsettings-cli-upgrade" ); |
217 | $this->showStatusMessage( $status ); |
218 | return $status; |
219 | } |
220 | |
221 | $status = $this->performInstallation( |
222 | [ $this, 'startStage' ], |
223 | [ $this, 'endStage' ] |
224 | ); |
225 | if ( $status->isOK() ) { |
226 | return Status::newGood(); |
227 | } else { |
228 | return $status; |
229 | } |
230 | } |
231 | |
232 | /** |
233 | * Write LocalSettings.php to a given path |
234 | * |
235 | * @param string $path Full path to write LocalSettings.php to |
236 | */ |
237 | public function writeConfigurationFile( $path ) { |
238 | $ls = InstallerOverrides::getLocalSettingsGenerator( $this ); |
239 | $ls->writeFile( "$path/LocalSettings.php" ); |
240 | } |
241 | |
242 | /** |
243 | * @param Task $task |
244 | */ |
245 | public function startStage( $task ) { |
246 | // @phan-suppress-next-line SecurityCheck-XSS -- it's CLI |
247 | echo $this->formatMessage( $task->getDescriptionMessage() ) . '... '; |
248 | } |
249 | |
250 | /** |
251 | * @param Task $task |
252 | * @param Status $status |
253 | */ |
254 | public function endStage( $task, $status ) { |
255 | $this->showStatusMessage( $status ); |
256 | if ( $status->isOK() ) { |
257 | $this->showMessage( 'config-install-step-done' ); |
258 | } else { |
259 | $this->showError( 'config-install-step-failed' ); |
260 | } |
261 | } |
262 | |
263 | public function showMessage( $msg, ...$params ) { |
264 | // @phan-suppress-next-line SecurityCheck-XSS |
265 | echo $this->getMessageText( $msg, $params ) . "\n"; |
266 | flush(); |
267 | } |
268 | |
269 | public function showError( $msg, ...$params ) { |
270 | // @phan-suppress-next-line SecurityCheck-XSS |
271 | echo "***{$this->getMessageText( $msg, $params )}***\n"; |
272 | flush(); |
273 | } |
274 | |
275 | /** |
276 | * @param string|MessageSpecifier $msg |
277 | * @param (string|int|float)[] $params Message parameters |
278 | * @return string |
279 | */ |
280 | protected function getMessageText( $msg, $params ) { |
281 | return $this->formatMessage( wfMessage( $msg, $params ) ); |
282 | } |
283 | |
284 | /** |
285 | * @param Message $message |
286 | * @return string |
287 | */ |
288 | protected function formatMessage( $message ) { |
289 | $text = $message->parse(); |
290 | $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 <$1>', $text ); |
291 | return Sanitizer::stripAllTags( $text ); |
292 | } |
293 | |
294 | public function showStatusMessage( Status $status ) { |
295 | // Show errors at the end in CLI installer to make them easier to notice |
296 | foreach ( $status->getMessages( 'warning' ) as $msg ) { |
297 | $this->showMessage( $msg ); |
298 | } |
299 | foreach ( $status->getMessages( 'error' ) as $msg ) { |
300 | $this->showMessage( $msg ); |
301 | } |
302 | } |
303 | |
304 | public function envCheckPath() { |
305 | if ( !$this->specifiedScriptPath ) { |
306 | $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) ); |
307 | } |
308 | |
309 | return parent::envCheckPath(); |
310 | } |
311 | |
312 | protected function envGetDefaultServer() { |
313 | // Use a basic value if the user didn't pass in --server |
314 | return 'http://localhost'; |
315 | } |
316 | |
317 | public function dirIsExecutable( $dir, $url ) { |
318 | $this->showMessage( 'config-no-cli-uploads-check', $dir ); |
319 | |
320 | return false; |
321 | } |
322 | } |