MediaWiki  REL1_24
LocalSettingsGenerator.php
Go to the documentation of this file.
00001 <?php
00030 class LocalSettingsGenerator {
00031 
00032     protected $extensions = array();
00033     protected $values = array();
00034     protected $groupPermissions = array();
00035     protected $dbSettings = '';
00036     protected $safeMode = false;
00037 
00041     protected $installer;
00042 
00048     public function __construct( Installer $installer ) {
00049         $this->installer = $installer;
00050 
00051         $this->extensions = $installer->getVar( '_Extensions' );
00052         $this->skins = $installer->getVar( '_Skins' );
00053 
00054         $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
00055 
00056         $confItems = array_merge(
00057             array(
00058                 'wgServer', 'wgScriptPath', 'wgScriptExtension',
00059                 'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
00060                 'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
00061                 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
00062                 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
00063                 'wgRightsText', 'wgMainCacheType', 'wgEnableUploads',
00064                 'wgMainCacheType', '_MemCachedServers', 'wgDBserver', 'wgDBuser',
00065                 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
00066                 'wgMetaNamespace', 'wgResourceLoaderMaxQueryLength', 'wgLogo',
00067             ),
00068             $db->getGlobalNames()
00069         );
00070 
00071         $unescaped = array( 'wgRightsIcon', 'wgLogo' );
00072         $boolItems = array(
00073             'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
00074             'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
00075         );
00076 
00077         foreach ( $confItems as $c ) {
00078             $val = $installer->getVar( $c );
00079 
00080             if ( in_array( $c, $boolItems ) ) {
00081                 $val = wfBoolToStr( $val );
00082             }
00083 
00084             if ( !in_array( $c, $unescaped ) && $val !== null ) {
00085                 $val = self::escapePhpString( $val );
00086             }
00087 
00088             $this->values[$c] = $val;
00089         }
00090 
00091         $this->dbSettings = $db->getLocalSettings();
00092         $this->safeMode = $installer->getVar( '_SafeMode' );
00093         $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
00094     }
00095 
00102     public function setGroupRights( $group, $rightsArr ) {
00103         $this->groupPermissions[$group] = $rightsArr;
00104     }
00105 
00113     public static function escapePhpString( $string ) {
00114         if ( is_array( $string ) || is_object( $string ) ) {
00115             return false;
00116         }
00117 
00118         return strtr(
00119             $string,
00120             array(
00121                 "\n" => "\\n",
00122                 "\r" => "\\r",
00123                 "\t" => "\\t",
00124                 "\\" => "\\\\",
00125                 "\$" => "\\\$",
00126                 "\"" => "\\\""
00127             )
00128         );
00129     }
00130 
00137     public function getText() {
00138         $localSettings = $this->getDefaultText();
00139 
00140         if ( count( $this->skins ) ) {
00141             $localSettings .= "
00142 # Enabled skins.
00143 # The following skins were automatically enabled:\n";
00144 
00145             foreach ( $this->skins as $skinName ) {
00146                 $encSkinName = self::escapePhpString( $skinName );
00147                 $localSettings .= "require_once \"\$IP/skins/$encSkinName/$encSkinName.php\";\n";
00148             }
00149 
00150             $localSettings .= "\n";
00151         }
00152 
00153         if ( count( $this->extensions ) ) {
00154             $localSettings .= "
00155 # Enabled Extensions. Most extensions are enabled by including the base extension file here
00156 # but check specific extension documentation for more details
00157 # The following extensions were automatically enabled:\n";
00158 
00159             foreach ( $this->extensions as $extName ) {
00160                 $encExtName = self::escapePhpString( $extName );
00161                 $localSettings .= "require_once \"\$IP/extensions/$encExtName/$encExtName.php\";\n";
00162             }
00163 
00164             $localSettings .= "\n";
00165         }
00166 
00167         $localSettings .= "
00168 # End of automatically generated settings.
00169 # Add more configuration options below.\n\n";
00170 
00171         return $localSettings;
00172     }
00173 
00179     public function writeFile( $fileName ) {
00180         file_put_contents( $fileName, $this->getText() );
00181     }
00182 
00186     protected function buildMemcachedServerList() {
00187         $servers = $this->values['_MemCachedServers'];
00188 
00189         if ( !$servers ) {
00190             return 'array()';
00191         } else {
00192             $ret = 'array( ';
00193             $servers = explode( ',', $servers );
00194 
00195             foreach ( $servers as $srv ) {
00196                 $srv = trim( $srv );
00197                 $ret .= "'$srv', ";
00198             }
00199 
00200             return rtrim( $ret, ', ' ) . ' )';
00201         }
00202     }
00203 
00207     protected function getDefaultText() {
00208         if ( !$this->values['wgImageMagickConvertCommand'] ) {
00209             $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
00210             $magic = '#';
00211         } else {
00212             $magic = '';
00213         }
00214 
00215         if ( !$this->values['wgShellLocale'] ) {
00216             $this->values['wgShellLocale'] = 'en_US.UTF-8';
00217             $locale = '#';
00218         } else {
00219             $locale = '';
00220         }
00221 
00222         $hashedUploads = $this->safeMode ? '' : '#';
00223         $metaNamespace = '';
00224         if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
00225             $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
00226         }
00227 
00228         $groupRights = '';
00229         $noFollow = '';
00230         if ( $this->groupPermissions ) {
00231             $groupRights .= "# The following permissions were set based on your choice in the installer\n";
00232             foreach ( $this->groupPermissions as $group => $rightArr ) {
00233                 $group = self::escapePhpString( $group );
00234                 foreach ( $rightArr as $right => $perm ) {
00235                     $right = self::escapePhpString( $right );
00236                     $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
00237                         wfBoolToStr( $perm ) . ";\n";
00238                 }
00239             }
00240             $groupRights .= "\n";
00241 
00242             if ( ( isset( $this->groupPermissions['*']['edit'] ) &&
00243                     $this->groupPermissions['*']['edit'] === false )
00244                 && ( isset( $this->groupPermissions['*']['createaccount'] ) &&
00245                     $this->groupPermissions['*']['createaccount'] === false )
00246                 && ( isset( $this->groupPermissions['*']['read'] ) &&
00247                     $this->groupPermissions['*']['read'] !== false )
00248             ) {
00249                 $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
00250                     . "# the general public and wish to apply nofollow to external links as a\n"
00251                     . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
00252                     . "# and open wikis will generally require other anti-spam measures; for more\n"
00253                     . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
00254                     . "\$wgNoFollowLinks = false;\n\n";
00255             }
00256         }
00257 
00258         $serverSetting = "";
00259         if ( array_key_exists( 'wgServer', $this->values ) && $this->values['wgServer'] !== null ) {
00260             $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
00261             $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";\n";
00262         }
00263 
00264         switch ( $this->values['wgMainCacheType'] ) {
00265             case 'anything':
00266             case 'db':
00267             case 'memcached':
00268             case 'accel':
00269                 $cacheType = 'CACHE_' . strtoupper( $this->values['wgMainCacheType'] );
00270                 break;
00271             case 'none':
00272             default:
00273                 $cacheType = 'CACHE_NONE';
00274         }
00275 
00276         $mcservers = $this->buildMemcachedServerList();
00277 
00278         return "<?php
00279 # This file was automatically generated by the MediaWiki {$GLOBALS['wgVersion']}
00280 # installer. If you make manual changes, please keep track in case you
00281 # need to recreate them later.
00282 #
00283 # See includes/DefaultSettings.php for all configurable settings
00284 # and their default values, but don't forget to make changes in _this_
00285 # file, not there.
00286 #
00287 # Further documentation for configuration settings may be found at:
00288 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
00289 
00290 # Protect against web entry
00291 if ( !defined( 'MEDIAWIKI' ) ) {
00292     exit;
00293 }
00294 
00295 ## Uncomment this to disable output compression
00296 # \$wgDisableOutputCompression = true;
00297 
00298 \$wgSitename = \"{$this->values['wgSitename']}\";
00299 {$metaNamespace}
00300 ## The URL base path to the directory containing the wiki;
00301 ## defaults for all runtime URL paths are based off of this.
00302 ## For more information on customizing the URLs
00303 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
00304 ## https://www.mediawiki.org/wiki/Manual:Short_URL
00305 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
00306 \$wgScriptExtension = \"{$this->values['wgScriptExtension']}\";
00307 ${serverSetting}
00308 ## The relative URL path to the skins directory
00309 \$wgStylePath = \"\$wgScriptPath/skins\";
00310 
00311 ## The relative URL path to the logo.  Make sure you change this from the default,
00312 ## or else you'll overwrite your logo when you upgrade!
00313 \$wgLogo = \"{$this->values['wgLogo']}\";
00314 
00315 ## UPO means: this is also a user preference option
00316 
00317 \$wgEnableEmail = {$this->values['wgEnableEmail']};
00318 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
00319 
00320 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
00321 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
00322 
00323 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
00324 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
00325 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
00326 
00327 ## Database settings
00328 \$wgDBtype = \"{$this->values['wgDBtype']}\";
00329 \$wgDBserver = \"{$this->values['wgDBserver']}\";
00330 \$wgDBname = \"{$this->values['wgDBname']}\";
00331 \$wgDBuser = \"{$this->values['wgDBuser']}\";
00332 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
00333 
00334 {$this->dbSettings}
00335 
00336 ## Shared memory settings
00337 \$wgMainCacheType = $cacheType;
00338 \$wgMemCachedServers = $mcservers;
00339 
00340 ## To enable image uploads, make sure the 'images' directory
00341 ## is writable, then set this to true:
00342 \$wgEnableUploads = {$this->values['wgEnableUploads']};
00343 {$magic}\$wgUseImageMagick = true;
00344 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
00345 
00346 # InstantCommons allows wiki to use images from http://commons.wikimedia.org
00347 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
00348 
00349 ## If you use ImageMagick (or any other shell command) on a
00350 ## Linux server, this will need to be set to the name of an
00351 ## available UTF-8 locale
00352 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
00353 
00354 ## If you want to use image uploads under safe mode,
00355 ## create the directories images/archive, images/thumb and
00356 ## images/temp, and make them all writable. Then uncomment
00357 ## this, if it's not already uncommented:
00358 {$hashedUploads}\$wgHashedUploadDirectory = false;
00359 
00360 ## Set \$wgCacheDirectory to a writable directory on the web server
00361 ## to make your wiki go slightly faster. The directory should not
00362 ## be publically accessible from the web.
00363 #\$wgCacheDirectory = \"\$IP/cache\";
00364 
00365 # Site language code, should be one of the list in ./languages/Names.php
00366 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
00367 
00368 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
00369 
00370 # Site upgrade key. Must be set to a string (default provided) to turn on the
00371 # web installer while LocalSettings.php is in place
00372 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
00373 
00374 ## For attaching licensing metadata to pages, and displaying an
00375 ## appropriate copyright notice / icon. GNU Free Documentation
00376 ## License and Creative Commons licenses are supported so far.
00377 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
00378 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
00379 \$wgRightsText = \"{$this->values['wgRightsText']}\";
00380 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
00381 
00382 # Path to the GNU diff3 utility. Used for conflict resolution.
00383 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
00384 
00385 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
00386 ## names, ie 'vector', 'monobook':
00387 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
00388 ";
00389     }
00390 }