Merge "Use "string|false" as @return instead of "string|bool" where appropiate"
[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', '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 $localSettings .= $this->generateRequireOnceLine( 'skins', $skinName );
147 }
148
149 $localSettings .= "\n";
150 }
151
152 if ( count( $this->extensions ) ) {
153 $localSettings .= "
154 # Enabled Extensions. Most extensions are enabled by including the base extension file here
155 # but check specific extension documentation for more details
156 # The following extensions were automatically enabled:\n";
157
158 foreach ( $this->extensions as $extName ) {
159 $localSettings .= $this->generateRequireOnceLine( 'extensions', $extName );
160 }
161
162 $localSettings .= "\n";
163 }
164
165 $localSettings .= "
166 # End of automatically generated settings.
167 # Add more configuration options below.\n\n";
168
169 return $localSettings;
170 }
171
172 /**
173 * @param string $dir Either "extensions" or "skins"
174 * @param string $name Name of extension/skin
175 * @return string
176 */
177 private function generateRequireOnceLine( $dir, $name ) {
178 $encName = self::escapePhpString( $name );
179 return "require_once \"\$IP/$dir/$encName/$encName.php\";\n";
180 }
181
182 /**
183 * Write the generated LocalSettings to a file
184 *
185 * @param string $fileName Full path to filename to write to
186 */
187 public function writeFile( $fileName ) {
188 file_put_contents( $fileName, $this->getText() );
189 }
190
191 /**
192 * @return string
193 */
194 protected function buildMemcachedServerList() {
195 $servers = $this->values['_MemCachedServers'];
196
197 if ( !$servers ) {
198 return 'array()';
199 } else {
200 $ret = 'array( ';
201 $servers = explode( ',', $servers );
202
203 foreach ( $servers as $srv ) {
204 $srv = trim( $srv );
205 $ret .= "'$srv', ";
206 }
207
208 return rtrim( $ret, ', ' ) . ' )';
209 }
210 }
211
212 /**
213 * @return string
214 */
215 protected function getDefaultText() {
216 if ( !$this->values['wgImageMagickConvertCommand'] ) {
217 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
218 $magic = '#';
219 } else {
220 $magic = '';
221 }
222
223 if ( !$this->values['wgShellLocale'] ) {
224 $this->values['wgShellLocale'] = 'en_US.UTF-8';
225 $locale = '#';
226 } else {
227 $locale = '';
228 }
229
230 $hashedUploads = $this->safeMode ? '' : '#';
231 $metaNamespace = '';
232 if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
233 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
234 }
235
236 $groupRights = '';
237 $noFollow = '';
238 if ( $this->groupPermissions ) {
239 $groupRights .= "# The following permissions were set based on your choice in the installer\n";
240 foreach ( $this->groupPermissions as $group => $rightArr ) {
241 $group = self::escapePhpString( $group );
242 foreach ( $rightArr as $right => $perm ) {
243 $right = self::escapePhpString( $right );
244 $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
245 wfBoolToStr( $perm ) . ";\n";
246 }
247 }
248 $groupRights .= "\n";
249
250 if ( ( isset( $this->groupPermissions['*']['edit'] ) &&
251 $this->groupPermissions['*']['edit'] === false )
252 && ( isset( $this->groupPermissions['*']['createaccount'] ) &&
253 $this->groupPermissions['*']['createaccount'] === false )
254 && ( isset( $this->groupPermissions['*']['read'] ) &&
255 $this->groupPermissions['*']['read'] !== false )
256 ) {
257 $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
258 . "# the general public and wish to apply nofollow to external links as a\n"
259 . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
260 . "# and open wikis will generally require other anti-spam measures; for more\n"
261 . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
262 . "\$wgNoFollowLinks = false;\n\n";
263 }
264 }
265
266 $serverSetting = "";
267 if ( array_key_exists( 'wgServer', $this->values ) && $this->values['wgServer'] !== null ) {
268 $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
269 $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";\n";
270 }
271
272 switch ( $this->values['wgMainCacheType'] ) {
273 case 'anything':
274 case 'db':
275 case 'memcached':
276 case 'accel':
277 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType'] );
278 break;
279 case 'none':
280 default:
281 $cacheType = 'CACHE_NONE';
282 }
283
284 $mcservers = $this->buildMemcachedServerList();
285
286 return "<?php
287 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
288 # installer. If you make manual changes, please keep track in case you
289 # need to recreate them later.
290 #
291 # See includes/DefaultSettings.php for all configurable settings
292 # and their default values, but don't forget to make changes in _this_
293 # file, not there.
294 #
295 # Further documentation for configuration settings may be found at:
296 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
297
298 # Protect against web entry
299 if ( !defined( 'MEDIAWIKI' ) ) {
300 exit;
301 }
302
303 ## Uncomment this to disable output compression
304 # \$wgDisableOutputCompression = true;
305
306 \$wgSitename = \"{$this->values['wgSitename']}\";
307 {$metaNamespace}
308 ## The URL base path to the directory containing the wiki;
309 ## defaults for all runtime URL paths are based off of this.
310 ## For more information on customizing the URLs
311 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
312 ## https://www.mediawiki.org/wiki/Manual:Short_URL
313 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
314 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
315 ${serverSetting}
316 ## The relative URL path to the skins directory
317 \$wgStylePath = \"\$wgScriptPath/skins\";
318
319 ## The relative URL path to the logo. Make sure you change this from the default,
320 ## or else you'll overwrite your logo when you upgrade!
321 \$wgLogo = \"{$this->values['wgLogo']}\";
322
323 ## UPO means: this is also a user preference option
324
325 \$wgEnableEmail = {$this->values['wgEnableEmail']};
326 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
327
328 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
329 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
330
331 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
332 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
333 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
334
335 ## Database settings
336 \$wgDBtype = \"{$this->values['wgDBtype']}\";
337 \$wgDBserver = \"{$this->values['wgDBserver']}\";
338 \$wgDBname = \"{$this->values['wgDBname']}\";
339 \$wgDBuser = \"{$this->values['wgDBuser']}\";
340 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
341
342 {$this->dbSettings}
343
344 ## Shared memory settings
345 \$wgMainCacheType = $cacheType;
346 \$wgMemCachedServers = $mcservers;
347
348 ## To enable image uploads, make sure the 'images' directory
349 ## is writable, then set this to true:
350 \$wgEnableUploads = {$this->values['wgEnableUploads']};
351 {$magic}\$wgUseImageMagick = true;
352 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
353
354 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
355 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
356
357 ## If you use ImageMagick (or any other shell command) on a
358 ## Linux server, this will need to be set to the name of an
359 ## available UTF-8 locale
360 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
361
362 ## If you want to use image uploads under safe mode,
363 ## create the directories images/archive, images/thumb and
364 ## images/temp, and make them all writable. Then uncomment
365 ## this, if it's not already uncommented:
366 {$hashedUploads}\$wgHashedUploadDirectory = false;
367
368 ## Set \$wgCacheDirectory to a writable directory on the web server
369 ## to make your wiki go slightly faster. The directory should not
370 ## be publically accessible from the web.
371 #\$wgCacheDirectory = \"\$IP/cache\";
372
373 # Site language code, should be one of the list in ./languages/Names.php
374 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
375
376 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
377
378 # Site upgrade key. Must be set to a string (default provided) to turn on the
379 # web installer while LocalSettings.php is in place
380 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
381
382 ## For attaching licensing metadata to pages, and displaying an
383 ## appropriate copyright notice / icon. GNU Free Documentation
384 ## License and Creative Commons licenses are supported so far.
385 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
386 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
387 \$wgRightsText = \"{$this->values['wgRightsText']}\";
388 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
389
390 # Path to the GNU diff3 utility. Used for conflict resolution.
391 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
392
393 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
394 ## names, ie 'vector', 'monobook':
395 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
396 ";
397 }
398 }