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