/** * CentralAuthSilentLoginRedirect hook handler * Points redirects from CentralAuth wiki to mobile domain if user has logged in from it * @see SpecialCentralLogin in CentralAuth extension * @param CentralAuthUser $centralUser * @param string $url to redirect to * @param array $info token information * * @return bool */ public static function onCentralAuthSilentLoginRedirect($centralUser, &$url, $info) { if (isset($info['mobileServer'])) { $mobileUrlParsed = wfParseUrl($info['mobileServer']); $urlParsed = wfParseUrl($url); $urlParsed['host'] = $mobileUrlParsed['host']; $url = wfAssembleUrl($urlParsed); } return true; }
/** * Expand a potentially local URL to a fully-qualified URL. Assumes $wgServer * is correct. * * The meaning of the PROTO_* constants is as follows: * PROTO_HTTP: Output a URL starting with http:// * 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 * PROTO_INTERNAL: Like PROTO_CANONICAL, but uses $wgInternalServer instead of $wgCanonicalServer * * @todo this won't work with current-path-relative URLs * like "subdir/foo.html", etc. * * @param string $url Either fully-qualified or a local path + query * @param string $defaultProto One of the PROTO_* constants. Determines the * protocol to use if $url or $wgServer is protocol-relative * @return string Fully-qualified URL, current-path-relative URL or false if * no valid URL can be constructed */ function wfExpandUrl($url, $defaultProto = PROTO_CURRENT) { global $wgServer, $wgCanonicalServer, $wgInternalServer, $wgRequest, $wgHttpsPort; if ($defaultProto === PROTO_CANONICAL) { $serverUrl = $wgCanonicalServer; } elseif ($defaultProto === PROTO_INTERNAL && $wgInternalServer !== false) { // Make $wgInternalServer fall back to $wgServer if not set $serverUrl = $wgInternalServer; } else { $serverUrl = $wgServer; if ($defaultProto === PROTO_CURRENT) { $defaultProto = $wgRequest->getProtocol() . '://'; } } // Analyze $serverUrl to obtain its protocol $bits = wfParseUrl($serverUrl); $serverHasProto = $bits && $bits['scheme'] != ''; if ($defaultProto === PROTO_CANONICAL || $defaultProto === PROTO_INTERNAL) { if ($serverHasProto) { $defaultProto = $bits['scheme'] . '://'; } else { // $wgCanonicalServer or $wgInternalServer 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) == '//') { $url = $defaultProtoWithoutSlashes . $url; } elseif (substr($url, 0, 1) == '/') { // If $serverUrl is protocol-relative, prepend $defaultProtoWithoutSlashes, // otherwise leave it alone. $url = ($serverHasProto ? '' : $defaultProtoWithoutSlashes) . $serverUrl . $url; } $bits = wfParseUrl($url); // ensure proper port for HTTPS arrives in URL // https://bugzilla.wikimedia.org/show_bug.cgi?id=65184 if ($defaultProto === PROTO_HTTPS && $wgHttpsPort != 443) { $bits['port'] = $wgHttpsPort; } if ($bits && isset($bits['path'])) { $bits['path'] = wfRemoveDotSegments($bits['path']); return wfAssembleUrl($bits); } elseif ($bits) { # No path to expand return $url; } elseif (substr($url, 0, 1) != '/') { # URL is a relative path return wfRemoveDotSegments($url); } # Expanded URL is not valid. return false; }
/** * @dataProvider provideURLParts */ public function testWfAssembleUrl($parts, $output) { $partsDump = print_r($parts, true); $this->assertEquals($output, wfAssembleUrl($parts), "Testing {$partsDump} assembles to {$output}"); }
/** * Expand a potentially local URL to a fully-qualified URL. Assumes $wgServer * is correct. * * The meaning of the PROTO_* constants is as follows: * PROTO_HTTP: Output a URL starting with http:// * 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 * PROTO_INTERNAL: Like PROTO_CANONICAL, but uses $wgInternalServer instead of $wgCanonicalServer * * @todo this won't work with current-path-relative URLs * like "subdir/foo.html", etc. * * @param $url String: either fully-qualified or a local path + query * @param $defaultProto Mixed: one of the PROTO_* constants. Determines the * protocol to use if $url or $wgServer is * protocol-relative * @return string Fully-qualified URL, current-path-relative URL or false if * no valid URL can be constructed */ function wfExpandUrl($url, $defaultProto = PROTO_CURRENT) { global $wgServer, $wgCanonicalServer, $wgInternalServer; $serverUrl = $wgServer; if ($defaultProto === PROTO_CANONICAL) { $serverUrl = $wgCanonicalServer; } // Make $wgInternalServer fall back to $wgServer if not set if ($defaultProto === PROTO_INTERNAL && $wgInternalServer !== false) { $serverUrl = $wgInternalServer; } if ($defaultProto === PROTO_CURRENT) { $defaultProto = WebRequest::detectProtocol() . '://'; } // Analyze $serverUrl to obtain its protocol $bits = wfParseUrl($serverUrl); $serverHasProto = $bits && $bits['scheme'] != ''; if ($defaultProto === PROTO_CANONICAL || $defaultProto === PROTO_INTERNAL) { if ($serverHasProto) { $defaultProto = $bits['scheme'] . '://'; } else { // $wgCanonicalServer or $wgInternalServer 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) == '//') { $url = $defaultProtoWithoutSlashes . $url; } elseif (substr($url, 0, 1) == '/') { // If $serverUrl is protocol-relative, prepend $defaultProtoWithoutSlashes, otherwise leave it alone $url = ($serverHasProto ? '' : $defaultProtoWithoutSlashes) . $serverUrl . $url; } $bits = wfParseUrl($url); if ($bits && isset($bits['path'])) { $bits['path'] = wfRemoveDotSegments($bits['path']); return wfAssembleUrl($bits); } elseif ($bits) { # No path to expand return $url; } elseif (substr($url, 0, 1) != '/') { # URL is a relative path return wfRemoveDotSegments($url); } # Expanded URL is not valid. return false; }
/** * Take a URL and return a copy that removes any mobile tokens * @param string $url * @return string */ public function getDesktopUrl($url) { $parsedUrl = wfParseUrl($url); $this->updateDesktopUrlHost($parsedUrl); $this->updateDesktopUrlQuery($parsedUrl); $desktopUrl = wfAssembleUrl($parsedUrl); return $desktopUrl; }
public function testUpdateMobileUrlPath() { $this->setMwGlobals(array('wgScriptPath' => '/wiki', 'wgMobileUrlTemplate' => "/mobile/%p")); $updateMobileUrlHost = self::getMethod("updateMobileUrlPath"); // check for constructing a templated URL $parsedUrl = wfParseUrl("http://en.wikipedia.org/wiki/Gustavus_Airport"); $updateMobileUrlHost->invokeArgs($this->makeContext(), array(&$parsedUrl)); $this->assertEquals("http://en.wikipedia.org/wiki/mobile/Gustavus_Airport", wfAssembleUrl($parsedUrl)); // check for maintaining an already templated URL $parsedUrl = wfParseUrl("http://en.wikipedia.org/wiki/mobile/Gustavus_Airport"); $updateMobileUrlHost->invokeArgs($this->makeContext(), array(&$parsedUrl)); $this->assertEquals("http://en.wikipedia.org/wiki/mobile/Gustavus_Airport", wfAssembleUrl($parsedUrl)); }