MediaWiki master
CliInstaller.php
Go to the documentation of this file.
1<?php
2
24
34
41class CliInstaller extends Installer {
43 private $specifiedScriptPath = false;
44
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',
59 ];
60
67 public function __construct( $siteName, $admin = null, array $options = [] ) {
68 global $wgPasswordPolicy;
69
70 parent::__construct();
71
72 if ( isset( $options['scriptpath'] ) ) {
73 $this->specifiedScriptPath = true;
74 }
75
76 foreach ( self::OPTION_MAP as $opt => $global ) {
77 if ( isset( $options[$opt] ) ) {
78 $GLOBALS[$global] = $options[$opt];
79 $this->setVar( $global, $options[$opt] );
80 }
81 }
82
83 if ( isset( $options['lang'] ) ) {
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 );
91 }
92
93 $this->setVar( 'wgSitename', $siteName );
94
95 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
96 $metaNS = $contLang->ucfirst( str_replace( ' ', '_', $siteName ) );
97 if ( $metaNS == 'MediaWiki' ) {
98 $metaNS = 'Project';
99 }
100 $this->setVar( 'wgMetaNamespace', $metaNS );
101
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'] ?? "" );
112
113 // Assume that if we're given the installer user, we'll create the account.
114 $this->setVar( '_CreateDBAccount', true );
115 }
116
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' ) );
123 }
124 $upp = new UserPasswordPolicy(
125 $wgPasswordPolicy['policies'],
126 $wgPasswordPolicy['checks']
127 );
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' ) ) );
133 }
134 $this->setVar( '_AdminPassword', $options['pass'] );
135 }
136 }
137
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 );
144 }
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 );
150 }
151 $this->setVar( '_Extensions', array_keys( $status->value ) );
152 }
153
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 );
159 }
160 $skins = $status->value;
161 } else {
162 $status = $this->findExtensions( 'skins' );
163 if ( !$status->isOK() ) {
164 throw new InstallException( $status );
165 }
166 $skins = array_keys( $status->value );
167 }
168 $this->setVar( '_Skins', $skins );
169
170 if ( $skins ) {
171 $skinNames = array_map( 'strtolower', $skins );
172 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
173 }
174
175 $this->setVar( '_WithDevelopmentSettings', isset( $options['with-developmentsettings'] ) );
176 }
177
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;
186 }
187 $extStatus = $this->getExtensionInfo( $type, $directory, $name );
188 if ( $extStatus->isOK() ) {
189 $extensions[] = $name;
190 } else {
191 $status->merge( $extStatus );
192 }
193 }
194 }
195 $extensions = array_unique( $extensions );
196 $status->value = $extensions;
197 return $status;
198 }
199
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' );
212 }
213
215 if ( $vars ) {
216 $status = Status::newFatal( "config-localsettings-cli-upgrade" );
217 $this->showStatusMessage( $status );
218 return $status;
219 }
220
221 $status = $this->performInstallation(
222 [ $this, 'startStage' ],
223 [ $this, 'endStage' ]
224 );
225 if ( $status->isOK() ) {
226 return Status::newGood();
227 } else {
228 return $status;
229 }
230 }
231
237 public function writeConfigurationFile( $path ) {
239 $ls->writeFile( "$path/LocalSettings.php" );
240 }
241
245 public function startStage( $task ) {
246 // @phan-suppress-next-line SecurityCheck-XSS -- it's CLI
247 echo $this->formatMessage( $task->getDescriptionMessage() ) . '... ';
248 }
249
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' );
260 }
261 }
262
263 public function showMessage( $msg, ...$params ) {
264 // @phan-suppress-next-line SecurityCheck-XSS
265 echo $this->getMessageText( $msg, $params ) . "\n";
266 flush();
267 }
268
269 public function showError( $msg, ...$params ) {
270 // @phan-suppress-next-line SecurityCheck-XSS
271 echo "***{$this->getMessageText( $msg, $params )}***\n";
272 flush();
273 }
274
280 protected function getMessageText( $msg, $params ) {
281 return $this->formatMessage( wfMessage( $msg, $params ) );
282 }
283
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 );
292 }
293
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 );
298 }
299 foreach ( $status->getMessages( 'error' ) as $msg ) {
300 $this->showMessage( $msg );
301 }
302 }
303
304 public function envCheckPath() {
305 if ( !$this->specifiedScriptPath ) {
306 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
307 }
308
309 return parent::envCheckPath();
310 }
311
312 protected function envGetDefaultServer() {
313 // Use a basic value if the user didn't pass in --server
314 return 'http://localhost';
315 }
316
317 public function dirIsExecutable( $dir, $url ) {
318 $this->showMessage( 'config-no-cli-uploads-check', $dir );
319
320 return false;
321 }
322}
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:570
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=[])
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.
Base class for installer tasks.
Definition Task.php:24
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:155
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
User class for the MediaWiki software.
Definition User.php:119
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.