3 * Core installer command line interface.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\MediaWikiServices
;
27 * Class for the core installer command line interface.
32 class CliInstaller
extends Installer
{
33 private $specifiedScriptPath = false;
35 private $optionMap = [
36 'dbtype' => 'wgDBtype',
37 'dbserver' => 'wgDBserver',
38 'dbname' => 'wgDBname',
39 'dbuser' => 'wgDBuser',
40 'dbpass' => 'wgDBpassword',
41 'dbprefix' => 'wgDBprefix',
42 'dbtableoptions' => 'wgDBTableOptions',
43 'dbport' => 'wgDBport',
44 'dbschema' => 'wgDBmwschema',
45 'dbpath' => 'wgSQLiteDataDir',
46 'server' => 'wgServer',
47 'scriptpath' => 'wgScriptPath',
51 * @param string $siteName
52 * @param string|null $admin
53 * @param array $options
55 function __construct( $siteName, $admin = null, array $options = [] ) {
58 parent
::__construct();
60 if ( isset( $options['scriptpath'] ) ) {
61 $this->specifiedScriptPath
= true;
64 foreach ( $this->optionMap
as $opt => $global ) {
65 if ( isset( $options[$opt] ) ) {
66 $GLOBALS[$global] = $options[$opt];
67 $this->setVar( $global, $options[$opt] );
71 if ( isset( $options['lang'] ) ) {
72 global $wgLang, $wgLanguageCode;
73 $this->setVar( '_UserLang', $options['lang'] );
74 $wgLanguageCode = $options['lang'];
75 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
76 $wgContLang = MediaWikiServices
::getInstance()->getContentLanguage();
77 $wgLang = Language
::factory( $options['lang'] );
78 RequestContext
::getMain()->setLanguage( $wgLang );
81 $this->setVar( 'wgSitename', $siteName );
83 $metaNS = $wgContLang->ucfirst( str_replace( ' ', '_', $siteName ) );
84 if ( $metaNS == 'MediaWiki' ) {
87 $this->setVar( 'wgMetaNamespace', $metaNS );
90 $this->setVar( '_AdminName', $admin );
93 if ( !isset( $options['installdbuser'] ) ) {
94 $this->setVar( '_InstallUser',
95 $this->getVar( 'wgDBuser' ) );
96 $this->setVar( '_InstallPassword',
97 $this->getVar( 'wgDBpassword' ) );
99 $this->setVar( '_InstallUser',
100 $options['installdbuser'] );
101 $this->setVar( '_InstallPassword',
102 $options['installdbpass'] ??
"" );
104 // Assume that if we're given the installer user, we'll create the account.
105 $this->setVar( '_CreateDBAccount', true );
108 if ( isset( $options['pass'] ) ) {
109 $this->setVar( '_AdminPassword', $options['pass'] );
112 // Detect and inject any extension found
113 if ( isset( $options['extensions'] ) ) {
114 $status = $this->validateExtensions(
115 'extension', 'extensions', $options['extensions'] );
116 if ( !$status->isOK() ) {
117 $this->showStatusMessage( $status );
119 $this->setVar( '_Extensions', $status->value
);
120 } elseif ( isset( $options['with-extensions'] ) ) {
121 $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
124 // Set up the default skins
125 if ( isset( $options['skins'] ) ) {
126 $status = $this->validateExtensions( 'skin', 'skins', $options['skins'] );
127 if ( !$status->isOK() ) {
128 $this->showStatusMessage( $status );
130 $skins = $status->value
;
132 $skins = array_keys( $this->findExtensions( 'skins' ) );
134 $this->setVar( '_Skins', $skins );
137 $skinNames = array_map( 'strtolower', $skins );
138 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
142 private function validateExtensions( $type, $directory, $nameLists ) {
144 $status = new Status
;
145 foreach ( (array)$nameLists as $nameList ) {
146 foreach ( explode( ',', $nameList ) as $name ) {
147 $name = trim( $name );
148 if ( $name === '' ) {
151 $extStatus = $this->getExtensionInfo( $type, $directory, $name );
152 if ( $extStatus->isOK() ) {
153 $extensions[] = $name;
155 $status->merge( $extStatus );
159 $extensions = array_unique( $extensions );
160 $status->value
= $extensions;
167 public function execute() {
168 // If APC is available, use that as the MainCacheType, instead of nothing.
169 // This is hacky and should be consolidated with WebInstallerOptions.
170 // This is here instead of in __construct(), because it should run run after
171 // doEnvironmentChecks(), which populates '_Caches'.
172 if ( count( $this->getVar( '_Caches' ) ) ) {
173 // We detected a CACHE_ACCEL implementation, use it.
174 $this->setVar( '_MainCacheType', 'accel' );
177 $vars = Installer
::getExistingLocalSettings();
179 $this->showStatusMessage(
180 Status
::newFatal( "config-localsettings-cli-upgrade" )
184 $this->performInstallation(
185 [ $this, 'startStage' ],
186 [ $this, 'endStage' ]
191 * Write LocalSettings.php to a given path
193 * @param string $path Full path to write LocalSettings.php to
195 public function writeConfigurationFile( $path ) {
196 $ls = InstallerOverrides
::getLocalSettingsGenerator( $this );
197 $ls->writeFile( "$path/LocalSettings.php" );
200 public function startStage( $step ) {
201 // Messages: config-install-database, config-install-tables, config-install-interwiki,
202 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
203 // config-install-extensions
204 $this->showMessage( "config-install-$step" );
207 public function endStage( $step, $status ) {
208 $this->showStatusMessage( $status );
209 $this->showMessage( 'config-install-step-done' );
212 public function showMessage( $msg, ...$params ) {
213 echo $this->getMessageText( $msg, $params ) . "\n";
217 public function showError( $msg, ...$params ) {
218 echo "***{$this->getMessageText( $msg, $params )}***\n";
224 * @param array $params
228 protected function getMessageText( $msg, $params ) {
229 $text = wfMessage( $msg, $params )->parse();
231 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 <$1>', $text );
233 return Sanitizer
::stripAllTags( $text );
239 public function showHelpBox( $msg /*, ... */ ) {
242 public function showStatusMessage( Status
$status ) {
243 $warnings = array_merge( $status->getWarningsArray(),
244 $status->getErrorsArray() );
246 if ( count( $warnings ) !== 0 ) {
247 foreach ( $warnings as $w ) {
248 $this->showMessage( ...$w );
252 if ( !$status->isOK() ) {
258 public function envCheckPath() {
259 if ( !$this->specifiedScriptPath
) {
260 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
263 return parent
::envCheckPath();
266 protected function envGetDefaultServer() {
267 return null; // Do not guess if installing from CLI
270 public function dirIsExecutable( $dir, $url ) {
271 $this->showMessage( 'config-no-cli-uploads-check', $dir );