Save value from CLI installers `--lang` argument
[lhc/web/wiklou.git] / includes / installer / CliInstaller.php
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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Class for the core installer command line interface.
28 *
29 * @ingroup Deployment
30 * @since 1.17
31 */
32 class CliInstaller extends Installer {
33 private $specifiedScriptPath = false;
34
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',
48 ];
49
50 /**
51 * @param string $siteName
52 * @param string|null $admin
53 * @param array $options
54 */
55 function __construct( $siteName, $admin = null, array $options = [] ) {
56 global $wgContLang;
57
58 parent::__construct();
59
60 if ( isset( $options['scriptpath'] ) ) {
61 $this->specifiedScriptPath = true;
62 }
63
64 foreach ( $this->optionMap as $opt => $global ) {
65 if ( isset( $options[$opt] ) ) {
66 $GLOBALS[$global] = $options[$opt];
67 $this->setVar( $global, $options[$opt] );
68 }
69 }
70
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 );
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( $options['installdbuser'] ) ) {
94 $this->setVar( '_InstallUser',
95 $this->getVar( 'wgDBuser' ) );
96 $this->setVar( '_InstallPassword',
97 $this->getVar( 'wgDBpassword' ) );
98 } else {
99 $this->setVar( '_InstallUser',
100 $options['installdbuser'] );
101 $this->setVar( '_InstallPassword',
102 $options['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( $options['pass'] ) ) {
109 $this->setVar( '_AdminPassword', $options['pass'] );
110 }
111
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 );
118 }
119 $this->setVar( '_Extensions', $status->value );
120 } elseif ( isset( $options['with-extensions'] ) ) {
121 $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
122 }
123
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 );
129 }
130 $skins = $status->value;
131 } else {
132 $skins = array_keys( $this->findExtensions( 'skins' ) );
133 }
134 $this->setVar( '_Skins', $skins );
135
136 if ( $skins ) {
137 $skinNames = array_map( 'strtolower', $skins );
138 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
139 }
140 }
141
142 private function validateExtensions( $type, $directory, $nameLists ) {
143 $extensions = [];
144 $status = new Status;
145 foreach ( (array)$nameLists as $nameList ) {
146 foreach ( explode( ',', $nameList ) as $name ) {
147 $name = trim( $name );
148 if ( $name === '' ) {
149 continue;
150 }
151 $extStatus = $this->getExtensionInfo( $type, $directory, $name );
152 if ( $extStatus->isOK() ) {
153 $extensions[] = $name;
154 } else {
155 $status->merge( $extStatus );
156 }
157 }
158 }
159 $extensions = array_unique( $extensions );
160 $status->value = $extensions;
161 return $status;
162 }
163
164 /**
165 * Main entry point.
166 */
167 public function execute() {
168 $vars = Installer::getExistingLocalSettings();
169 if ( $vars ) {
170 $this->showStatusMessage(
171 Status::newFatal( "config-localsettings-cli-upgrade" )
172 );
173 }
174
175 $this->performInstallation(
176 [ $this, 'startStage' ],
177 [ $this, 'endStage' ]
178 );
179 }
180
181 /**
182 * Write LocalSettings.php to a given path
183 *
184 * @param string $path Full path to write LocalSettings.php to
185 */
186 public function writeConfigurationFile( $path ) {
187 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
188 $ls->writeFile( "$path/LocalSettings.php" );
189 }
190
191 public function startStage( $step ) {
192 // Messages: config-install-database, config-install-tables, config-install-interwiki,
193 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
194 // config-install-extensions
195 $this->showMessage( "config-install-$step" );
196 }
197
198 public function endStage( $step, $status ) {
199 $this->showStatusMessage( $status );
200 $this->showMessage( 'config-install-step-done' );
201 }
202
203 public function showMessage( $msg /*, ... */ ) {
204 echo $this->getMessageText( func_get_args() ) . "\n";
205 flush();
206 }
207
208 public function showError( $msg /*, ... */ ) {
209 echo "***{$this->getMessageText( func_get_args() )}***\n";
210 flush();
211 }
212
213 /**
214 * @param array $params
215 *
216 * @return string
217 */
218 protected function getMessageText( $params ) {
219 $msg = array_shift( $params );
220
221 $text = wfMessage( $msg, $params )->parse();
222
223 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
224
225 return Sanitizer::stripAllTags( $text );
226 }
227
228 /**
229 * Dummy
230 */
231 public function showHelpBox( $msg /*, ... */ ) {
232 }
233
234 public function showStatusMessage( Status $status ) {
235 $warnings = array_merge( $status->getWarningsArray(),
236 $status->getErrorsArray() );
237
238 if ( count( $warnings ) !== 0 ) {
239 foreach ( $warnings as $w ) {
240 $this->showMessage( ...$w );
241 }
242 }
243
244 if ( !$status->isOK() ) {
245 echo "\n";
246 exit( 1 );
247 }
248 }
249
250 public function envCheckPath() {
251 if ( !$this->specifiedScriptPath ) {
252 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
253 }
254
255 return parent::envCheckPath();
256 }
257
258 protected function envGetDefaultServer() {
259 return null; // Do not guess if installing from CLI
260 }
261
262 public function dirIsExecutable( $dir, $url ) {
263 $this->showMessage( 'config-no-cli-uploads-check', $dir );
264
265 return false;
266 }
267 }