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