Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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 // If APC is available, use that as the MainCacheType, instead of nothing.
169 // This is hacky and should be consolidated with WebInstallerOptions.
170 // This is here instead of in __construct(), because it should run run after
171 // doEnvironmentChecks(), which populates '_Caches'.
172 if ( count( $this->getVar( '_Caches' ) ) ) {
173 // We detected a CACHE_ACCEL implementation, use it.
174 $this->setVar( '_MainCacheType', 'accel' );
175 }
176
177 $vars = Installer::getExistingLocalSettings();
178 if ( $vars ) {
179 $this->showStatusMessage(
180 Status::newFatal( "config-localsettings-cli-upgrade" )
181 );
182 }
183
184 $this->performInstallation(
185 [ $this, 'startStage' ],
186 [ $this, 'endStage' ]
187 );
188 }
189
190 /**
191 * Write LocalSettings.php to a given path
192 *
193 * @param string $path Full path to write LocalSettings.php to
194 */
195 public function writeConfigurationFile( $path ) {
196 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
197 $ls->writeFile( "$path/LocalSettings.php" );
198 }
199
200 public function startStage( $step ) {
201 // Messages: config-install-database, config-install-tables, config-install-interwiki,
202 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
203 // config-install-extensions
204 $this->showMessage( "config-install-$step" );
205 }
206
207 public function endStage( $step, $status ) {
208 $this->showStatusMessage( $status );
209 $this->showMessage( 'config-install-step-done' );
210 }
211
212 public function showMessage( $msg, ...$params ) {
213 echo $this->getMessageText( $msg, $params ) . "\n";
214 flush();
215 }
216
217 public function showError( $msg, ...$params ) {
218 echo "***{$this->getMessageText( $msg, $params )}***\n";
219 flush();
220 }
221
222 /**
223 * @param string $msg
224 * @param array $params
225 *
226 * @return string
227 */
228 protected function getMessageText( $msg, $params ) {
229 $text = wfMessage( $msg, $params )->parse();
230
231 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
232
233 return Sanitizer::stripAllTags( $text );
234 }
235
236 /**
237 * Dummy
238 */
239 public function showHelpBox( $msg /*, ... */ ) {
240 }
241
242 public function showStatusMessage( Status $status ) {
243 $warnings = array_merge( $status->getWarningsArray(),
244 $status->getErrorsArray() );
245
246 if ( count( $warnings ) !== 0 ) {
247 foreach ( $warnings as $w ) {
248 $this->showMessage( ...$w );
249 }
250 }
251
252 if ( !$status->isOK() ) {
253 echo "\n";
254 exit( 1 );
255 }
256 }
257
258 public function envCheckPath() {
259 if ( !$this->specifiedScriptPath ) {
260 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
261 }
262
263 return parent::envCheckPath();
264 }
265
266 protected function envGetDefaultServer() {
267 return null; // Do not guess if installing from CLI
268 }
269
270 public function dirIsExecutable( $dir, $url ) {
271 $this->showMessage( 'config-no-cli-uploads-check', $dir );
272
273 return false;
274 }
275 }