Merge "Typo fix"
[mediawiki.git] / includes / installer / LocalSettingsGenerator.php
blobcca8a4a903fe782ad4ff7a6b89745ea86a5a7208
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 subclass
48 public function __construct( Installer $installer ) {
49 $this->installer = $installer;
51 $this->extensions = $installer->getVar( '_Extensions' );
53 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
55 $confItems = array_merge(
56 array(
57 'wgServer', 'wgScriptPath', 'wgScriptExtension',
58 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
59 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
60 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
61 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
62 'wgRightsText', 'wgMainCacheType', 'wgEnableUploads',
63 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
64 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
65 'wgMetaNamespace', 'wgResourceLoaderMaxQueryLength', 'wgLogo',
67 $db->getGlobalNames()
70 $unescaped = array( 'wgRightsIcon', 'wgLogo' );
71 $boolItems = array(
72 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
73 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
76 foreach ( $confItems as $c ) {
77 $val = $installer->getVar( $c );
79 if ( in_array( $c, $boolItems ) ) {
80 $val = wfBoolToStr( $val );
83 if ( !in_array( $c, $unescaped ) ) {
84 $val = self::escapePhpString( $val );
87 $this->values[$c] = $val;
90 $this->dbSettings = $db->getLocalSettings();
91 $this->safeMode = $installer->getVar( '_SafeMode' );
92 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
95 /**
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 * array( '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
112 public static function escapePhpString( $string ) {
113 if ( is_array( $string ) || is_object( $string ) ) {
114 return false;
117 return strtr(
118 $string,
119 array(
120 "\n" => "\\n",
121 "\r" => "\\r",
122 "\t" => "\\t",
123 "\\" => "\\\\",
124 "\$" => "\\\$",
125 "\"" => "\\\""
131 * Return the full text of the generated LocalSettings.php file,
132 * including the extensions
134 * @return String
136 public function getText() {
137 $localSettings = $this->getDefaultText();
139 if ( count( $this->extensions ) ) {
140 $localSettings .= "
141 # Enabled Extensions. Most extensions are enabled by including the base extension file here
142 # but check specific extension documentation for more details
143 # The following extensions were automatically enabled:\n";
145 foreach ( $this->extensions as $extName ) {
146 $encExtName = self::escapePhpString( $extName );
147 $localSettings .= "require_once \"\$IP/extensions/$encExtName/$encExtName.php\";\n";
151 $localSettings .= "\n\n# End of automatically generated settings.
152 # Add more configuration options below.\n\n";
154 return $localSettings;
158 * Write the generated LocalSettings to a file
160 * @param string $fileName Full path to filename to write to
162 public function writeFile( $fileName ) {
163 file_put_contents( $fileName, $this->getText() );
167 * @return String
169 protected function buildMemcachedServerList() {
170 $servers = $this->values['_MemCachedServers'];
172 if ( !$servers ) {
173 return 'array()';
174 } else {
175 $ret = 'array( ';
176 $servers = explode( ',', $servers );
178 foreach ( $servers as $srv ) {
179 $srv = trim( $srv );
180 $ret .= "'$srv', ";
183 return rtrim( $ret, ', ' ) . ' )';
188 * @return String
190 protected function getDefaultText() {
191 if ( !$this->values['wgImageMagickConvertCommand'] ) {
192 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
193 $magic = '#';
194 } else {
195 $magic = '';
198 if ( !$this->values['wgShellLocale'] ) {
199 $this->values['wgShellLocale'] = 'en_US.UTF-8';
200 $locale = '#';
201 } else {
202 $locale = '';
205 //$rightsUrl = $this->values['wgRightsUrl'] ? '' : '#'; // TODO: Fixme, I'm unused!
206 $hashedUploads = $this->safeMode ? '' : '#';
207 $metaNamespace = '';
208 if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
209 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
212 $groupRights = '';
213 if ( $this->groupPermissions ) {
214 $groupRights .= "# The following permissions were set based on your choice in the installer\n";
215 foreach ( $this->groupPermissions as $group => $rightArr ) {
216 $group = self::escapePhpString( $group );
217 foreach ( $rightArr as $right => $perm ) {
218 $right = self::escapePhpString( $right );
219 $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
220 wfBoolToStr( $perm ) . ";\n";
225 switch ( $this->values['wgMainCacheType'] ) {
226 case 'anything':
227 case 'db':
228 case 'memcached':
229 case 'accel':
230 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType'] );
231 break;
232 case 'none':
233 default:
234 $cacheType = 'CACHE_NONE';
237 $mcservers = $this->buildMemcachedServerList();
238 return "<?php
239 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
240 # installer. If you make manual changes, please keep track in case you
241 # need to recreate them later.
243 # See includes/DefaultSettings.php for all configurable settings
244 # and their default values, but don't forget to make changes in _this_
245 # file, not there.
247 # Further documentation for configuration settings may be found at:
248 # http://www.mediawiki.org/wiki/Manual:Configuration_settings
250 # Protect against web entry
251 if ( !defined( 'MEDIAWIKI' ) ) {
252 exit;
255 ## Uncomment this to disable output compression
256 # \$wgDisableOutputCompression = true;
258 \$wgSitename = \"{$this->values['wgSitename']}\";
259 {$metaNamespace}
260 ## The URL base path to the directory containing the wiki;
261 ## defaults for all runtime URL paths are based off of this.
262 ## For more information on customizing the URLs
263 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
264 ## http://www.mediawiki.org/wiki/Manual:Short_URL
265 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
266 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
268 ## The protocol and server name to use in fully-qualified URLs
269 \$wgServer = \"{$this->values['wgServer']}\";
271 ## The relative URL path to the skins directory
272 \$wgStylePath = \"\$wgScriptPath/skins\";
274 ## The relative URL path to the logo. Make sure you change this from the default,
275 ## or else you'll overwrite your logo when you upgrade!
276 \$wgLogo = \"{$this->values['wgLogo']}\";
278 ## UPO means: this is also a user preference option
280 \$wgEnableEmail = {$this->values['wgEnableEmail']};
281 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
283 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
284 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
286 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
287 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
288 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
290 ## Database settings
291 \$wgDBtype = \"{$this->values['wgDBtype']}\";
292 \$wgDBserver = \"{$this->values['wgDBserver']}\";
293 \$wgDBname = \"{$this->values['wgDBname']}\";
294 \$wgDBuser = \"{$this->values['wgDBuser']}\";
295 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
297 {$this->dbSettings}
299 ## Shared memory settings
300 \$wgMainCacheType = $cacheType;
301 \$wgMemCachedServers = $mcservers;
303 ## To enable image uploads, make sure the 'images' directory
304 ## is writable, then set this to true:
305 \$wgEnableUploads = {$this->values['wgEnableUploads']};
306 {$magic}\$wgUseImageMagick = true;
307 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
309 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
310 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
312 ## If you use ImageMagick (or any other shell command) on a
313 ## Linux server, this will need to be set to the name of an
314 ## available UTF-8 locale
315 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
317 ## If you want to use image uploads under safe mode,
318 ## create the directories images/archive, images/thumb and
319 ## images/temp, and make them all writable. Then uncomment
320 ## this, if it's not already uncommented:
321 {$hashedUploads}\$wgHashedUploadDirectory = false;
323 ## Set \$wgCacheDirectory to a writable directory on the web server
324 ## to make your wiki go slightly faster. The directory should not
325 ## be publically accessible from the web.
326 #\$wgCacheDirectory = \"\$IP/cache\";
328 # Site language code, should be one of the list in ./languages/Names.php
329 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
331 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
333 # Site upgrade key. Must be set to a string (default provided) to turn on the
334 # web installer while LocalSettings.php is in place
335 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
337 ## Default skin: you can change the default skin. Use the internal symbolic
338 ## names, ie 'cologneblue', 'monobook', 'vector':
339 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
341 ## For attaching licensing metadata to pages, and displaying an
342 ## appropriate copyright notice / icon. GNU Free Documentation
343 ## License and Creative Commons licenses are supported so far.
344 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
345 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
346 \$wgRightsText = \"{$this->values['wgRightsText']}\";
347 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
349 # Path to the GNU diff3 utility. Used for conflict resolution.
350 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
352 {$groupRights}";