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