MediaWiki master
CliInstaller.php
Go to the documentation of this file.
1<?php
2
24
32
39class CliInstaller extends Installer {
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
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'] ) ) {
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
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
213 if ( $vars ) {
214 $status = Status::newFatal( "config-localsettings-cli-upgrade" );
215 $this->showStatusMessage( $status );
216 return $status;
217 }
218
219 $status = $this->performInstallation(
220 [ $this, 'startStage' ],
221 [ $this, 'endStage' ]
222 );
223 if ( $status->isOK() ) {
224 return Status::newGood();
225 } else {
226 return $status;
227 }
228 }
229
235 public function writeConfigurationFile( $path ) {
237 $ls->writeFile( "$path/LocalSettings.php" );
238 }
239
240 public function startStage( $step ) {
241 // Messages: config-install-database, config-install-tables, config-install-interwiki,
242 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
243 // config-install-extensions
244 $this->showMessage( "config-install-$step" );
245 }
246
247 public function endStage( $step, $status ) {
248 $this->showStatusMessage( $status );
249 if ( $status->isOK() ) {
250 $this->showMessage( 'config-install-step-done' );
251 } else {
252 $this->showError( 'config-install-step-failed' );
253 }
254 }
255
256 public function showMessage( $msg, ...$params ) {
257 // @phan-suppress-next-line SecurityCheck-XSS
258 echo $this->getMessageText( $msg, $params ) . "\n";
259 flush();
260 }
261
262 public function showError( $msg, ...$params ) {
263 // @phan-suppress-next-line SecurityCheck-XSS
264 echo "***{$this->getMessageText( $msg, $params )}***\n";
265 flush();
266 }
267
273 protected function getMessageText( $msg, $params ) {
274 $text = wfMessage( $msg, $params )->parse();
275
276 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
277
278 return Sanitizer::stripAllTags( $text );
279 }
280
286 public function showHelpBox( $msg, ...$params ) {
287 }
288
289 public function showStatusMessage( Status $status ) {
290 // Show errors at the end in CLI installer to make them easier to notice
291 foreach ( $status->getMessages( 'warning' ) as $msg ) {
292 $this->showMessage( $msg );
293 }
294 foreach ( $status->getMessages( 'error' ) as $msg ) {
295 $this->showMessage( $msg );
296 }
297 }
298
299 public function envCheckPath() {
300 if ( !$this->specifiedScriptPath ) {
301 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
302 }
303
304 return parent::envCheckPath();
305 }
306
307 protected function envGetDefaultServer() {
308 // Use a basic value if the user didn't pass in --server
309 return 'http://localhost';
310 }
311
312 public function dirIsExecutable( $dir, $url ) {
313 $this->showMessage( 'config-no-cli-uploads-check', $dir );
314
315 return false;
316 }
317}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Definition Setup.php:541
array $params
The job parameters.
Group all the pieces relevant to the context of a request into one instance.
Class for the core installer command line interface.
showStatusMessage(Status $status)
Show a message to the installing user by using a Status object.
envCheckPath()
Environment check to inform user which paths we've assumed.
writeConfigurationFile( $path)
Write LocalSettings.php to a given path.
envGetDefaultServer()
Helper function to be called from getDefaultSettings()
dirIsExecutable( $dir, $url)
Checks if scripts located in the given directory can be executed via the given URL.
showMessage( $msg,... $params)
UI interface for displaying a short message The parameters are like parameters to wfMessage().
showError( $msg,... $params)
Same as showMessage(), but for displaying errors.
__construct( $siteName, $admin=null, array $options=[])
showHelpBox( $msg,... $params)
Dummy.
Exception thrown if an error occurs during installation.
static getLocalSettingsGenerator(Installer $installer)
Instantiates and returns an instance of LocalSettingsGenerator or its descendant classes.
Base installer class.
Definition Installer.php:85
setVar( $name, $value)
Set a MW configuration variable, or internal installer configuration variable.
performInstallation( $startCB, $endCB)
Actually perform the installation.
findExtensions( $directory='extensions')
Find extensions or skins in a subdirectory of $IP.
getExtensionInfo( $type, $parentRelPath, $name)
getDefaultSkin(array $skinNames)
Returns a default value to be used for $wgDefaultSkin: normally the DefaultSkin from config-schema....
static getExistingLocalSettings()
Determine if LocalSettings.php exists.
getVar( $name, $default=null)
Get an MW configuration variable, or internal installer configuration variable.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Check if a user's password complies with any password policies that apply to that user,...
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
internal since 1.36
Definition User.php:93
getMessages(?string $type=null)
Returns a list of error messages, optionally only those of the given type.
$wgLanguageCode
Config variable stub for the LanguageCode setting, for use by phpdoc and IDEs.
$wgPasswordPolicy
Config variable stub for the PasswordPolicy setting, for use by phpdoc and IDEs.