Follow up r63404. If we are in safe mode, we do not want hashed uploads.
[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 require_once( \"\$IP/includes/DefaultSettings.php\" );
195
196 if ( \$wgCommandLineMode ) {
197 if ( isset( \$_SERVER ) && array_key_exists( 'REQUEST_METHOD', \$_SERVER ) ) {
198 die( \"This script must be run from the command line\\n\" );
199 }
200 }
201 ## Uncomment this to disable output compression
202 # \$wgDisableOutputCompression = true;
203
204 \$wgSitename = \"{$this->values['wgSitename']}\";
205
206 ## The URL base path to the directory containing the wiki;
207 ## defaults for all runtime URL paths are based off of this.
208 ## For more information on customizing the URLs please see:
209 ## http://www.mediawiki.org/wiki/Manual:Short_URL
210 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
211 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
212
213 ## The relative URL path to the skins directory
214 \$wgStylePath = \"\$wgScriptPath/skins\";
215
216 ## The relative URL path to the logo. Make sure you change this from the default,
217 ## or else you'll overwrite your logo when you upgrade!
218 \$wgLogo = \"\$wgStylePath/common/images/wiki.png\";
219
220 ## UPO means: this is also a user preference option
221
222 \$wgEnableEmail = {$this->values['wgEnableEmail']};
223 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
224
225 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
226 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
227
228 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
229 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
230 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
231
232 ## Database settings
233 \$wgDBtype = \"{$this->values['wgDBtype']}\";
234 \$wgDBserver = \"{$this->values['wgDBserver']}\";
235 \$wgDBname = \"{$this->values['wgDBname']}\";
236 \$wgDBuser = \"{$this->values['wgDBuser']}\";
237 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
238
239 {$this->dbSettings}
240
241 ## Shared memory settings
242 \$wgMainCacheType = $cacheType;
243 \$wgMemCachedServers = $mcservers;
244
245 ## To enable image uploads, make sure the 'images' directory
246 ## is writable, then set this to true:
247 \$wgEnableUploads = {$this->values['wgEnableUploads']};
248 {$magic}\$wgUseImageMagick = true;
249 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
250
251 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
252 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
253
254 ## If you use ImageMagick (or any other shell command) on a
255 ## Linux server, this will need to be set to the name of an
256 ## available UTF-8 locale
257 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
258
259 ## If you want to use image uploads under safe mode,
260 ## create the directories images/archive, images/thumb and
261 ## images/temp, and make them all writable. Then uncomment
262 ## this, if it's not already uncommented:
263 {$hashedUploads}\$wgHashedUploadDirectory = false;
264
265 ## If you have the appropriate support software installed
266 ## you can enable inline LaTeX equations:
267 \$wgUseTeX = false;
268
269 ## Set \$wgCacheDirectory to a writable directory on the web server
270 ## to make your wiki go slightly faster. The directory should not
271 ## be publically accessible from the web.
272 #\$wgCacheDirectory = \"\$IP/cache\";
273
274 \$wgLocalInterwiki = strtolower( \$wgSitename );
275
276 # Site language code, should be one of ./languages/Language(.*).php
277 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
278
279 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
280
281 ## Default skin: you can change the default skin. Use the internal symbolic
282 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
283 \$wgDefaultSkin = 'vector';
284
285 ## For attaching licensing metadata to pages, and displaying an
286 ## appropriate copyright notice / icon. GNU Free Documentation
287 ## License and Creative Commons licenses are supported so far.
288 {$rights}\$wgEnableCreativeCommonsRdf = true;
289 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
290 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
291 \$wgRightsText = \"{$this->values['wgRightsText']}\";
292 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
293 # \$wgRightsCode = \"{$this->values['wgRightsCode']}\"; # Not yet used
294
295 # Path to the GNU diff3 utility. Used for conflict resolution.
296 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
297
298 # When you make changes to this configuration file, this will make
299 # sure that cached pages are cleared.
300 \$wgCacheEpoch = max( \$wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
301 ";
302 }
303
304 }