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 $option
55 function __construct( $siteName, $admin = null, array $option = [] ) {
58 parent
::__construct();
60 if ( isset( $option['scriptpath'] ) ) {
61 $this->specifiedScriptPath
= true;
64 foreach ( $this->optionMap
as $opt => $global ) {
65 if ( isset( $option[$opt] ) ) {
66 $GLOBALS[$global] = $option[$opt];
67 $this->setVar( $global, $option[$opt] );
71 if ( isset( $option['lang'] ) ) {
72 global $wgLang, $wgLanguageCode;
73 $this->setVar( '_UserLang', $option['lang'] );
74 $wgLanguageCode = $option['lang'];
75 $wgContLang = MediaWikiServices
::getInstance()->getContentLanguage();
76 $wgLang = Language
::factory( $option['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( $option['installdbuser'] ) ) {
93 $this->setVar( '_InstallUser',
94 $this->getVar( 'wgDBuser' ) );
95 $this->setVar( '_InstallPassword',
96 $this->getVar( 'wgDBpassword' ) );
98 $this->setVar( '_InstallUser',
99 $option['installdbuser'] );
100 $this->setVar( '_InstallPassword',
101 $option['installdbpass'] ??
"" );
103 // Assume that if we're given the installer user, we'll create the account.
104 $this->setVar( '_CreateDBAccount', true );
107 if ( isset( $option['pass'] ) ) {
108 $this->setVar( '_AdminPassword', $option['pass'] );
111 // Detect and inject any extension found
112 if ( isset( $option['with-extensions'] ) ) {
113 $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
116 // Set up the default skins
117 $skins = array_keys( $this->findExtensions( 'skins' ) );
118 $this->setVar( '_Skins', $skins );
121 $skinNames = array_map( 'strtolower', $skins );
122 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
129 public function execute() {
130 $vars = Installer
::getExistingLocalSettings();
132 $this->showStatusMessage(
133 Status
::newFatal( "config-localsettings-cli-upgrade" )
137 $this->performInstallation(
138 [ $this, 'startStage' ],
139 [ $this, 'endStage' ]
144 * Write LocalSettings.php to a given path
146 * @param string $path Full path to write LocalSettings.php to
148 public function writeConfigurationFile( $path ) {
149 $ls = InstallerOverrides
::getLocalSettingsGenerator( $this );
150 $ls->writeFile( "$path/LocalSettings.php" );
153 public function startStage( $step ) {
154 // Messages: config-install-database, config-install-tables, config-install-interwiki,
155 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
156 // config-install-extensions
157 $this->showMessage( "config-install-$step" );
160 public function endStage( $step, $status ) {
161 $this->showStatusMessage( $status );
162 $this->showMessage( 'config-install-step-done' );
165 public function showMessage( $msg /*, ... */ ) {
166 echo $this->getMessageText( func_get_args() ) . "\n";
170 public function showError( $msg /*, ... */ ) {
171 echo "***{$this->getMessageText( func_get_args() )}***\n";
176 * @param array $params
180 protected function getMessageText( $params ) {
181 $msg = array_shift( $params );
183 $text = wfMessage( $msg, $params )->parse();
185 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 <$1>', $text );
187 return Sanitizer
::stripAllTags( $text );
193 public function showHelpBox( $msg /*, ... */ ) {
196 public function showStatusMessage( Status
$status ) {
197 $warnings = array_merge( $status->getWarningsArray(),
198 $status->getErrorsArray() );
200 if ( count( $warnings ) !== 0 ) {
201 foreach ( $warnings as $w ) {
202 $this->showMessage( ...$w );
206 if ( !$status->isOK() ) {
212 public function envCheckPath() {
213 if ( !$this->specifiedScriptPath
) {
214 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
217 return parent
::envCheckPath();
220 protected function envGetDefaultServer() {
221 return null; // Do not guess if installing from CLI
224 public function dirIsExecutable( $dir, $url ) {
225 $this->showMessage( 'config-no-cli-uploads-check', $dir );