Followup r75392. Per IRC, we should generate a default $wgUpgradeKey (took a semi...
[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'
49 ),
50 $db->getGlobalNames()
51 );
52
53 $unescaped = array( 'wgRightsIcon' );
54 $boolItems = array(
55 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
56 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
57 );
58
59 foreach( $confItems as $c ) {
60 $val = $installer->getVar( $c );
61
62 if( in_array( $c, $boolItems ) ) {
63 $val = wfBoolToStr( $val );
64 }
65
66 if ( !in_array( $c, $unescaped ) ) {
67 $val = self::escapePhpString( $val );
68 }
69
70 $this->values[$c] = $val;
71 }
72
73 $this->dbSettings = $db->getLocalSettings();
74 $this->safeMode = $installer->getVar( '_SafeMode' );
75 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
76 }
77
78 /**
79 * Returns the escaped version of a string of php code.
80 *
81 * @param $string String
82 *
83 * @return String
84 */
85 public static function escapePhpString( $string ) {
86 if ( is_array( $string ) || is_object( $string ) ) {
87 return false;
88 }
89
90 return strtr(
91 $string,
92 array(
93 "\n" => "\\n",
94 "\r" => "\\r",
95 "\t" => "\\t",
96 "\\" => "\\\\",
97 "\$" => "\\\$",
98 "\"" => "\\\""
99 )
100 );
101 }
102
103 /**
104 * Return the full text of the generated LocalSettings.php file,
105 * including the extensions
106 *
107 * @return String
108 */
109 public function getText() {
110 $localSettings = $this->getDefaultText();
111
112 if( count( $this->extensions ) ) {
113 $localSettings .= "\n# The following extensions were automatically enabled:\n";
114
115 foreach( $this->extensions as $ext ) {
116 $localSettings .= "require( 'extensions/$ext/$ext.php' );\n";
117 }
118 }
119
120 return $localSettings;
121 }
122
123 /**
124 * Write the generated LocalSettings to a file
125 *
126 * @param $fileName String Full path to filename to write to
127 */
128 public function writeFile( $fileName ) {
129 file_put_contents( $fileName, $this->getText() );
130 }
131
132 /**
133 * @return String
134 */
135 private function buildMemcachedServerList() {
136 $servers = $this->values['_MemCachedServers'];
137
138 if( !$servers ) {
139 return 'array()';
140 } else {
141 $ret = 'array( ';
142 $servers = explode( ',', $servers );
143
144 foreach( $servers as $srv ) {
145 $srv = trim( $srv );
146 $ret .= "'$srv', ";
147 }
148
149 return rtrim( $ret, ', ' ) . ' )';
150 }
151 }
152
153 /**
154 * @return String
155 */
156 private function getDefaultText() {
157 if( !$this->values['wgImageMagickConvertCommand'] ) {
158 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
159 $magic = '#';
160 } else {
161 $magic = '';
162 }
163
164 if( !$this->values['wgShellLocale'] ) {
165 $this->values['wgShellLocale'] = 'en_US.UTF-8';
166 $locale = '#';
167 } else {
168 $locale = '';
169 }
170
171 $rights = $this->values['wgRightsUrl'] ? '' : '#';
172 $hashedUploads = $this->safeMode ? '' : '#';
173
174 switch( $this->values['wgMainCacheType'] ) {
175 case 'anything':
176 case 'db':
177 case 'memcached':
178 case 'accel':
179 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType']);
180 break;
181 case 'none':
182 default:
183 $cacheType = 'CACHE_NONE';
184 }
185
186 $mcservers = $this->buildMemcachedServerList();
187 return "<?php
188 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
189 # installer. If you make manual changes, please keep track in case you
190 # need to recreate them later.
191 #
192 # See includes/DefaultSettings.php for all configurable settings
193 # and their default values, but don't forget to make changes in _this_
194 # file, not there.
195 #
196 # Further documentation for configuration settings may be found at:
197 # http://www.mediawiki.org/wiki/Manual:Configuration_settings
198
199 # If you customize your file layout, set \$IP to the directory that contains
200 # the other MediaWiki files. It will be used as a base to locate files.
201 if( defined( 'MW_INSTALL_PATH' ) ) {
202 \$IP = MW_INSTALL_PATH;
203 } else {
204 \$IP = dirname( __FILE__ );
205 }
206
207 require_once( \"\$IP/includes/DefaultSettings.php\" );
208
209 if ( \$wgCommandLineMode ) {
210 if ( isset( \$_SERVER ) && array_key_exists( 'REQUEST_METHOD', \$_SERVER ) ) {
211 die( \"This script must be run from the command line\\n\" );
212 }
213 }
214 ## Uncomment this to disable output compression
215 # \$wgDisableOutputCompression = true;
216
217 \$wgSitename = \"{$this->values['wgSitename']}\";
218
219 ## The URL base path to the directory containing the wiki;
220 ## defaults for all runtime URL paths are based off of this.
221 ## For more information on customizing the URLs please see:
222 ## http://www.mediawiki.org/wiki/Manual:Short_URL
223 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
224 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
225
226 ## The relative URL path to the skins directory
227 \$wgStylePath = \"\$wgScriptPath/skins\";
228
229 ## The relative URL path to the logo. Make sure you change this from the default,
230 ## or else you'll overwrite your logo when you upgrade!
231 \$wgLogo = \"\$wgStylePath/common/images/wiki.png\";
232
233 ## UPO means: this is also a user preference option
234
235 \$wgEnableEmail = {$this->values['wgEnableEmail']};
236 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
237
238 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
239 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
240
241 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
242 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
243 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
244
245 ## Database settings
246 \$wgDBtype = \"{$this->values['wgDBtype']}\";
247 \$wgDBserver = \"{$this->values['wgDBserver']}\";
248 \$wgDBname = \"{$this->values['wgDBname']}\";
249 \$wgDBuser = \"{$this->values['wgDBuser']}\";
250 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
251
252 {$this->dbSettings}
253
254 ## Shared memory settings
255 \$wgMainCacheType = $cacheType;
256 \$wgMemCachedServers = $mcservers;
257
258 ## To enable image uploads, make sure the 'images' directory
259 ## is writable, then set this to true:
260 \$wgEnableUploads = {$this->values['wgEnableUploads']};
261 {$magic}\$wgUseImageMagick = true;
262 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
263
264 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
265 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
266
267 ## If you use ImageMagick (or any other shell command) on a
268 ## Linux server, this will need to be set to the name of an
269 ## available UTF-8 locale
270 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
271
272 ## If you want to use image uploads under safe mode,
273 ## create the directories images/archive, images/thumb and
274 ## images/temp, and make them all writable. Then uncomment
275 ## this, if it's not already uncommented:
276 {$hashedUploads}\$wgHashedUploadDirectory = false;
277
278 ## If you have the appropriate support software installed
279 ## you can enable inline LaTeX equations:
280 \$wgUseTeX = false;
281
282 ## Set \$wgCacheDirectory to a writable directory on the web server
283 ## to make your wiki go slightly faster. The directory should not
284 ## be publically accessible from the web.
285 #\$wgCacheDirectory = \"\$IP/cache\";
286
287 \$wgLocalInterwiki = strtolower( \$wgSitename );
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 = 'vector';
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 # When you make changes to this configuration file, this will make
316 # sure that cached pages are cleared.
317 \$wgCacheEpoch = max( \$wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
318
319 # Enabled Extensions. Most extensions are enabled by including the base extension file here
320 # but check specific extension documentation for more details
321 ";
322 }
323
324 }