Merge "installer: Don't allow setting $wgDBmysql5" into REL1_31
[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 /**
25 * Class for the core installer command line interface.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class CliInstaller extends Installer {
31 private $specifiedScriptPath = false;
32
33 private $optionMap = [
34 'dbtype' => 'wgDBtype',
35 'dbserver' => 'wgDBserver',
36 'dbname' => 'wgDBname',
37 'dbuser' => 'wgDBuser',
38 'dbpass' => 'wgDBpassword',
39 'dbprefix' => 'wgDBprefix',
40 'dbtableoptions' => 'wgDBTableOptions',
41 'dbport' => 'wgDBport',
42 'dbschema' => 'wgDBmwschema',
43 'dbpath' => 'wgSQLiteDataDir',
44 'server' => 'wgServer',
45 'scriptpath' => 'wgScriptPath',
46 ];
47
48 /**
49 * @param string $siteName
50 * @param string $admin
51 * @param array $option
52 */
53 function __construct( $siteName, $admin = null, array $option = [] ) {
54 global $wgContLang;
55
56 parent::__construct();
57
58 if ( isset( $option['scriptpath'] ) ) {
59 $this->specifiedScriptPath = true;
60 }
61
62 foreach ( $this->optionMap as $opt => $global ) {
63 if ( isset( $option[$opt] ) ) {
64 $GLOBALS[$global] = $option[$opt];
65 $this->setVar( $global, $option[$opt] );
66 }
67 }
68
69 if ( isset( $option['lang'] ) ) {
70 global $wgLang, $wgLanguageCode;
71 $this->setVar( '_UserLang', $option['lang'] );
72 $wgContLang = Language::factory( $option['lang'] );
73 $wgLang = Language::factory( $option['lang'] );
74 $wgLanguageCode = $option['lang'];
75 RequestContext::getMain()->setLanguage( $wgLang );
76 }
77
78 $this->setVar( 'wgSitename', $siteName );
79
80 $metaNS = $wgContLang->ucfirst( str_replace( ' ', '_', $siteName ) );
81 if ( $metaNS == 'MediaWiki' ) {
82 $metaNS = 'Project';
83 }
84 $this->setVar( 'wgMetaNamespace', $metaNS );
85
86 if ( $admin ) {
87 $this->setVar( '_AdminName', $admin );
88 }
89
90 if ( !isset( $option['installdbuser'] ) ) {
91 $this->setVar( '_InstallUser',
92 $this->getVar( 'wgDBuser' ) );
93 $this->setVar( '_InstallPassword',
94 $this->getVar( 'wgDBpassword' ) );
95 } else {
96 $this->setVar( '_InstallUser',
97 $option['installdbuser'] );
98 $this->setVar( '_InstallPassword',
99 isset( $option['installdbpass'] ) ? $option['installdbpass'] : "" );
100
101 // Assume that if we're given the installer user, we'll create the account.
102 $this->setVar( '_CreateDBAccount', true );
103 }
104
105 if ( isset( $option['pass'] ) ) {
106 $this->setVar( '_AdminPassword', $option['pass'] );
107 }
108
109 // Detect and inject any extension found
110 if ( isset( $option['with-extensions'] ) ) {
111 $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
112 }
113
114 // Set up the default skins
115 $skins = array_keys( $this->findExtensions( 'skins' ) );
116 $this->setVar( '_Skins', $skins );
117
118 if ( $skins ) {
119 $skinNames = array_map( 'strtolower', $skins );
120 $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
121 }
122 }
123
124 /**
125 * Main entry point.
126 */
127 public function execute() {
128 $vars = Installer::getExistingLocalSettings();
129 if ( $vars ) {
130 $this->showStatusMessage(
131 Status::newFatal( "config-localsettings-cli-upgrade" )
132 );
133 }
134
135 $this->performInstallation(
136 [ $this, 'startStage' ],
137 [ $this, 'endStage' ]
138 );
139 }
140
141 /**
142 * Write LocalSettings.php to a given path
143 *
144 * @param string $path Full path to write LocalSettings.php to
145 */
146 public function writeConfigurationFile( $path ) {
147 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
148 $ls->writeFile( "$path/LocalSettings.php" );
149 }
150
151 public function startStage( $step ) {
152 // Messages: config-install-database, config-install-tables, config-install-interwiki,
153 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
154 // config-install-extensions
155 $this->showMessage( "config-install-$step" );
156 }
157
158 public function endStage( $step, $status ) {
159 $this->showStatusMessage( $status );
160 $this->showMessage( 'config-install-step-done' );
161 }
162
163 public function showMessage( $msg /*, ... */ ) {
164 echo $this->getMessageText( func_get_args() ) . "\n";
165 flush();
166 }
167
168 public function showError( $msg /*, ... */ ) {
169 echo "***{$this->getMessageText( func_get_args() )}***\n";
170 flush();
171 }
172
173 /**
174 * @param array $params
175 *
176 * @return string
177 */
178 protected function getMessageText( $params ) {
179 $msg = array_shift( $params );
180
181 $text = wfMessage( $msg, $params )->parse();
182
183 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
184
185 return Sanitizer::stripAllTags( $text );
186 }
187
188 /**
189 * Dummy
190 */
191 public function showHelpBox( $msg /*, ... */ ) {
192 }
193
194 public function showStatusMessage( Status $status ) {
195 $warnings = array_merge( $status->getWarningsArray(),
196 $status->getErrorsArray() );
197
198 if ( count( $warnings ) !== 0 ) {
199 foreach ( $warnings as $w ) {
200 call_user_func_array( [ $this, 'showMessage' ], $w );
201 }
202 }
203
204 if ( !$status->isOK() ) {
205 echo "\n";
206 exit( 1 );
207 }
208 }
209
210 public function envCheckPath() {
211 if ( !$this->specifiedScriptPath ) {
212 $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
213 }
214
215 return parent::envCheckPath();
216 }
217
218 protected function envGetDefaultServer() {
219 return null; // Do not guess if installing from CLI
220 }
221
222 public function dirIsExecutable( $dir, $url ) {
223 $this->showMessage( 'config-no-cli-uploads-check', $dir );
224
225 return false;
226 }
227 }