r110422 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110421‎ | r110422 | r110423 >
Date:20:04, 31 January 2012
Author:foxtrott
Status:deferred (Comments)
Tags:
Comment:
followup r110262: fix results of http_build_query
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -971,7 +971,7 @@
972972 // has to be turned into hidden inputs.
973973 if ( $inLinkType == 'button' || $inLinkType == 'post button' ) {
974974
975 - $query_components = explode( '&', http_build_query( $inQueryArr ) );
 975+ $query_components = explode( '&', http_build_query( $inQueryArr, '', '&' ) );
976976
977977 foreach ( $query_components as $query_component ) {
978978 $query_component = urldecode( $query_component );
@@ -982,7 +982,7 @@
983983 }
984984 } else {
985985 $link_url .= ( strstr( $link_url, '?' ) ) ? '&' : '?';
986 - $link_url .= http_build_query( $inQueryArr );
 986+ $link_url .= str_replace( '+', '%20', http_build_query( $inQueryArr, '', '&' ) );
987987 }
988988 }
989989 if ( $inLinkType == 'button' || $inLinkType == 'post button' ) {

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r110262- Open up SFAutoeditAPI to better access it from outside...foxtrott23:12, 29 January 2012

Comments

#Comment by Nikerabbit (talk | contribs)   07:11, 2 February 2012

Could you explain what this code is doing?

#Comment by F.trott (talk | contribs)   08:44, 2 February 2012

http_build_query turns an array into the query string of a URL. It has in PHP 5.2 the deficiency of turning spaces into +, so I encode them myself. The foreach loop is garbage.

#Comment by Nikerabbit (talk | contribs)   10:06, 2 February 2012

So why is it first building a query and then decoding it, manually?

#Comment by F.trott (talk | contribs)   10:16, 2 February 2012

http_build_query converts arrays recursively, you get something like "key[subkey][subsubkey]=value". It's not ideal, lots of string manipulation, but it's the easiest and probably still fastest solution I could come up with. I would be happy to put in something more efficient, if you know of anything.

#Comment by Nikerabbit (talk | contribs)   10:58, 2 February 2012

Flattening arrays is not that hard. You could use one of these (not thoroughly tested):

function flatten1( $a ) {
	do {
		$done = true;
		foreach ( $a as $key => $value ) {
			if ( is_array( $value ) ) {
				foreach ( $value as $subkey => $subvalue ) {
					$a[$key . "[$subkey]"] = $subvalue;
				}
				unset( $a[$key] );
				$done = false;
			}
		}
	} while( !$done );
	return $a;
}

function flatten2( $a, $first = true ) {
	foreach ( $a as $key => $value ) {
		if ( is_array( $value ) ) {
			$value = flatten2( $value, false );
			foreach ( $value as $subkey => $subvalue ) {
				$newkey = $first ? $key . "[$subkey]" : "$key][$subkey";
				$a[$newkey] = $subvalue;
			}
			unset( $a[$key] );
		}
	}
	return $a;
}

Note that don't convert all values to strings.