MediaWiki 1.43.1
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 $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
238 public function writeConfigurationFile( $path ) {
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
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
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}
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.
showSuccess( $msg,... $params)
Display a success message.
writeConfigurationFile( $path)
Write LocalSettings.php to a given path.
envGetDefaultServer()
Helper function to be called from envPrepServer()
dirIsExecutable( $dir, $url)
Checks if scripts located in the given directory can be executed via the given URL.
showWarning( $msg,... $params)
Display a warning message.
showMessage( $msg,... $params)
Display a short neutral message.
showError( $msg,... $params)
Display an error message.
__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:92
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:94
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.