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