r110685 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110684‎ | r110685 | r110686 >
Date:21:33, 3 February 2012
Author:foxtrott
Status:deferred
Tags:
Comment:
syntax w/o query string for autoedit & forminput;
fixes to correctly transcode parser function params;
avoid usage of array_merge_recursive;
fix to hiddenFieldHtml
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_AutoeditAPI.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_FormUtils.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_ParserFunctions.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_AutoeditAPI.php
@@ -320,7 +320,7 @@
321321 self::addToArray( $data, "wpSave", "Save" );
322322 }
323323 // and modify as specified
324 - $data = $this->array_merge_recursive_distinct( $data, $this->mOptions );
 324+ $data = SFUtils::array_merge_recursive_distinct( $data, $this->mOptions );
325325
326326 ////////////////////////////////////////////////////////////////////////
327327 // Store the modified form
@@ -518,40 +518,6 @@
519519 }
520520
521521 /**
522 - * array_merge_recursive merges arrays, but it converts values with duplicate
523 - * keys to arrays rather than overwriting the value in the first array with the duplicate
524 - * value in the second array, as array_merge does.
525 - *
526 - * array_merge_recursive_distinct does not change the datatypes of the values in the arrays.
527 - * Matching keys' values in the second array overwrite those in the first array.
528 - *
529 - * Parameters are passed by reference, though only for performance reasons. They're not
530 - * altered by this function.
531 - *
532 - * See http://www.php.net/manual/en/function.array-merge-recursive.php#92195
533 - *
534 - * @param array $array1
535 - * @param array $array2
536 - * @return array
537 - * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
538 - * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
539 - */
540 - private function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
541 -
542 - $merged = $array1;
543 -
544 - foreach ( $array2 as $key => &$value ) {
545 - if ( is_array( $value ) && isset( $merged[$key] ) && is_array( $merged[$key] ) ) {
546 - $merged[$key] = $this->array_merge_recursive_distinct( $merged[$key], $value );
547 - } else {
548 - $merged[$key] = $value;
549 - }
550 - }
551 -
552 - return $merged;
553 - }
554 -
555 - /**
556522 * Set HTTP error header and add error message to the ApiResult
557523 * @param String $msg
558524 */
Index: trunk/extensions/SemanticForms/includes/SF_FormUtils.php
@@ -42,7 +42,7 @@
4343 }
4444
4545 static function hiddenFieldHTML( $input_name, $cur_value ) {
46 - return "\t" . Html::hidden( $input_name, $cur_value ) . "\n";
 46+ return Html::hidden( $input_name, $cur_value );
4747 }
4848
4949 /**
Index: trunk/extensions/SemanticForms/includes/SF_ParserFunctions.php
@@ -172,6 +172,7 @@
173173 array_shift( $params ); // don't need the parser
174174 // set defaults
175175 $inFormName = $inValue = $inButtonStr = $inQueryStr = '';
 176+ $inQueryArr = array();
176177 $inAutocompletionSource = '';
177178 $inRemoteAutocompletion = false;
178179 $inSize = 25;
@@ -179,12 +180,20 @@
180181 // assign params - support unlabelled params, for backwards compatibility
181182 foreach ( $params as $i => $param ) {
182183 $elements = explode( '=', $param, 2 );
183 - $param_name = null;
184 - $value = trim( $param );
 184+
 185+ // set param_name and value
185186 if ( count( $elements ) > 1 ) {
186187 $param_name = trim( $elements[0] );
 188+
 189+ // parse (and sanitize) parameter values
187190 $value = trim( $parser->recursiveTagParse( $elements[1] ) );
 191+ } else {
 192+ $param_name = null;
 193+
 194+ // parse (and sanitize) parameter values
 195+ $value = trim( $parser->recursiveTagParse( $param ) );
188196 }
 197+
189198 if ( $param_name == 'form' )
190199 $inFormName = $value;
191200 elseif ( $param_name == 'size' )
@@ -193,9 +202,15 @@
194203 $inValue = $value;
195204 elseif ( $param_name == 'button text' )
196205 $inButtonStr = $value;
197 - elseif ( $param_name == 'query string' )
198 - $inQueryStr = $value;
199 - elseif ( $param_name == 'autocomplete on category' ) {
 206+ elseif ( $param_name == 'query string' ) {
 207+ // Change HTML-encoded ampersands directly to
 208+ // URL-encoded ampersands, so that the string
 209+ // doesn't get split up on the '&'.
 210+ $inQueryStr = str_replace( '&amp;', '%26', $value );
 211+
 212+ parse_str($inQueryStr, $arr);
 213+ $inQueryArr = SFUtils::array_merge_recursive_distinct( $inQueryArr, $arr );
 214+ } elseif ( $param_name == 'autocomplete on category' ) {
200215 $inAutocompletionSource = $value;
201216 $autocompletion_type = 'category';
202217 } elseif ( $param_name == 'autocomplete on namespace' ) {
@@ -206,17 +221,29 @@
207222 } elseif ( $param_name == null && $value == 'popup' ) {
208223 self::loadScriptsForPopupForm( $parser );
209224 $classStr = 'popupforminput';
 225+ } elseif ( $param_name !== null ) {
 226+
 227+ $value = urlencode($value);
 228+ parse_str("$param_name=$value", $arr);
 229+ $inQueryArr = SFUtils::array_merge_recursive_distinct( $inQueryArr, $arr );
 230+
 231+ } elseif ( $i == 0 ) {
 232+ $inFormName = $value;
 233+ } elseif ( $i == 1 ) {
 234+ $inSize = $value;
 235+ } elseif ( $i == 2 ) {
 236+ $inValue = $value;
 237+ } elseif ( $i == 3 ) {
 238+ $inButtonStr = $value;
 239+ } elseif ( $i == 4 ) {
 240+ // Change HTML-encoded ampersands directly to
 241+ // URL-encoded ampersands, so that the string
 242+ // doesn't get split up on the '&'.
 243+ $inQueryStr = str_replace( '&amp;', '%26', $value );
 244+
 245+ parse_str($inQueryStr, $arr);
 246+ $inQueryArr = SFUtils::array_merge_recursive_distinct( $inQueryArr, $arr );
210247 }
211 - elseif ( $i == 0 )
212 - $inFormName = $param;
213 - elseif ( $i == 1 )
214 - $inSize = $param;
215 - elseif ( $i == 2 )
216 - $inValue = $param;
217 - elseif ( $i == 3 )
218 - $inButtonStr = $param;
219 - elseif ( $i == 4 )
220 - $inQueryStr = $param;
221248 }
222249
223250 $fs = SFUtils::getSpecialPage( 'FormStart' );
@@ -274,22 +301,21 @@
275302 } else {
276303 $str .= SFFormUtils::hiddenFieldHTML( "form", $inFormName );
277304 }
278 - // Recreate the passed-in query string as a set of hidden
279 - // variables.
280 - // Change HTML-encoded ampersands to URL-encoded ampersands, so
281 - // that the string doesn't get split up on the '&'.
282 - $inQueryStr = str_replace( '&amp;', '%26', $inQueryStr );
283 - $query_components = explode( '&', $inQueryStr );
284 - foreach ( $query_components as $component ) {
285 - // change URL-encoded ampersands back
286 - $component = str_replace( '%26', '&', $component );
287 - $subcomponents = explode( '=', $component, 2 );
288 - $key = ( isset( $subcomponents[0] ) ) ? $subcomponents[0] : '';
289 - $val = ( isset( $subcomponents[1] ) ) ? $subcomponents[1] : '';
290 - if ( ! empty( $key ) ) {
291 - $str .= "\t\t\t" . Html::hidden( $key, $val ) . "\n";
 305+
 306+ // Recreate the passed-in query string as a set of hidden variables.
 307+ if ( !empty( $inQueryArr ) ) {
 308+ // query string has to be turned into hidden inputs.
 309+
 310+ $query_components = explode( '&', http_build_query( $inQueryArr, '', '&' ) );
 311+
 312+ foreach ( $query_components as $query_component ) {
 313+ $var_and_val = explode( '=', $query_component, 2 );
 314+ if ( count( $var_and_val ) == 2 ) {
 315+ $str .= SFFormUtils::hiddenFieldHTML( urldecode( $var_and_val[0] ), urldecode( $var_and_val[1] ) );
 316+ }
292317 }
293318 }
 319+
294320 $button_str = ( $inButtonStr != '' ) ? $inButtonStr : wfMsg( 'sf_formstart_createoredit' );
295321 $str .= <<<END
296322 <input type="submit" value="$button_str" /></p>
@@ -461,6 +487,7 @@
462488 $linkType = 'span';
463489 $summary = null;
464490 $classString = 'autoedit-trigger';
 491+ $inQueryArr = array();
465492
466493 // parse parameters
467494 $params = func_get_args();
@@ -486,16 +513,38 @@
487514 case 'summary':
488515 $summary = $parser->recursiveTagParse( $value );
489516 break;
 517+ case 'query string' :
 518+
 519+ // Change HTML-encoded ampersands directly to
 520+ // URL-encoded ampersands, so that the string
 521+ // doesn't get split up on the '&'.
 522+ $inQueryStr = str_replace( '&amp;', '%26', $value );
 523+
 524+ parse_str( $inQueryStr, $arr );
 525+ $inQueryArr = SFUtils::array_merge_recursive_distinct( $inQueryArr, $arr );
 526+ break;
 527+
490528 default :
491 - // The decode-encode sequence allows users to pass wikitext
492 - // to the target form without having it parsed right away.
493 - // To do that they need to use htmlentities instead of
494 - // braces and brackets
495 - $formcontent .=
496 - Html::hidden( $key, Sanitizer::decodeCharReferences( $value ) );
 529+
 530+ $value = urlencode( $parser->recursiveTagParse( $value ) );
 531+ parse_str( "$key=$value", $arr );
 532+ $inQueryArr = SFUtils::array_merge_recursive_distinct( $inQueryArr, $arr );
497533 }
498534 }
499535
 536+ // query string has to be turned into hidden inputs.
 537+ if ( !empty( $inQueryArr ) ) {
 538+
 539+ $query_components = explode( '&', http_build_query( $inQueryArr, '', '&' ) );
 540+
 541+ foreach ( $query_components as $query_component ) {
 542+ $var_and_val = explode( '=', $query_component, 2 );
 543+ if ( count( $var_and_val ) == 2 ) {
 544+ $formcontent .= Html::hidden( urldecode( $var_and_val[0] ), urldecode( $var_and_val[1] ) );
 545+ }
 546+ }
 547+ }
 548+
500549 if ( $linkString == null ) return null;
501550
502551 if ( $linkType == 'button' ) {
Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -927,9 +927,8 @@
928928 // doesn't get split up on the '&'.
929929 $inQueryStr = str_replace( '&amp;', '%26', $value );
930930
931 - $inQueryStr = Sanitizer::decodeCharReferences( $inQueryStr );
932931 parse_str($inQueryStr, $arr);
933 - $inQueryArr = array_merge_recursive( $inQueryArr, $arr );
 932+ $inQueryArr = self::array_merge_recursive_distinct( $inQueryArr, $arr );
934933 } elseif ( $param_name == 'tooltip' ) {
935934 $inTooltip = Sanitizer::decodeCharReferences( $value );
936935 } elseif ( $param_name == 'target' ) {
@@ -940,7 +939,7 @@
941940 } elseif ( $param_name !== null ) {
942941 $value = urlencode($value);
943942 parse_str("$param_name=$value", $arr);
944 - $inQueryArr = array_merge_recursive( $inQueryArr, $arr );
 943+ $inQueryArr = self::array_merge_recursive_distinct( $inQueryArr, $arr );
945944 }elseif ( $i == 0 ) {
946945 $inFormName = $value;
947946 } elseif ( $i == 1 ) {
@@ -953,9 +952,8 @@
954953 // doesn't get split up on the '&'.
955954 $inQueryStr = str_replace( '&amp;', '%26', $value );
956955
957 - $inQueryStr = Sanitizer::decodeCharReferences( $inQueryStr );
958956 parse_str($inQueryStr, $arr);
959 - $inQueryArr = array_merge_recursive( $inQueryArr, $arr );
 957+ $inQueryArr = self::array_merge_recursive_distinct( $inQueryArr, $arr );
960958 }
961959 }
962960
@@ -974,10 +972,9 @@
975973 $query_components = explode( '&', http_build_query( $inQueryArr, '', '&' ) );
976974
977975 foreach ( $query_components as $query_component ) {
978 - $query_component = urldecode( $query_component );
979976 $var_and_val = explode( '=', $query_component, 2 );
980977 if ( count( $var_and_val ) == 2 ) {
981 - $hidden_inputs .= SFFormUtils::hiddenFieldHTML( $var_and_val[0], $var_and_val[1] );
 978+ $hidden_inputs .= SFFormUtils::hiddenFieldHTML( urldecode( $var_and_val[0] ), urldecode( $var_and_val[1] ) );
982979 }
983980 }
984981 } else {
@@ -1044,4 +1041,40 @@
10451042
10461043 return true;
10471044 }
 1045+
 1046+ /**
 1047+ * array_merge_recursive merges arrays, but it converts values with duplicate
 1048+ * keys to arrays rather than overwriting the value in the first array with the duplicate
 1049+ * value in the second array, as array_merge does.
 1050+ *
 1051+ * array_merge_recursive_distinct does not change the datatypes of the values in the arrays.
 1052+ * Matching keys' values in the second array overwrite those in the first array.
 1053+ *
 1054+ * Parameters are passed by reference, though only for performance reasons. They're not
 1055+ * altered by this function.
 1056+ *
 1057+ * See http://www.php.net/manual/en/function.array-merge-recursive.php#92195
 1058+ *
 1059+ * @param array $array1
 1060+ * @param array $array2
 1061+ * @return array
 1062+ * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
 1063+ * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
 1064+ */
 1065+ public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
 1066+
 1067+ $merged = $array1;
 1068+
 1069+ foreach ( $array2 as $key => &$value ) {
 1070+ if ( is_array( $value ) && isset( $merged[$key] ) && is_array( $merged[$key] ) ) {
 1071+ $merged[$key] = self::array_merge_recursive_distinct( $merged[$key], $value );
 1072+ } else {
 1073+ $merged[$key] = $value;
 1074+ }
 1075+ }
 1076+
 1077+ return $merged;
 1078+ }
 1079+
 1080+
10481081 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r110774Revert of r110685 - I preferred the old formattingyaron20:02, 6 February 2012