Index: trunk/phase3/RELEASE-NOTES-1.19 |
— | — | @@ -115,6 +115,8 @@ |
116 | 116 | * (bug 29507) Change 'image link' to 'file link' in Special:Whatlinkshere |
117 | 117 | * If the db is really screwed up, and doesn't have a recentchanges table, |
118 | 118 | make the updater throw an exception instead of a fatal. |
| 119 | +* wfArrayToCGI() and wfCgiToArray() now handle nested and associative arrays |
| 120 | + correctly |
119 | 121 | |
120 | 122 | === API changes in 1.19 === |
121 | 123 | * BREAKING CHANGE: action=watch now requires POST and token. |
Index: trunk/phase3/tests/phpunit/includes/GlobalTest.php |
— | — | @@ -102,8 +102,20 @@ |
103 | 103 | wfArrayToCGI( |
104 | 104 | array( 'baz' => 'AT&T', 'ignore' => '' ), |
105 | 105 | array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) ); |
| 106 | + $this->assertEquals( |
| 107 | + "path%5B0%5D=wiki&path%5B1%5D=test&cfg%5Bservers%5D%5Bhttp%5D=localhost", |
| 108 | + wfArrayToCGI( array( |
| 109 | + 'path' => array( 'wiki', 'test' ), |
| 110 | + 'cfg' => array( 'servers' => array( 'http' => 'localhost' ) ) ) ) ); |
106 | 111 | } |
107 | 112 | |
| 113 | + function testCgiToArray() { |
| 114 | + $this->assertEquals( |
| 115 | + array( 'path' => array( 'wiki', 'test' ), |
| 116 | + 'cfg' => array( 'servers' => array( 'http' => 'localhost' ) ) ), |
| 117 | + wfCgiToArray( 'path%5B0%5D=wiki&path%5B1%5D=test&cfg%5Bservers%5D%5Bhttp%5D=localhost' ) ); |
| 118 | + } |
| 119 | + |
108 | 120 | function testMimeTypeMatch() { |
109 | 121 | $this->assertEquals( |
110 | 122 | 'text/html', |
Index: trunk/phase3/includes/GlobalFunctions.php |
— | — | @@ -318,9 +318,10 @@ |
319 | 319 | * |
320 | 320 | * @param $array1 Array( String|Array ) |
321 | 321 | * @param $array2 Array( String|Array ) |
| 322 | + * @param $prefix String |
322 | 323 | * @return String |
323 | 324 | */ |
324 | | -function wfArrayToCGI( $array1, $array2 = null ) { |
| 325 | +function wfArrayToCGI( $array1, $array2 = null, $prefix = '' ) { |
325 | 326 | if ( !is_null( $array2 ) ) { |
326 | 327 | $array1 = $array1 + $array2; |
327 | 328 | } |
— | — | @@ -331,20 +332,25 @@ |
332 | 333 | if ( $cgi != '' ) { |
333 | 334 | $cgi .= '&'; |
334 | 335 | } |
| 336 | + if ( $prefix !== '' ) { |
| 337 | + $key = $prefix . "[$key]"; |
| 338 | + } |
335 | 339 | if ( is_array( $value ) ) { |
336 | 340 | $firstTime = true; |
337 | | - foreach ( $value as $v ) { |
338 | | - $cgi .= ( $firstTime ? '' : '&') . |
339 | | - urlencode( $key . '[]' ) . '=' . |
340 | | - urlencode( $v ); |
| 341 | + foreach ( $value as $k => $v ) { |
| 342 | + $cgi .= $firstTime ? '' : '&'; |
| 343 | + if ( is_array( $v ) ) { |
| 344 | + $cgi .= wfArrayToCGI( $v, null, $key . "[$k]" ); |
| 345 | + } else { |
| 346 | + $cgi .= urlencode( $key . "[$k]" ) . '=' . urlencode( $v ); |
| 347 | + } |
341 | 348 | $firstTime = false; |
342 | 349 | } |
343 | 350 | } else { |
344 | 351 | if ( is_object( $value ) ) { |
345 | 352 | $value = $value->__toString(); |
346 | 353 | } |
347 | | - $cgi .= urlencode( $key ) . '=' . |
348 | | - urlencode( $value ); |
| 354 | + $cgi .= urlencode( $key ) . '=' . urlencode( $value ); |
349 | 355 | } |
350 | 356 | } |
351 | 357 | } |
— | — | @@ -362,19 +368,34 @@ |
363 | 369 | * @return array Array version of input |
364 | 370 | */ |
365 | 371 | function wfCgiToArray( $query ) { |
366 | | - if( isset( $query[0] ) && $query[0] == '?' ) { |
| 372 | + if ( isset( $query[0] ) && $query[0] == '?' ) { |
367 | 373 | $query = substr( $query, 1 ); |
368 | 374 | } |
369 | 375 | $bits = explode( '&', $query ); |
370 | 376 | $ret = array(); |
371 | | - foreach( $bits as $bit ) { |
372 | | - if( $bit === '' ) { |
| 377 | + foreach ( $bits as $bit ) { |
| 378 | + if ( $bit === '' ) { |
373 | 379 | continue; |
374 | 380 | } |
375 | 381 | list( $key, $value ) = explode( '=', $bit ); |
376 | 382 | $key = urldecode( $key ); |
377 | 383 | $value = urldecode( $value ); |
378 | | - $ret[$key] = $value; |
| 384 | + if ( strpos( $key, '[' ) !== false ) { |
| 385 | + $keys = array_reverse( explode( '[', $key ) ); |
| 386 | + $key = array_pop( $keys ); |
| 387 | + $temp = $value; |
| 388 | + foreach ( $keys as $k ) { |
| 389 | + $k = substr( $k, 0, -1 ); |
| 390 | + $temp = array( $k => $temp ); |
| 391 | + } |
| 392 | + if ( isset( $ret[$key] ) ) { |
| 393 | + $ret[$key] = array_merge( $ret[$key], $temp ); |
| 394 | + } else { |
| 395 | + $ret[$key] = $temp; |
| 396 | + } |
| 397 | + } else { |
| 398 | + $ret[$key] = $value; |
| 399 | + } |
379 | 400 | } |
380 | 401 | return $ret; |
381 | 402 | } |