Index: trunk/phase3/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php |
— | — | @@ -5,11 +5,13 @@ |
6 | 6 | |
7 | 7 | class wfExpandUrl extends MediaWikiTestCase { |
8 | 8 | /** @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; |
12 | 12 | $oldServer = $wgServer; |
| 13 | + $oldCanServer = $wgCanonicalServer; |
13 | 14 | $wgServer = $server; |
| 15 | + $wgCanonicalServer = $canServer; |
14 | 16 | |
15 | 17 | // Fake $_SERVER['HTTPS'] if needed |
16 | 18 | if ( $httpsMode ) { |
— | — | @@ -20,8 +22,9 @@ |
21 | 23 | |
22 | 24 | $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message ); |
23 | 25 | |
24 | | - // Restore $wgServer |
| 26 | + // Restore $wgServer and $wgCanonicalServer |
25 | 27 | $wgServer = $oldServer; |
| 28 | + $wgCanonicalServer = $oldCanServer; |
26 | 29 | } |
27 | 30 | |
28 | 31 | /** |
— | — | @@ -32,32 +35,43 @@ |
33 | 36 | public function provideExpandableUrls() { |
34 | 37 | $modes = array( 'http', 'https' ); |
35 | 38 | $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 ); |
37 | 40 | |
38 | 41 | $retval = array(); |
39 | 42 | foreach ( $modes as $mode ) { |
40 | 43 | $httpsMode = $mode == 'https'; |
41 | 44 | 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 )" ); |
56 | 75 | } |
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 )" ); |
62 | 76 | } |
63 | 77 | } |
64 | 78 | } |
Index: trunk/phase3/includes/Defines.php |
— | — | @@ -248,4 +248,4 @@ |
249 | 249 | define( 'PROTO_HTTPS', 'https://' ); |
250 | 250 | define( 'PROTO_RELATIVE', '//' ); |
251 | 251 | define( 'PROTO_CURRENT', null ); |
252 | | - |
| 252 | +define( 'PROTO_CANONICAL', 1 ); |
Index: trunk/phase3/includes/GlobalFunctions.php |
— | — | @@ -437,6 +437,7 @@ |
438 | 438 | * PROTO_HTTPS: Output a URL starting with https:// |
439 | 439 | * PROTO_RELATIVE: Output a URL starting with // (protocol-relative URL) |
440 | 440 | * 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 |
441 | 442 | * |
442 | 443 | * @todo this won't work with current-path-relative URLs |
443 | 444 | * like "subdir/foo.html", etc. |
— | — | @@ -446,21 +447,34 @@ |
447 | 448 | * @return string Fully-qualified URL |
448 | 449 | */ |
449 | 450 | function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) { |
450 | | - global $wgServer; |
| 451 | + global $wgServer, $wgCanonicalServer; |
| 452 | + $serverUrl = $defaultProto === PROTO_CANONICAL ? $wgCanonicalServer : $wgServer; |
| 453 | + |
451 | 454 | if ( $defaultProto === PROTO_CURRENT ) { |
452 | 455 | $defaultProto = WebRequest::detectProtocol() . '://'; |
453 | 456 | } |
454 | 457 | |
455 | | - // Analyze $wgServer to obtain its protocol |
456 | | - $bits = wfParseUrl( $wgServer ); |
| 458 | + // Analyze $serverUrl to obtain its protocol |
| 459 | + $bits = wfParseUrl( $serverUrl ); |
457 | 460 | $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 | + |
458 | 472 | $defaultProtoWithoutSlashes = substr( $defaultProto, 0, -2 ); |
459 | 473 | |
460 | 474 | if( substr( $url, 0, 2 ) == '//' ) { |
461 | 475 | return $defaultProtoWithoutSlashes . $url; |
462 | 476 | } 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; |
465 | 479 | } else { |
466 | 480 | return $url; |
467 | 481 | } |