Debian Codegraph @

includes/installer/CliInstaller.php

raw symbols SCIP
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\MediaWikiServices;
27 use MediaWiki\Parser\Sanitizer;
28 use MediaWiki\Password\UserPasswordPolicy;
29 use MediaWiki\Status\Status;
30 use MediaWiki\User\User;
31 use Wikimedia\Message\MessageSpecifier;
32
33 /**
34 * Class for the core installer command line interface.
35 *
36 * @ingroup Installer
37 * @since 1.17
38 */
39 class CliInstaller extends Installer {
40 /** @var bool */
41 private $specifiedScriptPath = false;
42
43 private const OPTION_MAP = [
44 'dbtype' => 'wgDBtype',
45 'dbserver' => 'wgDBserver',
46 'dbname' => 'wgDBname',
47 'dbuser' => 'wgDBuser',
48 'dbpass' => 'wgDBpassword',
49 'dbprefix' => 'wgDBprefix',
50 'dbtableoptions' => 'wgDBTableOptions',
51 'dbport' => 'wgDBport',
52 'dbssl' => 'wgDBssl',
53 'dbschema' => 'wgDBmwschema',
54 'dbpath' => 'wgSQLiteDataDir',
55 'server' => 'wgServer',
56 'scriptpath' => 'wgScriptPath',
57 ];
58
59 /**
60 * @param string $siteName
61 * @param string|null $admin
62 * @param array $options
63 * @throws InstallException
64 */
65 public function __construct( $siteName, $admin = null, array $options = [] ) {
66 global $wgPasswordPolicy;
67
68 parent::__construct();
69
70 if ( isset( $options['scriptpath'] ) ) {
71 $this->specifiedScriptPath = true;
72 }
73
74 foreach ( self::OPTION_MAP as $opt => $global ) {
75 if ( isset( $options[$opt] ) ) {
76 $GLOBALS[$global] = $options[$opt];
77 $this->setVar( $global, $options[$opt] );
78 }
79 }
80
81 if ( isset( $options['lang'] ) ) {
82 global $wgLang, $wgLanguageCode;
83 $this->setVar( '_UserLang', $options['lang'] );
84 $wgLanguageCode = $options['lang'];
85 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
86 $wgLang = MediaWikiServices::getInstance()->getLanguageFactory()
87 ->getLanguage( $options['lang'] );
88 RequestContext::getMain()->setLanguage( $wgLang );
89 }
90
91 $this->setVar( 'wgSitename', $siteName );
92
93 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
94 $metaNS = $contLang->ucfirst( str_replace( ' ', '_', $siteName ) );
95 if ( $metaNS == 'MediaWiki' ) {
96 $metaNS = 'Project';
97 }
98 $this->setVar( 'wgMetaNamespace', $metaNS );
99
100 if ( !isset( $options['installdbuser'] ) ) {
101 $this->setVar( '_InstallUser',
102 $this->getVar( 'wgDBuser' ) );
103 $this->setVar( '_InstallPassword',
104 $this->getVar( 'wgDBpassword' ) );
105 } else {
106 $this->setVar( '_InstallUser',
107 $options['installdbuser'] );
108 $this->setVar( '_InstallPassword',
109 $options['installdbpass'] ?? "" );
110
111 // Assume that if we're given the installer user, we'll create the account.
112 $this->setVar( '_CreateDBAccount', true );
113 }
114
115 if ( $admin ) {
116 $this->setVar( '_AdminName', $admin );
117 if ( isset( $options['pass'] ) ) {
118 $adminUser = User::newFromName( $admin );
119 if ( !$adminUser ) {
120 throw new InstallException( Status::newFatal( 'config-admin-name-invalid' ) );
121 }
122 $upp = new UserPasswordPolicy(
123 $wgPasswordPolicy['policies'],
124 $wgPasswordPolicy['checks']
125 );
126 $status = $upp->checkUserPasswordForGroups( $adminUser, $options['pass'],
127 [ 'bureaucrat', 'sysop', 'interface-admin' ] ); // per Installer::createSysop()
128 if ( !$status->isGood() ) {
129 throw new InstallException( Status::newFatal(
130 $status->getMessage( 'config-admin-error-password-invalid' ) ) );
131 }
132 $this->setVar( '_AdminPassword', $options['pass'] );
133 }
134 }
135
136 // Detect and inject any extension found
137 if ( isset( $options['extensions'] ) ) {
138 $status = $this->validateExtensions(
139 'extension', 'extensions', $options['extensions'] );
140 if ( !$status->isOK() ) {
141 throw new InstallException( $status );
142 }
143 $this->setVar( '_Extensions', $status->value );
144 } elseif ( isset( $options['with-extensions'] ) ) {
145 $status = $this->findExtensions();
146 if ( !$status->isOK() ) {
147 throw new InstallException( $status );
148 }
149 $this->setVar( '_Extensions', array_keys( $status->value ) );
150 }
151
152 // Set up the default skins
153 if ( isset( $options['skins'] ) ) {
154 $status = $this->validateExtensions( 'skin', 'skins', $options['skins'] );
155 if ( !$status->isOK() ) {
156 throw new InstallException( $status );
157 }
158 $skins = $status->value;
159 } else {
160 $status = $this->findExtensions( 'skins' );
161 if ( !$status->isOK() ) {
162 throw new InstallException( $status );
163 }
164 $skins = array_keys( $status->value );
165 }
166 $this->setVar( '_Skins', $skins );
167
168 if ( $skins ) {
169 $skinNames = array_map( 'strtolower', $skins );
170 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
171 }
172
173 $this->setVar( '_WithDevelopmentSettings', isset( $options['with-developmentsettings'] ) );
174 }
175
176 private function validateExtensions( $type, $directory, $nameLists ) {
177 $extensions = [];
178 $status = new Status;
179 foreach ( (array)$nameLists as $nameList ) {
180 foreach ( explode( ',', $nameList ) as $name ) {
181 $name = trim( $name );
182 if ( $name === '' ) {
183 continue;
184 }
185 $extStatus = $this->getExtensionInfo( $type, $directory, $name );
186 if ( $extStatus->isOK() ) {
187 $extensions[] = $name;
188 } else {
189 $status->merge( $extStatus );
190 }
191 }
192 }
193 $extensions = array_unique( $extensions );
194 $status->value = $extensions;
195 return $status;
196 }
197
198 /**
199 * Main entry point.
200 * @return Status
201 */
202 public function execute() {
203 // If APC is available, use that as the MainCacheType, instead of nothing.
204 // This is hacky and should be consolidated with WebInstallerOptions.
205 // This is here instead of in __construct(), because it should run after
206 // doEnvironmentChecks(), which populates '_Caches'.
207 if ( count( $this->getVar( '_Caches' ) ) ) {
208 // We detected a CACHE_ACCEL implementation, use it.
209 $this->setVar( '_MainCacheType', 'accel' );
210 }
211
212 $vars = Installer::getExistingLocalSettings();
213 if ( $vars ) {
214 $status = Status::newFatal( "config-localsettings-cli-upgrade" );
215 $this->showStatusMessage( $status );
216 return $status;
217 }
218
219 $result = $this->performInstallation(
220 [ $this, 'startStage' ],
221 [ $this, 'endStage' ]
222 );
223 // PerformInstallation bails on a fatal, so make sure the last item
224 // completed before giving 'next.' Likewise, only provide back on failure
225 $lastStepStatus = end( $result );
226 if ( $lastStepStatus->isOK() ) {
227 return Status::newGood();
228 } else {
229 return $lastStepStatus;
230 }
231 }
232
233 /**
234 * Write LocalSettings.php to a given path
235 *
236 * @param string $path Full path to write LocalSettings.php to
237 */
238 public function writeConfigurationFile( $path ) {
239 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
240 $ls->writeFile( "$path/LocalSettings.php" );
241 }
242
243 public function startStage( $step ) {
244 // Messages: config-install-database, config-install-tables, config-install-interwiki,
245 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
246 // config-install-extensions
247 $this->showMessage( "config-install-$step" );
248 }
249
250 public function endStage( $step, $status ) {
251 $this->showStatusMessage( $status );
252 if ( $status->isOK() ) {
253 $this->showMessage( 'config-install-step-done' );
254 } else {
255 $this->showError( 'config-install-step-failed' );
256 }
257 }
258
259 public function showMessage( $msg, ...$params ) {
260 // @phan-suppress-next-line SecurityCheck-XSS
261 echo $this->getMessageText( $msg, $params ) . "\n";
262 flush();
263 }
264
265 public function showSuccess( $msg, ...$params ) {
266 // @phan-suppress-next-line SecurityCheck-XSS
267 echo $this->getMessageText( $msg, $params ) . "\n";
268 flush();
269 }
270
271 public function showWarning( $msg, ...$params ) {
272 // @phan-suppress-next-line SecurityCheck-XSS
273 echo $this->getMessageText( $msg, $params ) . "\n";
274 flush();
275 }
276
277 public function showError( $msg, ...$params ) {
278 // @phan-suppress-next-line SecurityCheck-XSS
279 echo "***{$this->getMessageText( $msg, $params )}***\n";
280 flush();
281 }
282
283 /**
284 * @param string|MessageSpecifier $msg
285 * @param array $params
286 * @return string
287 */
288 protected function getMessageText( $msg, $params ) {
289 $text = wfMessage( $msg, $params )->parse();
290
291 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
292
293 return Sanitizer::stripAllTags( $text );
294 }
295
296 /**
297 * Dummy
298 * @param string $msg Key for wfMessage()
299 * @param mixed ...$params
300 */
301 public function showHelpBox( $msg, ...$params ) {
302 }
303
304 public function showStatusMessage( Status $status ) {
305 // Show errors at the end in CLI installer to make them easier to notice
306 foreach ( $status->getMessages( 'warning' ) as $msg ) {
307 $this->showMessage( $msg );
308 }
309 foreach ( $status->getMessages( 'error' ) as $msg ) {
310 $this->showMessage( $msg );
311 }
312 }
313
314 public function envCheckPath() {
315 if ( !$this->specifiedScriptPath ) {
316 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
317 }
318
319 return parent::envCheckPath();
320 }
321
322 protected function envGetDefaultServer() {
323 // Use a basic value if the user didn't pass in --server
324 return 'http://localhost';
325 }
326
327 public function dirIsExecutable( $dir, $url ) {
328 $this->showMessage( 'config-no-cli-uploads-check', $dir );
329
330 return false;
331 }
332 }