* Added file description headers
[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 * @return String
127 */
128 private function buildMemcachedServerList() {
129 $servers = $this->values['_MemCachedServers'];
130
131 if( !$servers ) {
132 return 'array()';
133 } else {
134 $ret = 'array( ';
135 $servers = explode( ',', $servers );
136
137 foreach( $servers as $srv ) {
138 $srv = trim( $srv );
139 $ret .= "'$srv', ";
140 }
141
142 return rtrim( $ret, ', ' ) . ' )';
143 }
144 }
145
146 /**
147 * @return String
148 */
149 private function getDefaultText() {
150 if( !$this->values['wgImageMagickConvertCommand'] ) {
151 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
152 $magic = '#';
153 } else {
154 $magic = '';
155 }
156
157 if( !$this->values['wgShellLocale'] ) {
158 $this->values['wgShellLocale'] = 'en_US.UTF-8';
159 $locale = '#';
160 } else {
161 $locale = '';
162 }
163
164 $rights = $this->values['wgRightsUrl'] ? '' : '#';
165 $hashedUploads = $this->safeMode ? '' : '#';
166
167 switch( $this->values['wgMainCacheType'] ) {
168 case 'anything':
169 case 'db':
170 case 'memcached':
171 case 'accel':
172 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType']);
173 break;
174 case 'none':
175 default:
176 $cacheType = 'CACHE_NONE';
177 }
178
179 $mcservers = $this->buildMemcachedServerList();
180 return "<?php
181 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
182 # installer. If you make manual changes, please keep track in case you
183 # need to recreate them later.
184 #
185 # See includes/DefaultSettings.php for all configurable settings
186 # and their default values, but don't forget to make changes in _this_
187 # file, not there.
188 #
189 # Further documentation for configuration settings may be found at:
190 # http://www.mediawiki.org/wiki/Manual:Configuration_settings
191
192 # If you customize your file layout, set \$IP to the directory that contains
193 # the other MediaWiki files. It will be used as a base to locate files.
194 if( defined( 'MW_INSTALL_PATH' ) ) {
195 \$IP = MW_INSTALL_PATH;
196 } else {
197 \$IP = dirname( __FILE__ );
198 }
199
200 require_once( \"\$IP/includes/DefaultSettings.php\" );
201
202 if ( \$wgCommandLineMode ) {
203 if ( isset( \$_SERVER ) && array_key_exists( 'REQUEST_METHOD', \$_SERVER ) ) {
204 die( \"This script must be run from the command line\\n\" );
205 }
206 }
207 ## Uncomment this to disable output compression
208 # \$wgDisableOutputCompression = true;
209
210 \$wgSitename = \"{$this->values['wgSitename']}\";
211
212 ## The URL base path to the directory containing the wiki;
213 ## defaults for all runtime URL paths are based off of this.
214 ## For more information on customizing the URLs please see:
215 ## http://www.mediawiki.org/wiki/Manual:Short_URL
216 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
217 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
218
219 ## The relative URL path to the skins directory
220 \$wgStylePath = \"\$wgScriptPath/skins\";
221
222 ## The relative URL path to the logo. Make sure you change this from the default,
223 ## or else you'll overwrite your logo when you upgrade!
224 \$wgLogo = \"\$wgStylePath/common/images/wiki.png\";
225
226 ## UPO means: this is also a user preference option
227
228 \$wgEnableEmail = {$this->values['wgEnableEmail']};
229 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
230
231 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
232 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
233
234 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
235 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
236 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
237
238 ## Database settings
239 \$wgDBtype = \"{$this->values['wgDBtype']}\";
240 \$wgDBserver = \"{$this->values['wgDBserver']}\";
241 \$wgDBname = \"{$this->values['wgDBname']}\";
242 \$wgDBuser = \"{$this->values['wgDBuser']}\";
243 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
244
245 {$this->dbSettings}
246
247 ## Shared memory settings
248 \$wgMainCacheType = $cacheType;
249 \$wgMemCachedServers = $mcservers;
250
251 ## To enable image uploads, make sure the 'images' directory
252 ## is writable, then set this to true:
253 \$wgEnableUploads = {$this->values['wgEnableUploads']};
254 {$magic}\$wgUseImageMagick = true;
255 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
256
257 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
258 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
259
260 ## If you use ImageMagick (or any other shell command) on a
261 ## Linux server, this will need to be set to the name of an
262 ## available UTF-8 locale
263 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
264
265 ## If you want to use image uploads under safe mode,
266 ## create the directories images/archive, images/thumb and
267 ## images/temp, and make them all writable. Then uncomment
268 ## this, if it's not already uncommented:
269 {$hashedUploads}\$wgHashedUploadDirectory = false;
270
271 ## If you have the appropriate support software installed
272 ## you can enable inline LaTeX equations:
273 \$wgUseTeX = false;
274
275 ## Set \$wgCacheDirectory to a writable directory on the web server
276 ## to make your wiki go slightly faster. The directory should not
277 ## be publically accessible from the web.
278 #\$wgCacheDirectory = \"\$IP/cache\";
279
280 \$wgLocalInterwiki = strtolower( \$wgSitename );
281
282 # Site language code, should be one of ./languages/Language(.*).php
283 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
284
285 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
286
287 ## Default skin: you can change the default skin. Use the internal symbolic
288 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
289 \$wgDefaultSkin = 'vector';
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 # When you make changes to this configuration file, this will make
305 # sure that cached pages are cleared.
306 \$wgCacheEpoch = max( \$wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
307 ";
308 }
309
310 }