Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.62% |
137 / 194 |
|
12.50% |
1 / 8 |
CRAP | |
0.00% |
0 / 1 |
| LocalSettingsGenerator | |
70.62% |
137 / 194 |
|
12.50% |
1 / 8 |
106.44 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
37 / 37 |
|
100.00% |
1 / 1 |
5 | |||
| setGroupRights | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| escapePhpString | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
3.00 | |||
| getText | |
37.50% |
6 / 16 |
|
0.00% |
0 / 1 |
11.10 | |||
| generateExtEnableLine | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| writeFile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| buildMemcachedServerList | |
33.33% |
3 / 9 |
|
0.00% |
0 / 1 |
5.67 | |||
| getDefaultText | |
74.53% |
79 / 106 |
|
0.00% |
0 / 1 |
37.17 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Installer; |
| 8 | |
| 9 | use InvalidArgumentException; |
| 10 | |
| 11 | /** |
| 12 | * Generate the LocalSettings.php file. |
| 13 | * |
| 14 | * @ingroup Installer |
| 15 | * @since 1.17 |
| 16 | */ |
| 17 | class LocalSettingsGenerator { |
| 18 | |
| 19 | /** @var string[] */ |
| 20 | protected $extensions = []; |
| 21 | /** @var string[] */ |
| 22 | protected $skins = []; |
| 23 | /** @var string[] */ |
| 24 | protected $values = []; |
| 25 | /** @var bool[][] */ |
| 26 | protected $groupPermissions = []; |
| 27 | /** @var string */ |
| 28 | protected $dbSettings = ''; |
| 29 | /** @var string */ |
| 30 | protected $IP; |
| 31 | |
| 32 | /** |
| 33 | * @var Installer |
| 34 | */ |
| 35 | protected $installer; |
| 36 | |
| 37 | public function __construct( Installer $installer ) { |
| 38 | $this->installer = $installer; |
| 39 | |
| 40 | $this->extensions = $installer->getVar( '_Extensions' ); |
| 41 | $this->skins = $installer->getVar( '_Skins' ); |
| 42 | $this->IP = $installer->getVar( 'IP' ); |
| 43 | |
| 44 | $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) ); |
| 45 | |
| 46 | $confItems = [ |
| 47 | 'wgServer', 'wgScriptPath', |
| 48 | 'wgPasswordSender', 'wgImageMagickConvertCommand', |
| 49 | 'wgLanguageCode', 'wgLocaltimezone', 'wgEnableEmail', 'wgEnableUserEmail', |
| 50 | 'wgDiff3', 'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication', |
| 51 | 'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon', |
| 52 | 'wgRightsText', '_MainCacheType', 'wgEnableUploads', |
| 53 | '_MemCachedServers', 'wgDBserver', 'wgDBuser', |
| 54 | 'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin', |
| 55 | 'wgMetaNamespace', 'wgAuthenticationTokenVersion', 'wgPingback', |
| 56 | '_Logo1x', '_LogoTagline', '_LogoWordmark', '_LogoIcon', |
| 57 | '_LogoWordmarkWidth', '_LogoWordmarkHeight', |
| 58 | '_LogoTaglineWidth', '_LogoTaglineHeight', '_WithDevelopmentSettings', |
| 59 | ...$db->getGlobalNames(), |
| 60 | ]; |
| 61 | |
| 62 | // The WebInstaller form field for "Logo" contains a literal "$wgResourceBasePath", |
| 63 | // and site admins are told in the help text that they can use $wgStylePath and $wgScriptPath |
| 64 | // within their input, such treat this as raw PHP for now. |
| 65 | $unescaped = [ 'wgRightsIcon', '_Caches', |
| 66 | '_Logo1x', '_LogoWordmark', '_LogoTagline', '_LogoIcon', |
| 67 | ]; |
| 68 | $boolItems = [ |
| 69 | 'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk', |
| 70 | 'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons', |
| 71 | 'wgPingback', |
| 72 | ]; |
| 73 | |
| 74 | foreach ( $confItems as $c ) { |
| 75 | $val = $installer->getVar( $c ); |
| 76 | |
| 77 | if ( in_array( $c, $boolItems ) ) { |
| 78 | $val = wfBoolToStr( $val ); |
| 79 | } |
| 80 | |
| 81 | if ( !in_array( $c, $unescaped ) && $val !== null ) { |
| 82 | $val = self::escapePhpString( $val ); |
| 83 | } |
| 84 | |
| 85 | $this->values[$c] = $val; |
| 86 | } |
| 87 | |
| 88 | $this->dbSettings = $db->getLocalSettings(); |
| 89 | $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender']; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * For $wgGroupPermissions, set a given ['group']['permission'] value. |
| 94 | * @param string $group Group name |
| 95 | * @param array $rightsArr An array of permissions, in the form of: |
| 96 | * [ 'right' => true, 'right2' => false ] |
| 97 | */ |
| 98 | public function setGroupRights( $group, $rightsArr ) { |
| 99 | $this->groupPermissions[$group] = $rightsArr; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the escaped version of a string of php code. |
| 104 | * |
| 105 | * @param string $string |
| 106 | * |
| 107 | * @return string|false |
| 108 | */ |
| 109 | public static function escapePhpString( $string ) { |
| 110 | if ( is_array( $string ) || is_object( $string ) ) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | return strtr( |
| 115 | $string, |
| 116 | [ |
| 117 | "\n" => "\\n", |
| 118 | "\r" => "\\r", |
| 119 | "\t" => "\\t", |
| 120 | "\\" => "\\\\", |
| 121 | "\$" => "\\\$", |
| 122 | "\"" => "\\\"" |
| 123 | ] |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Return the full text of the generated LocalSettings.php file, |
| 129 | * including the extensions and skins. |
| 130 | * |
| 131 | * @return string |
| 132 | */ |
| 133 | public function getText() { |
| 134 | $localSettings = $this->getDefaultText(); |
| 135 | |
| 136 | if ( count( $this->skins ) ) { |
| 137 | $localSettings .= " |
| 138 | // Enabled skins. |
| 139 | // The following skins were automatically enabled:\n"; |
| 140 | |
| 141 | foreach ( $this->skins as $skinName ) { |
| 142 | $localSettings .= $this->generateExtEnableLine( 'skins', $skinName ); |
| 143 | } |
| 144 | |
| 145 | $localSettings .= "\n"; |
| 146 | } |
| 147 | |
| 148 | if ( count( $this->extensions ) ) { |
| 149 | $localSettings .= " |
| 150 | // Enabled extensions. Most of the extensions are enabled by adding |
| 151 | // wfLoadExtension( 'ExtensionName' ); |
| 152 | // to LocalSettings.php. Check specific extension documentation for more details. |
| 153 | // The following extensions were automatically enabled:\n"; |
| 154 | |
| 155 | foreach ( $this->extensions as $extName ) { |
| 156 | $localSettings .= $this->generateExtEnableLine( 'extensions', $extName ); |
| 157 | } |
| 158 | |
| 159 | $localSettings .= "\n"; |
| 160 | } |
| 161 | |
| 162 | $localSettings .= " |
| 163 | // End of automatically generated settings. |
| 164 | // Add more configuration options below.\n\n"; |
| 165 | |
| 166 | return $localSettings; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Generate the appropriate line to enable the given extension or skin |
| 171 | * |
| 172 | * @param string $dir Either "extensions" or "skins" |
| 173 | * @param string $name Name of extension/skin |
| 174 | * @return string |
| 175 | */ |
| 176 | private function generateExtEnableLine( $dir, $name ) { |
| 177 | if ( $dir === 'extensions' ) { |
| 178 | $jsonFile = 'extension.json'; |
| 179 | $function = 'wfLoadExtension'; |
| 180 | } elseif ( $dir === 'skins' ) { |
| 181 | $jsonFile = 'skin.json'; |
| 182 | $function = 'wfLoadSkin'; |
| 183 | } else { |
| 184 | throw new InvalidArgumentException( '$dir was not "extensions" or "skins"' ); |
| 185 | } |
| 186 | |
| 187 | $encName = self::escapePhpString( $name ); |
| 188 | |
| 189 | if ( file_exists( "{$this->IP}/$dir/$encName/$jsonFile" ) ) { |
| 190 | return "$function( '$encName' );\n"; |
| 191 | } else { |
| 192 | return "require_once \"\$IP/$dir/$encName/$encName.php\";\n"; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Write the generated LocalSettings to a file |
| 198 | * |
| 199 | * @param string $fileName Full path to filename to write to |
| 200 | */ |
| 201 | public function writeFile( $fileName ) { |
| 202 | file_put_contents( $fileName, $this->getText() ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @return string |
| 207 | */ |
| 208 | protected function buildMemcachedServerList() { |
| 209 | $servers = $this->values['_MemCachedServers']; |
| 210 | |
| 211 | if ( !$servers ) { |
| 212 | return '[]'; |
| 213 | } else { |
| 214 | $ret = '[ '; |
| 215 | $servers = explode( ',', $servers ); |
| 216 | |
| 217 | foreach ( $servers as $srv ) { |
| 218 | $srv = trim( $srv ); |
| 219 | $ret .= "'$srv', "; |
| 220 | } |
| 221 | |
| 222 | return rtrim( $ret, ', ' ) . ' ]'; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @return string |
| 228 | */ |
| 229 | protected function getDefaultText() { |
| 230 | if ( !$this->values['wgImageMagickConvertCommand'] ) { |
| 231 | $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert'; |
| 232 | $magic = '#'; |
| 233 | } else { |
| 234 | $magic = ''; |
| 235 | } |
| 236 | |
| 237 | $metaNamespace = ''; |
| 238 | if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) { |
| 239 | $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n"; |
| 240 | } |
| 241 | |
| 242 | $groupRights = ''; |
| 243 | $noFollow = ''; |
| 244 | if ( $this->groupPermissions ) { |
| 245 | $groupRights .= "# The following permissions were set based on your choice in the installer\n"; |
| 246 | foreach ( $this->groupPermissions as $group => $rightArr ) { |
| 247 | $group = self::escapePhpString( $group ); |
| 248 | foreach ( $rightArr as $right => $perm ) { |
| 249 | $right = self::escapePhpString( $right ); |
| 250 | $groupRights .= "\$wgGroupPermissions[\"$group\"][\"$right\"] = " . |
| 251 | wfBoolToStr( $perm ) . ";\n"; |
| 252 | } |
| 253 | } |
| 254 | $groupRights .= "\n"; |
| 255 | |
| 256 | if ( ( isset( $this->groupPermissions['*']['edit'] ) && |
| 257 | $this->groupPermissions['*']['edit'] === false ) |
| 258 | && ( isset( $this->groupPermissions['*']['createaccount'] ) && |
| 259 | $this->groupPermissions['*']['createaccount'] === false ) |
| 260 | && ( isset( $this->groupPermissions['*']['read'] ) && |
| 261 | $this->groupPermissions['*']['read'] !== false ) |
| 262 | ) { |
| 263 | $noFollow = "// Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n" |
| 264 | . "// the general public and wish to apply nofollow to external links as a\n" |
| 265 | . "// deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n" |
| 266 | . "// and open wikis will generally require other anti-spam measures; for more\n" |
| 267 | . "// information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n" |
| 268 | . "\$wgNoFollowLinks = false;\n\n"; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | $serverSetting = ""; |
| 273 | if ( array_key_exists( 'wgServer', $this->values ) && $this->values['wgServer'] !== null ) { |
| 274 | $serverSetting = "\n// The protocol and server name to use in fully-qualified URLs\n"; |
| 275 | $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";"; |
| 276 | } |
| 277 | |
| 278 | switch ( $this->values['_MainCacheType'] ) { |
| 279 | case 'anything': |
| 280 | case 'db': |
| 281 | case 'memcached': |
| 282 | case 'accel': |
| 283 | $cacheType = 'CACHE_' . strtoupper( $this->values['_MainCacheType'] ); |
| 284 | break; |
| 285 | case 'none': |
| 286 | default: |
| 287 | $cacheType = 'CACHE_NONE'; |
| 288 | } |
| 289 | |
| 290 | $mcservers = $this->buildMemcachedServerList(); |
| 291 | if ( file_exists( dirname( __DIR__ ) . '/PlatformSettings.php' ) ) { |
| 292 | $platformSettings = "\n// Include platform/distribution defaults"; |
| 293 | $platformSettings .= "\nrequire_once \"\$IP/includes/PlatformSettings.php\";"; |
| 294 | } else { |
| 295 | $platformSettings = ''; |
| 296 | } |
| 297 | |
| 298 | $developmentSettings = ''; |
| 299 | if ( isset( $this->values['_WithDevelopmentSettings'] ) && $this->values['_WithDevelopmentSettings'] ) { |
| 300 | $developmentSettings = "\n// Include DevelopmentSettings.php"; |
| 301 | $developmentSettings .= "\nrequire_once \"\$IP/includes/DevelopmentSettings.php\";"; |
| 302 | } |
| 303 | |
| 304 | $this->values['taglineConfig'] = $this->values['_LogoTagline'] ? "\n\t'tagline' => [ |
| 305 | \"src\" => \"{$this->values['_LogoTagline']}\", |
| 306 | \"width\" => {$this->values['_LogoTaglineWidth']}, |
| 307 | \"height\" => {$this->values['_LogoTaglineHeight']} |
| 308 | ]," : ""; |
| 309 | |
| 310 | $this->values['wordmarkConfig'] = $this->values['_LogoWordmark'] ? "\n\t'wordmark' => [ |
| 311 | \"src\" => \"{$this->values['_LogoWordmark']}\", |
| 312 | \"width\" => {$this->values['_LogoWordmarkWidth']}, |
| 313 | \"height\" => {$this->values['_LogoWordmarkHeight']}, |
| 314 | ]," : ""; |
| 315 | |
| 316 | $this->values['sidebarLogo'] = $this->values['_Logo1x'] ?: $this->values['_LogoIcon']; |
| 317 | |
| 318 | $version = MW_VERSION; |
| 319 | return "<?php |
| 320 | /** |
| 321 | * This file was automatically generated by the MediaWiki {$version} |
| 322 | * installer. If you make manual changes, please keep track in case you |
| 323 | * need to recreate them later. |
| 324 | * |
| 325 | * See includes/MainConfigSchema.php for all configurable settings |
| 326 | * and their default values, but don't forget to make changes in _this_ |
| 327 | * file, not there. |
| 328 | * |
| 329 | * Further documentation for configuration settings may be found at: |
| 330 | * https://www.mediawiki.org/wiki/Manual:Configuration_settings |
| 331 | */ |
| 332 | |
| 333 | // Protect against web entry |
| 334 | if ( !defined( 'MEDIAWIKI' ) ) { |
| 335 | exit; |
| 336 | } |
| 337 | {$developmentSettings} |
| 338 | |
| 339 | {$platformSettings} |
| 340 | |
| 341 | // Uncomment this to disable output compression |
| 342 | // \$wgDisableOutputCompression = true; |
| 343 | |
| 344 | \$wgSitename = \"{$this->values['wgSitename']}\"; |
| 345 | {$metaNamespace} |
| 346 | // The URL base path to the directory containing the wiki; |
| 347 | // defaults for all runtime URL paths are based off of this. |
| 348 | // For more information on customizing the URLs |
| 349 | // (like /w/index.php/Page_title to /wiki/Page_title) please see: |
| 350 | // https://www.mediawiki.org/wiki/Manual:Short_URL |
| 351 | \$wgScriptPath = \"{$this->values['wgScriptPath']}\"; |
| 352 | {$serverSetting} |
| 353 | |
| 354 | // The URL path to static resources (images, scripts, etc.) |
| 355 | \$wgResourceBasePath = \$wgScriptPath; |
| 356 | |
| 357 | // The URL paths to the logo. Make sure you change this from the default, |
| 358 | // or else you'll overwrite your logo when you upgrade! |
| 359 | \$wgLogos = [ |
| 360 | '1x' => \"{$this->values['sidebarLogo']}\",{$this->values['wordmarkConfig']}{$this->values['taglineConfig']} |
| 361 | 'icon' => \"{$this->values['_LogoIcon']}\", |
| 362 | ]; |
| 363 | |
| 364 | // UPO means: this is also a user preference option |
| 365 | |
| 366 | \$wgEnableEmail = {$this->values['wgEnableEmail']}; |
| 367 | \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO |
| 368 | |
| 369 | \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\"; |
| 370 | \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\"; |
| 371 | |
| 372 | \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO |
| 373 | \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO |
| 374 | \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']}; |
| 375 | |
| 376 | // Database settings |
| 377 | \$wgDBtype = \"{$this->values['wgDBtype']}\"; |
| 378 | \$wgDBserver = \"{$this->values['wgDBserver']}\"; |
| 379 | \$wgDBname = \"{$this->values['wgDBname']}\"; |
| 380 | \$wgDBuser = \"{$this->values['wgDBuser']}\"; |
| 381 | \$wgDBpassword = \"{$this->values['wgDBpassword']}\"; |
| 382 | |
| 383 | {$this->dbSettings} |
| 384 | |
| 385 | // Shared database table |
| 386 | // This has no effect unless \$wgSharedDB is also set. |
| 387 | \$wgSharedTables[] = \"actor\"; |
| 388 | |
| 389 | // Shared memory settings |
| 390 | \$wgMainCacheType = $cacheType; |
| 391 | \$wgMemCachedServers = $mcservers; |
| 392 | |
| 393 | // To enable image uploads, make sure the 'images' directory |
| 394 | // is writable, then set this to true: |
| 395 | \$wgEnableUploads = {$this->values['wgEnableUploads']}; |
| 396 | {$magic}\$wgUseImageMagick = true; |
| 397 | {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\"; |
| 398 | |
| 399 | // InstantCommons allows wiki to use images from https://commons.wikimedia.org |
| 400 | \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']}; |
| 401 | |
| 402 | // Periodically send a pingback to https://www.mediawiki.org/ with basic data |
| 403 | // about this MediaWiki instance. The Wikimedia Foundation shares this data |
| 404 | // with MediaWiki developers to help guide future development efforts. |
| 405 | \$wgPingback = {$this->values['wgPingback']}; |
| 406 | |
| 407 | // Site language code, should be one of the list in ./includes/Languages/Data/Names.php |
| 408 | \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\"; |
| 409 | |
| 410 | // Time zone |
| 411 | \$wgLocaltimezone = \"{$this->values['wgLocaltimezone']}\"; |
| 412 | |
| 413 | // Set \$wgCacheDirectory to a writable directory on the web server |
| 414 | // to make your wiki go slightly faster. The directory should not |
| 415 | // be publicly accessible from the web. |
| 416 | \$wgCacheDirectory = \"\$IP/cache\"; |
| 417 | |
| 418 | \$wgSecretKey = \"{$this->values['wgSecretKey']}\"; |
| 419 | |
| 420 | // Changing this will log out all existing sessions. |
| 421 | \$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\"; |
| 422 | |
| 423 | // Site upgrade key. Must be set to a string (default provided) to turn on the |
| 424 | // web installer while LocalSettings.php is in place |
| 425 | \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\"; |
| 426 | |
| 427 | // For attaching licensing metadata to pages, and displaying an |
| 428 | // appropriate copyright notice / icon. GNU Free Documentation |
| 429 | // License and Creative Commons licenses are supported so far. |
| 430 | \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright |
| 431 | \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\"; |
| 432 | \$wgRightsText = \"{$this->values['wgRightsText']}\"; |
| 433 | \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\"; |
| 434 | |
| 435 | // Path to the GNU diff3 utility. Used for conflict resolution. |
| 436 | \$wgDiff3 = \"{$this->values['wgDiff3']}\"; |
| 437 | |
| 438 | {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic |
| 439 | // names, e.g. 'vector' or 'monobook': |
| 440 | \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\"; |
| 441 | "; |
| 442 | } |
| 443 | } |