* (bug 32786) Fix backwards compatibility for extensions using 1.17's Database::newFr...
[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 'wgServer', 'wgScriptPath', 'wgScriptExtension',
43 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
44 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
45 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
46 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
47 'wgRightsText', '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_once( \"\$IP/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'] ? '' : '#'; // TODO: Fixme, I'm unused!
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 protocol and server name to use in fully-qualified URLs
253 \$wgServer = \"{$this->values['wgServer']}\";
254
255 ## The relative URL path to the skins directory
256 \$wgStylePath = \"\$wgScriptPath/skins\";
257
258 ## The relative URL path to the logo. Make sure you change this from the default,
259 ## or else you'll overwrite your logo when you upgrade!
260 \$wgLogo = \"\$wgStylePath/common/images/wiki.png\";
261
262 ## UPO means: this is also a user preference option
263
264 \$wgEnableEmail = {$this->values['wgEnableEmail']};
265 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
266
267 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
268 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
269
270 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
271 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
272 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
273
274 ## Database settings
275 \$wgDBtype = \"{$this->values['wgDBtype']}\";
276 \$wgDBserver = \"{$this->values['wgDBserver']}\";
277 \$wgDBname = \"{$this->values['wgDBname']}\";
278 \$wgDBuser = \"{$this->values['wgDBuser']}\";
279 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
280
281 {$this->dbSettings}
282
283 ## Shared memory settings
284 \$wgMainCacheType = $cacheType;
285 \$wgMemCachedServers = $mcservers;
286
287 ## To enable image uploads, make sure the 'images' directory
288 ## is writable, then set this to true:
289 \$wgEnableUploads = {$this->values['wgEnableUploads']};
290 {$magic}\$wgUseImageMagick = true;
291 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
292
293 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
294 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
295
296 ## If you use ImageMagick (or any other shell command) on a
297 ## Linux server, this will need to be set to the name of an
298 ## available UTF-8 locale
299 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
300
301 ## If you want to use image uploads under safe mode,
302 ## create the directories images/archive, images/thumb and
303 ## images/temp, and make them all writable. Then uncomment
304 ## this, if it's not already uncommented:
305 {$hashedUploads}\$wgHashedUploadDirectory = false;
306
307 ## Set \$wgCacheDirectory to a writable directory on the web server
308 ## to make your wiki go slightly faster. The directory should not
309 ## be publically accessible from the web.
310 #\$wgCacheDirectory = \"\$IP/cache\";
311
312 # Site language code, should be one of the list in ./languages/Names.php
313 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
314
315 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
316
317 # Site upgrade key. Must be set to a string (default provided) to turn on the
318 # web installer while LocalSettings.php is in place
319 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
320
321 ## Default skin: you can change the default skin. Use the internal symbolic
322 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
323 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
324
325 ## For attaching licensing metadata to pages, and displaying an
326 ## appropriate copyright notice / icon. GNU Free Documentation
327 ## License and Creative Commons licenses are supported so far.
328 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
329 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
330 \$wgRightsText = \"{$this->values['wgRightsText']}\";
331 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
332
333 # Path to the GNU diff3 utility. Used for conflict resolution.
334 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
335
336 # Query string length limit for ResourceLoader. You should only set this if
337 # your web server has a query string length limit (then set it to that limit),
338 # or if you have suhosin.get.max_value_length set in php.ini (then set it to
339 # that value)
340 \$wgResourceLoaderMaxQueryLength = {$this->values['wgResourceLoaderMaxQueryLength']};
341
342 {$groupRights}";
343 }
344
345 }