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'];
333// Protect against web entry
334if ( !defined( 'MEDIAWIKI' ) ) {
337{$developmentSettings}
341// Uncomment this to disable output compression
342// \$wgDisableOutputCompression = true;
344\$wgSitename = \"{$this->values['wgSitename']}\";
346// The URL base path to the directory containing the wiki;
347// defaults for all runtime URL paths are based off of this.
348// For more information on customizing the URLs
349// (like /w/index.php/Page_title to /wiki/Page_title) please see:
350// https://www.mediawiki.org/wiki/Manual:Short_URL
351\$wgScriptPath = \"{$this->values['wgScriptPath']}\";
354// The URL path to static resources (images, scripts, etc.)
355\$wgResourceBasePath = \$wgScriptPath;
357// The URL paths to the logo. Make sure you change this from the default,
358// or else you'll overwrite your logo when you upgrade!
360 '1x' => \"{$this->values['sidebarLogo']}\",{$this->values['wordmarkConfig']}{$this->values['taglineConfig']}
361 'icon' => \"{$this->values['_LogoIcon']}\",
364// UPO means: this is also a user preference option
366\$wgEnableEmail = {$this->values['wgEnableEmail']};
367\$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
369\$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
370\$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
372\$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
373\$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
374\$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
377\$wgDBtype = \"{$this->values['wgDBtype']}\";
378\$wgDBserver = \"{$this->values['wgDBserver']}\";
379\$wgDBname = \"{$this->values['wgDBname']}\";
380\$wgDBuser = \"{$this->values['wgDBuser']}\";
381\$wgDBpassword = \"{$this->values['wgDBpassword']}\";
385// Shared database table
386// This has no effect unless \$wgSharedDB is also set.
387\$wgSharedTables[] = \"actor\";
389// Shared memory settings
390\$wgMainCacheType = $cacheType;
391\$wgMemCachedServers = $mcservers;
393// To enable image uploads, make sure the 'images' directory
394// is writable, then set this to true:
395\$wgEnableUploads = {$this->values['wgEnableUploads']};
396{$magic}\$wgUseImageMagick = true;
397{$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
399// InstantCommons allows wiki to use images from https://commons.wikimedia.org
400\$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
402// Periodically send a pingback to https://www.mediawiki.org/ with basic data
403// about this MediaWiki instance. The Wikimedia Foundation shares this data
404// with MediaWiki developers to help guide future development efforts.
405\$wgPingback = {$this->values['wgPingback']};
407// Site language code, should be one of the list in ./includes/Languages/Data/Names.php
408\$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
411\$wgLocaltimezone = \"{$this->values['wgLocaltimezone']}\";
413// Set \$wgCacheDirectory to a writable directory on the web server
414// to make your wiki go slightly faster. The directory should not
415// be publicly accessible from the web.
416\$wgCacheDirectory = \"\$IP/cache\";
418\$wgSecretKey = \"{$this->values['wgSecretKey']}\";
420// Changing this will log out all existing sessions.
421\$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\";
423// Site upgrade key. Must be set to a string (default provided) to turn on the
424// web installer while LocalSettings.php is in place
425\$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
427// For attaching licensing metadata to pages, and displaying an
428// appropriate copyright notice / icon. GNU Free Documentation
429// License and Creative Commons licenses are supported so far.
430\$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
431\$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
432\$wgRightsText = \"{$this->values['wgRightsText']}\";
433\$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
435// Path to the GNU diff3 utility. Used for conflict resolution.
436\$wgDiff3 = \"{$this->values['wgDiff3']}\";
438{$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
439// names, e.g. 'vector' or 'monobook':
440\$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')) $IP
Environment checks.