Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / installer / LocalSettingsGenerator.php
blobc0ba300d368e7152dbfe3aa6fd3019d075de82a2
1 <?php
2 /**
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
20 * @file
21 * @ingroup Deployment
24 /**
25 * Class for generating LocalSettings.php file.
27 * @ingroup Deployment
28 * @since 1.17
30 class LocalSettingsGenerator {
32 protected $extensions = array();
33 protected $values = array();
34 protected $groupPermissions = array();
35 protected $dbSettings = '';
36 protected $safeMode = false;
38 /**
39 * @var Installer
41 protected $installer;
43 /**
44 * Constructor.
46 * @param Installer $installer
48 public function __construct( Installer $installer ) {
49 $this->installer = $installer;
51 $this->extensions = $installer->getVar( '_Extensions' );
52 $this->skins = $installer->getVar( '_Skins' );
54 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
56 $confItems = array_merge(
57 array(
58 'wgServer', 'wgScriptPath', 'wgScriptExtension',
59 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
60 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
61 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
62 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
63 'wgRightsText', 'wgMainCacheType', 'wgEnableUploads',
64 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
65 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
66 'wgMetaNamespace', 'wgResourceLoaderMaxQueryLength', 'wgLogo',
68 $db->getGlobalNames()
71 $unescaped = array( 'wgRightsIcon', 'wgLogo' );
72 $boolItems = array(
73 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
74 '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->safeMode = $installer->getVar( '_SafeMode' );
93 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
96 /**
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 * array( '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
113 public static function escapePhpString( $string ) {
114 if ( is_array( $string ) || is_object( $string ) ) {
115 return false;
118 return strtr(
119 $string,
120 array(
121 "\n" => "\\n",
122 "\r" => "\\r",
123 "\t" => "\\t",
124 "\\" => "\\\\",
125 "\$" => "\\\$",
126 "\"" => "\\\""
132 * Return the full text of the generated LocalSettings.php file,
133 * including the extensions and skins.
135 * @return string
137 public function getText() {
138 $localSettings = $this->getDefaultText();
140 if ( count( $this->skins ) ) {
141 $localSettings .= "
142 # Enabled skins.
143 # The following skins were automatically enabled:\n";
145 foreach ( $this->skins as $skinName ) {
146 $encSkinName = self::escapePhpString( $skinName );
147 $localSettings .= "require_once \"\$IP/skins/$encSkinName/$encSkinName.php\";\n";
150 $localSettings .= "\n";
153 if ( count( $this->extensions ) ) {
154 $localSettings .= "
155 # Enabled Extensions. Most extensions are enabled by including the base extension file here
156 # but check specific extension documentation for more details
157 # The following extensions were automatically enabled:\n";
159 foreach ( $this->extensions as $extName ) {
160 $encExtName = self::escapePhpString( $extName );
161 $localSettings .= "require_once \"\$IP/extensions/$encExtName/$encExtName.php\";\n";
164 $localSettings .= "\n";
167 $localSettings .= "
168 # End of automatically generated settings.
169 # Add more configuration options below.\n\n";
171 return $localSettings;
175 * Write the generated LocalSettings to a file
177 * @param string $fileName Full path to filename to write to
179 public function writeFile( $fileName ) {
180 file_put_contents( $fileName, $this->getText() );
184 * @return string
186 protected function buildMemcachedServerList() {
187 $servers = $this->values['_MemCachedServers'];
189 if ( !$servers ) {
190 return 'array()';
191 } else {
192 $ret = 'array( ';
193 $servers = explode( ',', $servers );
195 foreach ( $servers as $srv ) {
196 $srv = trim( $srv );
197 $ret .= "'$srv', ";
200 return rtrim( $ret, ', ' ) . ' )';
205 * @return string
207 protected function getDefaultText() {
208 if ( !$this->values['wgImageMagickConvertCommand'] ) {
209 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
210 $magic = '#';
211 } else {
212 $magic = '';
215 if ( !$this->values['wgShellLocale'] ) {
216 $this->values['wgShellLocale'] = 'en_US.UTF-8';
217 $locale = '#';
218 } else {
219 $locale = '';
222 $hashedUploads = $this->safeMode ? '' : '#';
223 $metaNamespace = '';
224 if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
225 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
228 $groupRights = '';
229 $noFollow = '';
230 if ( $this->groupPermissions ) {
231 $groupRights .= "# The following permissions were set based on your choice in the installer\n";
232 foreach ( $this->groupPermissions as $group => $rightArr ) {
233 $group = self::escapePhpString( $group );
234 foreach ( $rightArr as $right => $perm ) {
235 $right = self::escapePhpString( $right );
236 $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
237 wfBoolToStr( $perm ) . ";\n";
240 $groupRights .= "\n";
242 if ( ( isset( $this->groupPermissions['*']['edit'] ) &&
243 $this->groupPermissions['*']['edit'] === false )
244 && ( isset( $this->groupPermissions['*']['createaccount'] ) &&
245 $this->groupPermissions['*']['createaccount'] === false )
246 && ( isset( $this->groupPermissions['*']['read'] ) &&
247 $this->groupPermissions['*']['read'] !== false )
249 $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
250 . "# the general public and wish to apply nofollow to external links as a\n"
251 . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
252 . "# and open wikis will generally require other anti-spam measures; for more\n"
253 . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
254 . "\$wgNoFollowLinks = false;\n\n";
258 $serverSetting = "";
259 if ( array_key_exists( 'wgServer', $this->values ) && $this->values['wgServer'] !== null ) {
260 $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
261 $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";\n";
264 switch ( $this->values['wgMainCacheType'] ) {
265 case 'anything':
266 case 'db':
267 case 'memcached':
268 case 'accel':
269 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType'] );
270 break;
271 case 'none':
272 default:
273 $cacheType = 'CACHE_NONE';
276 $mcservers = $this->buildMemcachedServerList();
278 return "<?php
279 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
280 # installer. If you make manual changes, please keep track in case you
281 # need to recreate them later.
283 # See includes/DefaultSettings.php for all configurable settings
284 # and their default values, but don't forget to make changes in _this_
285 # file, not there.
287 # Further documentation for configuration settings may be found at:
288 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
290 # Protect against web entry
291 if ( !defined( 'MEDIAWIKI' ) ) {
292 exit;
295 ## Uncomment this to disable output compression
296 # \$wgDisableOutputCompression = true;
298 \$wgSitename = \"{$this->values['wgSitename']}\";
299 {$metaNamespace}
300 ## The URL base path to the directory containing the wiki;
301 ## defaults for all runtime URL paths are based off of this.
302 ## For more information on customizing the URLs
303 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
304 ## https://www.mediawiki.org/wiki/Manual:Short_URL
305 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
306 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
307 ${serverSetting}
308 ## The relative URL path to the skins directory
309 \$wgStylePath = \"\$wgScriptPath/skins\";
311 ## The relative URL path to the logo. Make sure you change this from the default,
312 ## or else you'll overwrite your logo when you upgrade!
313 \$wgLogo = \"{$this->values['wgLogo']}\";
315 ## UPO means: this is also a user preference option
317 \$wgEnableEmail = {$this->values['wgEnableEmail']};
318 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
320 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
321 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
323 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
324 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
325 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
327 ## Database settings
328 \$wgDBtype = \"{$this->values['wgDBtype']}\";
329 \$wgDBserver = \"{$this->values['wgDBserver']}\";
330 \$wgDBname = \"{$this->values['wgDBname']}\";
331 \$wgDBuser = \"{$this->values['wgDBuser']}\";
332 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
334 {$this->dbSettings}
336 ## Shared memory settings
337 \$wgMainCacheType = $cacheType;
338 \$wgMemCachedServers = $mcservers;
340 ## To enable image uploads, make sure the 'images' directory
341 ## is writable, then set this to true:
342 \$wgEnableUploads = {$this->values['wgEnableUploads']};
343 {$magic}\$wgUseImageMagick = true;
344 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
346 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
347 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
349 ## If you use ImageMagick (or any other shell command) on a
350 ## Linux server, this will need to be set to the name of an
351 ## available UTF-8 locale
352 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
354 ## If you want to use image uploads under safe mode,
355 ## create the directories images/archive, images/thumb and
356 ## images/temp, and make them all writable. Then uncomment
357 ## this, if it's not already uncommented:
358 {$hashedUploads}\$wgHashedUploadDirectory = false;
360 ## Set \$wgCacheDirectory to a writable directory on the web server
361 ## to make your wiki go slightly faster. The directory should not
362 ## be publically accessible from the web.
363 #\$wgCacheDirectory = \"\$IP/cache\";
365 # Site language code, should be one of the list in ./languages/Names.php
366 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
368 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
370 # Site upgrade key. Must be set to a string (default provided) to turn on the
371 # web installer while LocalSettings.php is in place
372 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
374 ## For attaching licensing metadata to pages, and displaying an
375 ## appropriate copyright notice / icon. GNU Free Documentation
376 ## License and Creative Commons licenses are supported so far.
377 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
378 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
379 \$wgRightsText = \"{$this->values['wgRightsText']}\";
380 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
382 # Path to the GNU diff3 utility. Used for conflict resolution.
383 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
385 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
386 ## names, ie 'vector', 'monobook':
387 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";