From: Daniel Friesen Date: Sun, 11 Dec 2011 18:25:23 +0000 (+0000) Subject: Update wfArrayToCGI and wfCgiToArray: X-Git-Tag: 1.31.0-rc.0~26081 X-Git-Url: http://git.cyclocoop.org/%40spipnet%40?a=commitdiff_plain;h=57c8fe18c79e82c955b6582d3eee22ebd4ec952b;p=lhc%2Fweb%2Fwiklou.git Update wfArrayToCGI and wfCgiToArray: - 'foo' => '' now outputs "&foo=" instead of the key being omitted - 'foo' => null and 'foo' => false now omit the key instead of outputting "&foo=" - Added a test to make sure that 'foo' => true outputs "&foo=1" - Fixed a php notice caused when passing a =value-less bit like "&qwerty" to wfCgiToArray by treating it like php and extracting it as 'qwerty' => '' - Updated tests --- diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 3223a55886..96ecc2c035 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -322,7 +322,7 @@ function wfUrlencode( $s ) { /** * This function takes two arrays as input, and returns a CGI-style string, e.g. * "days=7&limit=100". Options in the first array override options in the second. - * Options set to "" will not be output. + * Options set to null or false will not be output. * * @param $array1 Array ( String|Array ) * @param $array2 Array ( String|Array ) @@ -336,7 +336,7 @@ function wfArrayToCGI( $array1, $array2 = null, $prefix = '' ) { $cgi = ''; foreach ( $array1 as $key => $value ) { - if ( $value !== '' ) { + if ( !is_null($value) && $value !== false ) { if ( $cgi != '' ) { $cgi .= '&'; } @@ -369,8 +369,7 @@ function wfArrayToCGI( $array1, $array2 = null, $prefix = '' ) { * This is the logical opposite of wfArrayToCGI(): it accepts a query string as * its argument and returns the same string in array form. This allows compa- * tibility with legacy functions that accept raw query strings instead of nice - * arrays. Of course, keys and values are urldecode()d. Don't try passing in- - * valid query strings, or it will explode. + * arrays. Of course, keys and values are urldecode()d. * * @param $query String: query string * @return array Array version of input @@ -385,7 +384,13 @@ function wfCgiToArray( $query ) { if ( $bit === '' ) { continue; } - list( $key, $value ) = explode( '=', $bit ); + if ( strpos( $bit, '=' ) === false ) { + // Pieces like &qwerty become 'qwerty' => '' (at least this is what php does) + $key = $bit; + $value = ''; + } else { + list( $key, $value ) = explode( '=', $bit ); + } $key = urldecode( $key ); $value = urldecode( $value ); if ( strpos( $key, '[' ) !== false ) { diff --git a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php index 5b5ee6400c..d79307176a 100644 --- a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php +++ b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php @@ -96,9 +96,9 @@ class GlobalTest extends MediaWikiTestCase { function testArrayToCGI() { $this->assertEquals( - "baz=AT%26T&foo=bar", + "baz=AT%26T&empty=&true=1&foo=bar", wfArrayToCGI( - array( 'baz' => 'AT&T', 'ignore' => '' ), + array( 'baz' => 'AT&T', 'empty' => '', 'ignored' => null, 'ignored2' => false, 'true' => true ), array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) ); $this->assertEquals( "path%5B0%5D=wiki&path%5B1%5D=test&cfg%5Bservers%5D%5Bhttp%5D=localhost", @@ -110,8 +110,9 @@ class GlobalTest extends MediaWikiTestCase { function testCgiToArray() { $this->assertEquals( array( 'path' => array( 'wiki', 'test' ), - 'cfg' => array( 'servers' => array( 'http' => 'localhost' ) ) ), - wfCgiToArray( 'path%5B0%5D=wiki&path%5B1%5D=test&cfg%5Bservers%5D%5Bhttp%5D=localhost' ) ); + 'cfg' => array( 'servers' => array( 'http' => 'localhost' ) ), + 'qwerty' => '' ), + wfCgiToArray( 'path%5B0%5D=wiki&path%5B1%5D=test&cfg%5Bservers%5D%5Bhttp%5D=localhost&qwerty' ) ); } function testMimeTypeMatch() {