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