Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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\Installer\InstallException;
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * Class for the core installer command line interface.
29 *
30 * @ingroup Deployment
31 * @since 1.17
32 */
33 class CliInstaller extends Installer {
34 private $specifiedScriptPath = false;
35
36 private $optionMap = [
37 'dbtype' => 'wgDBtype',
38 'dbserver' => 'wgDBserver',
39 'dbname' => 'wgDBname',
40 'dbuser' => 'wgDBuser',
41 'dbpass' => 'wgDBpassword',
42 'dbprefix' => 'wgDBprefix',
43 'dbtableoptions' => 'wgDBTableOptions',
44 'dbport' => 'wgDBport',
45 'dbschema' => 'wgDBmwschema',
46 'dbpath' => 'wgSQLiteDataDir',
47 'server' => 'wgServer',
48 'scriptpath' => 'wgScriptPath',
49 ];
50
51 /**
52 * @param string $siteName
53 * @param string|null $admin
54 * @param array $options
55 * @throws InstallException
56 */
57 function __construct( $siteName, $admin = null, array $options = [] ) {
58 global $wgContLang;
59
60 parent::__construct();
61
62 if ( isset( $options['scriptpath'] ) ) {
63 $this->specifiedScriptPath = true;
64 }
65
66 foreach ( $this->optionMap as $opt => $global ) {
67 if ( isset( $options[$opt] ) ) {
68 $GLOBALS[$global] = $options[$opt];
69 $this->setVar( $global, $options[$opt] );
70 }
71 }
72
73 if ( isset( $options['lang'] ) ) {
74 global $wgLang, $wgLanguageCode;
75 $this->setVar( '_UserLang', $options['lang'] );
76 $wgLanguageCode = $options['lang'];
77 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
78 $wgContLang = MediaWikiServices::getInstance()->getContentLanguage();
79 $wgLang = Language::factory( $options['lang'] );
80 RequestContext::getMain()->setLanguage( $wgLang );
81 }
82
83 $this->setVar( 'wgSitename', $siteName );
84
85 $metaNS = $wgContLang->ucfirst( str_replace( ' ', '_', $siteName ) );
86 if ( $metaNS == 'MediaWiki' ) {
87 $metaNS = 'Project';
88 }
89 $this->setVar( 'wgMetaNamespace', $metaNS );
90
91 if ( $admin ) {
92 $this->setVar( '_AdminName', $admin );
93 }
94
95 if ( !isset( $options['installdbuser'] ) ) {
96 $this->setVar( '_InstallUser',
97 $this->getVar( 'wgDBuser' ) );
98 $this->setVar( '_InstallPassword',
99 $this->getVar( 'wgDBpassword' ) );
100 } else {
101 $this->setVar( '_InstallUser',
102 $options['installdbuser'] );
103 $this->setVar( '_InstallPassword',
104 $options['installdbpass'] ?? "" );
105
106 // Assume that if we're given the installer user, we'll create the account.
107 $this->setVar( '_CreateDBAccount', true );
108 }
109
110 if ( isset( $options['pass'] ) ) {
111 $this->setVar( '_AdminPassword', $options['pass'] );
112 }
113
114 // Detect and inject any extension found
115 if ( isset( $options['extensions'] ) ) {
116 $status = $this->validateExtensions(
117 'extension', 'extensions', $options['extensions'] );
118 if ( !$status->isOK() ) {
119 throw new InstallException( $status );
120 }
121 $this->setVar( '_Extensions', $status->value );
122 } elseif ( isset( $options['with-extensions'] ) ) {
123 $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
124 }
125
126 // Set up the default skins
127 if ( isset( $options['skins'] ) ) {
128 $status = $this->validateExtensions( 'skin', 'skins', $options['skins'] );
129 if ( !$status->isOK() ) {
130 throw new InstallException( $status );
131 }
132 $skins = $status->value;
133 } else {
134 $skins = array_keys( $this->findExtensions( 'skins' ) );
135 }
136 $this->setVar( '_Skins', $skins );
137
138 if ( $skins ) {
139 $skinNames = array_map( 'strtolower', $skins );
140 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
141 }
142 }
143
144 private function validateExtensions( $type, $directory, $nameLists ) {
145 $extensions = [];
146 $status = new Status;
147 foreach ( (array)$nameLists as $nameList ) {
148 foreach ( explode( ',', $nameList ) as $name ) {
149 $name = trim( $name );
150 if ( $name === '' ) {
151 continue;
152 }
153 $extStatus = $this->getExtensionInfo( $type, $directory, $name );
154 if ( $extStatus->isOK() ) {
155 $extensions[] = $name;
156 } else {
157 $status->merge( $extStatus );
158 }
159 }
160 }
161 $extensions = array_unique( $extensions );
162 $status->value = $extensions;
163 return $status;
164 }
165
166 /**
167 * Main entry point.
168 */
169 public function execute() {
170 // If APC is available, use that as the MainCacheType, instead of nothing.
171 // This is hacky and should be consolidated with WebInstallerOptions.
172 // This is here instead of in __construct(), because it should run run after
173 // doEnvironmentChecks(), which populates '_Caches'.
174 if ( count( $this->getVar( '_Caches' ) ) ) {
175 // We detected a CACHE_ACCEL implementation, use it.
176 $this->setVar( '_MainCacheType', 'accel' );
177 }
178
179 $vars = Installer::getExistingLocalSettings();
180 if ( $vars ) {
181 $status = Status::newFatal( "config-localsettings-cli-upgrade" );
182 $this->showStatusMessage( $status );
183 return $status;
184 }
185
186 $result = $this->performInstallation(
187 [ $this, 'startStage' ],
188 [ $this, 'endStage' ]
189 );
190 // PerformInstallation bails on a fatal, so make sure the last item
191 // completed before giving 'next.' Likewise, only provide back on failure
192 $lastStepStatus = end( $result );
193 if ( $lastStepStatus->isOK() ) {
194 return Status::newGood();
195 } else {
196 return $lastStepStatus;
197 }
198 }
199
200 /**
201 * Write LocalSettings.php to a given path
202 *
203 * @param string $path Full path to write LocalSettings.php to
204 */
205 public function writeConfigurationFile( $path ) {
206 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
207 $ls->writeFile( "$path/LocalSettings.php" );
208 }
209
210 public function startStage( $step ) {
211 // Messages: config-install-database, config-install-tables, config-install-interwiki,
212 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
213 // config-install-extensions
214 $this->showMessage( "config-install-$step" );
215 }
216
217 public function endStage( $step, $status ) {
218 $this->showStatusMessage( $status );
219 $this->showMessage( 'config-install-step-done' );
220 }
221
222 public function showMessage( $msg, ...$params ) {
223 echo $this->getMessageText( $msg, $params ) . "\n";
224 flush();
225 }
226
227 public function showError( $msg, ...$params ) {
228 echo "***{$this->getMessageText( $msg, $params )}***\n";
229 flush();
230 }
231
232 /**
233 * @param string $msg
234 * @param array $params
235 *
236 * @return string
237 */
238 protected function getMessageText( $msg, $params ) {
239 $text = wfMessage( $msg, $params )->parse();
240
241 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
242
243 return Sanitizer::stripAllTags( $text );
244 }
245
246 /**
247 * Dummy
248 */
249 public function showHelpBox( $msg /*, ... */ ) {
250 }
251
252 public function showStatusMessage( Status $status ) {
253 $warnings = array_merge( $status->getWarningsArray(),
254 $status->getErrorsArray() );
255
256 if ( count( $warnings ) !== 0 ) {
257 foreach ( $warnings as $w ) {
258 $this->showMessage( ...$w );
259 }
260 }
261 }
262
263 public function envCheckPath() {
264 if ( !$this->specifiedScriptPath ) {
265 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
266 }
267
268 return parent::envCheckPath();
269 }
270
271 protected function envGetDefaultServer() {
272 return null; // Do not guess if installing from CLI
273 }
274
275 public function dirIsExecutable( $dir, $url ) {
276 $this->showMessage( 'config-no-cli-uploads-check', $dir );
277
278 return false;
279 }
280 }