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