r95006 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95005‎ | r95006 | r95007 >
Date:15:25, 19 August 2011
Author:catrope
Status:ok
Tags:
Comment:
Add a PROTO_CANONICAL mode to wfExpandUrl(), which uses $wgCanonicalServer
Modified paths:
  • /trunk/phase3/includes/Defines.php (modified) (history)
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php
@@ -5,11 +5,13 @@
66
77 class wfExpandUrl extends MediaWikiTestCase {
88 /** @dataProvider provideExpandableUrls */
9 - public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $httpsMode, $message ) {
10 - // Fake $wgServer
11 - global $wgServer;
 9+ public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $canServer, $httpsMode, $message ) {
 10+ // Fake $wgServer and $wgCanonicalServer
 11+ global $wgServer, $wgCanonicalServer;
1212 $oldServer = $wgServer;
 13+ $oldCanServer = $wgCanonicalServer;
1314 $wgServer = $server;
 15+ $wgCanonicalServer = $canServer;
1416
1517 // Fake $_SERVER['HTTPS'] if needed
1618 if ( $httpsMode ) {
@@ -20,8 +22,9 @@
2123
2224 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
2325
24 - // Restore $wgServer
 26+ // Restore $wgServer and $wgCanonicalServer
2527 $wgServer = $oldServer;
 28+ $wgCanonicalServer = $oldCanServer;
2629 }
2730
2831 /**
@@ -32,32 +35,43 @@
3336 public function provideExpandableUrls() {
3437 $modes = array( 'http', 'https' );
3538 $servers = array( 'http' => 'http://example.com', 'https' => 'https://example.com', 'protocol-relative' => '//example.com' );
36 - $defaultProtos = array( 'http' => PROTO_HTTP, 'https' => PROTO_HTTPS, 'protocol-relative' => PROTO_RELATIVE, 'current' => PROTO_CURRENT );
 39+ $defaultProtos = array( 'http' => PROTO_HTTP, 'https' => PROTO_HTTPS, 'protocol-relative' => PROTO_RELATIVE, 'current' => PROTO_CURRENT, 'canonical' => PROTO_CANONICAL );
3740
3841 $retval = array();
3942 foreach ( $modes as $mode ) {
4043 $httpsMode = $mode == 'https';
4144 foreach ( $servers as $serverDesc => $server ) {
42 - foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
43 - $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 )" );
44 - $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 )" );
45 - # Would be nice to support this, see fixme on wfExpandUrl()
46 - $retval[] = array( "wiki/FooBar", 'wiki/FooBar', $defaultProto, $server, $httpsMode, "Test non-expandable relative URLs (defaultProto: $protoDesc , wgServer: $server, current request protocol: $mode )" );
47 -
48 - // Determine expected protocol
49 - $p = $protoDesc . ':'; // default case
50 - if ( $protoDesc == 'protocol-relative' ) {
51 - $p = '';
52 - } else if ( $protoDesc == 'current' ) {
53 - $p = "$mode:";
54 - } else {
55 - $p = $protoDesc . ':';
 45+ foreach ( $modes as $canServerMode ) {
 46+ $canServer = "$canServerMode://example2.com";
 47+ foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
 48+ $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 )" );
 49+ $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 )" );
 50+ # Would be nice to support this, see fixme on wfExpandUrl()
 51+ $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 )" );
 52+
 53+ // Determine expected protocol
 54+ $p = $protoDesc . ':'; // default case
 55+ if ( $protoDesc == 'protocol-relative' ) {
 56+ $p = '';
 57+ } else if ( $protoDesc == 'current' ) {
 58+ $p = "$mode:";
 59+ } else if ( $protoDesc == 'canonical' ) {
 60+ $p = "$canServerMode:";
 61+ } else {
 62+ $p = $protoDesc . ':';
 63+ }
 64+ // Determine expected server name
 65+ if ( $protoDesc == 'canonical' ) {
 66+ $srv = $canServer;
 67+ } else if ( $serverDesc == 'protocol-relative' ) {
 68+ $srv = $p . $server;
 69+ } else {
 70+ $srv = $server;
 71+ }
 72+
 73+ $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 )" );
 74+ $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 )" );
5675 }
57 - // Determine expected server name
58 - $srv = $serverDesc == 'protocol-relative' ? $p . $server : $server;
59 -
60 - $retval[] = array( "$p//wikipedia.org", '//wikipedia.org', $defaultProto, $server, $httpsMode, "Test protocol-relative URL (defaultProto: $protoDesc, wgServer: $server, current request protocol: $mode )" );
61 - $retval[] = array( "$srv/wiki/FooBar", '/wiki/FooBar', $defaultProto, $server, $httpsMode, "Testing expanding URL beginning with / (defaultProto: $protoDesc , wgServer: $server, current request protocol: $mode )" );
6276 }
6377 }
6478 }
Index: trunk/phase3/includes/Defines.php
@@ -248,4 +248,4 @@
249249 define( 'PROTO_HTTPS', 'https://' );
250250 define( 'PROTO_RELATIVE', '//' );
251251 define( 'PROTO_CURRENT', null );
252 -
 252+define( 'PROTO_CANONICAL', 1 );
Index: trunk/phase3/includes/GlobalFunctions.php
@@ -437,6 +437,7 @@
438438 * PROTO_HTTPS: Output a URL starting with https://
439439 * PROTO_RELATIVE: Output a URL starting with // (protocol-relative URL)
440440 * PROTO_CURRENT: Output a URL starting with either http:// or https:// , depending on which protocol was used for the current incoming request
 441+ * PROTO_CANONICAL: For URLs without a domain, like /w/index.php , use $wgCanonicalServer. For protocol-relative URLs, use the protocol of $wgCanonicalServer
441442 *
442443 * @todo this won't work with current-path-relative URLs
443444 * like "subdir/foo.html", etc.
@@ -446,21 +447,34 @@
447448 * @return string Fully-qualified URL
448449 */
449450 function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) {
450 - global $wgServer;
 451+ global $wgServer, $wgCanonicalServer;
 452+ $serverUrl = $defaultProto === PROTO_CANONICAL ? $wgCanonicalServer : $wgServer;
 453+
451454 if ( $defaultProto === PROTO_CURRENT ) {
452455 $defaultProto = WebRequest::detectProtocol() . '://';
453456 }
454457
455 - // Analyze $wgServer to obtain its protocol
456 - $bits = wfParseUrl( $wgServer );
 458+ // Analyze $serverUrl to obtain its protocol
 459+ $bits = wfParseUrl( $serverUrl );
457460 $serverHasProto = $bits && $bits['scheme'] != '';
 461+
 462+ if ( $defaultProto === PROTO_CANONICAL ) {
 463+ if ( $serverHasProto ) {
 464+ $defaultProto = $bits['scheme'] . '://';
 465+ } else {
 466+ // $wgCanonicalServer doesn't have a protocol. This really isn't supposed to happen
 467+ // Fall back to HTTP in this ridiculous case
 468+ $defaultProto = PROTO_HTTP;
 469+ }
 470+ }
 471+
458472 $defaultProtoWithoutSlashes = substr( $defaultProto, 0, -2 );
459473
460474 if( substr( $url, 0, 2 ) == '//' ) {
461475 return $defaultProtoWithoutSlashes . $url;
462476 } elseif( substr( $url, 0, 1 ) == '/' ) {
463 - // If $wgServer is protocol-relative, prepend $defaultProtoWithoutSlashes, otherwise leave it alone
464 - return ( $serverHasProto ? '' : $defaultProtoWithoutSlashes ) . $wgServer . $url;
 477+ // If $serverUrl is protocol-relative, prepend $defaultProtoWithoutSlashes, otherwise leave it alone
 478+ return ( $serverHasProto ? '' : $defaultProtoWithoutSlashes ) . $serverUrl . $url;
465479 } else {
466480 return $url;
467481 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r955051.17wmf1: Merge a truckload of HTTPS / prot rel URL fixes: r93847, r94990, r9...catrope19:32, 25 August 2011
r964751.18: MFT r94737, r94990, r95000, r95001, r95002, r95006, r95007, r95010, r95...catrope19:37, 7 September 2011

Status & tagging log