Doc fixes and message cleanups to r69238
[lhc/web/wiklou.git] / includes / installer / LocalSettingsGenerator.php
1 <?php
2
3 class LocalSettingsGenerator {
4 private $extensions, $values = array();
5 private $configPath, $dbSettings = '';
6 private $safeMode = false;
7 private $installer;
8
9 /**
10 * Constructor.
11 * @param $installer Installer subclass
12 */
13 public function __construct( Installer $installer ) {
14 $this->installer = $installer;
15 $this->configPath = $installer->getVar( 'IP' ) . '/config';
16 $this->extensions = $installer->getVar( '_Extensions' );
17 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
18
19 $confItems = array_merge( array( 'wgScriptPath', 'wgScriptExtension',
20 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
21 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
22 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
23 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
24 'wgRightsText', 'wgRightsCode', 'wgMainCacheType', 'wgEnableUploads',
25 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
26 'wgDBpassword', 'wgUseInstantCommons' ), $db->getGlobalNames() );
27 $unescaped = array( 'wgRightsIcon' );
28 $boolItems = array( 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
29 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons' );
30 foreach( $confItems as $c ) {
31 $val = $installer->getVar( $c );
32 if( in_array( $c, $boolItems ) ) {
33 $val = wfBoolToStr( $val );
34 }
35 if ( !in_array( $c, $unescaped ) ) {
36 $val = self::escapePhpString( $val );
37 }
38 $this->values[$c] = $val;
39 }
40 $this->dbSettings = $db->getLocalSettings();
41 $this->safeMode = $installer->getVar( '_SafeMode' );
42 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
43 }
44
45 public static function escapePhpString( $string ) {
46 if ( is_array( $string ) || is_object( $string ) ) {
47 return false;
48 }
49 return strtr( $string,
50 array(
51 "\n" => "\\n",
52 "\r" => "\\r",
53 "\t" => "\\t",
54 "\\" => "\\\\",
55 "\$" => "\\\$",
56 "\"" => "\\\""
57 ));
58 }
59
60 /**
61 * Return the full text of the generated LocalSettings.php file,
62 * including the extensions
63 * @returns String
64 */
65 public function getText() {
66 $localSettings = $this->getDefaultText();
67 if( count( $this->extensions ) ) {
68 $localSettings .= "\n# The following extensions were automatically enabled:\n";
69 foreach( $this->extensions as $ext ) {
70 $localSettings .= "require( 'extensions/$ext/$ext.php' );\n";
71 }
72 }
73
74 return $localSettings;
75 }
76
77 /**
78 * Write the file
79 * @param $secretKey String A random string to
80 * @return boolean On successful file write
81 */
82 public function writeLocalSettings() {
83 $localSettings = $this->getText();
84 wfSuppressWarnings();
85 $ret = file_put_contents( $this->configPath . '/LocalSettings.php', $localSettings );
86 wfRestoreWarnings();
87
88 $status = Status::newGood();
89
90 if ( !$ret ) {
91 $status->fatal( 'config-install-localsettings-unwritable', $localSettings );
92 }
93
94 return $status;
95 }
96
97 private function buildMemcachedServerList() {
98 $servers = $this->values['_MemCachedServers'];
99 if( !$servers ) {
100 return 'array()';
101 } else {
102 $ret = 'array( ';
103 $servers = explode( ',', $servers );
104 foreach( $servers as $srv ) {
105 $srv = trim( $srv );
106 $ret .= "'$srv', ";
107 }
108 return rtrim( $ret, ', ' ) . ' )';
109 }
110 }
111
112 private function getDefaultText() {
113 if( !$this->values['wgImageMagickConvertCommand'] ) {
114 $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
115 $magic = '#';
116 } else {
117 $magic = '';
118 }
119 if( !$this->values['wgShellLocale'] ) {
120 $this->values['wgShellLocale'] = 'en_US.UTF-8';
121 $locale = '#';
122 } else {
123 $locale = '';
124 }
125 $rights = $this->values['wgRightsUrl'] ? '' : '#';
126 $hashedUploads = $this->safeMode ? '#' : '';
127 switch( $this->values['wgMainCacheType'] ) {
128 case 'anything':
129 case 'db':
130 case 'memcached':
131 case 'accel':
132 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType']);
133 break;
134 case 'none':
135 default:
136 $cacheType = 'CACHE_NONE';
137 }
138 $mcservers = $this->buildMemcachedServerList();
139 return "<?php
140 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
141 # installer. If you make manual changes, please keep track in case you
142 # need to recreate them later.
143 #
144 # See includes/DefaultSettings.php for all configurable settings
145 # and their default values, but don't forget to make changes in _this_
146 # file, not there.
147 #
148 # Further documentation for configuration settings may be found at:
149 # http://www.mediawiki.org/wiki/Manual:Configuration_settings
150
151 # If you customize your file layout, set \$IP to the directory that contains
152 # the other MediaWiki files. It will be used as a base to locate files.
153 if( defined( 'MW_INSTALL_PATH' ) ) {
154 \$IP = MW_INSTALL_PATH;
155 } else {
156 \$IP = dirname( __FILE__ );
157 }
158
159 \$path = array( \$IP, \"\$IP/includes\", \"\$IP/languages\" );
160 set_include_path( implode( PATH_SEPARATOR, \$path ) . PATH_SEPARATOR . get_include_path() );
161
162 require_once( \"\$IP/includes/DefaultSettings.php\" );
163
164 if ( \$wgCommandLineMode ) {
165 if ( isset( \$_SERVER ) && array_key_exists( 'REQUEST_METHOD', \$_SERVER ) ) {
166 die( \"This script must be run from the command line\\n\" );
167 }
168 }
169 ## Uncomment this to disable output compression
170 # \$wgDisableOutputCompression = true;
171
172 \$wgSitename = \"{$this->values['wgSitename']}\";
173
174 ## The URL base path to the directory containing the wiki;
175 ## defaults for all runtime URL paths are based off of this.
176 ## For more information on customizing the URLs please see:
177 ## http://www.mediawiki.org/wiki/Manual:Short_URL
178 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
179 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
180
181 ## The relative URL path to the skins directory
182 \$wgStylePath = \"\$wgScriptPath/skins\";
183
184 ## The relative URL path to the logo. Make sure you change this from the default,
185 ## or else you'll overwrite your logo when you upgrade!
186 \$wgLogo = \"\$wgStylePath/common/images/wiki.png\";
187
188 ## UPO means: this is also a user preference option
189
190 \$wgEnableEmail = {$this->values['wgEnableEmail']};
191 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
192
193 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
194 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
195
196 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
197 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
198 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
199
200 ## Database settings
201 \$wgDBtype = \"{$this->values['wgDBtype']}\";
202 \$wgDBserver = \"{$this->values['wgDBserver']}\";
203 \$wgDBname = \"{$this->values['wgDBname']}\";
204 \$wgDBuser = \"{$this->values['wgDBuser']}\";
205 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
206
207 {$this->dbSettings}
208
209 ## Shared memory settings
210 \$wgMainCacheType = $cacheType;
211 \$wgMemCachedServers = $mcservers;
212
213 ## To enable image uploads, make sure the 'images' directory
214 ## is writable, then set this to true:
215 \$wgEnableUploads = {$this->values['wgEnableUploads']};
216 {$magic}\$wgUseImageMagick = true;
217 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
218
219 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
220 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
221
222 ## If you use ImageMagick (or any other shell command) on a
223 ## Linux server, this will need to be set to the name of an
224 ## available UTF-8 locale
225 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
226
227 ## If you want to use image uploads under safe mode,
228 ## create the directories images/archive, images/thumb and
229 ## images/temp, and make them all writable. Then uncomment
230 ## this, if it's not already uncommented:
231 {$hashedUploads}\$wgHashedUploadDirectory = false;
232
233 ## If you have the appropriate support software installed
234 ## you can enable inline LaTeX equations:
235 \$wgUseTeX = false;
236
237 ## Set \$wgCacheDirectory to a writable directory on the web server
238 ## to make your wiki go slightly faster. The directory should not
239 ## be publically accessible from the web.
240 #\$wgCacheDirectory = \"\$IP/cache\";
241
242 \$wgLocalInterwiki = strtolower( \$wgSitename );
243
244 # Site language code, should be one of ./languages/Language(.*).php
245 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
246
247 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
248
249 ## Default skin: you can change the default skin. Use the internal symbolic
250 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
251 \$wgDefaultSkin = 'vector';
252
253 ## For attaching licensing metadata to pages, and displaying an
254 ## appropriate copyright notice / icon. GNU Free Documentation
255 ## License and Creative Commons licenses are supported so far.
256 {$rights}\$wgEnableCreativeCommonsRdf = true;
257 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
258 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
259 \$wgRightsText = \"{$this->values['wgRightsText']}\";
260 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
261 # \$wgRightsCode = \"{$this->values['wgRightsCode']}\"; # Not yet used
262
263 # Path to the GNU diff3 utility. Used for conflict resolution.
264 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
265
266 # When you make changes to this configuration file, this will make
267 # sure that cached pages are cleared.
268 \$wgCacheEpoch = max( \$wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
269 ";
270 }
271 }