9use InvalidArgumentException;
17class LocalSettingsGenerator {
20 protected $extensions = [];
22 protected $skins = [];
24 protected $values = [];
26 protected $groupPermissions = [];
28 protected $dbSettings =
'';
37 public function __construct( Installer $installer ) {
38 $this->installer = $installer;
40 $this->extensions = $installer->getVar(
'_Extensions' );
41 $this->skins = $installer->getVar(
'_Skins' );
42 $this->IP = $installer->getVar(
'IP' );
44 $db = $installer->getDBInstaller( $installer->getVar(
'wgDBtype' ) );
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(),
65 $unescaped = [
'wgRightsIcon',
'_Caches',
66 '_Logo1x',
'_LogoWordmark',
'_LogoTagline',
'_LogoIcon',
69 'wgEnableEmail',
'wgEnableUserEmail',
'wgEnotifUserTalk',
70 'wgEnotifWatchlist',
'wgEmailAuthentication',
'wgEnableUploads',
'wgUseInstantCommons',
74 foreach ( $confItems as $c ) {
75 $val = $installer->getVar( $c );
77 if ( in_array( $c, $boolItems ) ) {
81 if ( !in_array( $c, $unescaped ) && $val !==
null ) {
82 $val = self::escapePhpString( $val );
88 $this->dbSettings = $db->getLocalSettings();
89 $this->
values[
'wgEmergencyContact'] = $this->
values[
'wgPasswordSender'];
98 public function setGroupRights( $group, $rightsArr ) {
99 $this->groupPermissions[$group] = $rightsArr;
109 public static function escapePhpString( $string ) {
110 if ( is_array( $string ) || is_object( $string ) ) {
133 public function getText() {
134 $localSettings = $this->getDefaultText();
136 if ( count( $this->skins ) ) {
139# The following skins were automatically enabled:\n";
141 foreach ( $this->skins as $skinName ) {
142 $localSettings .= $this->generateExtEnableLine(
'skins', $skinName );
145 $localSettings .=
"\n";
148 if ( count( $this->extensions ) ) {
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";
155 foreach ( $this->extensions as $extName ) {
156 $localSettings .= $this->generateExtEnableLine(
'extensions', $extName );
159 $localSettings .=
"\n";
163# End of automatically generated settings.
164# Add more configuration options below.\n\n";
166 return $localSettings;
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';
184 throw new InvalidArgumentException(
'$dir was not "extensions" or "skins"' );
187 $encName = self::escapePhpString( $name );
189 if ( file_exists(
"{$this->IP}/$dir/$encName/$jsonFile" ) ) {
190 return "$function( '$encName' );\n";
192 return "require_once \"\$IP/$dir/$encName/$encName.php\";\n";
201 public function writeFile( $fileName ) {
202 file_put_contents( $fileName, $this->getText() );
208 protected function buildMemcachedServerList() {
209 $servers = $this->
values[
'_MemCachedServers'];
215 $servers = explode(
',', $servers );
217 foreach ( $servers as $srv ) {
222 return rtrim( $ret,
', ' ) .
' ]';
229 protected function getDefaultText() {
230 if ( !$this->values[
'wgImageMagickConvertCommand'] ) {
231 $this->
values[
'wgImageMagickConvertCommand'] =
'/usr/bin/convert';
238 if ( $this->values[
'wgMetaNamespace'] !== $this->values[
'wgSitename'] ) {
239 $metaNamespace =
"\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
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\"] = " .
254 $groupRights .=
"\n";
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 )
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";
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']}\";";
278 switch ( $this->values[
'_MainCacheType'] ) {
283 $cacheType =
'CACHE_' . strtoupper( $this->values[
'_MainCacheType'] );
287 $cacheType =
'CACHE_NONE';
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\";";
295 $platformSettings =
'';
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\";";
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']}
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']},
316 $this->
values[
'sidebarLogo'] = $this->
values[
'_Logo1x'] ?: $this->
values[
'_LogoIcon'];
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.
324# See includes/MainConfigSchema.php for all configurable settings
325# and their default values, but don't forget to make changes in _this_
328# Further documentation for configuration settings may be found at:
329# https://www.mediawiki.org/wiki/Manual:Configuration_settings
331# Protect against web entry
332if ( !defined( 'MEDIAWIKI' ) ) {
335{$developmentSettings}
339## Uncomment this to disable output compression
340# \$wgDisableOutputCompression = true;
342\$wgSitename = \"{$this->values['wgSitename']}\";
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']}\";
352## The URL path to static resources (images, scripts, etc.)
353\$wgResourceBasePath = \$wgScriptPath;
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!
358 '1x' => \"{$this->values['sidebarLogo']}\",{$this->values['wordmarkConfig']}{$this->values['taglineConfig']}
359 'icon' => \"{$this->values['_LogoIcon']}\",
362## UPO means: this is also a user preference option
364\$wgEnableEmail = {$this->values['wgEnableEmail']};
365\$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
367\$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
368\$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
370\$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
371\$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
372\$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
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']}\";
383# Shared database table
384# This has no effect unless \$wgSharedDB is also set.
385\$wgSharedTables[] = \"actor\";
387## Shared memory settings
388\$wgMainCacheType = $cacheType;
389\$wgMemCachedServers = $mcservers;
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']}\";
397# InstantCommons allows wiki to use images from https://commons.wikimedia.org
398\$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
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']};
405# Site language code, should be one of the list in ./includes/Languages/Data/Names.php
406\$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
409\$wgLocaltimezone = \"{$this->values['wgLocaltimezone']}\";
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\";
416\$wgSecretKey = \"{$this->values['wgSecretKey']}\";
418# Changing this will log out all existing sessions.
419\$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\";
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']}\";
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']}\";
433# Path to the GNU diff3 utility. Used for conflict resolution.
434\$wgDiff3 = \"{$this->values['wgDiff3']}\";
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']}\";
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(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.