Merge "SpecialMovepage: Convert form to use OOUI controls"
[lhc/web/wiklou.git] / includes / installer / LocalSettingsGenerator.php
1 <?php
2 /**
3 * Generator for LocalSettings.php file.
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 generating LocalSettings.php file.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class LocalSettingsGenerator {
31
32 protected $extensions = array();
33 protected $values = array();
34 protected $groupPermissions = array();
35 protected $dbSettings = '';
36 protected $safeMode = false;
37 protected $IP;
38
39 /**
40 * @var Installer
41 */
42 protected $installer;
43
44 /**
45 * Constructor.
46 *
47 * @param Installer $installer
48 */
49 public function __construct( Installer $installer ) {
50 $this->installer = $installer;
51
52 $this->extensions = $installer->getVar( '_Extensions' );
53 $this->skins = $installer->getVar( '_Skins' );
54 $this->IP = $installer->getVar( 'IP' );
55
56 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
57
58 $confItems = array_merge(
59 array(
60 'wgServer', 'wgScriptPath',
61 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
62 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
63 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
64 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
65 'wgRightsText', 'wgMainCacheType', 'wgEnableUploads',
66 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
67 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
68 'wgMetaNamespace', 'wgLogo',
69 ),
70 $db->getGlobalNames()
71 );
72
73 $unescaped = array( 'wgRightsIcon', 'wgLogo' );
74 $boolItems = array(
75 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
76 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
77 );
78
79 foreach ( $confItems as $c ) {
80 $val = $installer->getVar( $c );
81
82 if ( in_array( $c, $boolItems ) ) {
83 $val = wfBoolToStr( $val );
84 }
85
86 if ( !in_array( $c, $unescaped ) && $val !== null ) {
87 $val = self::escapePhpString( $val );
88 }
89
90 $this->values[$c] = $val;
91 }
92
93 $this->dbSettings = $db->getLocalSettings();
94 $this->safeMode = $installer->getVar( '_SafeMode' );
95 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
96 }
97
98 /**
99 * For $wgGroupPermissions, set a given ['group']['permission'] value.
100 * @param string $group Group name
101 * @param array $rightsArr An array of permissions, in the form of:
102 * array( 'right' => true, 'right2' => false )
103 */
104 public function setGroupRights( $group, $rightsArr ) {
105 $this->groupPermissions[$group] = $rightsArr;
106 }
107
108 /**
109 * Returns the escaped version of a string of php code.
110 *
111 * @param string $string
112 *
113 * @return string
114 */
115 public static function escapePhpString( $string ) {
116 if ( is_array( $string ) || is_object( $string ) ) {
117 return false;
118 }
119
120 return strtr(
121 $string,
122 array(
123 "\n" => "\\n",
124 "\r" => "\\r",
125 "\t" => "\\t",
126 "\\" => "\\\\",
127 "\$" => "\\\$",
128 "\"" => "\\\""
129 )
130 );
131 }
132
133 /**
134 * Return the full text of the generated LocalSettings.php file,
135 * including the extensions and skins.
136 *
137 * @return string
138 */
139 public function getText() {
140 $localSettings = $this->getDefaultText();
141
142 if ( count( $this->skins ) ) {
143 $localSettings .= "
144 # Enabled skins.
145 # The following skins were automatically enabled:\n";
146
147 foreach ( $this->skins as $skinName ) {
148 $localSettings .= $this->generateExtEnableLine( 'skins', $skinName );
149 }
150
151 $localSettings .= "\n";
152 }
153
154 if ( count( $this->extensions ) ) {
155 $localSettings .= "
156 # Enabled Extensions. Most extensions are enabled by including the base extension file here
157 # but check specific extension documentation for more details
158 # The following extensions were automatically enabled:\n";
159
160 foreach ( $this->extensions as $extName ) {
161 $localSettings .= $this->generateExtEnableLine( 'extensions', $extName );
162 }
163
164 $localSettings .= "\n";
165 }
166
167 $localSettings .= "
168 # End of automatically generated settings.
169 # Add more configuration options below.\n\n";
170
171 return $localSettings;
172 }
173
174 /**
175 * Generate the appropriate line to enable the given extension or skin
176 *
177 * @param string $dir Either "extensions" or "skins"
178 * @param string $name Name of extension/skin
179 * @throws InvalidArgumentException
180 * @return string
181 */
182 private function generateExtEnableLine( $dir, $name ) {
183 if ( $dir === 'extensions' ) {
184 $jsonFile = 'extension.json';
185 $function = 'wfLoadExtension';
186 } elseif ( $dir === 'skins' ) {
187 $jsonFile = 'skin.json';
188 $function = 'wfLoadSkin';
189 } else {
190 throw new InvalidArgumentException( '$dir was not "extensions" or "skins' );
191 }
192
193 $encName = self::escapePhpString( $name );
194
195 if ( file_exists( "{$this->IP}/$dir/$encName/$jsonFile" ) ) {
196 return "$function( '$encName' );\n";
197 } else {
198 return "require_once \"\$IP/$dir/$encName/$encName.php\";\n";
199 }
200 }
201
202 /**
203 * Write the generated LocalSettings to a file
204 *
205 * @param string $fileName Full path to filename to write to
206 */
207 public function writeFile( $fileName ) {
208 file_put_contents( $fileName, $this->getText() );
209 }
210
211 /**
212 * @return string
213 */
214 protected function buildMemcachedServerList() {
215 $servers = $this->values['_MemCachedServers'];
216
217 if ( !$servers ) {
218 return 'array()';
219 } else {
220 $ret = 'array( ';
221 $servers = explode( ',', $servers );
222
223 foreach ( $servers as $srv ) {
224 $srv = trim( $srv );
225 $ret .= "'$srv', ";
226 }
227
228 return rtrim( $ret, ', ' ) . ' )';
229 }
230 }
231
232 /**
233 * @return string
234 */
235 protected function getDefaultText() {
236 if ( !$this->values['wgImageMagickConvertCommand'] ) {
237 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
238 $magic = '#';
239 } else {
240 $magic = '';
241 }
242
243 if ( !$this->values['wgShellLocale'] ) {
244 $this->values['wgShellLocale'] = 'en_US.UTF-8';
245 $locale = '#';
246 } else {
247 $locale = '';
248 }
249
250 $hashedUploads = $this->safeMode ? '' : '#';
251 $metaNamespace = '';
252 if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
253 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
254 }
255
256 $groupRights = '';
257 $noFollow = '';
258 if ( $this->groupPermissions ) {
259 $groupRights .= "# The following permissions were set based on your choice in the installer\n";
260 foreach ( $this->groupPermissions as $group => $rightArr ) {
261 $group = self::escapePhpString( $group );
262 foreach ( $rightArr as $right => $perm ) {
263 $right = self::escapePhpString( $right );
264 $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
265 wfBoolToStr( $perm ) . ";\n";
266 }
267 }
268 $groupRights .= "\n";
269
270 if ( ( isset( $this->groupPermissions['*']['edit'] ) &&
271 $this->groupPermissions['*']['edit'] === false )
272 && ( isset( $this->groupPermissions['*']['createaccount'] ) &&
273 $this->groupPermissions['*']['createaccount'] === false )
274 && ( isset( $this->groupPermissions['*']['read'] ) &&
275 $this->groupPermissions['*']['read'] !== false )
276 ) {
277 $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
278 . "# the general public and wish to apply nofollow to external links as a\n"
279 . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
280 . "# and open wikis will generally require other anti-spam measures; for more\n"
281 . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
282 . "\$wgNoFollowLinks = false;\n\n";
283 }
284 }
285
286 $serverSetting = "";
287 if ( array_key_exists( 'wgServer', $this->values ) && $this->values['wgServer'] !== null ) {
288 $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
289 $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";\n";
290 }
291
292 switch ( $this->values['wgMainCacheType'] ) {
293 case 'anything':
294 case 'db':
295 case 'memcached':
296 case 'accel':
297 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType'] );
298 break;
299 case 'none':
300 default:
301 $cacheType = 'CACHE_NONE';
302 }
303
304 $mcservers = $this->buildMemcachedServerList();
305
306 return "<?php
307 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
308 # installer. If you make manual changes, please keep track in case you
309 # need to recreate them later.
310 #
311 # See includes/DefaultSettings.php for all configurable settings
312 # and their default values, but don't forget to make changes in _this_
313 # file, not there.
314 #
315 # Further documentation for configuration settings may be found at:
316 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
317
318 # Protect against web entry
319 if ( !defined( 'MEDIAWIKI' ) ) {
320 exit;
321 }
322
323 ## Uncomment this to disable output compression
324 # \$wgDisableOutputCompression = true;
325
326 \$wgSitename = \"{$this->values['wgSitename']}\";
327 {$metaNamespace}
328 ## The URL base path to the directory containing the wiki;
329 ## defaults for all runtime URL paths are based off of this.
330 ## For more information on customizing the URLs
331 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
332 ## https://www.mediawiki.org/wiki/Manual:Short_URL
333 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
334 ${serverSetting}
335
336 ## The relative URL path to the logo. Make sure you change this from the default,
337 ## or else you'll overwrite your logo when you upgrade!
338 \$wgLogo = \"{$this->values['wgLogo']}\";
339
340 ## UPO means: this is also a user preference option
341
342 \$wgEnableEmail = {$this->values['wgEnableEmail']};
343 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
344
345 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
346 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
347
348 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
349 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
350 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
351
352 ## Database settings
353 \$wgDBtype = \"{$this->values['wgDBtype']}\";
354 \$wgDBserver = \"{$this->values['wgDBserver']}\";
355 \$wgDBname = \"{$this->values['wgDBname']}\";
356 \$wgDBuser = \"{$this->values['wgDBuser']}\";
357 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
358
359 {$this->dbSettings}
360
361 ## Shared memory settings
362 \$wgMainCacheType = $cacheType;
363 \$wgMemCachedServers = $mcservers;
364
365 ## To enable image uploads, make sure the 'images' directory
366 ## is writable, then set this to true:
367 \$wgEnableUploads = {$this->values['wgEnableUploads']};
368 {$magic}\$wgUseImageMagick = true;
369 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
370
371 # InstantCommons allows wiki to use images from https://commons.wikimedia.org
372 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
373
374 ## If you use ImageMagick (or any other shell command) on a
375 ## Linux server, this will need to be set to the name of an
376 ## available UTF-8 locale
377 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
378
379 ## If you want to use image uploads under safe mode,
380 ## create the directories images/archive, images/thumb and
381 ## images/temp, and make them all writable. Then uncomment
382 ## this, if it's not already uncommented:
383 {$hashedUploads}\$wgHashedUploadDirectory = false;
384
385 ## Set \$wgCacheDirectory to a writable directory on the web server
386 ## to make your wiki go slightly faster. The directory should not
387 ## be publically accessible from the web.
388 #\$wgCacheDirectory = \"\$IP/cache\";
389
390 # Site language code, should be one of the list in ./languages/Names.php
391 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
392
393 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
394
395 # Site upgrade key. Must be set to a string (default provided) to turn on the
396 # web installer while LocalSettings.php is in place
397 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
398
399 ## For attaching licensing metadata to pages, and displaying an
400 ## appropriate copyright notice / icon. GNU Free Documentation
401 ## License and Creative Commons licenses are supported so far.
402 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
403 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
404 \$wgRightsText = \"{$this->values['wgRightsText']}\";
405 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
406
407 # Path to the GNU diff3 utility. Used for conflict resolution.
408 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
409
410 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
411 ## names, ie 'vector', 'monobook':
412 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
413 ";
414 }
415 }