Localisation updates for core and extension messages from translatewiki.net (2011...
[lhc/web/wiklou.git] / includes / installer / LocalSettingsGenerator.php
1 <?php
2 /**
3 * Generator for LocalSettings.php file.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Class for generating LocalSettings.php file.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 class LocalSettingsGenerator {
16
17 private $extensions = array();
18 private $values = array();
19 private $dbSettings = '';
20 private $safeMode = false;
21
22 /**
23 * @var Installer
24 */
25 private $installer;
26
27 /**
28 * Constructor.
29 *
30 * @param $installer Installer subclass
31 */
32 public function __construct( Installer $installer ) {
33 $this->installer = $installer;
34
35 $this->extensions = $installer->getVar( '_Extensions' );
36
37 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
38
39 $confItems = array_merge(
40 array(
41 'wgScriptPath', 'wgScriptExtension',
42 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
43 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
44 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
45 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
46 'wgRightsText', 'wgRightsCode', 'wgMainCacheType', 'wgEnableUploads',
47 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
48 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
49 'wgMetaNamespace'
50 ),
51 $db->getGlobalNames()
52 );
53
54 $unescaped = array( 'wgRightsIcon' );
55 $boolItems = array(
56 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
57 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
58 );
59
60 foreach( $confItems as $c ) {
61 $val = $installer->getVar( $c );
62
63 if( in_array( $c, $boolItems ) ) {
64 $val = wfBoolToStr( $val );
65 }
66
67 if ( !in_array( $c, $unescaped ) ) {
68 $val = self::escapePhpString( $val );
69 }
70
71 $this->values[$c] = $val;
72 }
73
74 $this->dbSettings = $db->getLocalSettings();
75 $this->safeMode = $installer->getVar( '_SafeMode' );
76 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
77 }
78
79 /**
80 * Returns the escaped version of a string of php code.
81 *
82 * @param $string String
83 *
84 * @return String
85 */
86 public static function escapePhpString( $string ) {
87 if ( is_array( $string ) || is_object( $string ) ) {
88 return false;
89 }
90
91 return strtr(
92 $string,
93 array(
94 "\n" => "\\n",
95 "\r" => "\\r",
96 "\t" => "\\t",
97 "\\" => "\\\\",
98 "\$" => "\\\$",
99 "\"" => "\\\""
100 )
101 );
102 }
103
104 /**
105 * Return the full text of the generated LocalSettings.php file,
106 * including the extensions
107 *
108 * @return String
109 */
110 public function getText() {
111 $localSettings = $this->getDefaultText();
112
113 if( count( $this->extensions ) ) {
114 $localSettings .= "\n# The following extensions were automatically enabled:\n";
115
116 foreach( $this->extensions as $extName ) {
117 $encExtName = self::escapePhpString( $extName );
118 $localSettings .= "require( \"extensions/$encExtName/$encExtName.php\" );\n";
119 }
120 }
121
122 return $localSettings;
123 }
124
125 /**
126 * Write the generated LocalSettings to a file
127 *
128 * @param $fileName String Full path to filename to write to
129 */
130 public function writeFile( $fileName ) {
131 file_put_contents( $fileName, $this->getText() );
132 }
133
134 /**
135 * @return String
136 */
137 private function buildMemcachedServerList() {
138 $servers = $this->values['_MemCachedServers'];
139
140 if( !$servers ) {
141 return 'array()';
142 } else {
143 $ret = 'array( ';
144 $servers = explode( ',', $servers );
145
146 foreach( $servers as $srv ) {
147 $srv = trim( $srv );
148 $ret .= "'$srv', ";
149 }
150
151 return rtrim( $ret, ', ' ) . ' )';
152 }
153 }
154
155 /**
156 * @return String
157 */
158 private function getDefaultText() {
159 if( !$this->values['wgImageMagickConvertCommand'] ) {
160 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
161 $magic = '#';
162 } else {
163 $magic = '';
164 }
165
166 if( !$this->values['wgShellLocale'] ) {
167 $this->values['wgShellLocale'] = 'en_US.UTF-8';
168 $locale = '#';
169 } else {
170 $locale = '';
171 }
172
173 $rights = $this->values['wgRightsUrl'] ? '' : '#';
174 $hashedUploads = $this->safeMode ? '' : '#';
175 $metaNamespace = '';
176 if( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
177 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
178 }
179
180 switch( $this->values['wgMainCacheType'] ) {
181 case 'anything':
182 case 'db':
183 case 'memcached':
184 case 'accel':
185 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType']);
186 break;
187 case 'none':
188 default:
189 $cacheType = 'CACHE_NONE';
190 }
191
192 $mcservers = $this->buildMemcachedServerList();
193 return "<?php
194 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
195 # installer. If you make manual changes, please keep track in case you
196 # need to recreate them later.
197 #
198 # See includes/DefaultSettings.php for all configurable settings
199 # and their default values, but don't forget to make changes in _this_
200 # file, not there.
201 #
202 # Further documentation for configuration settings may be found at:
203 # http://www.mediawiki.org/wiki/Manual:Configuration_settings
204
205 # Protect against web entry
206 if ( !defined( 'MEDIAWIKI' ) ) {
207 exit;
208 }
209
210 ## Uncomment this to disable output compression
211 # \$wgDisableOutputCompression = true;
212
213 \$wgSitename = \"{$this->values['wgSitename']}\";
214 {$metaNamespace}
215 ## The URL base path to the directory containing the wiki;
216 ## defaults for all runtime URL paths are based off of this.
217 ## For more information on customizing the URLs please see:
218 ## http://www.mediawiki.org/wiki/Manual:Short_URL
219 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
220 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
221
222 ## The relative URL path to the skins directory
223 \$wgStylePath = \"\$wgScriptPath/skins\";
224
225 ## The relative URL path to the logo. Make sure you change this from the default,
226 ## or else you'll overwrite your logo when you upgrade!
227 \$wgLogo = \"\$wgStylePath/common/images/wiki.png\";
228
229 ## UPO means: this is also a user preference option
230
231 \$wgEnableEmail = {$this->values['wgEnableEmail']};
232 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
233
234 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
235 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
236
237 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
238 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
239 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
240
241 ## Database settings
242 \$wgDBtype = \"{$this->values['wgDBtype']}\";
243 \$wgDBserver = \"{$this->values['wgDBserver']}\";
244 \$wgDBname = \"{$this->values['wgDBname']}\";
245 \$wgDBuser = \"{$this->values['wgDBuser']}\";
246 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
247
248 {$this->dbSettings}
249
250 ## Shared memory settings
251 \$wgMainCacheType = $cacheType;
252 \$wgMemCachedServers = $mcservers;
253
254 ## To enable image uploads, make sure the 'images' directory
255 ## is writable, then set this to true:
256 \$wgEnableUploads = {$this->values['wgEnableUploads']};
257 {$magic}\$wgUseImageMagick = true;
258 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
259
260 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
261 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
262
263 ## If you use ImageMagick (or any other shell command) on a
264 ## Linux server, this will need to be set to the name of an
265 ## available UTF-8 locale
266 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
267
268 ## If you want to use image uploads under safe mode,
269 ## create the directories images/archive, images/thumb and
270 ## images/temp, and make them all writable. Then uncomment
271 ## this, if it's not already uncommented:
272 {$hashedUploads}\$wgHashedUploadDirectory = false;
273
274 ## If you have the appropriate support software installed
275 ## you can enable inline LaTeX equations:
276 \$wgUseTeX = false;
277
278 ## Set \$wgCacheDirectory to a writable directory on the web server
279 ## to make your wiki go slightly faster. The directory should not
280 ## be publically accessible from the web.
281 #\$wgCacheDirectory = \"\$IP/cache\";
282
283 # Site language code, should be one of ./languages/Language(.*).php
284 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
285
286 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
287
288 # Site upgrade key. Must be set to a string (default provided) to turn on the
289 # web installer while LocalSettings.php is in place
290 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
291
292 ## Default skin: you can change the default skin. Use the internal symbolic
293 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
294 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
295
296 ## For attaching licensing metadata to pages, and displaying an
297 ## appropriate copyright notice / icon. GNU Free Documentation
298 ## License and Creative Commons licenses are supported so far.
299 {$rights}\$wgEnableCreativeCommonsRdf = true;
300 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
301 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
302 \$wgRightsText = \"{$this->values['wgRightsText']}\";
303 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
304 # \$wgRightsCode = \"{$this->values['wgRightsCode']}\"; # Not yet used
305
306 # Path to the GNU diff3 utility. Used for conflict resolution.
307 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
308
309 # Enabled Extensions. Most extensions are enabled by including the base extension file here
310 # but check specific extension documentation for more details
311 ";
312 }
313
314 }