From 99ece2db2b4393447d6328fb4b9fd6bc9e033ac9 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Mon, 2 May 2016 19:17:48 -0700 Subject: [PATCH] Support hash fragments in wfAppendQuery() Change-Id: Icb99d5479836fea25a47451b5a758dd71f642f71 --- includes/GlobalFunctions.php | 14 ++++++++++++++ .../includes/GlobalFunctions/wfAppendQueryTest.php | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 5c42bc26cf..b5de66f67c 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -501,12 +501,26 @@ function wfAppendQuery( $url, $query ) { $query = wfArrayToCgi( $query ); } if ( $query != '' ) { + // Remove the fragment, if there is one + $fragment = false; + $hashPos = strpos( $url, '#' ); + if ( $hashPos !== false ) { + $fragment = substr( $url, $hashPos ); + $url = substr( $url, 0, $hashPos ); + } + + // Add parameter if ( false === strpos( $url, '?' ) ) { $url .= '?'; } else { $url .= '&'; } $url .= $query; + + // Put the fragment back + if ( $fragment !== false ) { + $url .= $fragment; + } } return $url; } diff --git a/tests/phpunit/includes/GlobalFunctions/wfAppendQueryTest.php b/tests/phpunit/includes/GlobalFunctions/wfAppendQueryTest.php index eb9adea646..bb71610b64 100644 --- a/tests/phpunit/includes/GlobalFunctions/wfAppendQueryTest.php +++ b/tests/phpunit/includes/GlobalFunctions/wfAppendQueryTest.php @@ -61,6 +61,18 @@ class WfAppendQueryTest extends MediaWikiTestCase { 'baz=quux&foo=baz', 'http://www.example.org/index.php?foo=bar&baz=quux&foo=baz', 'Modify query string' + ], + [ + 'http://www.example.org/index.php#baz', + 'foo=bar', + 'http://www.example.org/index.php?foo=bar#baz', + 'URL with fragment' + ], + [ + 'http://www.example.org/index.php?foo=bar#baz', + 'quux=blah', + 'http://www.example.org/index.php?foo=bar&quux=blah#baz', + 'URL with query string and fragment' ] ]; } -- 2.20.1