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
25 * Class for the core installer command line interface.
30 class CliInstaller
extends Installer
{
31 private $specifiedScriptPath = false;
33 private $optionMap = [
34 'dbtype' => 'wgDBtype',
35 'dbserver' => 'wgDBserver',
36 'dbname' => 'wgDBname',
37 'dbuser' => 'wgDBuser',
38 'dbpass' => 'wgDBpassword',
39 'dbprefix' => 'wgDBprefix',
40 'dbtableoptions' => 'wgDBTableOptions',
41 'dbport' => 'wgDBport',
42 'dbschema' => 'wgDBmwschema',
43 'dbpath' => 'wgSQLiteDataDir',
44 'server' => 'wgServer',
45 'scriptpath' => 'wgScriptPath',
49 * @param string $siteName
50 * @param string|null $admin
51 * @param array $option
53 function __construct( $siteName, $admin = null, array $option = [] ) {
56 parent
::__construct();
58 if ( isset( $option['scriptpath'] ) ) {
59 $this->specifiedScriptPath
= true;
62 foreach ( $this->optionMap
as $opt => $global ) {
63 if ( isset( $option[$opt] ) ) {
64 $GLOBALS[$global] = $option[$opt];
65 $this->setVar( $global, $option[$opt] );
69 if ( isset( $option['lang'] ) ) {
70 global $wgLang, $wgLanguageCode;
71 $this->setVar( '_UserLang', $option['lang'] );
72 $wgContLang = Language
::factory( $option['lang'] );
73 $wgLang = Language
::factory( $option['lang'] );
74 $wgLanguageCode = $option['lang'];
75 RequestContext
::getMain()->setLanguage( $wgLang );
78 $this->setVar( 'wgSitename', $siteName );
80 $metaNS = $wgContLang->ucfirst( str_replace( ' ', '_', $siteName ) );
81 if ( $metaNS == 'MediaWiki' ) {
84 $this->setVar( 'wgMetaNamespace', $metaNS );
87 $this->setVar( '_AdminName', $admin );
90 if ( !isset( $option['installdbuser'] ) ) {
91 $this->setVar( '_InstallUser',
92 $this->getVar( 'wgDBuser' ) );
93 $this->setVar( '_InstallPassword',
94 $this->getVar( 'wgDBpassword' ) );
96 $this->setVar( '_InstallUser',
97 $option['installdbuser'] );
98 $this->setVar( '_InstallPassword',
99 $option['installdbpass'] ??
"" );
101 // Assume that if we're given the installer user, we'll create the account.
102 $this->setVar( '_CreateDBAccount', true );
105 if ( isset( $option['pass'] ) ) {
106 $this->setVar( '_AdminPassword', $option['pass'] );
109 // Detect and inject any extension found
110 if ( isset( $option['with-extensions'] ) ) {
111 $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
114 // Set up the default skins
115 $skins = array_keys( $this->findExtensions( 'skins' ) );
116 $this->setVar( '_Skins', $skins );
119 $skinNames = array_map( 'strtolower', $skins );
120 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
127 public function execute() {
128 $vars = Installer
::getExistingLocalSettings();
130 $this->showStatusMessage(
131 Status
::newFatal( "config-localsettings-cli-upgrade" )
135 $this->performInstallation(
136 [ $this, 'startStage' ],
137 [ $this, 'endStage' ]
142 * Write LocalSettings.php to a given path
144 * @param string $path Full path to write LocalSettings.php to
146 public function writeConfigurationFile( $path ) {
147 $ls = InstallerOverrides
::getLocalSettingsGenerator( $this );
148 $ls->writeFile( "$path/LocalSettings.php" );
151 public function startStage( $step ) {
152 // Messages: config-install-database, config-install-tables, config-install-interwiki,
153 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
154 // config-install-extensions
155 $this->showMessage( "config-install-$step" );
158 public function endStage( $step, $status ) {
159 $this->showStatusMessage( $status );
160 $this->showMessage( 'config-install-step-done' );
163 public function showMessage( $msg /*, ... */ ) {
164 echo $this->getMessageText( func_get_args() ) . "\n";
168 public function showError( $msg /*, ... */ ) {
169 echo "***{$this->getMessageText( func_get_args() )}***\n";
174 * @param array $params
178 protected function getMessageText( $params ) {
179 $msg = array_shift( $params );
181 $text = wfMessage( $msg, $params )->parse();
183 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 <$1>', $text );
185 return Sanitizer
::stripAllTags( $text );
191 public function showHelpBox( $msg /*, ... */ ) {
194 public function showStatusMessage( Status
$status ) {
195 $warnings = array_merge( $status->getWarningsArray(),
196 $status->getErrorsArray() );
198 if ( count( $warnings ) !== 0 ) {
199 foreach ( $warnings as $w ) {
200 $this->showMessage( ...$w );
204 if ( !$status->isOK() ) {
210 public function envCheckPath() {
211 if ( !$this->specifiedScriptPath
) {
212 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
215 return parent
::envCheckPath();
218 protected function envGetDefaultServer() {
219 return null; // Do not guess if installing from CLI
222 public function dirIsExecutable( $dir, $url ) {
223 $this->showMessage( 'config-no-cli-uploads-check', $dir );