Style and doc improvements
[lhc/web/wiklou.git] / includes / installer / LocalSettingsGenerator.php
1 <?php
2
3 class LocalSettingsGenerator {
4
5 private $extensions = array();
6 private $values = array();
7 private $configPath = '';
8 private $dbSettings = '';
9 private $safeMode = false;
10
11 /**
12 * @var Installer
13 */
14 private $installer;
15
16 /**
17 * Constructor.
18 *
19 * @param $installer Installer subclass
20 */
21 public function __construct( Installer $installer ) {
22 $this->installer = $installer;
23
24 $this->configPath = $installer->getVar( 'IP' ) . '/config';
25 $this->extensions = $installer->getVar( '_Extensions' );
26
27 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
28
29 $confItems = array_merge(
30 array(
31 'wgScriptPath', 'wgScriptExtension',
32 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
33 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
34 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
35 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
36 'wgRightsText', 'wgRightsCode', 'wgMainCacheType', 'wgEnableUploads',
37 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
38 'wgDBpassword', 'wgUseInstantCommons'
39 ),
40 $db->getGlobalNames()
41 );
42
43 $unescaped = array( 'wgRightsIcon' );
44 $boolItems = array(
45 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
46 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
47 );
48
49 foreach( $confItems as $c ) {
50 $val = $installer->getVar( $c );
51
52 if( in_array( $c, $boolItems ) ) {
53 $val = wfBoolToStr( $val );
54 }
55
56 if ( !in_array( $c, $unescaped ) ) {
57 $val = self::escapePhpString( $val );
58 }
59
60 $this->values[$c] = $val;
61 }
62
63 $this->dbSettings = $db->getLocalSettings();
64 $this->safeMode = $installer->getVar( '_SafeMode' );
65 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
66 }
67
68 /**
69 * Returns the escaped version of a string of php code.
70 *
71 * @param $string String
72 *
73 * @return String
74 */
75 public static function escapePhpString( $string ) {
76 if ( is_array( $string ) || is_object( $string ) ) {
77 return false;
78 }
79
80 return strtr(
81 $string,
82 array(
83 "\n" => "\\n",
84 "\r" => "\\r",
85 "\t" => "\\t",
86 "\\" => "\\\\",
87 "\$" => "\\\$",
88 "\"" => "\\\""
89 )
90 );
91 }
92
93 /**
94 * Return the full text of the generated LocalSettings.php file,
95 * including the extensions
96 *
97 * @return String
98 */
99 public function getText() {
100 $localSettings = $this->getDefaultText();
101
102 if( count( $this->extensions ) ) {
103 $localSettings .= "\n# The following extensions were automatically enabled:\n";
104
105 foreach( $this->extensions as $ext ) {
106 $localSettings .= "require( 'extensions/$ext/$ext.php' );\n";
107 }
108 }
109
110 return $localSettings;
111 }
112
113 /**
114 * @return String
115 */
116 private function buildMemcachedServerList() {
117 $servers = $this->values['_MemCachedServers'];
118
119 if( !$servers ) {
120 return 'array()';
121 } else {
122 $ret = 'array( ';
123 $servers = explode( ',', $servers );
124
125 foreach( $servers as $srv ) {
126 $srv = trim( $srv );
127 $ret .= "'$srv', ";
128 }
129
130 return rtrim( $ret, ', ' ) . ' )';
131 }
132 }
133
134 /**
135 * @return String
136 */
137 private function getDefaultText() {
138 if( !$this->values['wgImageMagickConvertCommand'] ) {
139 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
140 $magic = '#';
141 } else {
142 $magic = '';
143 }
144
145 if( !$this->values['wgShellLocale'] ) {
146 $this->values['wgShellLocale'] = 'en_US.UTF-8';
147 $locale = '#';
148 } else {
149 $locale = '';
150 }
151
152 $rights = $this->values['wgRightsUrl'] ? '' : '#';
153 $hashedUploads = $this->safeMode ? '#' : '';
154
155 switch( $this->values['wgMainCacheType'] ) {
156 case 'anything':
157 case 'db':
158 case 'memcached':
159 case 'accel':
160 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType']);
161 break;
162 case 'none':
163 default:
164 $cacheType = 'CACHE_NONE';
165 }
166
167 $mcservers = $this->buildMemcachedServerList();
168 return "<?php
169 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
170 # installer. If you make manual changes, please keep track in case you
171 # need to recreate them later.
172 #
173 # See includes/DefaultSettings.php for all configurable settings
174 # and their default values, but don't forget to make changes in _this_
175 # file, not there.
176 #
177 # Further documentation for configuration settings may be found at:
178 # http://www.mediawiki.org/wiki/Manual:Configuration_settings
179
180 # If you customize your file layout, set \$IP to the directory that contains
181 # the other MediaWiki files. It will be used as a base to locate files.
182 if( defined( 'MW_INSTALL_PATH' ) ) {
183 \$IP = MW_INSTALL_PATH;
184 } else {
185 \$IP = dirname( __FILE__ );
186 }
187
188 \$path = array( \$IP, \"\$IP/includes\", \"\$IP/languages\" );
189 set_include_path( implode( PATH_SEPARATOR, \$path ) . PATH_SEPARATOR . get_include_path() );
190
191 require_once( \"\$IP/includes/DefaultSettings.php\" );
192
193 if ( \$wgCommandLineMode ) {
194 if ( isset( \$_SERVER ) && array_key_exists( 'REQUEST_METHOD', \$_SERVER ) ) {
195 die( \"This script must be run from the command line\\n\" );
196 }
197 }
198 ## Uncomment this to disable output compression
199 # \$wgDisableOutputCompression = true;
200
201 \$wgSitename = \"{$this->values['wgSitename']}\";
202
203 ## The URL base path to the directory containing the wiki;
204 ## defaults for all runtime URL paths are based off of this.
205 ## For more information on customizing the URLs please see:
206 ## http://www.mediawiki.org/wiki/Manual:Short_URL
207 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
208 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
209
210 ## The relative URL path to the skins directory
211 \$wgStylePath = \"\$wgScriptPath/skins\";
212
213 ## The relative URL path to the logo. Make sure you change this from the default,
214 ## or else you'll overwrite your logo when you upgrade!
215 \$wgLogo = \"\$wgStylePath/common/images/wiki.png\";
216
217 ## UPO means: this is also a user preference option
218
219 \$wgEnableEmail = {$this->values['wgEnableEmail']};
220 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
221
222 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
223 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
224
225 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
226 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
227 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
228
229 ## Database settings
230 \$wgDBtype = \"{$this->values['wgDBtype']}\";
231 \$wgDBserver = \"{$this->values['wgDBserver']}\";
232 \$wgDBname = \"{$this->values['wgDBname']}\";
233 \$wgDBuser = \"{$this->values['wgDBuser']}\";
234 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
235
236 {$this->dbSettings}
237
238 ## Shared memory settings
239 \$wgMainCacheType = $cacheType;
240 \$wgMemCachedServers = $mcservers;
241
242 ## To enable image uploads, make sure the 'images' directory
243 ## is writable, then set this to true:
244 \$wgEnableUploads = {$this->values['wgEnableUploads']};
245 {$magic}\$wgUseImageMagick = true;
246 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
247
248 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
249 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
250
251 ## If you use ImageMagick (or any other shell command) on a
252 ## Linux server, this will need to be set to the name of an
253 ## available UTF-8 locale
254 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
255
256 ## If you want to use image uploads under safe mode,
257 ## create the directories images/archive, images/thumb and
258 ## images/temp, and make them all writable. Then uncomment
259 ## this, if it's not already uncommented:
260 {$hashedUploads}\$wgHashedUploadDirectory = false;
261
262 ## If you have the appropriate support software installed
263 ## you can enable inline LaTeX equations:
264 \$wgUseTeX = false;
265
266 ## Set \$wgCacheDirectory to a writable directory on the web server
267 ## to make your wiki go slightly faster. The directory should not
268 ## be publically accessible from the web.
269 #\$wgCacheDirectory = \"\$IP/cache\";
270
271 \$wgLocalInterwiki = strtolower( \$wgSitename );
272
273 # Site language code, should be one of ./languages/Language(.*).php
274 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
275
276 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
277
278 ## Default skin: you can change the default skin. Use the internal symbolic
279 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
280 \$wgDefaultSkin = 'vector';
281
282 ## For attaching licensing metadata to pages, and displaying an
283 ## appropriate copyright notice / icon. GNU Free Documentation
284 ## License and Creative Commons licenses are supported so far.
285 {$rights}\$wgEnableCreativeCommonsRdf = true;
286 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
287 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
288 \$wgRightsText = \"{$this->values['wgRightsText']}\";
289 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
290 # \$wgRightsCode = \"{$this->values['wgRightsCode']}\"; # Not yet used
291
292 # Path to the GNU diff3 utility. Used for conflict resolution.
293 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
294
295 # When you make changes to this configuration file, this will make
296 # sure that cached pages are cleared.
297 \$wgCacheEpoch = max( \$wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
298 ";
299 }
300
301 }