MediaWiki REL1_39
CliInstaller.php
Go to the documentation of this file.
1<?php
26
33class CliInstaller extends Installer {
34 private $specifiedScriptPath = false;
35
36 private $optionMap = [
37 'dbtype' => 'wgDBtype',
38 'dbserver' => 'wgDBserver',
39 'dbname' => 'wgDBname',
40 'dbuser' => 'wgDBuser',
41 'dbpass' => 'wgDBpassword',
42 'dbprefix' => 'wgDBprefix',
43 'dbtableoptions' => 'wgDBTableOptions',
44 'dbport' => 'wgDBport',
45 'dbssl' => 'wgDBssl',
46 'dbschema' => 'wgDBmwschema',
47 'dbpath' => 'wgSQLiteDataDir',
48 'server' => 'wgServer',
49 'scriptpath' => 'wgScriptPath',
50 ];
51
58 public function __construct( $siteName, $admin = null, array $options = [] ) {
59 global $wgPasswordPolicy;
60
61 parent::__construct();
62
63 if ( isset( $options['scriptpath'] ) ) {
64 $this->specifiedScriptPath = true;
65 }
66
67 foreach ( $this->optionMap as $opt => $global ) {
68 if ( isset( $options[$opt] ) ) {
69 $GLOBALS[$global] = $options[$opt];
70 $this->setVar( $global, $options[$opt] );
71 }
72 }
73
74 if ( isset( $options['lang'] ) ) {
76 $this->setVar( '_UserLang', $options['lang'] );
77 $wgLanguageCode = $options['lang'];
78 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
79 $wgLang = MediaWikiServices::getInstance()->getLanguageFactory()
80 ->getLanguage( $options['lang'] );
81 RequestContext::getMain()->setLanguage( $wgLang );
82 }
83
84 $this->setVar( 'wgSitename', $siteName );
85
86 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
87 $metaNS = $contLang->ucfirst( str_replace( ' ', '_', $siteName ) );
88 if ( $metaNS == 'MediaWiki' ) {
89 $metaNS = 'Project';
90 }
91 $this->setVar( 'wgMetaNamespace', $metaNS );
92
93 if ( !isset( $options['installdbuser'] ) ) {
94 $this->setVar( '_InstallUser',
95 $this->getVar( 'wgDBuser' ) );
96 $this->setVar( '_InstallPassword',
97 $this->getVar( 'wgDBpassword' ) );
98 } else {
99 $this->setVar( '_InstallUser',
100 $options['installdbuser'] );
101 $this->setVar( '_InstallPassword',
102 $options['installdbpass'] ?? "" );
103
104 // Assume that if we're given the installer user, we'll create the account.
105 $this->setVar( '_CreateDBAccount', true );
106 }
107
108 if ( $admin ) {
109 $this->setVar( '_AdminName', $admin );
110 if ( isset( $options['pass'] ) ) {
111 $adminUser = User::newFromName( $admin );
112 if ( !$adminUser ) {
113 throw new InstallException( Status::newFatal( 'config-admin-name-invalid' ) );
114 }
115 $upp = new UserPasswordPolicy(
116 $wgPasswordPolicy['policies'],
117 $wgPasswordPolicy['checks']
118 );
119 $status = $upp->checkUserPasswordForGroups( $adminUser, $options['pass'],
120 [ 'bureaucrat', 'sysop', 'interface-admin' ] ); // per Installer::createSysop()
121 if ( !$status->isGood() ) {
122 throw new InstallException( Status::newFatal(
123 $status->getMessage( 'config-admin-error-password-invalid' ) ) );
124 }
125 $this->setVar( '_AdminPassword', $options['pass'] );
126 }
127 }
128
129 // Detect and inject any extension found
130 if ( isset( $options['extensions'] ) ) {
131 $status = $this->validateExtensions(
132 'extension', 'extensions', $options['extensions'] );
133 if ( !$status->isOK() ) {
134 throw new InstallException( $status );
135 }
136 $this->setVar( '_Extensions', $status->value );
137 } elseif ( isset( $options['with-extensions'] ) ) {
138 $status = $this->findExtensions();
139 if ( !$status->isOK() ) {
140 throw new InstallException( $status );
141 }
142 $this->setVar( '_Extensions', array_keys( $status->value ) );
143 }
144
145 // Set up the default skins
146 if ( isset( $options['skins'] ) ) {
147 $status = $this->validateExtensions( 'skin', 'skins', $options['skins'] );
148 if ( !$status->isOK() ) {
149 throw new InstallException( $status );
150 }
151 $skins = $status->value;
152 } else {
153 $status = $this->findExtensions( 'skins' );
154 if ( !$status->isOK() ) {
155 throw new InstallException( $status );
156 }
157 $skins = array_keys( $status->value );
158 }
159 $this->setVar( '_Skins', $skins );
160
161 if ( $skins ) {
162 $skinNames = array_map( 'strtolower', $skins );
163 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
164 }
165 }
166
167 private function validateExtensions( $type, $directory, $nameLists ) {
168 $extensions = [];
169 $status = new Status;
170 foreach ( (array)$nameLists as $nameList ) {
171 foreach ( explode( ',', $nameList ) as $name ) {
172 $name = trim( $name );
173 if ( $name === '' ) {
174 continue;
175 }
176 $extStatus = $this->getExtensionInfo( $type, $directory, $name );
177 if ( $extStatus->isOK() ) {
178 $extensions[] = $name;
179 } else {
180 $status->merge( $extStatus );
181 }
182 }
183 }
184 $extensions = array_unique( $extensions );
185 $status->value = $extensions;
186 return $status;
187 }
188
193 public function execute() {
194 // If APC is available, use that as the MainCacheType, instead of nothing.
195 // This is hacky and should be consolidated with WebInstallerOptions.
196 // This is here instead of in __construct(), because it should run after
197 // doEnvironmentChecks(), which populates '_Caches'.
198 if ( count( $this->getVar( '_Caches' ) ) ) {
199 // We detected a CACHE_ACCEL implementation, use it.
200 $this->setVar( '_MainCacheType', 'accel' );
201 }
202
204 if ( $vars ) {
205 $status = Status::newFatal( "config-localsettings-cli-upgrade" );
206 $this->showStatusMessage( $status );
207 return $status;
208 }
209
210 $result = $this->performInstallation(
211 [ $this, 'startStage' ],
212 [ $this, 'endStage' ]
213 );
214 // PerformInstallation bails on a fatal, so make sure the last item
215 // completed before giving 'next.' Likewise, only provide back on failure
216 $lastStepStatus = end( $result );
217 if ( $lastStepStatus->isOK() ) {
218 return Status::newGood();
219 } else {
220 return $lastStepStatus;
221 }
222 }
223
229 public function writeConfigurationFile( $path ) {
231 $ls->writeFile( "$path/LocalSettings.php" );
232 }
233
234 public function startStage( $step ) {
235 // Messages: config-install-database, config-install-tables, config-install-interwiki,
236 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
237 // config-install-extensions
238 $this->showMessage( "config-install-$step" );
239 }
240
241 public function endStage( $step, $status ) {
242 $this->showStatusMessage( $status );
243 $this->showMessage( 'config-install-step-done' );
244 }
245
246 public function showMessage( $msg, ...$params ) {
247 // @phan-suppress-next-line SecurityCheck-XSS
248 echo $this->getMessageText( $msg, $params ) . "\n";
249 flush();
250 }
251
252 public function showError( $msg, ...$params ) {
253 // @phan-suppress-next-line SecurityCheck-XSS
254 echo "***{$this->getMessageText( $msg, $params )}***\n";
255 flush();
256 }
257
264 protected function getMessageText( $msg, $params ) {
265 $text = wfMessage( $msg, $params )->parse();
266
267 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
268
269 return Sanitizer::stripAllTags( $text );
270 }
271
277 public function showHelpBox( $msg, ...$params ) {
278 }
279
280 public function showStatusMessage( Status $status ) {
281 $warnings = array_merge( $status->getWarningsArray(),
282 $status->getErrorsArray() );
283
284 if ( count( $warnings ) !== 0 ) {
285 foreach ( $warnings as $w ) {
286 $this->showMessage( ...$w );
287 }
288 }
289 }
290
291 public function envCheckPath() {
292 if ( !$this->specifiedScriptPath ) {
293 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
294 }
295
296 return parent::envCheckPath();
297 }
298
299 protected function envGetDefaultServer() {
300 // Use a basic value if the user didn't pass in --server
301 return 'http://localhost';
302 }
303
304 public function dirIsExecutable( $dir, $url ) {
305 $this->showMessage( 'config-no-cli-uploads-check', $dir );
306
307 return false;
308 }
309}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgLang
Definition Setup.php:497
Class for the core installer command line interface.
showMessage( $msg,... $params)
UI interface for displaying a short message The parameters are like parameters to wfMessage().
showHelpBox( $msg,... $params)
Dummy.
execute()
Main entry point.
__construct( $siteName, $admin=null, array $options=[])
getMessageText( $msg, $params)
showStatusMessage(Status $status)
Show a message to the installing user by using a Status object.
writeConfigurationFile( $path)
Write LocalSettings.php to a given path.
dirIsExecutable( $dir, $url)
Checks if scripts located in the given directory can be executed via the given URL.
showError( $msg,... $params)
Same as showMessage(), but for displaying errors.
envGetDefaultServer()
Helper function to be called from envPrepServer()
envCheckPath()
Environment check to inform user which paths we've assumed.
startStage( $step)
endStage( $step, $status)
static getLocalSettingsGenerator(Installer $installer)
Instantiates and returns an instance of LocalSettingsGenerator or its descendant classes.
Base installer class.
Definition Installer.php:57
getExtensionInfo( $type, $parentRelPath, $name)
getDefaultSkin(array $skinNames)
Returns a default value to be used for $wgDefaultSkin: normally the DefaultSkin from config-schema....
setVar( $name, $value)
Set a MW configuration variable, or internal installer configuration variable.
static getExistingLocalSettings()
Determine if LocalSettings.php exists.
performInstallation( $startCB, $endCB)
Actually perform the installation.
getVar( $name, $default=null)
Get an MW configuration variable, or internal installer configuration variable.
findExtensions( $directory='extensions')
Find extensions or skins in a subdirectory of $IP.
Exception thrown if an error occur which installation.
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
getErrorsArray()
Get the list of errors (but not warnings)
Definition Status.php:358
getWarningsArray()
Get the list of warnings (but not errors)
Definition Status.php:370
Check if a user's password complies with any password policies that apply to that user,...
static newFromName( $name, $validate='valid')
Definition User.php:607
$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.