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