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 $skins = [];
34 protected $values = [];
35 protected $groupPermissions = [];
36 protected $dbSettings = '';
45 * @param Installer $installer
47 public function __construct( Installer
$installer ) {
48 $this->installer
= $installer;
50 $this->extensions
= $installer->getVar( '_Extensions' );
51 $this->skins
= $installer->getVar( '_Skins' );
52 $this->IP
= $installer->getVar( 'IP' );
54 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
56 $confItems = array_merge(
58 'wgServer', 'wgScriptPath',
59 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
60 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
61 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
62 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
63 'wgRightsText', '_MainCacheType', 'wgEnableUploads',
64 '_MemCachedServers', 'wgDBserver', 'wgDBuser',
65 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
66 'wgMetaNamespace', 'wgLogo', 'wgAuthenticationTokenVersion', 'wgPingback',
71 $unescaped = [ 'wgRightsIcon', 'wgLogo', '_Caches' ];
73 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
74 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons',
78 foreach ( $confItems as $c ) {
79 $val = $installer->getVar( $c );
81 if ( in_array( $c, $boolItems ) ) {
82 $val = wfBoolToStr( $val );
85 if ( !in_array( $c, $unescaped ) && $val !== null ) {
86 $val = self
::escapePhpString( $val );
89 $this->values
[$c] = $val;
92 $this->dbSettings
= $db->getLocalSettings();
93 $this->values
['wgEmergencyContact'] = $this->values
['wgPasswordSender'];
97 * For $wgGroupPermissions, set a given ['group']['permission'] value.
98 * @param string $group Group name
99 * @param array $rightsArr An array of permissions, in the form of:
100 * [ 'right' => true, 'right2' => false ]
102 public function setGroupRights( $group, $rightsArr ) {
103 $this->groupPermissions
[$group] = $rightsArr;
107 * Returns the escaped version of a string of php code.
109 * @param string $string
111 * @return string|false
113 public static function escapePhpString( $string ) {
114 if ( is_array( $string ) ||
is_object( $string ) ) {
132 * Return the full text of the generated LocalSettings.php file,
133 * including the extensions and skins.
137 public function getText() {
138 $localSettings = $this->getDefaultText();
140 if ( count( $this->skins
) ) {
143 # The following skins were automatically enabled:\n";
145 foreach ( $this->skins
as $skinName ) {
146 $localSettings .= $this->generateExtEnableLine( 'skins', $skinName );
149 $localSettings .= "\n";
152 if ( count( $this->extensions
) ) {
154 # Enabled extensions. Most of the extensions are enabled by adding
155 # wfLoadExtensions('ExtensionName');
156 # to LocalSettings.php. Check specific extension documentation for more details.
157 # The following extensions were automatically enabled:\n";
159 foreach ( $this->extensions
as $extName ) {
160 $localSettings .= $this->generateExtEnableLine( 'extensions', $extName );
163 $localSettings .= "\n";
167 # End of automatically generated settings.
168 # Add more configuration options below.\n\n";
170 return $localSettings;
174 * Generate the appropriate line to enable the given extension or skin
176 * @param string $dir Either "extensions" or "skins"
177 * @param string $name Name of extension/skin
178 * @throws InvalidArgumentException
181 private function generateExtEnableLine( $dir, $name ) {
182 if ( $dir === 'extensions' ) {
183 $jsonFile = 'extension.json';
184 $function = 'wfLoadExtension';
185 } elseif ( $dir === 'skins' ) {
186 $jsonFile = 'skin.json';
187 $function = 'wfLoadSkin';
189 throw new InvalidArgumentException( '$dir was not "extensions" or "skins"' );
192 $encName = self
::escapePhpString( $name );
194 if ( file_exists( "{$this->IP}/$dir/$encName/$jsonFile" ) ) {
195 return "$function( '$encName' );\n";
197 return "require_once \"\$IP/$dir/$encName/$encName.php\";\n";
202 * Write the generated LocalSettings to a file
204 * @param string $fileName Full path to filename to write to
206 public function writeFile( $fileName ) {
207 file_put_contents( $fileName, $this->getText() );
213 protected function buildMemcachedServerList() {
214 $servers = $this->values
['_MemCachedServers'];
220 $servers = explode( ',', $servers );
222 foreach ( $servers as $srv ) {
227 return rtrim( $ret, ', ' ) . ' ]';
234 protected function getDefaultText() {
235 if ( !$this->values
['wgImageMagickConvertCommand'] ) {
236 $this->values
['wgImageMagickConvertCommand'] = '/usr/bin/convert';
242 if ( !$this->values
['wgShellLocale'] ) {
243 $this->values
['wgShellLocale'] = 'C.UTF-8';
250 if ( $this->values
['wgMetaNamespace'] !== $this->values
['wgSitename'] ) {
251 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
256 if ( $this->groupPermissions
) {
257 $groupRights .= "# The following permissions were set based on your choice in the installer\n";
258 foreach ( $this->groupPermissions
as $group => $rightArr ) {
259 $group = self
::escapePhpString( $group );
260 foreach ( $rightArr as $right => $perm ) {
261 $right = self
::escapePhpString( $right );
262 $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
263 wfBoolToStr( $perm ) . ";\n";
266 $groupRights .= "\n";
268 if ( ( isset( $this->groupPermissions
['*']['edit'] ) &&
269 $this->groupPermissions
['*']['edit'] === false )
270 && ( isset( $this->groupPermissions
['*']['createaccount'] ) &&
271 $this->groupPermissions
['*']['createaccount'] === false )
272 && ( isset( $this->groupPermissions
['*']['read'] ) &&
273 $this->groupPermissions
['*']['read'] !== false )
275 $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
276 . "# the general public and wish to apply nofollow to external links as a\n"
277 . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
278 . "# and open wikis will generally require other anti-spam measures; for more\n"
279 . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
280 . "\$wgNoFollowLinks = false;\n\n";
285 if ( array_key_exists( 'wgServer', $this->values
) && $this->values
['wgServer'] !== null ) {
286 $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
287 $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";";
290 switch ( $this->values
['_MainCacheType'] ) {
295 $cacheType = 'CACHE_' . strtoupper( $this->values
['_MainCacheType'] );
299 $cacheType = 'CACHE_NONE';
302 $mcservers = $this->buildMemcachedServerList();
303 if ( file_exists( dirname( __DIR__
) . '/PlatformSettings.php' ) ) {
304 $platformSettings = "\n## Include platform/distribution defaults";
305 $platformSettings .= "\nrequire_once \"\$IP/includes/PlatformSettings.php\";";
307 $platformSettings = '';
311 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
312 # installer. If you make manual changes, please keep track in case you
313 # need to recreate them later.
315 # See includes/DefaultSettings.php for all configurable settings
316 # and their default values, but don't forget to make changes in _this_
319 # Further documentation for configuration settings may be found at:
320 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
322 # Protect against web entry
323 if ( !defined( 'MEDIAWIKI' ) ) {
328 ## Uncomment this to disable output compression
329 # \$wgDisableOutputCompression = true;
331 \$wgSitename = \"{$this->values['wgSitename']}\";
333 ## The URL base path to the directory containing the wiki;
334 ## defaults for all runtime URL paths are based off of this.
335 ## For more information on customizing the URLs
336 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
337 ## https://www.mediawiki.org/wiki/Manual:Short_URL
338 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
341 ## The URL path to static resources (images, scripts, etc.)
342 \$wgResourceBasePath = \$wgScriptPath;
344 ## The URL path to the logo. Make sure you change this from the default,
345 ## or else you'll overwrite your logo when you upgrade!
346 \$wgLogo = \"{$this->values['wgLogo']}\";
348 ## UPO means: this is also a user preference option
350 \$wgEnableEmail = {$this->values['wgEnableEmail']};
351 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
353 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
354 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
356 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
357 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
358 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
361 \$wgDBtype = \"{$this->values['wgDBtype']}\";
362 \$wgDBserver = \"{$this->values['wgDBserver']}\";
363 \$wgDBname = \"{$this->values['wgDBname']}\";
364 \$wgDBuser = \"{$this->values['wgDBuser']}\";
365 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
369 ## Shared memory settings
370 \$wgMainCacheType = $cacheType;
371 \$wgMemCachedServers = $mcservers;
373 ## To enable image uploads, make sure the 'images' directory
374 ## is writable, then set this to true:
375 \$wgEnableUploads = {$this->values['wgEnableUploads']};
376 {$magic}\$wgUseImageMagick = true;
377 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
379 # InstantCommons allows wiki to use images from https://commons.wikimedia.org
380 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
382 # Periodically send a pingback to https://www.mediawiki.org/ with basic data
383 # about this MediaWiki instance. The Wikimedia Foundation shares this data
384 # with MediaWiki developers to help guide future development efforts.
385 \$wgPingback = {$this->values['wgPingback']};
387 ## If you use ImageMagick (or any other shell command) on a
388 ## Linux server, this will need to be set to the name of an
389 ## available UTF-8 locale
390 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
392 ## Set \$wgCacheDirectory to a writable directory on the web server
393 ## to make your wiki go slightly faster. The directory should not
394 ## be publicly accessible from the web.
395 #\$wgCacheDirectory = \"\$IP/cache\";
397 # Site language code, should be one of the list in ./languages/data/Names.php
398 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
400 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
402 # Changing this will log out all existing sessions.
403 \$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\";
405 # Site upgrade key. Must be set to a string (default provided) to turn on the
406 # web installer while LocalSettings.php is in place
407 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
409 ## For attaching licensing metadata to pages, and displaying an
410 ## appropriate copyright notice / icon. GNU Free Documentation
411 ## License and Creative Commons licenses are supported so far.
412 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
413 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
414 \$wgRightsText = \"{$this->values['wgRightsText']}\";
415 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
417 # Path to the GNU diff3 utility. Used for conflict resolution.
418 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
420 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
421 ## names, ie 'vector', 'monobook':
422 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";