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