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 = [] ) {
58  global $wgContLang;
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 ( $admin ) {
93  $this->setVar( '_AdminName', $admin );
94  }
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 ( isset( $options['pass'] ) ) {
112  $this->setVar( '_AdminPassword', $options['pass'] );
113  }
114 
115  // Detect and inject any extension found
116  if ( isset( $options['extensions'] ) ) {
117  $status = $this->validateExtensions(
118  'extension', 'extensions', $options['extensions'] );
119  if ( !$status->isOK() ) {
120  throw new InstallException( $status );
121  }
122  $this->setVar( '_Extensions', $status->value );
123  } elseif ( isset( $options['with-extensions'] ) ) {
124  $status = $this->findExtensions();
125  if ( !$status->isOK() ) {
126  throw new InstallException( $status );
127  }
128  $this->setVar( '_Extensions', array_keys( $status->value ) );
129  }
130 
131  // Set up the default skins
132  if ( isset( $options['skins'] ) ) {
133  $status = $this->validateExtensions( 'skin', 'skins', $options['skins'] );
134  if ( !$status->isOK() ) {
135  throw new InstallException( $status );
136  }
137  $skins = $status->value;
138  } else {
139  $status = $this->findExtensions( 'skins' );
140  if ( !$status->isOK() ) {
141  throw new InstallException( $status );
142  }
143  $skins = array_keys( $status->value );
144  }
145  $this->setVar( '_Skins', $skins );
146 
147  if ( $skins ) {
148  $skinNames = array_map( 'strtolower', $skins );
149  $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
150  }
151  }
152 
153  private function validateExtensions( $type, $directory, $nameLists ) {
154  $extensions = [];
155  $status = new Status;
156  foreach ( (array)$nameLists as $nameList ) {
157  foreach ( explode( ',', $nameList ) as $name ) {
158  $name = trim( $name );
159  if ( $name === '' ) {
160  continue;
161  }
162  $extStatus = $this->getExtensionInfo( $type, $directory, $name );
163  if ( $extStatus->isOK() ) {
164  $extensions[] = $name;
165  } else {
166  $status->merge( $extStatus );
167  }
168  }
169  }
170  $extensions = array_unique( $extensions );
171  $status->value = $extensions;
172  return $status;
173  }
174 
179  public function execute() {
180  // If APC is available, use that as the MainCacheType, instead of nothing.
181  // This is hacky and should be consolidated with WebInstallerOptions.
182  // This is here instead of in __construct(), because it should run run after
183  // doEnvironmentChecks(), which populates '_Caches'.
184  if ( count( $this->getVar( '_Caches' ) ) ) {
185  // We detected a CACHE_ACCEL implementation, use it.
186  $this->setVar( '_MainCacheType', 'accel' );
187  }
188 
190  if ( $vars ) {
191  $status = Status::newFatal( "config-localsettings-cli-upgrade" );
192  $this->showStatusMessage( $status );
193  return $status;
194  }
195 
196  $result = $this->performInstallation(
197  [ $this, 'startStage' ],
198  [ $this, 'endStage' ]
199  );
200  // PerformInstallation bails on a fatal, so make sure the last item
201  // completed before giving 'next.' Likewise, only provide back on failure
202  $lastStepStatus = end( $result );
203  if ( $lastStepStatus->isOK() ) {
204  return Status::newGood();
205  } else {
206  return $lastStepStatus;
207  }
208  }
209 
215  public function writeConfigurationFile( $path ) {
217  $ls->writeFile( "$path/LocalSettings.php" );
218  }
219 
220  public function startStage( $step ) {
221  // Messages: config-install-database, config-install-tables, config-install-interwiki,
222  // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
223  // config-install-extensions
224  $this->showMessage( "config-install-$step" );
225  }
226 
227  public function endStage( $step, $status ) {
228  $this->showStatusMessage( $status );
229  $this->showMessage( 'config-install-step-done' );
230  }
231 
232  public function showMessage( $msg, ...$params ) {
233  echo $this->getMessageText( $msg, $params ) . "\n";
234  flush();
235  }
236 
237  public function showError( $msg, ...$params ) {
238  echo "***{$this->getMessageText( $msg, $params )}***\n";
239  flush();
240  }
241 
248  protected function getMessageText( $msg, $params ) {
249  $text = wfMessage( $msg, $params )->parse();
250 
251  $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
252 
253  return Sanitizer::stripAllTags( $text );
254  }
255 
261  public function showHelpBox( $msg, ...$params ) {
262  }
263 
264  public function showStatusMessage( Status $status ) {
265  $warnings = array_merge( $status->getWarningsArray(),
266  $status->getErrorsArray() );
267 
268  if ( count( $warnings ) !== 0 ) {
269  foreach ( $warnings as $w ) {
270  $this->showMessage( ...$w );
271  }
272  }
273  }
274 
275  public function envCheckPath() {
276  if ( !$this->specifiedScriptPath ) {
277  $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
278  }
279 
280  return parent::envCheckPath();
281  }
282 
283  protected function envGetDefaultServer() {
284  // Use a basic value if the user didn't pass in --server
285  return 'http://localhost';
286  }
287 
288  public function dirIsExecutable( $dir, $url ) {
289  $this->showMessage( 'config-no-cli-uploads-check', $dir );
290 
291  return false;
292  }
293 }
Installer\__construct
__construct()
Constructor, always call this from child classes.
Definition: Installer.php:408
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:130
Installer\showStatusMessage
showStatusMessage(Status $status)
Show a message to the installing user by using a Status object.
Installer\dirIsExecutable
dirIsExecutable( $dir, $url)
Checks if scripts located in the given directory can be executed via the given URL.
Definition: Installer.php:1180
Installer\performInstallation
performInstallation( $startCB, $endCB)
Actually perform the installation.
Definition: Installer.php:1602
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1263
Installer\envGetDefaultServer
envGetDefaultServer()
Helper function to be called from envPrepServer()
Installer\setVar
setVar( $name, $value)
Set a MW configuration variable, or internal installer configuration variable.
Definition: Installer.php:530
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
Installer\showMessage
showMessage( $msg,... $params)
UI interface for displaying a short message The parameters are like parameters to wfMessage().
StatusValue\isOK
isOK()
Returns whether the operation completed.
Definition: StatusValue.php:130
MediaWiki\Installer\InstallException
Exception thrown if an error occur which installation.
Definition: InstallException.php:29
$wgLang
$wgLang
Definition: Setup.php:846
StatusValue\merge
merge( $other, $overwriteValue=false)
Merge another status object into this one.
Definition: StatusValue.php:223
Installer\getExistingLocalSettings
static getExistingLocalSettings()
Determine if LocalSettings.php exists.
Definition: Installer.php:595
Installer\envCheckPath
envCheckPath()
Environment check to inform user which paths we've assumed.
Definition: Installer.php:971
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:544
Installer\getDefaultSkin
getDefaultSkin(array $skinNames)
Returns a default value to be used for $wgDefaultSkin: normally the one set in DefaultSettings,...
Definition: Installer.php:1469
Installer\findExtensions
findExtensions( $directory='extensions')
Find extensions or skins in a subdirectory of $IP.
Definition: Installer.php:1269
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
$wgLanguageCode
$wgLanguageCode
Site language code.
Definition: DefaultSettings.php:2949
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:447
Status\getWarningsArray
getWarningsArray()
Get the list of warnings (but not errors)
Definition: Status.php:361
$path
$path
Definition: NoLocalSettings.php:25
Installer
Base installer class.
Definition: Installer.php:52
Installer\showError
showError( $msg,... $params)
Same as showMessage(), but for displaying errors.
Status\getErrorsArray
getErrorsArray()
Get the list of errors (but not warnings)
Definition: Status.php:350
$wgContLang
$wgContLang
Definition: Setup.php:766
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
Installer\getExtensionInfo
getExtensionInfo( $type, $parentRelPath, $name)
Definition: Installer.php:1332
$type
$type
Definition: testCompression.php:50