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