PHPSessionHandler: Implement SessionHandlerInterface
[mediawiki.git] / includes / installer / LocalSettingsGenerator.php
blob4f20c7003a792dae74f863b6d910a2144a96af5e
1 <?php
2 /**
3 * Generator for LocalSettings.php file.
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.
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.
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
20 * @file
21 * @ingroup Deployment
24 /**
25 * Class for generating LocalSettings.php file.
27 * @ingroup Deployment
28 * @since 1.17
30 class LocalSettingsGenerator {
32 protected $extensions = array();
33 protected $values = array();
34 protected $groupPermissions = array();
35 protected $dbSettings = '';
36 protected $safeMode = false;
37 protected $IP;
39 /**
40 * @var Installer
42 protected $installer;
44 /**
45 * Constructor.
47 * @param Installer $installer
49 public function __construct( Installer $installer ) {
50 $this->installer = $installer;
52 $this->extensions = $installer->getVar( '_Extensions' );
53 $this->skins = $installer->getVar( '_Skins' );
54 $this->IP = $installer->getVar( 'IP' );
56 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
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', '_MainCacheType', 'wgEnableUploads',
66 '_MemCachedServers', 'wgDBserver', 'wgDBuser',
67 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
68 'wgMetaNamespace', 'wgLogo', 'wgAuthenticationTokenVersion',
70 $db->getGlobalNames()
73 $unescaped = array( 'wgRightsIcon', 'wgLogo' );
74 $boolItems = array(
75 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
76 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
79 foreach ( $confItems as $c ) {
80 $val = $installer->getVar( $c );
82 if ( in_array( $c, $boolItems ) ) {
83 $val = wfBoolToStr( $val );
86 if ( !in_array( $c, $unescaped ) && $val !== null ) {
87 $val = self::escapePhpString( $val );
90 $this->values[$c] = $val;
93 $this->dbSettings = $db->getLocalSettings();
94 $this->safeMode = $installer->getVar( '_SafeMode' );
95 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
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 )
104 public function setGroupRights( $group, $rightsArr ) {
105 $this->groupPermissions[$group] = $rightsArr;
109 * Returns the escaped version of a string of php code.
111 * @param string $string
113 * @return string
115 public static function escapePhpString( $string ) {
116 if ( is_array( $string ) || is_object( $string ) ) {
117 return false;
120 return strtr(
121 $string,
122 array(
123 "\n" => "\\n",
124 "\r" => "\\r",
125 "\t" => "\\t",
126 "\\" => "\\\\",
127 "\$" => "\\\$",
128 "\"" => "\\\""
134 * Return the full text of the generated LocalSettings.php file,
135 * including the extensions and skins.
137 * @return string
139 public function getText() {
140 $localSettings = $this->getDefaultText();
142 if ( count( $this->skins ) ) {
143 $localSettings .= "
144 # Enabled skins.
145 # The following skins were automatically enabled:\n";
147 foreach ( $this->skins as $skinName ) {
148 $localSettings .= $this->generateExtEnableLine( 'skins', $skinName );
151 $localSettings .= "\n";
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";
161 foreach ( $this->extensions as $extName ) {
162 $localSettings .= $this->generateExtEnableLine( 'extensions', $extName );
165 $localSettings .= "\n";
168 $localSettings .= "
169 # End of automatically generated settings.
170 # Add more configuration options below.\n\n";
172 return $localSettings;
176 * Generate the appropriate line to enable the given extension or skin
178 * @param string $dir Either "extensions" or "skins"
179 * @param string $name Name of extension/skin
180 * @throws InvalidArgumentException
181 * @return string
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' );
194 $encName = self::escapePhpString( $name );
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";
204 * Write the generated LocalSettings to a file
206 * @param string $fileName Full path to filename to write to
208 public function writeFile( $fileName ) {
209 file_put_contents( $fileName, $this->getText() );
213 * @return string
215 protected function buildMemcachedServerList() {
216 $servers = $this->values['_MemCachedServers'];
218 if ( !$servers ) {
219 return 'array()';
220 } else {
221 $ret = 'array( ';
222 $servers = explode( ',', $servers );
224 foreach ( $servers as $srv ) {
225 $srv = trim( $srv );
226 $ret .= "'$srv', ";
229 return rtrim( $ret, ', ' ) . ' )';
234 * @return string
236 protected function getDefaultText() {
237 if ( !$this->values['wgImageMagickConvertCommand'] ) {
238 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
239 $magic = '#';
240 } else {
241 $magic = '';
244 if ( !$this->values['wgShellLocale'] ) {
245 $this->values['wgShellLocale'] = 'en_US.UTF-8';
246 $locale = '#';
247 } else {
248 $locale = '';
251 $hashedUploads = $this->safeMode ? '' : '#';
252 $metaNamespace = '';
253 if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
254 $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
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";
269 $groupRights .= "\n";
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 )
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";
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']}\";";
293 switch ( $this->values['_MainCacheType'] ) {
294 case 'anything':
295 case 'db':
296 case 'memcached':
297 case 'accel':
298 $cacheType = 'CACHE_' . strtoupper( $this->values['_MainCacheType'] );
299 break;
300 case 'none':
301 default:
302 $cacheType = 'CACHE_NONE';
305 $mcservers = $this->buildMemcachedServerList();
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.
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.
316 # Further documentation for configuration settings may be found at:
317 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
319 # Protect against web entry
320 if ( !defined( 'MEDIAWIKI' ) ) {
321 exit;
324 ## Uncomment this to disable output compression
325 # \$wgDisableOutputCompression = true;
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}
337 ## The URL path to static resources (images, scripts, etc.)
338 \$wgResourceBasePath = \$wgScriptPath;
340 ## The URL path to the logo. Make sure you change this from the default,
341 ## or else you'll overwrite your logo when you upgrade!
342 \$wgLogo = \"{$this->values['wgLogo']}\";
344 ## UPO means: this is also a user preference option
346 \$wgEnableEmail = {$this->values['wgEnableEmail']};
347 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
349 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
350 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
352 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
353 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
354 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
356 ## Database settings
357 \$wgDBtype = \"{$this->values['wgDBtype']}\";
358 \$wgDBserver = \"{$this->values['wgDBserver']}\";
359 \$wgDBname = \"{$this->values['wgDBname']}\";
360 \$wgDBuser = \"{$this->values['wgDBuser']}\";
361 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
363 {$this->dbSettings}
365 ## Shared memory settings
366 \$wgMainCacheType = $cacheType;
367 \$wgMemCachedServers = $mcservers;
369 ## To enable image uploads, make sure the 'images' directory
370 ## is writable, then set this to true:
371 \$wgEnableUploads = {$this->values['wgEnableUploads']};
372 {$magic}\$wgUseImageMagick = true;
373 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
375 # InstantCommons allows wiki to use images from https://commons.wikimedia.org
376 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
378 ## If you use ImageMagick (or any other shell command) on a
379 ## Linux server, this will need to be set to the name of an
380 ## available UTF-8 locale
381 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
383 ## If you want to use image uploads under safe mode,
384 ## create the directories images/archive, images/thumb and
385 ## images/temp, and make them all writable. Then uncomment
386 ## this, if it's not already uncommented:
387 {$hashedUploads}\$wgHashedUploadDirectory = false;
389 ## Set \$wgCacheDirectory to a writable directory on the web server
390 ## to make your wiki go slightly faster. The directory should not
391 ## be publically accessible from the web.
392 #\$wgCacheDirectory = \"\$IP/cache\";
394 # Site language code, should be one of the list in ./languages/data/Names.php
395 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
397 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
399 # Changing this will log out all existing sessions.
400 \$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\";
402 # Site upgrade key. Must be set to a string (default provided) to turn on the
403 # web installer while LocalSettings.php is in place
404 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
406 ## For attaching licensing metadata to pages, and displaying an
407 ## appropriate copyright notice / icon. GNU Free Documentation
408 ## License and Creative Commons licenses are supported so far.
409 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
410 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
411 \$wgRightsText = \"{$this->values['wgRightsText']}\";
412 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
414 # Path to the GNU diff3 utility. Used for conflict resolution.
415 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
417 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
418 ## names, ie 'vector', 'monobook':
419 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";