MediaWiki  1.34.0
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  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 = Language::factory( $options['lang'] );
80  RequestContext::getMain()->setLanguage( $wgLang );
81  }
82 
83  $this->setVar( 'wgSitename', $siteName );
84 
85  $metaNS = $wgContLang->ucfirst( str_replace( ' ', '_', $siteName ) );
86  if ( $metaNS == 'MediaWiki' ) {
87  $metaNS = 'Project';
88  }
89  $this->setVar( 'wgMetaNamespace', $metaNS );
90 
91  if ( $admin ) {
92  $this->setVar( '_AdminName', $admin );
93  }
94 
95  if ( !isset( $options['installdbuser'] ) ) {
96  $this->setVar( '_InstallUser',
97  $this->getVar( 'wgDBuser' ) );
98  $this->setVar( '_InstallPassword',
99  $this->getVar( 'wgDBpassword' ) );
100  } else {
101  $this->setVar( '_InstallUser',
102  $options['installdbuser'] );
103  $this->setVar( '_InstallPassword',
104  $options['installdbpass'] ?? "" );
105 
106  // Assume that if we're given the installer user, we'll create the account.
107  $this->setVar( '_CreateDBAccount', true );
108  }
109 
110  if ( isset( $options['pass'] ) ) {
111  $this->setVar( '_AdminPassword', $options['pass'] );
112  }
113 
114  // Detect and inject any extension found
115  if ( isset( $options['extensions'] ) ) {
116  $status = $this->validateExtensions(
117  'extension', 'extensions', $options['extensions'] );
118  if ( !$status->isOK() ) {
119  throw new InstallException( $status );
120  }
121  $this->setVar( '_Extensions', $status->value );
122  } elseif ( isset( $options['with-extensions'] ) ) {
123  $status = $this->findExtensions();
124  if ( !$status->isOK() ) {
125  throw new InstallException( $status );
126  }
127  $this->setVar( '_Extensions', array_keys( $status->value ) );
128  }
129 
130  // Set up the default skins
131  if ( isset( $options['skins'] ) ) {
132  $status = $this->validateExtensions( 'skin', 'skins', $options['skins'] );
133  if ( !$status->isOK() ) {
134  throw new InstallException( $status );
135  }
136  $skins = $status->value;
137  } else {
138  $status = $this->findExtensions( 'skins' );
139  if ( !$status->isOK() ) {
140  throw new InstallException( $status );
141  }
142  $skins = array_keys( $status->value );
143  }
144  $this->setVar( '_Skins', $skins );
145 
146  if ( $skins ) {
147  $skinNames = array_map( 'strtolower', $skins );
148  $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
149  }
150  }
151 
152  private function validateExtensions( $type, $directory, $nameLists ) {
153  $extensions = [];
154  $status = new Status;
155  foreach ( (array)$nameLists as $nameList ) {
156  foreach ( explode( ',', $nameList ) as $name ) {
157  $name = trim( $name );
158  if ( $name === '' ) {
159  continue;
160  }
161  $extStatus = $this->getExtensionInfo( $type, $directory, $name );
162  if ( $extStatus->isOK() ) {
163  $extensions[] = $name;
164  } else {
165  $status->merge( $extStatus );
166  }
167  }
168  }
169  $extensions = array_unique( $extensions );
170  $status->value = $extensions;
171  return $status;
172  }
173 
177  public function execute() {
178  // If APC is available, use that as the MainCacheType, instead of nothing.
179  // This is hacky and should be consolidated with WebInstallerOptions.
180  // This is here instead of in __construct(), because it should run run after
181  // doEnvironmentChecks(), which populates '_Caches'.
182  if ( count( $this->getVar( '_Caches' ) ) ) {
183  // We detected a CACHE_ACCEL implementation, use it.
184  $this->setVar( '_MainCacheType', 'accel' );
185  }
186 
188  if ( $vars ) {
189  $status = Status::newFatal( "config-localsettings-cli-upgrade" );
190  $this->showStatusMessage( $status );
191  return $status;
192  }
193 
194  $result = $this->performInstallation(
195  [ $this, 'startStage' ],
196  [ $this, 'endStage' ]
197  );
198  // PerformInstallation bails on a fatal, so make sure the last item
199  // completed before giving 'next.' Likewise, only provide back on failure
200  $lastStepStatus = end( $result );
201  if ( $lastStepStatus->isOK() ) {
202  return Status::newGood();
203  } else {
204  return $lastStepStatus;
205  }
206  }
207 
213  public function writeConfigurationFile( $path ) {
215  $ls->writeFile( "$path/LocalSettings.php" );
216  }
217 
218  public function startStage( $step ) {
219  // Messages: config-install-database, config-install-tables, config-install-interwiki,
220  // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
221  // config-install-extensions
222  $this->showMessage( "config-install-$step" );
223  }
224 
225  public function endStage( $step, $status ) {
226  $this->showStatusMessage( $status );
227  $this->showMessage( 'config-install-step-done' );
228  }
229 
230  public function showMessage( $msg, ...$params ) {
231  echo $this->getMessageText( $msg, $params ) . "\n";
232  flush();
233  }
234 
235  public function showError( $msg, ...$params ) {
236  echo "***{$this->getMessageText( $msg, $params )}***\n";
237  flush();
238  }
239 
246  protected function getMessageText( $msg, $params ) {
247  $text = wfMessage( $msg, $params )->parse();
248 
249  $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
250 
251  return Sanitizer::stripAllTags( $text );
252  }
253 
257  public function showHelpBox( $msg /*, ... */ ) {
258  }
259 
260  public function showStatusMessage( Status $status ) {
261  $warnings = array_merge( $status->getWarningsArray(),
262  $status->getErrorsArray() );
263 
264  if ( count( $warnings ) !== 0 ) {
265  foreach ( $warnings as $w ) {
266  $this->showMessage( ...$w );
267  }
268  }
269  }
270 
271  public function envCheckPath() {
272  if ( !$this->specifiedScriptPath ) {
273  $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
274  }
275 
276  return parent::envCheckPath();
277  }
278 
279  protected function envGetDefaultServer() {
280  // Use a basic value if the user didn't pass in --server
281  return 'http://localhost';
282  }
283 
284  public function dirIsExecutable( $dir, $url ) {
285  $this->showMessage( 'config-no-cli-uploads-check', $dir );
286 
287  return false;
288  }
289 }
Installer\__construct
__construct()
Constructor, always call this from child classes.
Definition: Installer.php:402
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:117
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:1193
Installer\performInstallation
performInstallation( $startCB, $endCB)
Actually perform the installation.
Definition: Installer.php:1614
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
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:528
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:881
Installer\getExistingLocalSettings
static getExistingLocalSettings()
Determine if LocalSettings.php exists.
Definition: Installer.php:593
Installer\envCheckPath
envCheckPath()
Environment check to inform user which paths we've assumed.
Definition: Installer.php:984
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:542
Installer\getDefaultSkin
getDefaultSkin(array $skinNames)
Returns a default value to be used for $wgDefaultSkin: normally the one set in DefaultSettings,...
Definition: Installer.php:1482
Installer\findExtensions
findExtensions( $directory='extensions')
Find extensions or skins in a subdirectory of $IP.
Definition: Installer.php:1282
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
$wgLanguageCode
$wgLanguageCode
Site language code.
Definition: DefaultSettings.php:2948
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:431
$status
return $status
Definition: SyntaxHighlight.php:347
$path
$path
Definition: NoLocalSettings.php:25
Installer
Base installer class.
Definition: Installer.php:46
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:217
Installer\showError
showError( $msg,... $params)
Same as showMessage(), but for displaying errors.
$wgContLang
$wgContLang
Definition: Setup.php:801
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
Installer\getExtensionInfo
getExtensionInfo( $type, $parentRelPath, $name)
Definition: Installer.php:1345
$type
$type
Definition: testCompression.php:48