From 101e3cfa2d254c1ce6b7ac7cd77da3eeafa0df8a Mon Sep 17 00:00:00 2001 From: gicode Date: Tue, 15 Nov 2011 17:38:20 +0000 Subject: [PATCH] Add wfAssembleUrl and unit tests. This is the next step towards fixing bug 32168. This function is the inverse of wfParseUrl and is useful when you need to modify part of a URL and have to put it back together. Further, with the addition of this function, there is sufficient code in core to create a proper URI class. --- RELEASE-NOTES-1.19 | 1 + includes/GlobalFunctions.php | 54 +++++++++ .../GlobalFunctions/wfAssembleUrlTest.php | 111 ++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 tests/phpunit/includes/GlobalFunctions/wfAssembleUrlTest.php diff --git a/RELEASE-NOTES-1.19 b/RELEASE-NOTES-1.19 index 1fd3603bfc..2c57f474aa 100644 --- a/RELEASE-NOTES-1.19 +++ b/RELEASE-NOTES-1.19 @@ -135,6 +135,7 @@ production. * (bug 32168) Add wfRemoveDotSegments for use in wfExpandUrl * (bug 32358) Do not display "No higher resolution available" for dimensionless files (like audio files) +* (bug 32168) Add wfAssembleUrl for use in wfExpandUrl === API changes in 1.19 === * (bug 19838) siprop=interwikimap can now use the interwiki cache. diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 42b683a20d..8e4f693766 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -487,6 +487,60 @@ function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) { } } +/** + * This function will reassemble a URL parsed with wfParseURL. This is useful + * if you need to edit part of a URL and put it back together. + * + * This is the basic structure used (brackets contain keys for $urlParts): + * [scheme][delimiter][user]:[pass]@[host]:[port][path]?[query]#[fragment] + * + * @todo Need to integrate this into wfExpandUrl (bug 32168) + * + * @param $urlParts Array URL parts, as output from wfParseUrl + * @return string URL assembled from its component parts + */ +function wfAssembleUrl( $urlParts ) { + $result = ''; + + if ( array_key_exists( 'delimiter', $urlParts ) ) { + if ( array_key_exists( 'scheme', $urlParts ) ) { + $result .= $urlParts['scheme']; + } + + $result .= $urlParts['delimiter']; + } + + if ( array_key_exists( 'host', $urlParts ) ) { + if ( array_key_exists( 'user', $urlParts ) ) { + $result .= $urlParts['user']; + if ( array_key_exists( 'pass', $urlParts ) ) { + $result .= ':' . $urlParts['pass']; + } + $result .= '@'; + } + + $result .= $urlParts['host']; + + if ( array_key_exists( 'port', $urlParts ) ) { + $result .= ':' . $urlParts['port']; + } + } + + if ( array_key_exists( 'path', $urlParts ) ) { + $result .= $urlParts['path']; + } + + if ( array_key_exists( 'query', $urlParts ) ) { + $result .= '?' . $urlParts['query']; + } + + if ( array_key_exists( 'fragment', $urlParts ) ) { + $result .= '#' . $urlParts['fragment']; + } + + return $result; +} + /** * Remove all dot-segments in the provided URL path. For example, * '/a/./b/../c/' becomes '/a/c/'. For details on the algorithm, please see diff --git a/tests/phpunit/includes/GlobalFunctions/wfAssembleUrlTest.php b/tests/phpunit/includes/GlobalFunctions/wfAssembleUrlTest.php new file mode 100644 index 0000000000..be6c99e753 --- /dev/null +++ b/tests/phpunit/includes/GlobalFunctions/wfAssembleUrlTest.php @@ -0,0 +1,111 @@ +assertEquals( + $output, + wfAssembleUrl( $parts ), + "Testing $partsDump assembles to $output" + ); + } + + /** + * Provider of URL parts for testing wfAssembleUrl() + * + * @return array + */ + public function provideURLParts() { + $schemes = array( + '' => array(), + '//' => array( + 'delimiter' => '//', + ), + 'http://' => array( + 'scheme' => 'http', + 'delimiter' => '://', + ), + ); + + $hosts = array( + '' => array(), + 'example.com' => array( + 'host' => 'example.com', + ), + 'example.com:123' => array( + 'host' => 'example.com', + 'port' => 123, + ), + 'id@example.com' => array( + 'user' => 'id', + 'host' => 'example.com', + ), + 'id@example.com:123' => array( + 'user' => 'id', + 'host' => 'example.com', + 'port' => 123, + ), + 'id:key@example.com' => array( + 'user' => 'id', + 'pass' => 'key', + 'host' => 'example.com', + ), + 'id:key@example.com:123' => array( + 'user' => 'id', + 'pass' => 'key', + 'host' => 'example.com', + 'port' => 123, + ), + ); + + $cases = array(); + foreach ( $schemes as $scheme => $schemeParts ) { + foreach ( $hosts as $host => $hostParts ) { + foreach ( array( '', '/path' ) as $path ) { + foreach ( array( '', 'query' ) as $query ) { + foreach ( array( '', 'fragment' ) as $fragment ) { + $parts = array_merge( + $schemeParts, + $hostParts + ); + $url = $scheme . + $host . + $path; + + if ( $path ) { + $parts['path'] = $path; + } + if ( $query ) { + $parts['query'] = $query; + $url .= '?' . $query; + } + if( $fragment ) { + $parts['fragment'] = $fragment; + $url .= '#' . $fragment; + } + + + $cases[] = array( + $parts, + $url, + ); + } + } + } + } + } + + $complexURL = 'http://id:key@example.org:321' . + '/over/there?name=ferret&foo=bar#nose'; + $cases[] = array( + wfParseUrl( $complexURL ), + $complexURL, + ); + + return $cases; + } +} -- 2.20.1