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