Fixup r75858, copy+paste error
[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 $configPath = '';
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->configPath = $installer->getVar( 'IP' ) . '/config';
37 $this->extensions = $installer->getVar( '_Extensions' );
38
39 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
40
41 $confItems = array_merge(
42 array(
43 'wgScriptPath', 'wgScriptExtension',
44 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
45 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
46 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
47 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
48 'wgRightsText', 'wgRightsCode', 'wgMainCacheType', 'wgEnableUploads',
49 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
50 'wgDBpassword', 'wgUseInstantCommons'
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 * Returns the escaped version of a string of php code.
82 *
83 * @param $string String
84 *
85 * @return String
86 */
87 public static function escapePhpString( $string ) {
88 if ( is_array( $string ) || is_object( $string ) ) {
89 return false;
90 }
91
92 return strtr(
93 $string,
94 array(
95 "\n" => "\\n",
96 "\r" => "\\r",
97 "\t" => "\\t",
98 "\\" => "\\\\",
99 "\$" => "\\\$",
100 "\"" => "\\\""
101 )
102 );
103 }
104
105 /**
106 * Return the full text of the generated LocalSettings.php file,
107 * including the extensions
108 *
109 * @return String
110 */
111 public function getText() {
112 $localSettings = $this->getDefaultText();
113
114 if( count( $this->extensions ) ) {
115 $localSettings .= "\n# The following extensions were automatically enabled:\n";
116
117 foreach( $this->extensions as $ext ) {
118 $localSettings .= "require( 'extensions/$ext/$ext.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
176 switch( $this->values['wgMainCacheType'] ) {
177 case 'anything':
178 case 'db':
179 case 'memcached':
180 case 'accel':
181 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType']);
182 break;
183 case 'none':
184 default:
185 $cacheType = 'CACHE_NONE';
186 }
187
188 $mcservers = $this->buildMemcachedServerList();
189 return "<?php
190 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
191 # installer. If you make manual changes, please keep track in case you
192 # need to recreate them later.
193 #
194 # See includes/DefaultSettings.php for all configurable settings
195 # and their default values, but don't forget to make changes in _this_
196 # file, not there.
197 #
198 # Further documentation for configuration settings may be found at:
199 # http://www.mediawiki.org/wiki/Manual:Configuration_settings
200
201 # If you customize your file layout, set \$IP to the directory that contains
202 # the other MediaWiki files. It will be used as a base to locate files.
203 if( defined( 'MW_INSTALL_PATH' ) ) {
204 \$IP = MW_INSTALL_PATH;
205 } else {
206 \$IP = dirname( __FILE__ );
207 }
208
209 require_once( \"\$IP/includes/DefaultSettings.php\" );
210
211 if ( \$wgCommandLineMode ) {
212 if ( isset( \$_SERVER ) && array_key_exists( 'REQUEST_METHOD', \$_SERVER ) ) {
213 die( \"This script must be run from the command line\\n\" );
214 }
215 }
216 ## Uncomment this to disable output compression
217 # \$wgDisableOutputCompression = true;
218
219 \$wgSitename = \"{$this->values['wgSitename']}\";
220
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 \$wgLocalInterwiki = strtolower( \$wgSitename );
290
291 # Site language code, should be one of ./languages/Language(.*).php
292 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
293
294 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
295
296 ## Default skin: you can change the default skin. Use the internal symbolic
297 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
298 \$wgDefaultSkin = 'vector';
299
300 ## For attaching licensing metadata to pages, and displaying an
301 ## appropriate copyright notice / icon. GNU Free Documentation
302 ## License and Creative Commons licenses are supported so far.
303 {$rights}\$wgEnableCreativeCommonsRdf = true;
304 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
305 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
306 \$wgRightsText = \"{$this->values['wgRightsText']}\";
307 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
308 # \$wgRightsCode = \"{$this->values['wgRightsCode']}\"; # Not yet used
309
310 # Path to the GNU diff3 utility. Used for conflict resolution.
311 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
312
313 # When you make changes to this configuration file, this will make
314 # sure that cached pages are cleared.
315 \$wgCacheEpoch = max( \$wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
316
317 # Enabled Extensions. Most extensions are enabled by including the base extension file here
318 # but check specific extension documentation for more details
319 ";
320 }
321
322 }