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 $wgContLang = MediaWikiServices
::getInstance()->getContentLanguage();
76 $wgLang = Language
::factory( $options['lang'] );
77 RequestContext
::getMain()->setLanguage( $wgLang );
80 $this->setVar( 'wgSitename', $siteName );
82 $metaNS = $wgContLang->ucfirst( str_replace( ' ', '_', $siteName ) );
83 if ( $metaNS == 'MediaWiki' ) {
86 $this->setVar( 'wgMetaNamespace', $metaNS );
89 $this->setVar( '_AdminName', $admin );
92 if ( !isset( $options['installdbuser'] ) ) {
93 $this->setVar( '_InstallUser',
94 $this->getVar( 'wgDBuser' ) );
95 $this->setVar( '_InstallPassword',
96 $this->getVar( 'wgDBpassword' ) );
98 $this->setVar( '_InstallUser',
99 $options['installdbuser'] );
100 $this->setVar( '_InstallPassword',
101 $options['installdbpass'] ??
"" );
103 // Assume that if we're given the installer user, we'll create the account.
104 $this->setVar( '_CreateDBAccount', true );
107 if ( isset( $options['pass'] ) ) {
108 $this->setVar( '_AdminPassword', $options['pass'] );
111 // Detect and inject any extension found
112 if ( isset( $options['extensions'] ) ) {
113 $status = $this->validateExtensions(
114 'extension', 'extensions', $options['extensions'] );
115 if ( !$status->isOK() ) {
116 $this->showStatusMessage( $status );
118 $this->setVar( '_Extensions', $status->value
);
119 } elseif ( isset( $options['with-extensions'] ) ) {
120 $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
123 // Set up the default skins
124 if ( isset( $options['skins'] ) ) {
125 $status = $this->validateExtensions( 'skin', 'skins', $options['skins'] );
126 if ( !$status->isOK() ) {
127 $this->showStatusMessage( $status );
129 $skins = $status->value
;
131 $skins = array_keys( $this->findExtensions( 'skins' ) );
133 $this->setVar( '_Skins', $skins );
136 $skinNames = array_map( 'strtolower', $skins );
137 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
141 private function validateExtensions( $type, $directory, $nameLists ) {
143 $status = new Status
;
144 foreach ( (array)$nameLists as $nameList ) {
145 foreach ( explode( ',', $nameList ) as $name ) {
146 $name = trim( $name );
147 if ( $name === '' ) {
150 $extStatus = $this->getExtensionInfo( $type, $directory, $name );
151 if ( $extStatus->isOK() ) {
152 $extensions[] = $name;
154 $status->merge( $extStatus );
158 $extensions = array_unique( $extensions );
159 $status->value
= $extensions;
166 public function execute() {
167 $vars = Installer
::getExistingLocalSettings();
169 $this->showStatusMessage(
170 Status
::newFatal( "config-localsettings-cli-upgrade" )
174 $this->performInstallation(
175 [ $this, 'startStage' ],
176 [ $this, 'endStage' ]
181 * Write LocalSettings.php to a given path
183 * @param string $path Full path to write LocalSettings.php to
185 public function writeConfigurationFile( $path ) {
186 $ls = InstallerOverrides
::getLocalSettingsGenerator( $this );
187 $ls->writeFile( "$path/LocalSettings.php" );
190 public function startStage( $step ) {
191 // Messages: config-install-database, config-install-tables, config-install-interwiki,
192 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
193 // config-install-extensions
194 $this->showMessage( "config-install-$step" );
197 public function endStage( $step, $status ) {
198 $this->showStatusMessage( $status );
199 $this->showMessage( 'config-install-step-done' );
202 public function showMessage( $msg /*, ... */ ) {
203 echo $this->getMessageText( func_get_args() ) . "\n";
207 public function showError( $msg /*, ... */ ) {
208 echo "***{$this->getMessageText( func_get_args() )}***\n";
213 * @param array $params
217 protected function getMessageText( $params ) {
218 $msg = array_shift( $params );
220 $text = wfMessage( $msg, $params )->parse();
222 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 <$1>', $text );
224 return Sanitizer
::stripAllTags( $text );
230 public function showHelpBox( $msg /*, ... */ ) {
233 public function showStatusMessage( Status
$status ) {
234 $warnings = array_merge( $status->getWarningsArray(),
235 $status->getErrorsArray() );
237 if ( count( $warnings ) !== 0 ) {
238 foreach ( $warnings as $w ) {
239 $this->showMessage( ...$w );
243 if ( !$status->isOK() ) {
249 public function envCheckPath() {
250 if ( !$this->specifiedScriptPath
) {
251 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
254 return parent
::envCheckPath();
257 protected function envGetDefaultServer() {
258 return null; // Do not guess if installing from CLI
261 public function dirIsExecutable( $dir, $url ) {
262 $this->showMessage( 'config-no-cli-uploads-check', $dir );