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