/**
* 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 )
$cgi = '';
foreach ( $array1 as $key => $value ) {
- if ( $value !== '' ) {
+ if ( !is_null($value) && $value !== false ) {
if ( $cgi != '' ) {
$cgi .= '&';
}
* 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
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 ) {
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",
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() {