Fixes for r90105, r90193:
authorTim Starling <tstarling@users.mediawiki.org>
Thu, 16 Jun 2011 05:52:16 +0000 (05:52 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Thu, 16 Jun 2011 05:52:16 +0000 (05:52 +0000)
* Actually removed $wgProto.
* Per Aryeh's suggestions on the future of $wgServer: made $wgServer detection in DefaultSettings.php more permanent by merging it with the new code from r90105. This means that bug 14977 is properly fixed now.
* Require entry points to set up the autoloader before including DefaultSettings.php. Comments on bug 14977 indicate that at some point in the past, this may have broken something. Anything that breaks now should just be fixed, we need the autoloader. Tested the most common entry points.
* Since the detection code has moved from Installer to WebRequest, I also moved the relevant test file and updated the test. The function under test is now public static, so r90154 is superseded.

RELEASE-NOTES-1.18
includes/DefaultSettings.php
includes/WebRequest.php
includes/installer/Installer.php
tests/phpunit/includes/WebRequestTest.php [new file with mode: 0644]
tests/phpunit/includes/installer/InstallerTest.php [deleted file]

index a6f20c7..7b86b21 100644 (file)
@@ -255,6 +255,8 @@ production.
 * (bug 29263) Add LTR class to the shared CSS to be used for left-to-right text
   such as SQL queries shown in dberrortext and similar messages in RTL
   environments
+* (bug 14977) Fixed $wgServer detection in cases where an IPv6 address is used 
+  as the server name.
 
 === API changes in 1.18 ===
 * (bug 26339) Throw warning when truncating an overlarge API result.
index d255994..b41a7f5 100644 (file)
@@ -26,10 +26,9 @@ if( !defined( 'MEDIAWIKI' ) ) {
        die( 1 );
 }
 
-# Create a site configuration object. Not used for much in a default install
-if ( !defined( 'MW_COMPILED' ) ) {
-       require_once( "$IP/includes/SiteConfiguration.php" );
-}
+# Create a site configuration object. Not used for much in a default install.
+# Note: this (and other things) will break if the autoloader is not enabled. 
+# Please include includes/AutoLoader.php before including this file.
 $wgConf = new SiteConfiguration;
 /** @endcond */
 
@@ -51,36 +50,7 @@ $wgSitename         = 'MediaWiki';
  * wrong server, it will redirect incorrectly after you save a page. In that
  * case, set this variable to fix it.
  */
-$wgServer = '';
-
-/** @cond file_level_code */
-if( isset( $_SERVER['SERVER_NAME'] )
-       # KLUGE: lighttpd 1.4.28 truncates IPv6 addresses at the first colon,
-       # giving bogus hostnames like "[2001"; check for presence of both
-       # brackets to detect this.
-       && ($_SERVER['SERVER_NAME'][0] !== '[' || substr($_SERVER['SERVER_NAME'], -1) === ']')
-       ) {
-       $serverName = $_SERVER['SERVER_NAME'];
-} elseif( isset( $_SERVER['HOSTNAME'] ) ) {
-       $serverName = $_SERVER['HOSTNAME'];
-} elseif( isset( $_SERVER['SERVER_ADDR'] ) ) {
-       $serverName = $_SERVER['SERVER_ADDR'];
-} else {
-       $serverName = 'localhost';
-}
-
-$wgProto = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
-
-$wgServer = $wgProto.'://' . $serverName;
-# If the port is a non-standard one, add it to the URL
-if(    isset( $_SERVER['SERVER_PORT'] )
-       && !strpos( $serverName, ':' )
-       && (    ( $wgProto == 'http' && $_SERVER['SERVER_PORT'] != 80 )
-        || ( $wgProto == 'https' && $_SERVER['SERVER_PORT'] != 443 ) ) ) {
-
-       $wgServer .= ":" . $_SERVER['SERVER_PORT'];
-}
-/** @endcond */
+$wgServer = WebRequest::detectServer();
 
 /************************************************************************//**
  * @name   Script path settings
@@ -986,6 +956,8 @@ $wgDjvuOutputExtension = 'jpg';
  * @{
  */
 
+$serverName = substr( $wgServer, strrpos( $wgServer, '/' ) + 1 );
+
 /**
  * Site admin email address.
  */
index a7e4c7c..5a883eb 100644 (file)
@@ -124,6 +124,45 @@ class WebRequest {
                return $matches;
        }
 
+       /**
+        * Work out an appropriate URL prefix containing scheme and host, based on
+        * information detected from $_SERVER
+        */
+       public static function detectServer() {
+               if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') {
+                       $proto = 'https';
+                       $stdPort = 443;
+               } else {
+                       $proto = 'http';
+                       $stdPort = 80;
+               }
+
+               $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' );
+               $host = 'localhost';
+               $port = $stdPort;
+               foreach ( $varNames as $varName ) {
+                       if ( !isset( $_SERVER[$varName] ) ) {
+                               continue;
+                       }
+                       $parts = IP::splitHostAndPort( $_SERVER[$varName] );
+                       if ( !$parts ) {
+                               // Invalid, do not use
+                               continue;
+                       }
+                       $host = $parts[0];
+                       if ( $parts[1] === false ) {
+                               if ( isset( $_SERVER['SERVER_PORT'] ) ) {
+                                       $port = $_SERVER['SERVER_PORT'];
+                               } // else leave it as $stdPort
+                       } else {
+                               $port = $parts[1];
+                       }
+                       break;
+               }
+
+               return $proto . '://' . IP::combineHostAndPort( $host, $port, $stdPort );
+       }
+
        /**
         * Check for title, action, and/or variant data in the URL
         * and interpolate it into the GET variables.
index 1d2a938..4fd9389 100644 (file)
@@ -843,38 +843,7 @@ abstract class Installer {
         * Environment check for the server hostname.
         */
        protected function envCheckServer() {
-               if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') {
-                       $proto = 'https';
-                       $stdPort = 443;
-               } else {
-                       $proto = 'http';
-                       $stdPort = 80;
-               }
-
-               $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' );
-               $host = 'localhost';
-               $port = $stdPort;
-               foreach ( $varNames as $varName ) {
-                       if ( !isset( $_SERVER[$varName] ) ) {
-                               continue;
-                       }
-                       $parts = IP::splitHostAndPort( $_SERVER[$varName] );
-                       if ( !$parts ) {
-                               // Invalid, do not use
-                               continue;
-                       }
-                       $host = $parts[0];
-                       if ( $parts[1] === false ) {
-                               if ( isset( $_SERVER['SERVER_PORT'] ) ) {
-                                       $port = $_SERVER['SERVER_PORT'];
-                               } // else leave it as $stdPort
-                       } else {
-                               $port = $parts[1];
-                       }
-                       break;
-               }
-
-               $server = $proto . '://' . IP::combineHostAndPort( $host, $port, $stdPort );
+               $server = WebRequest::detectServer();
                $this->showMessage( 'config-using-server', $server );
                $this->setVar( 'wgServer', $server );
        }
diff --git a/tests/phpunit/includes/WebRequestTest.php b/tests/phpunit/includes/WebRequestTest.php
new file mode 100644 (file)
index 0000000..1cfbd3f
--- /dev/null
@@ -0,0 +1,88 @@
+<?php
+
+class WebRequestTest extends MediaWikiTestCase {
+       /**
+        * @dataProvider provideDetectServer
+        */
+       function testDetectServer( $expected, $input, $description ) {
+               $oldServer = $_SERVER;
+               $_SERVER = $input;
+               $result = WebRequest::detectServer();
+               $_SERVER = $oldServer;
+               $this->assertEquals( $expected, $result, $description );
+       }
+
+       function provideDetectServer() {
+               return array(
+                       array(
+                               'http://x',
+                               array(
+                                       'HTTP_HOST' => 'x'
+                               ),
+                               'Host header'
+                       ),
+                       array(
+                               'https://x',
+                               array(
+                                       'HTTP_HOST' => 'x',
+                                       'HTTPS' => 'on',
+                               ),
+                               'Host header with secure'
+                       ),
+                       array(
+                               'http://x',
+                               array(
+                                       'HTTP_HOST' => 'x',
+                                       'SERVER_PORT' => 80,
+                               ),
+                               'Default SERVER_PORT',
+                       ),
+                       array(
+                               'http://x',
+                               array(
+                                       'HTTP_HOST' => 'x',
+                                       'HTTPS' => 'off',
+                               ),
+                               'Secure off'
+                       ),
+                       array(
+                               'http://y',
+                               array(
+                                       'SERVER_NAME' => 'y',
+                               ),
+                               'Server name'
+                       ),
+                       array(
+                               'http://x',
+                               array(
+                                       'HTTP_HOST' => 'x',
+                                       'SERVER_NAME' => 'y',
+                               ),
+                               'Host server name precedence'
+                       ),
+                       array(
+                               'http://[::1]:81',
+                               array(
+                                       'HTTP_HOST' => '[::1]',
+                                       'SERVER_NAME' => '::1',
+                                       'SERVER_PORT' => '81',
+                               ),
+                               'Apache bug 26005'
+                       ),
+                       array(
+                               'http://localhost',
+                               array(
+                                       'SERVER_NAME' => '[2001'
+                               ),
+                               'Kind of like lighttpd per commit message in MW r83847',
+                       ),
+                       array(
+                               'http://[2a01:e35:2eb4:1::2]:777',
+                               array(
+                                       'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
+                               ),
+                               'Possible lighttpd environment per bug 14977 comment 13',
+                       ),
+               );
+       }
+}
diff --git a/tests/phpunit/includes/installer/InstallerTest.php b/tests/phpunit/includes/installer/InstallerTest.php
deleted file mode 100644 (file)
index bcba752..0000000
+++ /dev/null
@@ -1,106 +0,0 @@
-<?php
-
-class Installer_TestHelper extends Installer {
-       function showMessage( $msg ) {}
-       function showError( $msg ) {}
-       function showStatusMessage( Status $status ) {}
-
-       function __construct() {
-               $this->settings = array();
-       }
-
-}
-
-class InstallerTest extends MediaWikiTestCase {
-       /**
-        * @dataProvider provideEnvCheckServer
-        */
-       function testEnvCheckServer( $expected, $input, $description ) {
-               $installer = new Installer_TestHelper;
-               $oldServer = $_SERVER;
-               $_SERVER = $input;
-               $rm = new ReflectionMethod( 'Installer_TestHelper', 'envCheckServer' );
-               if( !method_exists( $rm, 'setAccessible' ) ) {
-                       $this->markTestIncomplete( "Test requires PHP 5.3.2 or above for ReflectionMethod::setAccessible" );
-               } else {
-                       $rm->setAccessible( true );
-                       $rm->invoke( $installer );
-                       $_SERVER = $oldServer;
-                       $this->assertEquals( $expected, $installer->getVar( 'wgServer' ), $description );
-               }
-       }
-
-       function provideEnvCheckServer() {
-               return array(
-                       array(
-                               'http://x',
-                               array(
-                                       'HTTP_HOST' => 'x'
-                               ),
-                               'Host header'
-                       ),
-                       array(
-                               'https://x',
-                               array(
-                                       'HTTP_HOST' => 'x',
-                                       'HTTPS' => 'on',
-                               ),
-                               'Host header with secure'
-                       ),
-                       array(
-                               'http://x',
-                               array(
-                                       'HTTP_HOST' => 'x',
-                                       'SERVER_PORT' => 80,
-                               ),
-                               'Default SERVER_PORT',
-                       ),
-                       array(
-                               'http://x',
-                               array(
-                                       'HTTP_HOST' => 'x',
-                                       'HTTPS' => 'off',
-                               ),
-                               'Secure off'
-                       ),
-                       array(
-                               'http://y',
-                               array(
-                                       'SERVER_NAME' => 'y',
-                               ),
-                               'Server name'
-                       ),
-                       array(
-                               'http://x',
-                               array(
-                                       'HTTP_HOST' => 'x',
-                                       'SERVER_NAME' => 'y',
-                               ),
-                               'Host server name precedence'
-                       ),
-                       array(
-                               'http://[::1]:81',
-                               array(
-                                       'HTTP_HOST' => '[::1]',
-                                       'SERVER_NAME' => '::1',
-                                       'SERVER_PORT' => '81',
-                               ),
-                               'Apache bug 26005'
-                       ),
-                       array(
-                               'http://localhost',
-                               array(
-                                       'SERVER_NAME' => '[2001'
-                               ),
-                               'Kind of like lighttpd per commit message in MW r83847',
-                       ),
-                       array(
-                               'http://[2a01:e35:2eb4:1::2]:777',
-                               array(
-                                       'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
-                               ),
-                               'Possible lighttpd environment per bug 14977 comment 13',
-                       ),
-               );
-       }
-}