Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / installer / CliInstaller.php
blob0469cc32074be7b851228cb8eea3e1e27255a401
1 <?php
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.
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.
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
19 * @file
20 * @ingroup Installer
23 namespace MediaWiki\Installer;
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;
35 /**
36 * Class for the core installer command line interface.
38 * @ingroup Installer
39 * @since 1.17
41 class CliInstaller extends Installer {
42 /** @var bool */
43 private $specifiedScriptPath = false;
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',
61 /**
62 * @param string $siteName
63 * @param string|null $admin
64 * @param array $options
65 * @throws InstallException
67 public function __construct( $siteName, $admin = null, array $options = [] ) {
68 global $wgPasswordPolicy;
70 parent::__construct();
72 if ( isset( $options['scriptpath'] ) ) {
73 $this->specifiedScriptPath = true;
76 foreach ( self::OPTION_MAP as $opt => $global ) {
77 if ( isset( $options[$opt] ) ) {
78 $GLOBALS[$global] = $options[$opt];
79 $this->setVar( $global, $options[$opt] );
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 );
93 $this->setVar( 'wgSitename', $siteName );
95 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
96 $metaNS = $contLang->ucfirst( str_replace( ' ', '_', $siteName ) );
97 if ( $metaNS == 'MediaWiki' ) {
98 $metaNS = 'Project';
100 $this->setVar( 'wgMetaNamespace', $metaNS );
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'] ?? "" );
113 // Assume that if we're given the installer user, we'll create the account.
114 $this->setVar( '_CreateDBAccount', true );
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' ) );
124 $upp = new UserPasswordPolicy(
125 $wgPasswordPolicy['policies'],
126 $wgPasswordPolicy['checks']
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' ) ) );
134 $this->setVar( '_AdminPassword', $options['pass'] );
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 );
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 );
151 $this->setVar( '_Extensions', array_keys( $status->value ) );
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 );
160 $skins = $status->value;
161 } else {
162 $status = $this->findExtensions( 'skins' );
163 if ( !$status->isOK() ) {
164 throw new InstallException( $status );
166 $skins = array_keys( $status->value );
168 $this->setVar( '_Skins', $skins );
170 if ( $skins ) {
171 $skinNames = array_map( 'strtolower', $skins );
172 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
175 $this->setVar( '_WithDevelopmentSettings', isset( $options['with-developmentsettings'] ) );
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;
187 $extStatus = $this->getExtensionInfo( $type, $directory, $name );
188 if ( $extStatus->isOK() ) {
189 $extensions[] = $name;
190 } else {
191 $status->merge( $extStatus );
195 $extensions = array_unique( $extensions );
196 $status->value = $extensions;
197 return $status;
201 * Main entry point.
202 * @return Status
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' );
214 $vars = Installer::getExistingLocalSettings();
215 if ( $vars ) {
216 $status = Status::newFatal( "config-localsettings-cli-upgrade" );
217 $this->showStatusMessage( $status );
218 return $status;
221 $status = $this->performInstallation(
222 [ $this, 'startStage' ],
223 [ $this, 'endStage' ]
225 if ( $status->isOK() ) {
226 return Status::newGood();
227 } else {
228 return $status;
233 * Write LocalSettings.php to a given path
235 * @param string $path Full path to write LocalSettings.php to
237 public function writeConfigurationFile( $path ) {
238 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
239 $ls->writeFile( "$path/LocalSettings.php" );
243 * @param Task $task
245 public function startStage( $task ) {
246 // @phan-suppress-next-line SecurityCheck-XSS -- it's CLI
247 echo $this->formatMessage( $task->getDescriptionMessage() ) . '... ';
251 * @param Task $task
252 * @param Status $status
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' );
263 public function showMessage( $msg, ...$params ) {
264 // @phan-suppress-next-line SecurityCheck-XSS
265 echo $this->getMessageText( $msg, $params ) . "\n";
266 flush();
269 public function showError( $msg, ...$params ) {
270 // @phan-suppress-next-line SecurityCheck-XSS
271 echo "***{$this->getMessageText( $msg, $params )}***\n";
272 flush();
276 * @param string|MessageSpecifier $msg
277 * @param (string|int|float)[] $params Message parameters
278 * @return string
280 protected function getMessageText( $msg, $params ) {
281 return $this->formatMessage( wfMessage( $msg, $params ) );
285 * @param Message $message
286 * @return string
288 protected function formatMessage( $message ) {
289 $text = $message->parse();
290 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
291 return Sanitizer::stripAllTags( $text );
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 );
299 foreach ( $status->getMessages( 'error' ) as $msg ) {
300 $this->showMessage( $msg );
304 public function envCheckPath() {
305 if ( !$this->specifiedScriptPath ) {
306 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
309 return parent::envCheckPath();
312 protected function envGetDefaultServer() {
313 // Use a basic value if the user didn't pass in --server
314 return 'http://localhost';
317 public function dirIsExecutable( $dir, $url ) {
318 $this->showMessage( 'config-no-cli-uploads-check', $dir );
320 return false;