LocalSettings generation tweaks:
[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 .= "
115 # Enabled Extensions. Most extensions are enabled by including the base extension file here
116 # but check specific extension documentation for more details
117 # The following extensions were automatically enabled:\n";
118
119 foreach( $this->extensions as $extName ) {
120 $encExtName = self::escapePhpString( $extName );
121 $localSettings .= "require( \"extensions/$encExtName/$encExtName.php\" );\n";
122 }
123 }
124
125 $localSettings .= "\n\n# End of automatically generated settings.
126 # Add more configuration options below.\n\n";
127
128 return $localSettings;
129 }
130
131 /**
132 * Write the generated LocalSettings to a file
133 *
134 * @param $fileName String Full path to filename to write to
135 */
136 public function writeFile( $fileName ) {
137 file_put_contents( $fileName, $this->getText() );
138 }
139
140 /**
141 * @return String
142 */
143 private function buildMemcachedServerList() {
144 $servers = $this->values['_MemCachedServers'];
145
146 if( !$servers ) {
147 return 'array()';
148 } else {
149 $ret = 'array( ';
150 $servers = explode( ',', $servers );
151
152 foreach( $servers as $srv ) {
153 $srv = trim( $srv );
154 $ret .= "'$srv', ";
155 }
156
157 return rtrim( $ret, ', ' ) . ' )';
158 }
159 }
160
161 /**
162 * @return String
163 */
164 private function getDefaultText() {
165 if( !$this->values['wgImageMagickConvertCommand'] ) {
166 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
167 $magic = '#';
168 } else {
169 $magic = '';
170 }
171
172 if( !$this->values['wgShellLocale'] ) {
173 $this->values['wgShellLocale'] = 'en_US.UTF-8';
174 $locale = '#';
175 } else {
176 $locale = '';
177 }
178
179 $rights = $this->values['wgRightsUrl'] ? '' : '#';
180 $hashedUploads = $this->safeMode ? '' : '#';
181 $metaNamespace = '';
182 if( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
183 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
184 }
185
186 switch( $this->values['wgMainCacheType'] ) {
187 case 'anything':
188 case 'db':
189 case 'memcached':
190 case 'accel':
191 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType']);
192 break;
193 case 'none':
194 default:
195 $cacheType = 'CACHE_NONE';
196 }
197
198 $mcservers = $this->buildMemcachedServerList();
199 return "<?php
200 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
201 # installer. If you make manual changes, please keep track in case you
202 # need to recreate them later.
203 #
204 # See includes/DefaultSettings.php for all configurable settings
205 # and their default values, but don't forget to make changes in _this_
206 # file, not there.
207 #
208 # Further documentation for configuration settings may be found at:
209 # http://www.mediawiki.org/wiki/Manual:Configuration_settings
210
211 # Protect against web entry
212 if ( !defined( 'MEDIAWIKI' ) ) {
213 exit;
214 }
215
216 ## Uncomment this to disable output compression
217 # \$wgDisableOutputCompression = true;
218
219 \$wgSitename = \"{$this->values['wgSitename']}\";
220 {$metaNamespace}
221 ## The URL base path to the directory containing the wiki;
222 ## defaults for all runtime URL paths are based off of this.
223 ## For more information on customizing the URLs please see:
224 ## http://www.mediawiki.org/wiki/Manual:Short_URL
225 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
226 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
227
228 ## The relative URL path to the skins directory
229 \$wgStylePath = \"\$wgScriptPath/skins\";
230
231 ## The relative URL path to the logo. Make sure you change this from the default,
232 ## or else you'll overwrite your logo when you upgrade!
233 \$wgLogo = \"\$wgStylePath/common/images/wiki.png\";
234
235 ## UPO means: this is also a user preference option
236
237 \$wgEnableEmail = {$this->values['wgEnableEmail']};
238 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
239
240 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
241 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
242
243 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
244 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
245 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
246
247 ## Database settings
248 \$wgDBtype = \"{$this->values['wgDBtype']}\";
249 \$wgDBserver = \"{$this->values['wgDBserver']}\";
250 \$wgDBname = \"{$this->values['wgDBname']}\";
251 \$wgDBuser = \"{$this->values['wgDBuser']}\";
252 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
253
254 {$this->dbSettings}
255
256 ## Shared memory settings
257 \$wgMainCacheType = $cacheType;
258 \$wgMemCachedServers = $mcservers;
259
260 ## To enable image uploads, make sure the 'images' directory
261 ## is writable, then set this to true:
262 \$wgEnableUploads = {$this->values['wgEnableUploads']};
263 {$magic}\$wgUseImageMagick = true;
264 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
265
266 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
267 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
268
269 ## If you use ImageMagick (or any other shell command) on a
270 ## Linux server, this will need to be set to the name of an
271 ## available UTF-8 locale
272 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
273
274 ## If you want to use image uploads under safe mode,
275 ## create the directories images/archive, images/thumb and
276 ## images/temp, and make them all writable. Then uncomment
277 ## this, if it's not already uncommented:
278 {$hashedUploads}\$wgHashedUploadDirectory = false;
279
280 ## If you have the appropriate support software installed
281 ## you can enable inline LaTeX equations:
282 \$wgUseTeX = false;
283
284 ## Set \$wgCacheDirectory to a writable directory on the web server
285 ## to make your wiki go slightly faster. The directory should not
286 ## be publically accessible from the web.
287 #\$wgCacheDirectory = \"\$IP/cache\";
288
289 # Site language code, should be one of ./languages/Language(.*).php
290 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
291
292 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
293
294 # Site upgrade key. Must be set to a string (default provided) to turn on the
295 # web installer while LocalSettings.php is in place
296 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
297
298 ## Default skin: you can change the default skin. Use the internal symbolic
299 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
300 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
301
302 ## For attaching licensing metadata to pages, and displaying an
303 ## appropriate copyright notice / icon. GNU Free Documentation
304 ## License and Creative Commons licenses are supported so far.
305 {$rights}\$wgEnableCreativeCommonsRdf = true;
306 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
307 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
308 \$wgRightsText = \"{$this->values['wgRightsText']}\";
309 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
310 # \$wgRightsCode = \"{$this->values['wgRightsCode']}\"; # Not yet used
311
312 # Path to the GNU diff3 utility. Used for conflict resolution.
313 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
314 ";
315 }
316
317 }