http://www.mediawiki.org/wiki/Special:Code/MediaWiki/95006
Revision: 95006
Author: catrope
Date: 2011-08-19 15:25:50 +0000 (Fri, 19 Aug 2011)
Log Message:
-----------
Add a PROTO_CANONICAL mode to wfExpandUrl(), which uses $wgCanonicalServer
Modified Paths:
--------------
trunk/phase3/includes/Defines.php
trunk/phase3/includes/GlobalFunctions.php
trunk/phase3/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php
Modified: trunk/phase3/includes/Defines.php
===================================================================
--- trunk/phase3/includes/Defines.php 2011-08-19 15:03:16 UTC (rev 95005)
+++ trunk/phase3/includes/Defines.php 2011-08-19 15:25:50 UTC (rev 95006)
@@ -248,4 +248,4 @@
define( 'PROTO_HTTPS', 'https://' );
define( 'PROTO_RELATIVE', '//' );
define( 'PROTO_CURRENT', null );
-
+define( 'PROTO_CANONICAL', 1 );
Modified: trunk/phase3/includes/GlobalFunctions.php
===================================================================
--- trunk/phase3/includes/GlobalFunctions.php 2011-08-19 15:03:16 UTC (rev
95005)
+++ trunk/phase3/includes/GlobalFunctions.php 2011-08-19 15:25:50 UTC (rev
95006)
@@ -437,6 +437,7 @@
* PROTO_HTTPS: Output a URL starting with https://
* PROTO_RELATIVE: Output a URL starting with // (protocol-relative URL)
* PROTO_CURRENT: Output a URL starting with either http:// or https:// ,
depending on which protocol was used for the current incoming request
+ * PROTO_CANONICAL: For URLs without a domain, like /w/index.php , use
$wgCanonicalServer. For protocol-relative URLs, use the protocol of
$wgCanonicalServer
*
* @todo this won't work with current-path-relative URLs
* like "subdir/foo.html", etc.
@@ -446,21 +447,34 @@
* @return string Fully-qualified URL
*/
function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) {
- global $wgServer;
+ global $wgServer, $wgCanonicalServer;
+ $serverUrl = $defaultProto === PROTO_CANONICAL ? $wgCanonicalServer :
$wgServer;
+
if ( $defaultProto === PROTO_CURRENT ) {
$defaultProto = WebRequest::detectProtocol() . '://';
}
- // Analyze $wgServer to obtain its protocol
- $bits = wfParseUrl( $wgServer );
+ // Analyze $serverUrl to obtain its protocol
+ $bits = wfParseUrl( $serverUrl );
$serverHasProto = $bits && $bits['scheme'] != '';
+
+ if ( $defaultProto === PROTO_CANONICAL ) {
+ if ( $serverHasProto ) {
+ $defaultProto = $bits['scheme'] . '://';
+ } else {
+ // $wgCanonicalServer doesn't have a protocol. This
really isn't supposed to happen
+ // Fall back to HTTP in this ridiculous case
+ $defaultProto = PROTO_HTTP;
+ }
+ }
+
$defaultProtoWithoutSlashes = substr( $defaultProto, 0, -2 );
if( substr( $url, 0, 2 ) == '//' ) {
return $defaultProtoWithoutSlashes . $url;
} elseif( substr( $url, 0, 1 ) == '/' ) {
- // If $wgServer is protocol-relative, prepend
$defaultProtoWithoutSlashes, otherwise leave it alone
- return ( $serverHasProto ? '' : $defaultProtoWithoutSlashes ) .
$wgServer . $url;
+ // If $serverUrl is protocol-relative, prepend
$defaultProtoWithoutSlashes, otherwise leave it alone
+ return ( $serverHasProto ? '' : $defaultProtoWithoutSlashes ) .
$serverUrl . $url;
} else {
return $url;
}
Modified:
trunk/phase3/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php
===================================================================
--- trunk/phase3/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php
2011-08-19 15:03:16 UTC (rev 95005)
+++ trunk/phase3/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php
2011-08-19 15:25:50 UTC (rev 95006)
@@ -5,11 +5,13 @@
class wfExpandUrl extends MediaWikiTestCase {
/** @dataProvider provideExpandableUrls */
- public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto,
$server, $httpsMode, $message ) {
- // Fake $wgServer
- global $wgServer;
+ public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto,
$server, $canServer, $httpsMode, $message ) {
+ // Fake $wgServer and $wgCanonicalServer
+ global $wgServer, $wgCanonicalServer;
$oldServer = $wgServer;
+ $oldCanServer = $wgCanonicalServer;
$wgServer = $server;
+ $wgCanonicalServer = $canServer;
// Fake $_SERVER['HTTPS'] if needed
if ( $httpsMode ) {
@@ -20,8 +22,9 @@
$this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl,
$defaultProto ), $message );
- // Restore $wgServer
+ // Restore $wgServer and $wgCanonicalServer
$wgServer = $oldServer;
+ $wgCanonicalServer = $oldCanServer;
}
/**
@@ -32,32 +35,43 @@
public function provideExpandableUrls() {
$modes = array( 'http', 'https' );
$servers = array( 'http' => 'http://example.com', 'https' =>
'https://example.com', 'protocol-relative' => '//example.com' );
- $defaultProtos = array( 'http' => PROTO_HTTP, 'https' =>
PROTO_HTTPS, 'protocol-relative' => PROTO_RELATIVE, 'current' => PROTO_CURRENT
);
+ $defaultProtos = array( 'http' => PROTO_HTTP, 'https' =>
PROTO_HTTPS, 'protocol-relative' => PROTO_RELATIVE, 'current' => PROTO_CURRENT,
'canonical' => PROTO_CANONICAL );
$retval = array();
foreach ( $modes as $mode ) {
$httpsMode = $mode == 'https';
foreach ( $servers as $serverDesc => $server ) {
- foreach ( $defaultProtos as $protoDesc =>
$defaultProto ) {
- $retval[] = array(
'http://example.com', 'http://example.com', $defaultProto, $server, $httpsMode,
"Testing fully qualified http URLs (no need to expand) (defaultProto:
$protoDesc , wgServer: $server, current request protocol: $mode )" );
- $retval[] = array(
'https://example.com', 'https://example.com', $defaultProto, $server,
$httpsMode, "Testing fully qualified https URLs (no need to expand)
(defaultProto: $protoDesc , wgServer: $server, current request protocol: $mode
)" );
- # Would be nice to support this, see
fixme on wfExpandUrl()
- $retval[] = array( "wiki/FooBar",
'wiki/FooBar', $defaultProto, $server, $httpsMode, "Test non-expandable
relative URLs (defaultProto: $protoDesc , wgServer: $server, current request
protocol: $mode )" );
-
- // Determine expected protocol
- $p = $protoDesc . ':'; // default case
- if ( $protoDesc == 'protocol-relative'
) {
- $p = '';
- } else if ( $protoDesc == 'current' ) {
- $p = "$mode:";
- } else {
- $p = $protoDesc . ':';
+ foreach ( $modes as $canServerMode ) {
+ $canServer =
"$canServerMode://example2.com";
+ foreach ( $defaultProtos as $protoDesc
=> $defaultProto ) {
+ $retval[] = array(
'http://example.com', 'http://example.com', $defaultProto, $server, $canServer,
$httpsMode, "Testing fully qualified http URLs (no need to expand)
(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer,
current request protocol: $mode )" );
+ $retval[] = array(
'https://example.com', 'https://example.com', $defaultProto, $server,
$canServer, $httpsMode, "Testing fully qualified https URLs (no need to expand)
(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer,
current request protocol: $mode )" );
+ # Would be nice to support
this, see fixme on wfExpandUrl()
+ $retval[] = array(
"wiki/FooBar", 'wiki/FooBar', $defaultProto, $server, $canServer, $httpsMode,
"Test non-expandable relative URLs (defaultProto: $protoDesc , wgServer:
$server, wgCanonicalServer: $canServer, current request protocol: $mode )" );
+
+ // Determine expected protocol
+ $p = $protoDesc . ':'; //
default case
+ if ( $protoDesc ==
'protocol-relative' ) {
+ $p = '';
+ } else if ( $protoDesc ==
'current' ) {
+ $p = "$mode:";
+ } else if ( $protoDesc ==
'canonical' ) {
+ $p = "$canServerMode:";
+ } else {
+ $p = $protoDesc . ':';
+ }
+ // Determine expected server
name
+ if ( $protoDesc == 'canonical'
) {
+ $srv = $canServer;
+ } else if ( $serverDesc ==
'protocol-relative' ) {
+ $srv = $p . $server;
+ } else {
+ $srv = $server;
+ }
+
+ $retval[] = array(
"$p//wikipedia.org", '//wikipedia.org', $defaultProto, $server, $canServer,
$httpsMode, "Test protocol-relative URL (defaultProto: $protoDesc, wgServer:
$server, wgCanonicalServer: $canServer, current request protocol: $mode )" );
+ $retval[] = array(
"$srv/wiki/FooBar", '/wiki/FooBar', $defaultProto, $server, $canServer,
$httpsMode, "Testing expanding URL beginning with / (defaultProto: $protoDesc ,
wgServer: $server, wgCanonicalServer: $canServer, current request protocol:
$mode )" );
}
- // Determine expected server name
- $srv = $serverDesc ==
'protocol-relative' ? $p . $server : $server;
-
- $retval[] = array( "$p//wikipedia.org",
'//wikipedia.org', $defaultProto, $server, $httpsMode, "Test protocol-relative
URL (defaultProto: $protoDesc, wgServer: $server, current request protocol:
$mode )" );
- $retval[] = array( "$srv/wiki/FooBar",
'/wiki/FooBar', $defaultProto, $server, $httpsMode, "Testing expanding URL
beginning with / (defaultProto: $protoDesc , wgServer: $server, current request
protocol: $mode )" );
}
}
}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs