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