MediaWiki master
LocalSettingsGenerator.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Installer;
8
9use InvalidArgumentException;
10
17class LocalSettingsGenerator {
18
20 protected $extensions = [];
22 protected $skins = [];
24 protected $values = [];
26 protected $groupPermissions = [];
28 protected $dbSettings = '';
30 protected $IP;
31
35 protected $installer;
36
37 public function __construct( Installer $installer ) {
38 $this->installer = $installer;
39
40 $this->extensions = $installer->getVar( '_Extensions' );
41 $this->skins = $installer->getVar( '_Skins' );
42 $this->IP = $installer->getVar( 'IP' );
43
44 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
45
46 $confItems = [
47 'wgServer', 'wgScriptPath',
48 'wgPasswordSender', 'wgImageMagickConvertCommand',
49 'wgLanguageCode', 'wgLocaltimezone', 'wgEnableEmail', 'wgEnableUserEmail',
50 'wgDiff3', 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
51 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
52 'wgRightsText', '_MainCacheType', 'wgEnableUploads',
53 '_MemCachedServers', 'wgDBserver', 'wgDBuser',
54 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
55 'wgMetaNamespace', 'wgAuthenticationTokenVersion', 'wgPingback',
56 '_Logo1x', '_LogoTagline', '_LogoWordmark', '_LogoIcon',
57 '_LogoWordmarkWidth', '_LogoWordmarkHeight',
58 '_LogoTaglineWidth', '_LogoTaglineHeight', '_WithDevelopmentSettings',
59 ...$db->getGlobalNames(),
60 ];
61
62 // The WebInstaller form field for "Logo" contains a literal "$wgResourceBasePath",
63 // and site admins are told in the help text that they can use $wgStylePath and $wgScriptPath
64 // within their input, such treat this as raw PHP for now.
65 $unescaped = [ 'wgRightsIcon', '_Caches',
66 '_Logo1x', '_LogoWordmark', '_LogoTagline', '_LogoIcon',
67 ];
68 $boolItems = [
69 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
70 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons',
71 'wgPingback',
72 ];
73
74 foreach ( $confItems as $c ) {
75 $val = $installer->getVar( $c );
76
77 if ( in_array( $c, $boolItems ) ) {
78 $val = wfBoolToStr( $val );
79 }
80
81 if ( !in_array( $c, $unescaped ) && $val !== null ) {
82 $val = self::escapePhpString( $val );
83 }
84
85 $this->values[$c] = $val;
86 }
87
88 $this->dbSettings = $db->getLocalSettings();
89 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
90 }
91
98 public function setGroupRights( $group, $rightsArr ) {
99 $this->groupPermissions[$group] = $rightsArr;
100 }
101
109 public static function escapePhpString( $string ) {
110 if ( is_array( $string ) || is_object( $string ) ) {
111 return false;
112 }
113
114 return strtr(
115 $string,
116 [
117 "\n" => "\\n",
118 "\r" => "\\r",
119 "\t" => "\\t",
120 "\\" => "\\\\",
121 "\$" => "\\\$",
122 "\"" => "\\\""
123 ]
124 );
125 }
126
133 public function getText() {
134 $localSettings = $this->getDefaultText();
135
136 if ( count( $this->skins ) ) {
137 $localSettings .= "
138# Enabled skins.
139# The following skins were automatically enabled:\n";
140
141 foreach ( $this->skins as $skinName ) {
142 $localSettings .= $this->generateExtEnableLine( 'skins', $skinName );
143 }
144
145 $localSettings .= "\n";
146 }
147
148 if ( count( $this->extensions ) ) {
149 $localSettings .= "
150# Enabled extensions. Most of the extensions are enabled by adding
151# wfLoadExtension( 'ExtensionName' );
152# to LocalSettings.php. Check specific extension documentation for more details.
153# The following extensions were automatically enabled:\n";
154
155 foreach ( $this->extensions as $extName ) {
156 $localSettings .= $this->generateExtEnableLine( 'extensions', $extName );
157 }
158
159 $localSettings .= "\n";
160 }
161
162 $localSettings .= "
163# End of automatically generated settings.
164# Add more configuration options below.\n\n";
165
166 return $localSettings;
167 }
168
176 private function generateExtEnableLine( $dir, $name ) {
177 if ( $dir === 'extensions' ) {
178 $jsonFile = 'extension.json';
179 $function = 'wfLoadExtension';
180 } elseif ( $dir === 'skins' ) {
181 $jsonFile = 'skin.json';
182 $function = 'wfLoadSkin';
183 } else {
184 throw new InvalidArgumentException( '$dir was not "extensions" or "skins"' );
185 }
186
187 $encName = self::escapePhpString( $name );
188
189 if ( file_exists( "{$this->IP}/$dir/$encName/$jsonFile" ) ) {
190 return "$function( '$encName' );\n";
191 } else {
192 return "require_once \"\$IP/$dir/$encName/$encName.php\";\n";
193 }
194 }
195
201 public function writeFile( $fileName ) {
202 file_put_contents( $fileName, $this->getText() );
203 }
204
208 protected function buildMemcachedServerList() {
209 $servers = $this->values['_MemCachedServers'];
210
211 if ( !$servers ) {
212 return '[]';
213 } else {
214 $ret = '[ ';
215 $servers = explode( ',', $servers );
216
217 foreach ( $servers as $srv ) {
218 $srv = trim( $srv );
219 $ret .= "'$srv', ";
220 }
221
222 return rtrim( $ret, ', ' ) . ' ]';
223 }
224 }
225
229 protected function getDefaultText() {
230 if ( !$this->values['wgImageMagickConvertCommand'] ) {
231 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
232 $magic = '#';
233 } else {
234 $magic = '';
235 }
236
237 $metaNamespace = '';
238 if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
239 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
240 }
241
242 $groupRights = '';
243 $noFollow = '';
244 if ( $this->groupPermissions ) {
245 $groupRights .= "# The following permissions were set based on your choice in the installer\n";
246 foreach ( $this->groupPermissions as $group => $rightArr ) {
247 $group = self::escapePhpString( $group );
248 foreach ( $rightArr as $right => $perm ) {
249 $right = self::escapePhpString( $right );
250 $groupRights .= "\$wgGroupPermissions[\"$group\"][\"$right\"] = " .
251 wfBoolToStr( $perm ) . ";\n";
252 }
253 }
254 $groupRights .= "\n";
255
256 if ( ( isset( $this->groupPermissions['*']['edit'] ) &&
257 $this->groupPermissions['*']['edit'] === false )
258 && ( isset( $this->groupPermissions['*']['createaccount'] ) &&
259 $this->groupPermissions['*']['createaccount'] === false )
260 && ( isset( $this->groupPermissions['*']['read'] ) &&
261 $this->groupPermissions['*']['read'] !== false )
262 ) {
263 $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
264 . "# the general public and wish to apply nofollow to external links as a\n"
265 . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
266 . "# and open wikis will generally require other anti-spam measures; for more\n"
267 . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
268 . "\$wgNoFollowLinks = false;\n\n";
269 }
270 }
271
272 $serverSetting = "";
273 if ( array_key_exists( 'wgServer', $this->values ) && $this->values['wgServer'] !== null ) {
274 $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
275 $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";";
276 }
277
278 switch ( $this->values['_MainCacheType'] ) {
279 case 'anything':
280 case 'db':
281 case 'memcached':
282 case 'accel':
283 $cacheType = 'CACHE_' . strtoupper( $this->values['_MainCacheType'] );
284 break;
285 case 'none':
286 default:
287 $cacheType = 'CACHE_NONE';
288 }
289
290 $mcservers = $this->buildMemcachedServerList();
291 if ( file_exists( dirname( __DIR__ ) . '/PlatformSettings.php' ) ) {
292 $platformSettings = "\n## Include platform/distribution defaults";
293 $platformSettings .= "\nrequire_once \"\$IP/includes/PlatformSettings.php\";";
294 } else {
295 $platformSettings = '';
296 }
297
298 $developmentSettings = '';
299 if ( isset( $this->values['_WithDevelopmentSettings'] ) && $this->values['_WithDevelopmentSettings'] ) {
300 $developmentSettings = "\n## Include DevelopmentSettings.php";
301 $developmentSettings .= "\nrequire_once \"\$IP/includes/DevelopmentSettings.php\";";
302 }
303
304 $this->values['taglineConfig'] = $this->values['_LogoTagline'] ? "\n\t'tagline' => [
305 \"src\" => \"{$this->values['_LogoTagline']}\",
306 \"width\" => {$this->values['_LogoTaglineWidth']},
307 \"height\" => {$this->values['_LogoTaglineHeight']}
308 ]," : "";
309
310 $this->values['wordmarkConfig'] = $this->values['_LogoWordmark'] ? "\n\t'wordmark' => [
311 \"src\" => \"{$this->values['_LogoWordmark']}\",
312 \"width\" => {$this->values['_LogoWordmarkWidth']},
313 \"height\" => {$this->values['_LogoWordmarkHeight']},
314 ]," : "";
315
316 $this->values['sidebarLogo'] = $this->values['_Logo1x'] ?: $this->values['_LogoIcon'];
317
318 $version = MW_VERSION;
319 return "<?php
320# This file was automatically generated by the MediaWiki {$version}
321# installer. If you make manual changes, please keep track in case you
322# need to recreate them later.
323#
324# See includes/MainConfigSchema.php for all configurable settings
325# and their default values, but don't forget to make changes in _this_
326# file, not there.
327#
328# Further documentation for configuration settings may be found at:
329# https://www.mediawiki.org/wiki/Manual:Configuration_settings
330
331# Protect against web entry
332if ( !defined( 'MEDIAWIKI' ) ) {
333 exit;
334}
335{$developmentSettings}
336
337{$platformSettings}
338
339## Uncomment this to disable output compression
340# \$wgDisableOutputCompression = true;
341
342\$wgSitename = \"{$this->values['wgSitename']}\";
343{$metaNamespace}
344## The URL base path to the directory containing the wiki;
345## defaults for all runtime URL paths are based off of this.
346## For more information on customizing the URLs
347## (like /w/index.php/Page_title to /wiki/Page_title) please see:
348## https://www.mediawiki.org/wiki/Manual:Short_URL
349\$wgScriptPath = \"{$this->values['wgScriptPath']}\";
350{$serverSetting}
351
352## The URL path to static resources (images, scripts, etc.)
353\$wgResourceBasePath = \$wgScriptPath;
354
355## The URL paths to the logo. Make sure you change this from the default,
356## or else you'll overwrite your logo when you upgrade!
357\$wgLogos = [
358 '1x' => \"{$this->values['sidebarLogo']}\",{$this->values['wordmarkConfig']}{$this->values['taglineConfig']}
359 'icon' => \"{$this->values['_LogoIcon']}\",
360];
361
362## UPO means: this is also a user preference option
363
364\$wgEnableEmail = {$this->values['wgEnableEmail']};
365\$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
366
367\$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
368\$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
369
370\$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
371\$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
372\$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
373
374## Database settings
375\$wgDBtype = \"{$this->values['wgDBtype']}\";
376\$wgDBserver = \"{$this->values['wgDBserver']}\";
377\$wgDBname = \"{$this->values['wgDBname']}\";
378\$wgDBuser = \"{$this->values['wgDBuser']}\";
379\$wgDBpassword = \"{$this->values['wgDBpassword']}\";
380
381{$this->dbSettings}
382
383# Shared database table
384# This has no effect unless \$wgSharedDB is also set.
385\$wgSharedTables[] = \"actor\";
386
387## Shared memory settings
388\$wgMainCacheType = $cacheType;
389\$wgMemCachedServers = $mcservers;
390
391## To enable image uploads, make sure the 'images' directory
392## is writable, then set this to true:
393\$wgEnableUploads = {$this->values['wgEnableUploads']};
394{$magic}\$wgUseImageMagick = true;
395{$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
396
397# InstantCommons allows wiki to use images from https://commons.wikimedia.org
398\$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
399
400# Periodically send a pingback to https://www.mediawiki.org/ with basic data
401# about this MediaWiki instance. The Wikimedia Foundation shares this data
402# with MediaWiki developers to help guide future development efforts.
403\$wgPingback = {$this->values['wgPingback']};
404
405# Site language code, should be one of the list in ./includes/Languages/Data/Names.php
406\$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
407
408# Time zone
409\$wgLocaltimezone = \"{$this->values['wgLocaltimezone']}\";
410
411## Set \$wgCacheDirectory to a writable directory on the web server
412## to make your wiki go slightly faster. The directory should not
413## be publicly accessible from the web.
414#\$wgCacheDirectory = \"\$IP/cache\";
415
416\$wgSecretKey = \"{$this->values['wgSecretKey']}\";
417
418# Changing this will log out all existing sessions.
419\$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\";
420
421# Site upgrade key. Must be set to a string (default provided) to turn on the
422# web installer while LocalSettings.php is in place
423\$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
424
425## For attaching licensing metadata to pages, and displaying an
426## appropriate copyright notice / icon. GNU Free Documentation
427## License and Creative Commons licenses are supported so far.
428\$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
429\$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
430\$wgRightsText = \"{$this->values['wgRightsText']}\";
431\$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
432
433# Path to the GNU diff3 utility. Used for conflict resolution.
434\$wgDiff3 = \"{$this->values['wgDiff3']}\";
435
436{$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
437## names, e.g. 'vector' or 'monobook':
438\$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
439";
440 }
441}
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:23
wfBoolToStr( $value)
Convenience function converts boolean values into "true" or "false" (string) values.
if(!defined('MEDIAWIKI')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:90
__construct(array $options, callable $shouldModifyCallback, callable $modifyCallback)
@ values
A comma-separated list.
Definition ListType.php:26