3 * Generator for LocalSettings.php file.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Class for generating LocalSettings.php file.
30 class LocalSettingsGenerator
{
32 protected $extensions = [];
33 protected $values = [];
34 protected $groupPermissions = [];
35 protected $dbSettings = '';
44 * @param Installer $installer
46 public function __construct( Installer
$installer ) {
47 $this->installer
= $installer;
49 $this->extensions
= $installer->getVar( '_Extensions' );
50 $this->skins
= $installer->getVar( '_Skins' );
51 $this->IP
= $installer->getVar( 'IP' );
53 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
55 $confItems = array_merge(
57 'wgServer', 'wgScriptPath',
58 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
59 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
60 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
61 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
62 'wgRightsText', '_MainCacheType', 'wgEnableUploads',
63 '_MemCachedServers', 'wgDBserver', 'wgDBuser',
64 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
65 'wgMetaNamespace', 'wgLogo', 'wgAuthenticationTokenVersion', 'wgPingback',
70 $unescaped = [ 'wgRightsIcon', 'wgLogo', '_Caches' ];
72 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
73 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons',
77 foreach ( $confItems as $c ) {
78 $val = $installer->getVar( $c );
80 if ( in_array( $c, $boolItems ) ) {
81 $val = wfBoolToStr( $val );
84 if ( !in_array( $c, $unescaped ) && $val !== null ) {
85 $val = self
::escapePhpString( $val );
88 $this->values
[$c] = $val;
91 $this->dbSettings
= $db->getLocalSettings();
92 $this->values
['wgEmergencyContact'] = $this->values
['wgPasswordSender'];
96 * For $wgGroupPermissions, set a given ['group']['permission'] value.
97 * @param string $group Group name
98 * @param array $rightsArr An array of permissions, in the form of:
99 * [ 'right' => true, 'right2' => false ]
101 public function setGroupRights( $group, $rightsArr ) {
102 $this->groupPermissions
[$group] = $rightsArr;
106 * Returns the escaped version of a string of php code.
108 * @param string $string
110 * @return string|false
112 public static function escapePhpString( $string ) {
113 if ( is_array( $string ) ||
is_object( $string ) ) {
131 * Return the full text of the generated LocalSettings.php file,
132 * including the extensions and skins.
136 public function getText() {
137 $localSettings = $this->getDefaultText();
139 if ( count( $this->skins
) ) {
142 # The following skins were automatically enabled:\n";
144 foreach ( $this->skins
as $skinName ) {
145 $localSettings .= $this->generateExtEnableLine( 'skins', $skinName );
148 $localSettings .= "\n";
151 if ( count( $this->extensions
) ) {
153 # Enabled extensions. Most of the extensions are enabled by adding
154 # wfLoadExtensions('ExtensionName');
155 # to LocalSettings.php. Check specific extension documentation for more details.
156 # The following extensions were automatically enabled:\n";
158 foreach ( $this->extensions
as $extName ) {
159 $localSettings .= $this->generateExtEnableLine( 'extensions', $extName );
162 $localSettings .= "\n";
166 # End of automatically generated settings.
167 # Add more configuration options below.\n\n";
169 return $localSettings;
173 * Generate the appropriate line to enable the given extension or skin
175 * @param string $dir Either "extensions" or "skins"
176 * @param string $name Name of extension/skin
177 * @throws InvalidArgumentException
180 private function generateExtEnableLine( $dir, $name ) {
181 if ( $dir === 'extensions' ) {
182 $jsonFile = 'extension.json';
183 $function = 'wfLoadExtension';
184 } elseif ( $dir === 'skins' ) {
185 $jsonFile = 'skin.json';
186 $function = 'wfLoadSkin';
188 throw new InvalidArgumentException( '$dir was not "extensions" or "skins' );
191 $encName = self
::escapePhpString( $name );
193 if ( file_exists( "{$this->IP}/$dir/$encName/$jsonFile" ) ) {
194 return "$function( '$encName' );\n";
196 return "require_once \"\$IP/$dir/$encName/$encName.php\";\n";
201 * Write the generated LocalSettings to a file
203 * @param string $fileName Full path to filename to write to
205 public function writeFile( $fileName ) {
206 file_put_contents( $fileName, $this->getText() );
212 protected function buildMemcachedServerList() {
213 $servers = $this->values
['_MemCachedServers'];
219 $servers = explode( ',', $servers );
221 foreach ( $servers as $srv ) {
226 return rtrim( $ret, ', ' ) . ' ]';
233 protected function getDefaultText() {
234 if ( !$this->values
['wgImageMagickConvertCommand'] ) {
235 $this->values
['wgImageMagickConvertCommand'] = '/usr/bin/convert';
241 if ( !$this->values
['wgShellLocale'] ) {
242 $this->values
['wgShellLocale'] = 'C.UTF-8';
249 if ( $this->values
['wgMetaNamespace'] !== $this->values
['wgSitename'] ) {
250 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
255 if ( $this->groupPermissions
) {
256 $groupRights .= "# The following permissions were set based on your choice in the installer\n";
257 foreach ( $this->groupPermissions
as $group => $rightArr ) {
258 $group = self
::escapePhpString( $group );
259 foreach ( $rightArr as $right => $perm ) {
260 $right = self
::escapePhpString( $right );
261 $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
262 wfBoolToStr( $perm ) . ";\n";
265 $groupRights .= "\n";
267 if ( ( isset( $this->groupPermissions
['*']['edit'] ) &&
268 $this->groupPermissions
['*']['edit'] === false )
269 && ( isset( $this->groupPermissions
['*']['createaccount'] ) &&
270 $this->groupPermissions
['*']['createaccount'] === false )
271 && ( isset( $this->groupPermissions
['*']['read'] ) &&
272 $this->groupPermissions
['*']['read'] !== false )
274 $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
275 . "# the general public and wish to apply nofollow to external links as a\n"
276 . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
277 . "# and open wikis will generally require other anti-spam measures; for more\n"
278 . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
279 . "\$wgNoFollowLinks = false;\n\n";
284 if ( array_key_exists( 'wgServer', $this->values
) && $this->values
['wgServer'] !== null ) {
285 $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
286 $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";";
289 switch ( $this->values
['_MainCacheType'] ) {
294 $cacheType = 'CACHE_' . strtoupper( $this->values
['_MainCacheType'] );
298 $cacheType = 'CACHE_NONE';
301 $mcservers = $this->buildMemcachedServerList();
304 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
305 # installer. If you make manual changes, please keep track in case you
306 # need to recreate them later.
308 # See includes/DefaultSettings.php for all configurable settings
309 # and their default values, but don't forget to make changes in _this_
312 # Further documentation for configuration settings may be found at:
313 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
315 # Protect against web entry
316 if ( !defined( 'MEDIAWIKI' ) ) {
320 ## Uncomment this to disable output compression
321 # \$wgDisableOutputCompression = true;
323 \$wgSitename = \"{$this->values['wgSitename']}\";
325 ## The URL base path to the directory containing the wiki;
326 ## defaults for all runtime URL paths are based off of this.
327 ## For more information on customizing the URLs
328 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
329 ## https://www.mediawiki.org/wiki/Manual:Short_URL
330 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
333 ## The URL path to static resources (images, scripts, etc.)
334 \$wgResourceBasePath = \$wgScriptPath;
336 ## The URL path to the logo. Make sure you change this from the default,
337 ## or else you'll overwrite your logo when you upgrade!
338 \$wgLogo = \"{$this->values['wgLogo']}\";
340 ## UPO means: this is also a user preference option
342 \$wgEnableEmail = {$this->values['wgEnableEmail']};
343 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
345 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
346 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
348 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
349 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
350 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
353 \$wgDBtype = \"{$this->values['wgDBtype']}\";
354 \$wgDBserver = \"{$this->values['wgDBserver']}\";
355 \$wgDBname = \"{$this->values['wgDBname']}\";
356 \$wgDBuser = \"{$this->values['wgDBuser']}\";
357 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
361 ## Shared memory settings
362 \$wgMainCacheType = $cacheType;
363 \$wgMemCachedServers = $mcservers;
365 ## To enable image uploads, make sure the 'images' directory
366 ## is writable, then set this to true:
367 \$wgEnableUploads = {$this->values['wgEnableUploads']};
368 {$magic}\$wgUseImageMagick = true;
369 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
371 # InstantCommons allows wiki to use images from https://commons.wikimedia.org
372 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
374 # Periodically send a pingback to https://www.mediawiki.org/ with basic data
375 # about this MediaWiki instance. The Wikimedia Foundation shares this data
376 # with MediaWiki developers to help guide future development efforts.
377 \$wgPingback = {$this->values['wgPingback']};
379 ## If you use ImageMagick (or any other shell command) on a
380 ## Linux server, this will need to be set to the name of an
381 ## available UTF-8 locale
382 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
384 ## Set \$wgCacheDirectory to a writable directory on the web server
385 ## to make your wiki go slightly faster. The directory should not
386 ## be publically accessible from the web.
387 #\$wgCacheDirectory = \"\$IP/cache\";
389 # Site language code, should be one of the list in ./languages/data/Names.php
390 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
392 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
394 # Changing this will log out all existing sessions.
395 \$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\";
397 # Site upgrade key. Must be set to a string (default provided) to turn on the
398 # web installer while LocalSettings.php is in place
399 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
401 ## For attaching licensing metadata to pages, and displaying an
402 ## appropriate copyright notice / icon. GNU Free Documentation
403 ## License and Creative Commons licenses are supported so far.
404 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
405 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
406 \$wgRightsText = \"{$this->values['wgRightsText']}\";
407 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
409 # Path to the GNU diff3 utility. Used for conflict resolution.
410 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
412 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
413 ## names, ie 'vector', 'monobook':
414 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";