Added wfLogDBError() call to LoadBalancer::doWait()
[lhc/web/wiklou.git] / includes / installer / LocalSettingsGenerator.php
1 <?php
2 /**
3 * Generator for LocalSettings.php file.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 /**
25 * Class for generating LocalSettings.php file.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class LocalSettingsGenerator {
31
32 protected $extensions = array();
33 protected $values = array();
34 protected $groupPermissions = array();
35 protected $dbSettings = '';
36 protected $safeMode = false;
37
38 /**
39 * @var Installer
40 */
41 protected $installer;
42
43 /**
44 * Constructor.
45 *
46 * @param Installer $installer
47 */
48 public function __construct( Installer $installer ) {
49 $this->installer = $installer;
50
51 $this->extensions = $installer->getVar( '_Extensions' );
52 $this->skins = $installer->getVar( '_Skins' );
53
54 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
55
56 $confItems = array_merge(
57 array(
58 'wgServer', 'wgScriptPath', 'wgScriptExtension',
59 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
60 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
61 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
62 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
63 'wgRightsText', 'wgMainCacheType', 'wgEnableUploads',
64 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
65 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
66 'wgMetaNamespace', 'wgResourceLoaderMaxQueryLength', 'wgLogo',
67 ),
68 $db->getGlobalNames()
69 );
70
71 $unescaped = array( 'wgRightsIcon', 'wgLogo' );
72 $boolItems = array(
73 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
74 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
75 );
76
77 foreach ( $confItems as $c ) {
78 $val = $installer->getVar( $c );
79
80 if ( in_array( $c, $boolItems ) ) {
81 $val = wfBoolToStr( $val );
82 }
83
84 if ( !in_array( $c, $unescaped ) && $val !== null ) {
85 $val = self::escapePhpString( $val );
86 }
87
88 $this->values[$c] = $val;
89 }
90
91 $this->dbSettings = $db->getLocalSettings();
92 $this->safeMode = $installer->getVar( '_SafeMode' );
93 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
94 }
95
96 /**
97 * For $wgGroupPermissions, set a given ['group']['permission'] value.
98 * @param string $group Group name
99 * @param array $rightsArr An array of permissions, in the form of:
100 * array( 'right' => true, 'right2' => false )
101 */
102 public function setGroupRights( $group, $rightsArr ) {
103 $this->groupPermissions[$group] = $rightsArr;
104 }
105
106 /**
107 * Returns the escaped version of a string of php code.
108 *
109 * @param string $string
110 *
111 * @return string
112 */
113 public static function escapePhpString( $string ) {
114 if ( is_array( $string ) || is_object( $string ) ) {
115 return false;
116 }
117
118 return strtr(
119 $string,
120 array(
121 "\n" => "\\n",
122 "\r" => "\\r",
123 "\t" => "\\t",
124 "\\" => "\\\\",
125 "\$" => "\\\$",
126 "\"" => "\\\""
127 )
128 );
129 }
130
131 /**
132 * Return the full text of the generated LocalSettings.php file,
133 * including the extensions and skins.
134 *
135 * @return string
136 */
137 public function getText() {
138 $localSettings = $this->getDefaultText();
139
140 if ( count( $this->skins ) ) {
141 $localSettings .= "
142 # Enabled skins.
143 # The following skins were automatically enabled:\n";
144
145 foreach ( $this->skins as $skinName ) {
146 $encSkinName = self::escapePhpString( $skinName );
147 $localSettings .= "require_once \"\$IP/skins/$encSkinName/$encSkinName.php\";\n";
148 }
149
150 $localSettings .= "\n";
151 }
152
153 if ( count( $this->extensions ) ) {
154 $localSettings .= "
155 # Enabled Extensions. Most extensions are enabled by including the base extension file here
156 # but check specific extension documentation for more details
157 # The following extensions were automatically enabled:\n";
158
159 foreach ( $this->extensions as $extName ) {
160 $encExtName = self::escapePhpString( $extName );
161 $localSettings .= "require_once \"\$IP/extensions/$encExtName/$encExtName.php\";\n";
162 }
163
164 $localSettings .= "\n";
165 }
166
167 $localSettings .= "
168 # End of automatically generated settings.
169 # Add more configuration options below.\n\n";
170
171 return $localSettings;
172 }
173
174 /**
175 * Write the generated LocalSettings to a file
176 *
177 * @param string $fileName Full path to filename to write to
178 */
179 public function writeFile( $fileName ) {
180 file_put_contents( $fileName, $this->getText() );
181 }
182
183 /**
184 * @return string
185 */
186 protected function buildMemcachedServerList() {
187 $servers = $this->values['_MemCachedServers'];
188
189 if ( !$servers ) {
190 return 'array()';
191 } else {
192 $ret = 'array( ';
193 $servers = explode( ',', $servers );
194
195 foreach ( $servers as $srv ) {
196 $srv = trim( $srv );
197 $ret .= "'$srv', ";
198 }
199
200 return rtrim( $ret, ', ' ) . ' )';
201 }
202 }
203
204 /**
205 * @return string
206 */
207 protected function getDefaultText() {
208 if ( !$this->values['wgImageMagickConvertCommand'] ) {
209 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
210 $magic = '#';
211 } else {
212 $magic = '';
213 }
214
215 if ( !$this->values['wgShellLocale'] ) {
216 $this->values['wgShellLocale'] = 'en_US.UTF-8';
217 $locale = '#';
218 } else {
219 $locale = '';
220 }
221
222 $hashedUploads = $this->safeMode ? '' : '#';
223 $metaNamespace = '';
224 if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
225 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
226 }
227
228 $groupRights = '';
229 $noFollow = '';
230 if ( $this->groupPermissions ) {
231 $groupRights .= "# The following permissions were set based on your choice in the installer\n";
232 foreach ( $this->groupPermissions as $group => $rightArr ) {
233 $group = self::escapePhpString( $group );
234 foreach ( $rightArr as $right => $perm ) {
235 $right = self::escapePhpString( $right );
236 $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
237 wfBoolToStr( $perm ) . ";\n";
238 }
239 }
240 $groupRights .= "\n";
241
242 if ( ( isset( $this->groupPermissions['*']['edit'] ) &&
243 $this->groupPermissions['*']['edit'] === false )
244 && ( isset( $this->groupPermissions['*']['createaccount'] ) &&
245 $this->groupPermissions['*']['createaccount'] === false )
246 && ( isset( $this->groupPermissions['*']['read'] ) &&
247 $this->groupPermissions['*']['read'] !== false )
248 ) {
249 $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
250 . "# the general public and wish to apply nofollow to external links as a\n"
251 . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
252 . "# and open wikis will generally require other anti-spam measures; for more\n"
253 . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
254 . "\$wgNoFollowLinks = false;\n\n";
255 }
256 }
257
258 $serverSetting = "";
259 if ( array_key_exists( 'wgServer', $this->values ) && $this->values['wgServer'] !== null ) {
260 $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
261 $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";\n";
262 }
263
264 switch ( $this->values['wgMainCacheType'] ) {
265 case 'anything':
266 case 'db':
267 case 'memcached':
268 case 'accel':
269 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType'] );
270 break;
271 case 'none':
272 default:
273 $cacheType = 'CACHE_NONE';
274 }
275
276 $mcservers = $this->buildMemcachedServerList();
277
278 return "<?php
279 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
280 # installer. If you make manual changes, please keep track in case you
281 # need to recreate them later.
282 #
283 # See includes/DefaultSettings.php for all configurable settings
284 # and their default values, but don't forget to make changes in _this_
285 # file, not there.
286 #
287 # Further documentation for configuration settings may be found at:
288 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
289
290 # Protect against web entry
291 if ( !defined( 'MEDIAWIKI' ) ) {
292 exit;
293 }
294
295 ## Uncomment this to disable output compression
296 # \$wgDisableOutputCompression = true;
297
298 \$wgSitename = \"{$this->values['wgSitename']}\";
299 {$metaNamespace}
300 ## The URL base path to the directory containing the wiki;
301 ## defaults for all runtime URL paths are based off of this.
302 ## For more information on customizing the URLs
303 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
304 ## https://www.mediawiki.org/wiki/Manual:Short_URL
305 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
306 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
307 ${serverSetting}
308 ## The relative URL path to the skins directory
309 \$wgStylePath = \"\$wgScriptPath/skins\";
310
311 ## The relative URL path to the logo. Make sure you change this from the default,
312 ## or else you'll overwrite your logo when you upgrade!
313 \$wgLogo = \"{$this->values['wgLogo']}\";
314
315 ## UPO means: this is also a user preference option
316
317 \$wgEnableEmail = {$this->values['wgEnableEmail']};
318 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
319
320 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
321 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
322
323 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
324 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
325 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
326
327 ## Database settings
328 \$wgDBtype = \"{$this->values['wgDBtype']}\";
329 \$wgDBserver = \"{$this->values['wgDBserver']}\";
330 \$wgDBname = \"{$this->values['wgDBname']}\";
331 \$wgDBuser = \"{$this->values['wgDBuser']}\";
332 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
333
334 {$this->dbSettings}
335
336 ## Shared memory settings
337 \$wgMainCacheType = $cacheType;
338 \$wgMemCachedServers = $mcservers;
339
340 ## To enable image uploads, make sure the 'images' directory
341 ## is writable, then set this to true:
342 \$wgEnableUploads = {$this->values['wgEnableUploads']};
343 {$magic}\$wgUseImageMagick = true;
344 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
345
346 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
347 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
348
349 ## If you use ImageMagick (or any other shell command) on a
350 ## Linux server, this will need to be set to the name of an
351 ## available UTF-8 locale
352 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
353
354 ## If you want to use image uploads under safe mode,
355 ## create the directories images/archive, images/thumb and
356 ## images/temp, and make them all writable. Then uncomment
357 ## this, if it's not already uncommented:
358 {$hashedUploads}\$wgHashedUploadDirectory = false;
359
360 ## Set \$wgCacheDirectory to a writable directory on the web server
361 ## to make your wiki go slightly faster. The directory should not
362 ## be publically accessible from the web.
363 #\$wgCacheDirectory = \"\$IP/cache\";
364
365 # Site language code, should be one of the list in ./languages/Names.php
366 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
367
368 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
369
370 # Site upgrade key. Must be set to a string (default provided) to turn on the
371 # web installer while LocalSettings.php is in place
372 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
373
374 ## For attaching licensing metadata to pages, and displaying an
375 ## appropriate copyright notice / icon. GNU Free Documentation
376 ## License and Creative Commons licenses are supported so far.
377 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
378 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
379 \$wgRightsText = \"{$this->values['wgRightsText']}\";
380 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
381
382 # Path to the GNU diff3 utility. Used for conflict resolution.
383 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
384
385 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
386 ## names, ie 'vector', 'monobook':
387 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
388 ";
389 }
390 }