r110262 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110261‎ | r110262 | r110263 >
Date:23:12, 29 January 2012
Author:foxtrott
Status:deferred
Tags:
Comment:
- Open up SFAutoeditAPI to better access it from outside
- minor bugfix in SFFormPrinter (formHTML crashed when param $page_name was set to null)
- refactoring SFParserFunctions::renderFormLink so its functionality can be used from outside
- allowing a simplified syntax for the #formlink parser function
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_AutoeditAPI.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_FormPrinter.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_FormPrinter.php
@@ -377,7 +377,7 @@
378378 $this->mPageTitle = $wgTitle;
379379 } elseif ( $is_query ) {
380380 $this->mPageTitle = Title::newFromText( 'RunQuery dummy title' );
381 - } elseif ( $page_name === '' ) {
 381+ } elseif ( $page_name === '' || $page_name === null ) {
382382 $this->mPageTitle = Title::newFromText(
383383 $wgRequest->getVal( 'namespace' ) . ":Semantic Forms permissions test" );
384384 } else {
Index: trunk/extensions/SemanticForms/includes/SF_AutoeditAPI.php
@@ -125,6 +125,13 @@
126126 }
127127
128128 /**
 129+ * Sets the options array
 130+ */
 131+ function setOptions( $options ) {
 132+ $this->mOptions = $options;
 133+ }
 134+
 135+ /**
129136 * Evaluates the parameters, performs the requested API query, and sets up
130137 * the result.
131138 */
@@ -228,7 +235,7 @@
229236 * @param bool $prefillFromExisting If this is set, existing values in the page will be used to prefill the form.
230237 * @return true or an error message
231238 */
232 - private function storeSemanticData( $prefillFromExisting = true ) {
 239+ public function storeSemanticData( $prefillFromExisting = true ) {
233240
234241 global $wgOut, $wgRequest;
235242
@@ -310,7 +317,7 @@
311318 );
312319 }
313320 } else {
314 - $this->addToArray( $data, "wpSave", "Save" );
 321+ self::addToArray( $data, "wpSave", "Save" );
315322 }
316323 // and modify as specified
317324 $data = $this->array_merge_recursive_distinct( $data, $this->mOptions );
@@ -337,9 +344,11 @@
338345 return $this->reportError( $formedit->mError );
339346 } else {
340347
341 - header( "X-Location: " . $wgOut->getRedirect() );
342 - header( "X-Form: " . $formedit->mForm );
343 - header( "X-Target: " . $formedit->mTarget );
 348+ if ( !headers_sent() ) {
 349+ header( "X-Location: " . $wgOut->getRedirect() );
 350+ header( "X-Form: " . $formedit->mForm );
 351+ header( "X-Target: " . $formedit->mTarget );
 352+ }
344353
345354 if ( $this->isApiQuery() ) {
346355 $this->getResult()->addValue( null, 'result',
@@ -389,7 +398,7 @@
390399 case 'checkbox':
391400 case 'radio':
392401 if ( $input->getAttribute( 'checked' ) )
393 - $this->addToArray( $data, $name, $input->getAttribute( 'value' ) );
 402+ self::addToArray( $data, $name, $input->getAttribute( 'value' ) );
394403 break;
395404
396405 // case 'button':
@@ -399,12 +408,12 @@
400409 // case 'reset':
401410 // case 'submit':
402411 case 'text':
403 - $this->addToArray( $data, $name, $input->getAttribute( 'value' ) );
 412+ self::addToArray( $data, $name, $input->getAttribute( 'value' ) );
404413 break;
405414
406415 case 'submit':
407416 if ( $name == "wpSave" )
408 - $this->addToArray( $data, $name, $input->getAttribute( 'value' ) );
 417+ self::addToArray( $data, $name, $input->getAttribute( 'value' ) );
409418 }
410419 }
411420
@@ -422,12 +431,12 @@
423432 $options = $select->getElementsByTagName( 'option' );
424433
425434 if ( count( $options ) && ( !$select->hasAttribute( "multiple" ) || $options->item( 0 )->hasAttribute( 'selected' ) ) ) {
426 - $this->addToArray( $data, $name, $options->item( 0 )->getAttribute( 'value' ) );
 435+ self::addToArray( $data, $name, $options->item( 0 )->getAttribute( 'value' ) );
427436 }
428437
429438 for ( $o = 1; $o < $options->length; $o++ ) {
430439 if ( $options->item( $o )->hasAttribute( 'selected' ) )
431 - $this->addToArray( $data, $name, $options->item( $o )->getAttribute( 'value' ) );
 440+ self::addToArray( $data, $name, $options->item( $o )->getAttribute( 'value' ) );
432441 }
433442 }
434443
@@ -442,7 +451,7 @@
443452 if ( !$name )
444453 continue;
445454
446 - $this->addToArray( $data, $name, $textarea->textContent );
 455+ self::addToArray( $data, $name, $textarea->textContent );
447456 }
448457
449458 return $form;
@@ -467,7 +476,7 @@
468477 if ( $key == "query" || $key == "query string" ) {
469478 $this->parseDataFromQueryString( $data, $value );
470479 } else {
471 - $this->addToArray( $data, $key, $value );
 480+ self::addToArray( $data, $key, $value );
472481 }
473482 }
474483
@@ -480,7 +489,7 @@
481490 // Format: 1stLevelName[2ndLevel][3rdLevel][...], i.e. normal array notation
482491 // $value: the value to insert
483492 // $toplevel: if this is a toplevel value.
484 - private function addToArray( &$array, $key, $value, $toplevel = true ) {
 493+ public static function addToArray( &$array, $key, $value, $toplevel = true ) {
485494 $matches = array();
486495
487496 if ( preg_match( '/^([^\[\]]*)\[([^\[\]]*)\](.*)/', $key, $matches ) ) {
@@ -497,7 +506,7 @@
498507 if ( !array_key_exists( $key, $array ) )
499508 $array[$key] = array();
500509
501 - $this->addToArray( $array[$key], $matches[2] . $matches[3], $value, false );
 510+ self::addToArray( $array[$key], $matches[2] . $matches[3], $value, false );
502511 } else {
503512
504513 if ( $key ) {
@@ -548,7 +557,9 @@
549558 */
550559 private function reportError( $msg ) {
551560 if ( $this->isApiQuery() ) {
552 - header( 'HTTP/Status: 400 Bad Request' );
 561+ if ( !headers_sent() ) {
 562+ header( 'HTTP/Status: 400 Bad Request' );
 563+ }
553564 $this->getResult()->addValue( null, 'result', array( 'code' => '400', '*' => $msg ) );
554565 }
555566 return $msg;
Index: trunk/extensions/SemanticForms/includes/SF_ParserFunctions.php
@@ -156,114 +156,13 @@
157157 }
158158
159159 static function renderFormLink ( &$parser ) {
160 - global $wgVersion;
161160
162161 $params = func_get_args();
163162 array_shift( $params ); // We don't need the parser.
164 - // Set defaults.
165 - $inFormName = $inLinkStr = $inLinkType = $inTooltip =
166 - $inQueryStr = $inTargetName = '';
167 - $classStr = "";
168 - // assign params
169 - // - support unlabelled params, for backwards compatibility
170 - // - parse and sanitize all parameter values
171 - foreach ( $params as $i => $param ) {
172 - $elements = explode( '=', $param, 2 );
173 -
174 - // set param_name and value
175 - if ( count( $elements ) > 1 ) {
176 - $param_name = trim( $elements[0] );
177 -
178 - // parse (and sanitize) parameter values
179 - $value = trim( $parser->recursiveTagParse( $elements[1] ) );
180 - } else {
181 - $param_name = null;
182 -
183 - // parse (and sanitize) parameter values
184 - $value = trim( $parser->recursiveTagParse( $param ) );
185 - }
186 -
187 - if ( $param_name == 'form' ) {
188 - $inFormName = $value;
189 - } elseif ( $param_name == 'link text' ) {
190 - $inLinkStr = $value;
191 - } elseif ( $param_name == 'link type' ) {
192 - $inLinkType = $value;
193 - } elseif ( $param_name == 'query string' ) {
194 - $inQueryStr = Sanitizer::decodeCharReferences( $value );
195 - } elseif ( $param_name == 'tooltip' ) {
196 - $inTooltip = Sanitizer::decodeCharReferences( $value );
197 - } elseif ( $param_name == 'target' ) {
198 - $inTargetName = $value;
199 - } elseif ( $param_name == null && $value == 'popup' ) {
200 - self::loadScriptsForPopupForm( $parser );
201 - $classStr = 'popupformlink';
202 - }
203 - elseif ( $i == 0 ) {
204 - $inFormName = $value;
205 - } elseif ( $i == 1 ) {
206 - $inLinkStr = $value;
207 - } elseif ( $i == 2 ) {
208 - $inLinkType = $value;
209 - } elseif ( $i == 3 ) {
210 - $inQueryStr = Sanitizer::decodeCharReferences( $value );
211 - }
212 - }
213 -
214 - $ad = SFUtils::getSpecialPage( 'FormEdit' );
215 - $link_url = $ad->getTitle()->getLocalURL() . "/$inFormName";
216 - if ( ! empty( $inTargetName ) ) {
217 - $link_url .= "/$inTargetName";
218 - }
219 - $link_url = str_replace( ' ', '_', $link_url );
220 - $hidden_inputs = "";
221 - if ( $inQueryStr != '' ) {
222 - // Special handling for the buttons - query string
223 - // has to be turned into hidden inputs.
224 - if ( $inLinkType == 'button' || $inLinkType == 'post button' ) {
225 - // Change HTML-encoded ampersands to
226 - // URL-encoded ampersands, so that the string
227 - // doesn't get split up on the '&'.
228 - $inQueryStr = str_replace( '&amp;', '%26', $inQueryStr );
229 - $query_components = explode( '&', $inQueryStr );
230 - foreach ( $query_components as $query_component ) {
231 - $query_component = urldecode( $query_component );
232 - $var_and_val = explode( '=', $query_component );
233 - if ( count( $var_and_val ) == 2 ) {
234 - $hidden_inputs .= SFFormUtils::hiddenFieldHTML( $var_and_val[0], $var_and_val[1] );
235 - }
236 - }
237 - } else {
238 - $link_url .= ( strstr( $link_url, '?' ) ) ? '&' : '?';
239 - // URL-encode the spaces, newlines, ampersands etc.
240 - // in the query string.
241 - // (Should this just be a general urlencode?)
242 - $inQueryStr = str_replace( array( ' ', '+', '&amp;', "\n", '#' ),
243 - array( '%20', '%2B', '%26', '%0A', '%23' ),
244 - $inQueryStr );
245 - $link_url .= $inQueryStr;
246 - }
247 - }
248 - if ( $inLinkType == 'button' || $inLinkType == 'post button' ) {
249 - $formMethod = ( $inLinkType == 'button' ) ? 'get' : 'post';
250 - $str = Html::rawElement( 'form', array( 'action' => $link_url, 'method' => $formMethod, 'class' => $classStr ),
251 - Html::rawElement( 'button', array( 'type' => 'submit', 'value' => $inLinkStr ), $inLinkStr ) .
252 - $hidden_inputs
253 - );
254 - } else {
255 - // If a target page has been specified but it doesn't
256 - // exist, make it a red link.
257 - if ( ! empty( $inTargetName ) ) {
258 - $targetTitle = Title::newFromText( $inTargetName );
259 - if ( is_null( $targetTitle ) || !$targetTitle->exists() ) {
260 - $classStr .= " new";
261 - }
262 - }
263 - $str = Html::rawElement( 'a', array( 'href' => $link_url, 'class' => $classStr, 'title' => $inTooltip ), $inLinkStr );
264 - }
 163+
265164 // hack to remove newline from beginning of output, thanks to
266165 // http://jimbojw.com/wiki/index.php?title=Raw_HTML_Output_from_a_MediaWiki_Parser_Function
267 - return $parser->insertStripItem( $str, $parser->mStripState );
 166+ return $parser->insertStripItem( SFUtils::createFormLink( $parser, 'FormEdit', $params ), $parser->mStripState );
268167 }
269168
270169 static function renderFormInput ( &$parser ) {
Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -888,4 +888,122 @@
889889 return true;
890890 }
891891
 892+ static function createFormLink ( &$parser, $specialPageName, $params ) {
 893+ global $wgVersion;
 894+
 895+ // Set defaults.
 896+ $inFormName = $inLinkStr = $inLinkType = $inTooltip =
 897+ $inQueryStr = $inTargetName = '';
 898+ $classStr = "";
 899+ $inQueryArr = array();
 900+ // assign params
 901+ // - support unlabelled params, for backwards compatibility
 902+ // - parse and sanitize all parameter values
 903+ foreach ( $params as $i => $param ) {
 904+ $elements = explode( '=', $param, 2 );
 905+
 906+ // set param_name and value
 907+ if ( count( $elements ) > 1 ) {
 908+ $param_name = trim( $elements[0] );
 909+
 910+ // parse (and sanitize) parameter values
 911+ $value = trim( $parser->recursiveTagParse( $elements[1] ) );
 912+ } else {
 913+ $param_name = null;
 914+
 915+ // parse (and sanitize) parameter values
 916+ $value = trim( $parser->recursiveTagParse( $param ) );
 917+ }
 918+
 919+ if ( $param_name == 'form' ) {
 920+ $inFormName = $value;
 921+ } elseif ( $param_name == 'link text' ) {
 922+ $inLinkStr = $value;
 923+ } elseif ( $param_name == 'link type' ) {
 924+ $inLinkType = $value;
 925+ } elseif ( $param_name == 'query string' ) {
 926+ // Change HTML-encoded ampersands directly to
 927+ // URL-encoded ampersands, so that the string
 928+ // doesn't get split up on the '&'.
 929+ $inQueryStr = str_replace( '&amp;', '%26', $value );
 930+
 931+ $inQueryStr = Sanitizer::decodeCharReferences( $inQueryStr );
 932+ parse_str($inQueryStr, $arr);
 933+ $inQueryArr = array_merge_recursive( $inQueryArr, $arr );
 934+ } elseif ( $param_name == 'tooltip' ) {
 935+ $inTooltip = Sanitizer::decodeCharReferences( $value );
 936+ } elseif ( $param_name == 'target' ) {
 937+ $inTargetName = $value;
 938+ } elseif ( $param_name == null && $value == 'popup' ) {
 939+ self::loadScriptsForPopupForm( $parser );
 940+ $classStr = 'popupformlink';
 941+ } elseif ( $param_name !== null ) {
 942+ $value = urlencode($value);
 943+ parse_str("$param_name=$value", $arr);
 944+ $inQueryArr = array_merge_recursive( $inQueryArr, $arr );
 945+ }elseif ( $i == 0 ) {
 946+ $inFormName = $value;
 947+ } elseif ( $i == 1 ) {
 948+ $inLinkStr = $value;
 949+ } elseif ( $i == 2 ) {
 950+ $inLinkType = $value;
 951+ } elseif ( $i == 3 ) {
 952+ // Change HTML-encoded ampersands directly to
 953+ // URL-encoded ampersands, so that the string
 954+ // doesn't get split up on the '&'.
 955+ $inQueryStr = str_replace( '&amp;', '%26', $value );
 956+
 957+ $inQueryStr = Sanitizer::decodeCharReferences( $inQueryStr );
 958+ parse_str($inQueryStr, $arr);
 959+ $inQueryArr = array_merge_recursive( $inQueryArr, $arr );
 960+ }
 961+ }
 962+
 963+ $ad = SFUtils::getSpecialPage( $specialPageName );
 964+ $link_url = $ad->getTitle()->getLocalURL() . "/$inFormName";
 965+ if ( ! empty( $inTargetName ) ) {
 966+ $link_url .= "/$inTargetName";
 967+ }
 968+ $link_url = str_replace( ' ', '_', $link_url );
 969+ $hidden_inputs = "";
 970+ if ( ! empty($inQueryArr) ) {
 971+ // Special handling for the buttons - query string
 972+ // has to be turned into hidden inputs.
 973+ if ( $inLinkType == 'button' || $inLinkType == 'post button' ) {
 974+
 975+ $query_components = explode( '&', http_build_query( $inQueryArr ) );
 976+
 977+ foreach ( $query_components as $query_component ) {
 978+ $query_component = urldecode( $query_component );
 979+ $var_and_val = explode( '=', $query_component, 2 );
 980+ if ( count( $var_and_val ) == 2 ) {
 981+ $hidden_inputs .= SFFormUtils::hiddenFieldHTML( $var_and_val[0], $var_and_val[1] );
 982+ }
 983+ }
 984+ } else {
 985+ $link_url .= ( strstr( $link_url, '?' ) ) ? '&' : '?';
 986+ $link_url .= http_build_query( $inQueryArr );
 987+ }
 988+ }
 989+ if ( $inLinkType == 'button' || $inLinkType == 'post button' ) {
 990+ $formMethod = ( $inLinkType == 'button' ) ? 'get' : 'post';
 991+ $str = Html::rawElement( 'form', array( 'action' => $link_url, 'method' => $formMethod, 'class' => $classStr ),
 992+ Html::rawElement( 'button', array( 'type' => 'submit', 'value' => $inLinkStr ), $inLinkStr ) .
 993+ $hidden_inputs
 994+ );
 995+ } else {
 996+ // If a target page has been specified but it doesn't
 997+ // exist, make it a red link.
 998+ if ( ! empty( $inTargetName ) ) {
 999+ $targetTitle = Title::newFromText( $inTargetName );
 1000+ if ( is_null( $targetTitle ) || !$targetTitle->exists() ) {
 1001+ $classStr .= " new";
 1002+ }
 1003+ }
 1004+ $str = Html::rawElement( 'a', array( 'href' => $link_url, 'class' => $classStr, 'title' => $inTooltip ), $inLinkStr );
 1005+ }
 1006+
 1007+ return $str;
 1008+ }
 1009+
8921010 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r110324followup r110262: move left-behind functionfoxtrott20:18, 30 January 2012
r110422followup r110262: fix results of http_build_queryfoxtrott20:04, 31 January 2012