Update wfArrayToCGI and wfCgiToArray:
authorDaniel Friesen <dantman@users.mediawiki.org>
Sun, 11 Dec 2011 18:25:23 +0000 (18:25 +0000)
committerDaniel Friesen <dantman@users.mediawiki.org>
Sun, 11 Dec 2011 18:25:23 +0000 (18:25 +0000)
- '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

includes/GlobalFunctions.php
tests/phpunit/includes/GlobalFunctions/GlobalTest.php

index 3223a55..96ecc2c 100644 (file)
@@ -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 ) {
index 5b5ee64..d793071 100644 (file)
@@ -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() {