Mass conversion of $wgContLang to service
[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 $option
54 */
55 function __construct( $siteName, $admin = null, array $option = [] ) {
56 global $wgContLang;
57
58 parent::__construct();
59
60 if ( isset( $option['scriptpath'] ) ) {
61 $this->specifiedScriptPath = true;
62 }
63
64 foreach ( $this->optionMap as $opt => $global ) {
65 if ( isset( $option[$opt] ) ) {
66 $GLOBALS[$global] = $option[$opt];
67 $this->setVar( $global, $option[$opt] );
68 }
69 }
70
71 if ( isset( $option['lang'] ) ) {
72 global $wgLang, $wgLanguageCode;
73 $this->setVar( '_UserLang', $option['lang'] );
74 $wgLanguageCode = $option['lang'];
75 $wgContLang = MediaWikiServices::getInstance()->getContentLanguage();
76 $wgLang = Language::factory( $option['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( $option['installdbuser'] ) ) {
93 $this->setVar( '_InstallUser',
94 $this->getVar( 'wgDBuser' ) );
95 $this->setVar( '_InstallPassword',
96 $this->getVar( 'wgDBpassword' ) );
97 } else {
98 $this->setVar( '_InstallUser',
99 $option['installdbuser'] );
100 $this->setVar( '_InstallPassword',
101 $option['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( $option['pass'] ) ) {
108 $this->setVar( '_AdminPassword', $option['pass'] );
109 }
110
111 // Detect and inject any extension found
112 if ( isset( $option['with-extensions'] ) ) {
113 $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
114 }
115
116 // Set up the default skins
117 $skins = array_keys( $this->findExtensions( 'skins' ) );
118 $this->setVar( '_Skins', $skins );
119
120 if ( $skins ) {
121 $skinNames = array_map( 'strtolower', $skins );
122 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
123 }
124 }
125
126 /**
127 * Main entry point.
128 */
129 public function execute() {
130 $vars = Installer::getExistingLocalSettings();
131 if ( $vars ) {
132 $this->showStatusMessage(
133 Status::newFatal( "config-localsettings-cli-upgrade" )
134 );
135 }
136
137 $this->performInstallation(
138 [ $this, 'startStage' ],
139 [ $this, 'endStage' ]
140 );
141 }
142
143 /**
144 * Write LocalSettings.php to a given path
145 *
146 * @param string $path Full path to write LocalSettings.php to
147 */
148 public function writeConfigurationFile( $path ) {
149 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
150 $ls->writeFile( "$path/LocalSettings.php" );
151 }
152
153 public function startStage( $step ) {
154 // Messages: config-install-database, config-install-tables, config-install-interwiki,
155 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
156 // config-install-extensions
157 $this->showMessage( "config-install-$step" );
158 }
159
160 public function endStage( $step, $status ) {
161 $this->showStatusMessage( $status );
162 $this->showMessage( 'config-install-step-done' );
163 }
164
165 public function showMessage( $msg /*, ... */ ) {
166 echo $this->getMessageText( func_get_args() ) . "\n";
167 flush();
168 }
169
170 public function showError( $msg /*, ... */ ) {
171 echo "***{$this->getMessageText( func_get_args() )}***\n";
172 flush();
173 }
174
175 /**
176 * @param array $params
177 *
178 * @return string
179 */
180 protected function getMessageText( $params ) {
181 $msg = array_shift( $params );
182
183 $text = wfMessage( $msg, $params )->parse();
184
185 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
186
187 return Sanitizer::stripAllTags( $text );
188 }
189
190 /**
191 * Dummy
192 */
193 public function showHelpBox( $msg /*, ... */ ) {
194 }
195
196 public function showStatusMessage( Status $status ) {
197 $warnings = array_merge( $status->getWarningsArray(),
198 $status->getErrorsArray() );
199
200 if ( count( $warnings ) !== 0 ) {
201 foreach ( $warnings as $w ) {
202 $this->showMessage( ...$w );
203 }
204 }
205
206 if ( !$status->isOK() ) {
207 echo "\n";
208 exit( 1 );
209 }
210 }
211
212 public function envCheckPath() {
213 if ( !$this->specifiedScriptPath ) {
214 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
215 }
216
217 return parent::envCheckPath();
218 }
219
220 protected function envGetDefaultServer() {
221 return null; // Do not guess if installing from CLI
222 }
223
224 public function dirIsExecutable( $dir, $url ) {
225 $this->showMessage( 'config-no-cli-uploads-check', $dir );
226
227 return false;
228 }
229 }