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