23use InvalidArgumentException;
31class LocalSettingsGenerator {
34 protected $extensions = [];
36 protected $skins = [];
38 protected $values = [];
40 protected $groupPermissions = [];
42 protected $dbSettings =
'';
51 public function __construct( Installer $installer ) {
52 $this->installer = $installer;
54 $this->extensions = $installer->getVar(
'_Extensions' );
55 $this->skins = $installer->getVar(
'_Skins' );
56 $this->IP = $installer->getVar(
'IP' );
58 $db = $installer->getDBInstaller( $installer->getVar(
'wgDBtype' ) );
60 $confItems = array_merge(
62 'wgServer',
'wgScriptPath',
63 'wgPasswordSender',
'wgImageMagickConvertCommand',
64 'wgLanguageCode',
'wgLocaltimezone',
'wgEnableEmail',
'wgEnableUserEmail',
65 'wgDiff3',
'wgEnotifUserTalk',
'wgEnotifWatchlist',
'wgEmailAuthentication',
66 'wgDBtype',
'wgSecretKey',
'wgRightsUrl',
'wgSitename',
'wgRightsIcon',
67 'wgRightsText',
'_MainCacheType',
'wgEnableUploads',
68 '_MemCachedServers',
'wgDBserver',
'wgDBuser',
69 'wgDBpassword',
'wgUseInstantCommons',
'wgUpgradeKey',
'wgDefaultSkin',
70 'wgMetaNamespace',
'wgAuthenticationTokenVersion',
'wgPingback',
71 '_Logo1x',
'_LogoTagline',
'_LogoWordmark',
'_LogoIcon',
72 '_LogoWordmarkWidth',
'_LogoWordmarkHeight',
73 '_LogoTaglineWidth',
'_LogoTaglineHeight',
'_WithDevelopmentSettings'
81 $unescaped = [
'wgRightsIcon',
'_Caches',
82 '_Logo1x',
'_LogoWordmark',
'_LogoTagline',
'_LogoIcon',
85 'wgEnableEmail',
'wgEnableUserEmail',
'wgEnotifUserTalk',
86 'wgEnotifWatchlist',
'wgEmailAuthentication',
'wgEnableUploads',
'wgUseInstantCommons',
90 foreach ( $confItems as $c ) {
91 $val = $installer->getVar( $c );
93 if ( in_array( $c, $boolItems ) ) {
97 if ( !in_array( $c, $unescaped ) && $val !==
null ) {
98 $val = self::escapePhpString( $val );
101 $this->values[$c] = $val;
104 $this->dbSettings = $db->getLocalSettings();
105 $this->values[
'wgEmergencyContact'] = $this->values[
'wgPasswordSender'];
114 public function setGroupRights( $group, $rightsArr ) {
115 $this->groupPermissions[$group] = $rightsArr;
125 public static function escapePhpString( $string ) {
126 if ( is_array( $string ) || is_object( $string ) ) {
149 public function getText() {
150 $localSettings = $this->getDefaultText();
152 if ( count( $this->skins ) ) {
155# The following skins were automatically enabled:\n";
157 foreach ( $this->skins as $skinName ) {
158 $localSettings .= $this->generateExtEnableLine(
'skins', $skinName );
161 $localSettings .=
"\n";
164 if ( count( $this->extensions ) ) {
166# Enabled extensions. Most of the extensions are enabled by adding
167# wfLoadExtension( 'ExtensionName' );
168# to LocalSettings.php. Check specific extension documentation for more details.
169# The following extensions were automatically enabled:\n";
171 foreach ( $this->extensions as $extName ) {
172 $localSettings .= $this->generateExtEnableLine(
'extensions', $extName );
175 $localSettings .=
"\n";
179# End of automatically generated settings.
180# Add more configuration options below.\n\n";
182 return $localSettings;
192 private function generateExtEnableLine( $dir, $name ) {
193 if ( $dir ===
'extensions' ) {
194 $jsonFile =
'extension.json';
195 $function =
'wfLoadExtension';
196 } elseif ( $dir ===
'skins' ) {
197 $jsonFile =
'skin.json';
198 $function =
'wfLoadSkin';
200 throw new InvalidArgumentException(
'$dir was not "extensions" or "skins"' );
203 $encName = self::escapePhpString( $name );
205 if ( file_exists(
"{$this->IP}/$dir/$encName/$jsonFile" ) ) {
206 return "$function( '$encName' );\n";
208 return "require_once \"\$IP/$dir/$encName/$encName.php\";\n";
217 public function writeFile( $fileName ) {
218 file_put_contents( $fileName, $this->getText() );
224 protected function buildMemcachedServerList() {
225 $servers = $this->values[
'_MemCachedServers'];
231 $servers = explode(
',', $servers );
233 foreach ( $servers as $srv ) {
238 return rtrim( $ret,
', ' ) .
' ]';
245 protected function getDefaultText() {
246 if ( !$this->values[
'wgImageMagickConvertCommand'] ) {
247 $this->values[
'wgImageMagickConvertCommand'] =
'/usr/bin/convert';
254 if ( $this->values[
'wgMetaNamespace'] !== $this->values[
'wgSitename'] ) {
255 $metaNamespace =
"\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
260 if ( $this->groupPermissions ) {
261 $groupRights .=
"# The following permissions were set based on your choice in the installer\n";
262 foreach ( $this->groupPermissions as $group => $rightArr ) {
263 $group = self::escapePhpString( $group );
264 foreach ( $rightArr as $right => $perm ) {
265 $right = self::escapePhpString( $right );
266 $groupRights .=
"\$wgGroupPermissions[\"$group\"][\"$right\"] = " .
270 $groupRights .=
"\n";
272 if ( ( isset( $this->groupPermissions[
'*'][
'edit'] ) &&
273 $this->groupPermissions[
'*'][
'edit'] ===
false )
274 && ( isset( $this->groupPermissions[
'*'][
'createaccount'] ) &&
275 $this->groupPermissions[
'*'][
'createaccount'] ===
false )
276 && ( isset( $this->groupPermissions[
'*'][
'read'] ) &&
277 $this->groupPermissions[
'*'][
'read'] !==
false )
279 $noFollow =
"# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
280 .
"# the general public and wish to apply nofollow to external links as a\n"
281 .
"# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
282 .
"# and open wikis will generally require other anti-spam measures; for more\n"
283 .
"# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
284 .
"\$wgNoFollowLinks = false;\n\n";
289 if ( array_key_exists(
'wgServer', $this->values ) && $this->values[
'wgServer'] !==
null ) {
290 $serverSetting =
"\n## The protocol and server name to use in fully-qualified URLs\n";
291 $serverSetting .=
"\$wgServer = \"{$this->values['wgServer']}\";";
294 switch ( $this->values[
'_MainCacheType'] ) {
299 $cacheType =
'CACHE_' . strtoupper( $this->values[
'_MainCacheType'] );
303 $cacheType =
'CACHE_NONE';
306 $mcservers = $this->buildMemcachedServerList();
307 if ( file_exists( dirname( __DIR__ ) .
'/PlatformSettings.php' ) ) {
308 $platformSettings =
"\n## Include platform/distribution defaults";
309 $platformSettings .=
"\nrequire_once \"\$IP/includes/PlatformSettings.php\";";
311 $platformSettings =
'';
314 $developmentSettings =
'';
315 if ( isset( $this->values[
'_WithDevelopmentSettings'] ) && $this->values[
'_WithDevelopmentSettings'] ) {
316 $developmentSettings =
"\n## Include DevelopmentSettings.php";
317 $developmentSettings .=
"\nrequire_once \"\$IP/includes/DevelopmentSettings.php\";";
320 $this->values[
'taglineConfig'] = $this->values[
'_LogoTagline'] ?
"\n\t'tagline' => [
321 \"src\" => \"{$this->values['_LogoTagline']}\",
322 \"width\" => {$this->values['_LogoTaglineWidth']},
323 \"height\" => {$this->values['_LogoTaglineHeight']}
326 $this->values[
'wordmarkConfig'] = $this->values[
'_LogoWordmark'] ?
"\n\t'wordmark' => [
327 \"src\" => \"{$this->values['_LogoWordmark']}\",
328 \"width\" => {$this->values['_LogoWordmarkWidth']},
329 \"height\" => {$this->values['_LogoWordmarkHeight']},
332 $this->values[
'sidebarLogo'] = $this->values[
'_Logo1x'] ?: $this->values[
'_LogoIcon'];
336# This file was automatically generated by the MediaWiki {$version}
337# installer. If you make manual changes, please keep track in case you
338# need to recreate them later.
340# See includes/MainConfigSchema.php for all configurable settings
341# and their default values, but don't forget to make changes in _this_
344# Further documentation for configuration settings may be found at:
345# https://www.mediawiki.org/wiki/Manual:Configuration_settings
347# Protect against web entry
348if ( !defined( 'MEDIAWIKI' ) ) {
351{$developmentSettings}
355## Uncomment this to disable output compression
356# \$wgDisableOutputCompression = true;
358\$wgSitename = \"{$this->values['wgSitename']}\";
360## The URL base path to the directory containing the wiki;
361## defaults for all runtime URL paths are based off of this.
362## For more information on customizing the URLs
363## (like /w/index.php/Page_title to /wiki/Page_title) please see:
364## https://www.mediawiki.org/wiki/Manual:Short_URL
365\$wgScriptPath = \"{$this->values['wgScriptPath']}\";
368## The URL path to static resources (images, scripts, etc.)
369\$wgResourceBasePath = \$wgScriptPath;
371## The URL paths to the logo. Make sure you change this from the default,
372## or else you'll overwrite your logo when you upgrade!
374 '1x' => \"{$this->values['sidebarLogo']}\",{$this->values['wordmarkConfig']}{$this->values['taglineConfig']}
375 'icon' => \"{$this->values['_LogoIcon']}\",
378## UPO means: this is also a user preference option
380\$wgEnableEmail = {$this->values['wgEnableEmail']};
381\$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
383\$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
384\$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
386\$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
387\$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
388\$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
391\$wgDBtype = \"{$this->values['wgDBtype']}\";
392\$wgDBserver = \"{$this->values['wgDBserver']}\";
393\$wgDBname = \"{$this->values['wgDBname']}\";
394\$wgDBuser = \"{$this->values['wgDBuser']}\";
395\$wgDBpassword = \"{$this->values['wgDBpassword']}\";
399# Shared database table
400# This has no effect unless \$wgSharedDB is also set.
401\$wgSharedTables[] = \"actor\";
403## Shared memory settings
404\$wgMainCacheType = $cacheType;
405\$wgMemCachedServers = $mcservers;
407## To enable image uploads, make sure the 'images' directory
408## is writable, then set this to true:
409\$wgEnableUploads = {$this->values['wgEnableUploads']};
410{$magic}\$wgUseImageMagick = true;
411{$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
413# InstantCommons allows wiki to use images from https://commons.wikimedia.org
414\$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
416# Periodically send a pingback to https://www.mediawiki.org/ with basic data
417# about this MediaWiki instance. The Wikimedia Foundation shares this data
418# with MediaWiki developers to help guide future development efforts.
419\$wgPingback = {$this->values['wgPingback']};
421# Site language code, should be one of the list in ./includes/languages/data/Names.php
422\$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
425\$wgLocaltimezone = \"{$this->values['wgLocaltimezone']}\";
427## Set \$wgCacheDirectory to a writable directory on the web server
428## to make your wiki go slightly faster. The directory should not
429## be publicly accessible from the web.
430#\$wgCacheDirectory = \"\$IP/cache\";
432\$wgSecretKey = \"{$this->values['wgSecretKey']}\";
434# Changing this will log out all existing sessions.
435\$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\";
437# Site upgrade key. Must be set to a string (default provided) to turn on the
438# web installer while LocalSettings.php is in place
439\$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
441## For attaching licensing metadata to pages, and displaying an
442## appropriate copyright notice / icon. GNU Free Documentation
443## License and Creative Commons licenses are supported so far.
444\$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
445\$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
446\$wgRightsText = \"{$this->values['wgRightsText']}\";
447\$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
449# Path to the GNU diff3 utility. Used for conflict resolution.
450\$wgDiff3 = \"{$this->values['wgDiff3']}\";
452{$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
453## names, e.g. 'vector' or 'monobook':
454\$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
const MW_VERSION
The running version of MediaWiki.
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.