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