Completed
Branch master (939199)
by
unknown
39:35
created

includes/installer/CliInstaller.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Core installer command line interface.
4
 *
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.
9
 *
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.
14
 *
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
19
 *
20
 * @file
21
 * @ingroup Deployment
22
 */
23
24
/**
25
 * Class for the core installer command line interface.
26
 *
27
 * @ingroup Deployment
28
 * @since 1.17
29
 */
30
class CliInstaller extends Installer {
31
	private $specifiedScriptPath = false;
32
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
		'dbmysql5' => 'wgDBmysql5',
42
		'dbport' => 'wgDBport',
43
		'dbschema' => 'wgDBmwschema',
44
		'dbpath' => 'wgSQLiteDataDir',
45
		'server' => 'wgServer',
46
		'scriptpath' => 'wgScriptPath',
47
	];
48
49
	/**
50
	 * Constructor.
51
	 *
52
	 * @param string $siteName
53
	 * @param string $admin
54
	 * @param array $option
55
	 */
56
	function __construct( $siteName, $admin = null, array $option = [] ) {
57
		global $wgContLang;
58
59
		parent::__construct();
60
61
		if ( isset( $option['scriptpath'] ) ) {
62
			$this->specifiedScriptPath = true;
63
		}
64
65
		foreach ( $this->optionMap as $opt => $global ) {
66
			if ( isset( $option[$opt] ) ) {
67
				$GLOBALS[$global] = $option[$opt];
68
				$this->setVar( $global, $option[$opt] );
69
			}
70
		}
71
72
		if ( isset( $option['lang'] ) ) {
73
			global $wgLang, $wgLanguageCode;
74
			$this->setVar( '_UserLang', $option['lang'] );
75
			$wgContLang = Language::factory( $option['lang'] );
76
			$wgLang = Language::factory( $option['lang'] );
77
			$wgLanguageCode = $option['lang'];
78
			RequestContext::getMain()->setLanguage( $wgLang );
79
		}
80
81
		$this->setVar( 'wgSitename', $siteName );
82
83
		$metaNS = $wgContLang->ucfirst( str_replace( ' ', '_', $siteName ) );
84
		if ( $metaNS == 'MediaWiki' ) {
85
			$metaNS = 'Project';
86
		}
87
		$this->setVar( 'wgMetaNamespace', $metaNS );
88
89
		if ( $admin ) {
90
			$this->setVar( '_AdminName', $admin );
91
		}
92
93
		if ( !isset( $option['installdbuser'] ) ) {
94
			$this->setVar( '_InstallUser',
95
				$this->getVar( 'wgDBuser' ) );
96
			$this->setVar( '_InstallPassword',
97
				$this->getVar( 'wgDBpassword' ) );
98
		} else {
99
			$this->setVar( '_InstallUser',
100
				$option['installdbuser'] );
101
			$this->setVar( '_InstallPassword',
102
				isset( $option['installdbpass'] ) ? $option['installdbpass'] : "" );
103
104
			// Assume that if we're given the installer user, we'll create the account.
105
			$this->setVar( '_CreateDBAccount', true );
106
		}
107
108
		if ( isset( $option['pass'] ) ) {
109
			$this->setVar( '_AdminPassword', $option['pass'] );
110
		}
111
112
		// Set up the default skins
113
		$skins = $this->findExtensions( 'skins' );
114
		$this->setVar( '_Skins', $skins );
115
116
		if ( $skins ) {
117
			$skinNames = array_map( 'strtolower', $skins );
118
			$this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
119
		}
120
	}
121
122
	/**
123
	 * Main entry point.
124
	 */
125
	public function execute() {
126
		$vars = Installer::getExistingLocalSettings();
127
		if ( $vars ) {
128
			$this->showStatusMessage(
129
				Status::newFatal( "config-localsettings-cli-upgrade" )
130
			);
131
		}
132
133
		$this->performInstallation(
134
			[ $this, 'startStage' ],
135
			[ $this, 'endStage' ]
136
		);
137
	}
138
139
	/**
140
	 * Write LocalSettings.php to a given path
141
	 *
142
	 * @param string $path Full path to write LocalSettings.php to
143
	 */
144
	public function writeConfigurationFile( $path ) {
145
		$ls = InstallerOverrides::getLocalSettingsGenerator( $this );
146
		$ls->writeFile( "$path/LocalSettings.php" );
147
	}
148
149
	public function startStage( $step ) {
150
		// Messages: config-install-database, config-install-tables, config-install-interwiki,
151
		// config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
152
		// config-install-extensions
153
		$this->showMessage( "config-install-$step" );
154
	}
155
156
	public function endStage( $step, $status ) {
157
		$this->showStatusMessage( $status );
158
		$this->showMessage( 'config-install-step-done' );
159
	}
160
161
	public function showMessage( $msg /*, ... */ ) {
162
		echo $this->getMessageText( func_get_args() ) . "\n";
163
		flush();
164
	}
165
166
	public function showError( $msg /*, ... */ ) {
167
		echo "***{$this->getMessageText( func_get_args() )}***\n";
168
		flush();
169
	}
170
171
	/**
172
	 * @param array $params
173
	 *
174
	 * @return string
175
	 */
176
	protected function getMessageText( $params ) {
177
		$msg = array_shift( $params );
178
179
		$text = wfMessage( $msg, $params )->parse();
180
181
		$text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
182
183
		return html_entity_decode( strip_tags( $text ), ENT_QUOTES );
184
	}
185
186
	/**
187
	 * Dummy
188
	 */
189
	public function showHelpBox( $msg /*, ... */ ) {
190
	}
191
192
	public function showStatusMessage( Status $status ) {
193
		$warnings = array_merge( $status->getWarningsArray(),
194
			$status->getErrorsArray() );
195
196
		if ( count( $warnings ) !== 0 ) {
197
			foreach ( $warnings as $w ) {
198
				call_user_func_array( [ $this, 'showMessage' ], $w );
199
			}
200
		}
201
202
		if ( !$status->isOK() ) {
203
			echo "\n";
204
			exit( 1 );
0 ignored issues
show
Coding Style Compatibility introduced by
The method showStatusMessage() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
205
		}
206
	}
207
208
	public function envCheckPath() {
209
		if ( !$this->specifiedScriptPath ) {
210
			$this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
211
		}
212
213
		return parent::envCheckPath();
214
	}
215
216
	protected function envGetDefaultServer() {
217
		return null; // Do not guess if installing from CLI
218
	}
219
220
	public function dirIsExecutable( $dir, $url ) {
221
		$this->showMessage( 'config-no-cli-uploads-check', $dir );
222
223
		return false;
224
	}
225
}
226