Merge "Revert "Unwrap HTML loaded from parser cache""
[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 $wgContLang = MediaWikiServices::getInstance()->getContentLanguage();
76 $wgLang = Language::factory( $options['lang'] );
77 RequestContext::getMain()->setLanguage( $wgLang );
78 }
79
80 $this->setVar( 'wgSitename', $siteName );
81
82 $metaNS = $wgContLang->ucfirst( str_replace( ' ', '_', $siteName ) );
83 if ( $metaNS == 'MediaWiki' ) {
84 $metaNS = 'Project';
85 }
86 $this->setVar( 'wgMetaNamespace', $metaNS );
87
88 if ( $admin ) {
89 $this->setVar( '_AdminName', $admin );
90 }
91
92 if ( !isset( $options['installdbuser'] ) ) {
93 $this->setVar( '_InstallUser',
94 $this->getVar( 'wgDBuser' ) );
95 $this->setVar( '_InstallPassword',
96 $this->getVar( 'wgDBpassword' ) );
97 } else {
98 $this->setVar( '_InstallUser',
99 $options['installdbuser'] );
100 $this->setVar( '_InstallPassword',
101 $options['installdbpass'] ?? "" );
102
103 // Assume that if we're given the installer user, we'll create the account.
104 $this->setVar( '_CreateDBAccount', true );
105 }
106
107 if ( isset( $options['pass'] ) ) {
108 $this->setVar( '_AdminPassword', $options['pass'] );
109 }
110
111 // Detect and inject any extension found
112 if ( isset( $options['extensions'] ) ) {
113 $status = $this->validateExtensions(
114 'extension', 'extensions', $options['extensions'] );
115 if ( !$status->isOK() ) {
116 $this->showStatusMessage( $status );
117 }
118 $this->setVar( '_Extensions', $status->value );
119 } elseif ( isset( $options['with-extensions'] ) ) {
120 $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
121 }
122
123 // Set up the default skins
124 if ( isset( $options['skins'] ) ) {
125 $status = $this->validateExtensions( 'skin', 'skins', $options['skins'] );
126 if ( !$status->isOK() ) {
127 $this->showStatusMessage( $status );
128 }
129 $skins = $status->value;
130 } else {
131 $skins = array_keys( $this->findExtensions( 'skins' ) );
132 }
133 $this->setVar( '_Skins', $skins );
134
135 if ( $skins ) {
136 $skinNames = array_map( 'strtolower', $skins );
137 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
138 }
139 }
140
141 private function validateExtensions( $type, $directory, $nameLists ) {
142 $extensions = [];
143 $status = new Status;
144 foreach ( (array)$nameLists as $nameList ) {
145 foreach ( explode( ',', $nameList ) as $name ) {
146 $name = trim( $name );
147 if ( $name === '' ) {
148 continue;
149 }
150 $extStatus = $this->getExtensionInfo( $type, $directory, $name );
151 if ( $extStatus->isOK() ) {
152 $extensions[] = $name;
153 } else {
154 $status->merge( $extStatus );
155 }
156 }
157 }
158 $extensions = array_unique( $extensions );
159 $status->value = $extensions;
160 return $status;
161 }
162
163 /**
164 * Main entry point.
165 */
166 public function execute() {
167 $vars = Installer::getExistingLocalSettings();
168 if ( $vars ) {
169 $this->showStatusMessage(
170 Status::newFatal( "config-localsettings-cli-upgrade" )
171 );
172 }
173
174 $this->performInstallation(
175 [ $this, 'startStage' ],
176 [ $this, 'endStage' ]
177 );
178 }
179
180 /**
181 * Write LocalSettings.php to a given path
182 *
183 * @param string $path Full path to write LocalSettings.php to
184 */
185 public function writeConfigurationFile( $path ) {
186 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
187 $ls->writeFile( "$path/LocalSettings.php" );
188 }
189
190 public function startStage( $step ) {
191 // Messages: config-install-database, config-install-tables, config-install-interwiki,
192 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
193 // config-install-extensions
194 $this->showMessage( "config-install-$step" );
195 }
196
197 public function endStage( $step, $status ) {
198 $this->showStatusMessage( $status );
199 $this->showMessage( 'config-install-step-done' );
200 }
201
202 public function showMessage( $msg /*, ... */ ) {
203 echo $this->getMessageText( func_get_args() ) . "\n";
204 flush();
205 }
206
207 public function showError( $msg /*, ... */ ) {
208 echo "***{$this->getMessageText( func_get_args() )}***\n";
209 flush();
210 }
211
212 /**
213 * @param array $params
214 *
215 * @return string
216 */
217 protected function getMessageText( $params ) {
218 $msg = array_shift( $params );
219
220 $text = wfMessage( $msg, $params )->parse();
221
222 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
223
224 return Sanitizer::stripAllTags( $text );
225 }
226
227 /**
228 * Dummy
229 */
230 public function showHelpBox( $msg /*, ... */ ) {
231 }
232
233 public function showStatusMessage( Status $status ) {
234 $warnings = array_merge( $status->getWarningsArray(),
235 $status->getErrorsArray() );
236
237 if ( count( $warnings ) !== 0 ) {
238 foreach ( $warnings as $w ) {
239 $this->showMessage( ...$w );
240 }
241 }
242
243 if ( !$status->isOK() ) {
244 echo "\n";
245 exit( 1 );
246 }
247 }
248
249 public function envCheckPath() {
250 if ( !$this->specifiedScriptPath ) {
251 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
252 }
253
254 return parent::envCheckPath();
255 }
256
257 protected function envGetDefaultServer() {
258 return null; // Do not guess if installing from CLI
259 }
260
261 public function dirIsExecutable( $dir, $url ) {
262 $this->showMessage( 'config-no-cli-uploads-check', $dir );
263
264 return false;
265 }
266 }