MediaWiki  REL1_19
LocalSettingsGenerator.php
Go to the documentation of this file.
00001 <?php
00015 class LocalSettingsGenerator {
00016 
00017         private $extensions = array();
00018         private $values = array();
00019         private $groupPermissions = array();
00020         private $dbSettings = '';
00021         private $safeMode = false;
00022 
00026         private $installer;
00027 
00033         public function __construct( Installer $installer ) {
00034                 $this->installer = $installer;
00035 
00036                 $this->extensions = $installer->getVar( '_Extensions' );
00037 
00038                 $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
00039 
00040                 $confItems = array_merge(
00041                         array(
00042                                 'wgServer', 'wgScriptPath', 'wgScriptExtension',
00043                                 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
00044                                 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
00045                                 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
00046                                 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
00047                                 'wgRightsText', 'wgMainCacheType', 'wgEnableUploads',
00048                                 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
00049                                 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
00050                                 'wgMetaNamespace', 'wgResourceLoaderMaxQueryLength'
00051                         ),
00052                         $db->getGlobalNames()
00053                 );
00054 
00055                 $unescaped = array( 'wgRightsIcon' );
00056                 $boolItems = array(
00057                         'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
00058                         'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
00059                 );
00060 
00061                 foreach( $confItems as $c ) {
00062                         $val = $installer->getVar( $c );
00063 
00064                         if( in_array( $c, $boolItems ) ) {
00065                                 $val = wfBoolToStr( $val );
00066                         }
00067 
00068                         if ( !in_array( $c, $unescaped ) ) {
00069                                 $val = self::escapePhpString( $val );
00070                         }
00071 
00072                         $this->values[$c] = $val;
00073                 }
00074 
00075                 $this->dbSettings = $db->getLocalSettings();
00076                 $this->safeMode = $installer->getVar( '_SafeMode' );
00077                 $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
00078         }
00079 
00086         public function setGroupRights( $group, $rightsArr ) {
00087                 $this->groupPermissions[$group] = $rightsArr;
00088         }
00089 
00097         public static function escapePhpString( $string ) {
00098                 if ( is_array( $string ) || is_object( $string ) ) {
00099                         return false;
00100                 }
00101 
00102                 return strtr(
00103                         $string,
00104                         array(
00105                                 "\n" => "\\n",
00106                                 "\r" => "\\r",
00107                                 "\t" => "\\t",
00108                                 "\\" => "\\\\",
00109                                 "\$" => "\\\$",
00110                                 "\"" => "\\\""
00111                         )
00112                 );
00113         }
00114 
00121         public function getText() {
00122                 $localSettings = $this->getDefaultText();
00123 
00124                 if( count( $this->extensions ) ) {
00125                         $localSettings .= "
00126 # Enabled Extensions. Most extensions are enabled by including the base extension file here
00127 # but check specific extension documentation for more details
00128 # The following extensions were automatically enabled:\n";
00129 
00130                         foreach( $this->extensions as $extName ) {
00131                                 $encExtName = self::escapePhpString( $extName );
00132                                 $localSettings .= "require_once( \"\$IP/extensions/$encExtName/$encExtName.php\" );\n";
00133                         }
00134                 }
00135 
00136                 $localSettings .= "\n\n# End of automatically generated settings.
00137 # Add more configuration options below.\n\n";
00138 
00139                 return $localSettings;
00140         }
00141 
00147         public function writeFile( $fileName ) {
00148                 file_put_contents( $fileName, $this->getText() );
00149         }
00150 
00154         private function buildMemcachedServerList() {
00155                 $servers = $this->values['_MemCachedServers'];
00156 
00157                 if( !$servers ) {
00158                         return 'array()';
00159                 } else {
00160                         $ret = 'array( ';
00161                         $servers = explode( ',', $servers );
00162 
00163                         foreach( $servers as $srv ) {
00164                                 $srv = trim( $srv );
00165                                 $ret .= "'$srv', ";
00166                         }
00167 
00168                         return rtrim( $ret, ', ' ) . ' )';
00169                 }
00170         }
00171 
00175         private function getDefaultText() {
00176                 if( !$this->values['wgImageMagickConvertCommand'] ) {
00177                         $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
00178                         $magic = '#';
00179                 } else {
00180                         $magic = '';
00181                 }
00182 
00183                 if( !$this->values['wgShellLocale'] ) {
00184                         $this->values['wgShellLocale'] = 'en_US.UTF-8';
00185                         $locale = '#';
00186                 } else {
00187                         $locale = '';
00188                 }
00189 
00190                 //$rightsUrl = $this->values['wgRightsUrl'] ? '' : '#'; // TODO: Fixme, I'm unused!
00191                 $hashedUploads = $this->safeMode ? '' : '#';
00192                 $metaNamespace = '';
00193                 if( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
00194                         $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
00195                 }
00196 
00197                 $groupRights = '';
00198                 if( $this->groupPermissions ) {
00199                         $groupRights .= "# The following permissions were set based on your choice in the installer\n";
00200                         foreach( $this->groupPermissions as $group => $rightArr ) {
00201                                 $group = self::escapePhpString( $group );
00202                                 foreach( $rightArr as $right => $perm ) {
00203                                         $right = self::escapePhpString( $right );
00204                                         $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
00205                                                 wfBoolToStr( $perm ) . ";\n";
00206                                 }
00207                         }
00208                 }
00209 
00210                 switch( $this->values['wgMainCacheType'] ) {
00211                         case 'anything':
00212                         case 'db':
00213                         case 'memcached':
00214                         case 'accel':
00215                                 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType']);
00216                                 break;
00217                         case 'none':
00218                         default:
00219                                 $cacheType = 'CACHE_NONE';
00220                 }
00221 
00222                 $mcservers = $this->buildMemcachedServerList();
00223                 return "<?php
00224 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
00225 # installer. If you make manual changes, please keep track in case you
00226 # need to recreate them later.
00227 #
00228 # See includes/DefaultSettings.php for all configurable settings
00229 # and their default values, but don't forget to make changes in _this_
00230 # file, not there.
00231 #
00232 # Further documentation for configuration settings may be found at:
00233 # http://www.mediawiki.org/wiki/Manual:Configuration_settings
00234 
00235 # Protect against web entry
00236 if ( !defined( 'MEDIAWIKI' ) ) {
00237         exit;
00238 }
00239 
00240 ## Uncomment this to disable output compression
00241 # \$wgDisableOutputCompression = true;
00242 
00243 \$wgSitename      = \"{$this->values['wgSitename']}\";
00244 {$metaNamespace}
00245 ## The URL base path to the directory containing the wiki;
00246 ## defaults for all runtime URL paths are based off of this.
00247 ## For more information on customizing the URLs please see:
00248 ## http://www.mediawiki.org/wiki/Manual:Short_URL
00249 \$wgScriptPath       = \"{$this->values['wgScriptPath']}\";
00250 \$wgScriptExtension  = \"{$this->values['wgScriptExtension']}\";
00251 
00252 ## The protocol and server name to use in fully-qualified URLs
00253 \$wgServer           = \"{$this->values['wgServer']}\";
00254 
00255 ## The relative URL path to the skins directory
00256 \$wgStylePath        = \"\$wgScriptPath/skins\";
00257 
00258 ## The relative URL path to the logo.  Make sure you change this from the default,
00259 ## or else you'll overwrite your logo when you upgrade!
00260 \$wgLogo             = \"\$wgStylePath/common/images/wiki.png\";
00261 
00262 ## UPO means: this is also a user preference option
00263 
00264 \$wgEnableEmail      = {$this->values['wgEnableEmail']};
00265 \$wgEnableUserEmail  = {$this->values['wgEnableUserEmail']}; # UPO
00266 
00267 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
00268 \$wgPasswordSender   = \"{$this->values['wgPasswordSender']}\";
00269 
00270 \$wgEnotifUserTalk      = {$this->values['wgEnotifUserTalk']}; # UPO
00271 \$wgEnotifWatchlist     = {$this->values['wgEnotifWatchlist']}; # UPO
00272 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
00273 
00274 ## Database settings
00275 \$wgDBtype           = \"{$this->values['wgDBtype']}\";
00276 \$wgDBserver         = \"{$this->values['wgDBserver']}\";
00277 \$wgDBname           = \"{$this->values['wgDBname']}\";
00278 \$wgDBuser           = \"{$this->values['wgDBuser']}\";
00279 \$wgDBpassword       = \"{$this->values['wgDBpassword']}\";
00280 
00281 {$this->dbSettings}
00282 
00283 ## Shared memory settings
00284 \$wgMainCacheType    = $cacheType;
00285 \$wgMemCachedServers = $mcservers;
00286 
00287 ## To enable image uploads, make sure the 'images' directory
00288 ## is writable, then set this to true:
00289 \$wgEnableUploads  = {$this->values['wgEnableUploads']};
00290 {$magic}\$wgUseImageMagick = true;
00291 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
00292 
00293 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
00294 \$wgUseInstantCommons  = {$this->values['wgUseInstantCommons']};
00295 
00296 ## If you use ImageMagick (or any other shell command) on a
00297 ## Linux server, this will need to be set to the name of an
00298 ## available UTF-8 locale
00299 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
00300 
00301 ## If you want to use image uploads under safe mode,
00302 ## create the directories images/archive, images/thumb and
00303 ## images/temp, and make them all writable. Then uncomment
00304 ## this, if it's not already uncommented:
00305 {$hashedUploads}\$wgHashedUploadDirectory = false;
00306 
00307 ## Set \$wgCacheDirectory to a writable directory on the web server
00308 ## to make your wiki go slightly faster. The directory should not
00309 ## be publically accessible from the web.
00310 #\$wgCacheDirectory = \"\$IP/cache\";
00311 
00312 # Site language code, should be one of the list in ./languages/Names.php
00313 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
00314 
00315 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
00316 
00317 # Site upgrade key. Must be set to a string (default provided) to turn on the
00318 # web installer while LocalSettings.php is in place
00319 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
00320 
00321 ## Default skin: you can change the default skin. Use the internal symbolic
00322 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
00323 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
00324 
00325 ## For attaching licensing metadata to pages, and displaying an
00326 ## appropriate copyright notice / icon. GNU Free Documentation
00327 ## License and Creative Commons licenses are supported so far.
00328 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
00329 \$wgRightsUrl  = \"{$this->values['wgRightsUrl']}\";
00330 \$wgRightsText = \"{$this->values['wgRightsText']}\";
00331 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
00332 
00333 # Path to the GNU diff3 utility. Used for conflict resolution.
00334 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
00335 
00336 # Query string length limit for ResourceLoader. You should only set this if
00337 # your web server has a query string length limit (then set it to that limit),
00338 # or if you have suhosin.get.max_value_length set in php.ini (then set it to
00339 # that value)
00340 \$wgResourceLoaderMaxQueryLength = {$this->values['wgResourceLoaderMaxQueryLength']};
00341 
00342 {$groupRights}";
00343         }
00344 
00345 }