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