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