Support hash fragments in wfAppendQuery()
authorRoan Kattouw <roan.kattouw@gmail.com>
Tue, 3 May 2016 02:17:48 +0000 (19:17 -0700)
committerRoan Kattouw <roan.kattouw@gmail.com>
Tue, 3 May 2016 02:18:24 +0000 (19:18 -0700)
Change-Id: Icb99d5479836fea25a47451b5a758dd71f642f71

includes/GlobalFunctions.php
tests/phpunit/includes/GlobalFunctions/wfAppendQueryTest.php

index 5c42bc2..b5de66f 100644 (file)
@@ -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;
 }
index eb9adea..bb71610 100644 (file)
@@ -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'
                        ]
                ];
        }