r90404 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90403‎ | r90404 | r90405 >
Date:17:12, 19 June 2011
Author:yaron
Status:deferred
Tags:
Comment:
Improved some formatting and contents
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_AutoEditAjaxHandler.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_AutoEditAjaxHandler.php
@@ -19,7 +19,7 @@
2020
2121 static function handleAutoEdit ( $optionsString = null, $prefillFromExisting = 'true' ) {
2222 $handler = new self( $optionsString );
23 - return $handler -> storeSemanticData( $prefillFromExisting === 'true' );
 23+ return $handler->storeSemanticData( $prefillFromExisting === 'true' );
2424 }
2525
2626 function __construct ( $options ) {
@@ -30,22 +30,22 @@
3131
3232 // if ( version_compare( substr( $wgVersion, 0, 4 ), '1.17', '<' ) ) {
3333 if ( !StubObject::isRealObject( $wgParser ) )
34 - $wgParser -> _unstub();
 34+ $wgParser->_unstub();
3535
3636 // perform offensive operation
37 - $wgParser -> startExternalParse( $title, ParserOptions::newFromUser( $wgUser ), Parser::OT_HTML, true );
 37+ $wgParser->startExternalParse( $title, ParserOptions::newFromUser( $wgUser ), Parser::OT_HTML, true );
3838 // } else {
39 -// $wgParser -> startExternalParse( $title, ParserOptions::newFromUser( $wgUser ), Parser::OT_HTML, true );
 39+// $wgParser->startExternalParse( $title, ParserOptions::newFromUser( $wgUser ), Parser::OT_HTML, true );
4040 // }
4141
4242 // parse options
43 - $this -> parseDataFromQueryString( $this -> mOptions, $options, true );
 43+ $this->parseDataFromQueryString( $this->mOptions, $options, true );
4444 }
4545
4646 /**
4747 *
48 - * @global $wgOut
49 - * @global $wgRequest
 48+ * @global $wgOut
 49+ * @global $wgRequest
5050 * @global <type> $wgUser
5151 * @global <type> $wgParser
5252 * @return <type>
@@ -54,12 +54,12 @@
5555
5656 global $wgOut, $wgRequest, $wgParser, $wgTitle;
5757
58 - if ( !array_key_exists( 'ok text', $this -> mOptions ) ) {
59 - $this -> mOptions[ 'ok text' ] = wfMsg( 'sf_autoedit_success' );
 58+ if ( !array_key_exists( 'ok text', $this->mOptions ) ) {
 59+ $this->mOptions['ok text'] = wfMsg( 'sf_autoedit_success' );
6060 }
6161
62 - if ( !array_key_exists( 'error text', $this -> mOptions ) ) {
63 - $this -> mOptions[ 'error text' ] = '$1';
 62+ if ( !array_key_exists( 'error text', $this->mOptions ) ) {
 63+ $this->mOptions['error text'] = '$1';
6464 }
6565
6666
@@ -71,17 +71,17 @@
7272 }
7373
7474 // If we have no target article and no form we might as well stop right away
75 - if ( !array_key_exists( 'target', $this -> mOptions )
76 - && !array_key_exists( 'form', $this -> mOptions ) ) {
 75+ if ( !array_key_exists( 'target', $this->mOptions )
 76+ && !array_key_exists( 'form', $this->mOptions ) ) {
7777 return 'autoedit-notargetspecified';
7878 }
7979
8080 // check if form was specified
81 - if ( !array_key_exists( 'form', $this -> mOptions ) ) {
 81+ if ( !array_key_exists( 'form', $this->mOptions ) ) {
8282
83 - // no form specified, find one
84 - // get title object and id for requested target article
85 - $title = Title::newFromText( $this -> mOptions[ 'target' ] );
 83+ // If no form was specified, find the default one for
 84+ // this page.
 85+ $title = Title::newFromText( $this->mOptions['target'] );
8686 $form_names = SFFormLinker::getDefaultFormsForPage( $title );
8787
8888 // if no form can be found, return
@@ -94,84 +94,83 @@
9595 return 'autoedit-toomanyformsfound';
9696 }
9797
98 - // use the first found form
99 - $this -> mOptions[ 'form' ] = $form_names[ 0 ];
 98+ // There should now be exactly one form.
 99+ $this->mOptions['form'] = $form_names[0];
100100 }
101101
102102 // we only care for the form's body
103 - $wgOut -> setArticleBodyOnly( true );
 103+ $wgOut->setArticleBodyOnly( true );
104104
105105 $formedit = new SFFormEdit();
106106 $data = array( );
107107
108 - ////////////////////////////////////////////////////////////////////////
109 - // First get the Semantic Form and extract its data (if requested)
110 - // and modify or set as specified in the options string
111 -
 108+ // Get the form definition and target page (if there is one),
 109+ // as specified in the options string, then create the actual
 110+ // HTML form from them, and call that form to modify or create
 111+ // the page.
112112 if ( $prefillFromExisting ) {
 113+ $wgRequest = new FauxRequest( $this->mOptions, true );
113114
114 - $wgRequest = new FauxRequest( $this -> mOptions, true );
115 -
116115 // get the Semantic Form
117 - if ( array_key_exists( 'target', $this -> mOptions ) ) {
118 - $formedit -> execute( $this -> mOptions[ 'form' ] . '/' . $this -> mOptions[ 'target' ] );
 116+ if ( array_key_exists( 'target', $this->mOptions ) ) {
 117+ $formedit->execute( $this->mOptions['form'] . '/' . $this->mOptions['target'] );
119118 } else {
120 - $formedit -> execute( $this -> mOptions[ 'form' ] );
 119+ $formedit->execute( $this->mOptions['form'] );
121120 }
122121
123122 // extract its data
124 - $form = $this -> parseDataFromHTMLFrag( $data, trim( $wgOut -> getHTML() ), 'sfForm' );
 123+ $form = $this->parseDataFromHTMLFrag( $data, trim( $wgOut->getHTML() ), 'sfForm' );
125124
126125 if ( !$form ) {
127126 // something went wrong
128127 return array(
129128 'autoedit-nosemanticform',
130129 array(
131 - $this -> mOptions[ 'target' ],
132 - $this -> mOptions[ 'form' ]
 130+ $this->mOptions['target'],
 131+ $this->mOptions['form']
133132 )
134133 );
135134 }
136135 } else {
137 - $this -> addToArray( $data, "wpSave", "Save" );
 136+ $this->addToArray( $data, "wpSave", "Save" );
138137 }
139138 // and modify as specified
140 - $data = $this -> array_merge_recursive_distinct( $data, $this -> mOptions );
 139+ $data = $this->array_merge_recursive_distinct( $data, $this->mOptions );
141140
142141 ////////////////////////////////////////////////////////////////////////
143142 // Store the modified form
144 - //$wgOut -> clearHTML();
 143+ //$wgOut->clearHTML();
145144 $wgRequest = new FauxRequest( $data, true );
146145
147146 // get the MW form
148 - if ( array_key_exists( 'target', $this -> mOptions ) ) {
149 - $formedit -> execute( $this -> mOptions[ 'form' ] . '/' . $this -> mOptions[ 'target' ], false );
 147+ if ( array_key_exists( 'target', $this->mOptions ) ) {
 148+ $formedit->execute( $this->mOptions['form'] . '/' . $this->mOptions['target'], false );
150149 } else {
151 - $formedit -> execute( $this -> mOptions[ 'form' ], false );
 150+ $formedit->execute( $this->mOptions['form'], false );
152151 }
153152
154 - $wgParser -> getOptions() -> enableLimitReport( false );
 153+ $wgParser->getOptions()->enableLimitReport( false );
155154
156 - if ( $formedit -> mError ) {
 155+ if ( $formedit->mError ) {
 156+ // TODO Should this be sanitized? i.e. all HTML tags removed?
 157+ $msg = $formedit->mError;
157158
158 - $msg = $formedit -> mError; // Should this be sanitized? I.e. all html tags removed?
159 -
160 - $msg = $wgParser -> parse(
161 - wfMsgReplaceArgs( $this -> mOptions[ 'error text' ], array( $msg ) ),
 159+ $msg = $wgParser->parse(
 160+ wfMsgReplaceArgs( $this->mOptions['error text'], array( $msg ) ),
162161 $wgTitle,
163 - $wgParser -> getOptions()
164 - ) -> getText();
 162+ $wgParser->getOptions()
 163+ )->getText();
165164
166165 $result = new AjaxResponse( $msg );
167 - $result -> setResponseCode( '400 Bad Request' );
 166+ $result->setResponseCode( '400 Bad Request' );
168167 return $result;
169168 } else {
170169
171 - header( "X-Location: " . $wgOut -> getRedirect() );
172 - header( "X-Form: " . $formedit -> mForm );
173 - header( "X-Target: " . $formedit -> mTarget );
 170+ header( "X-Location: " . $wgOut->getRedirect() );
 171+ header( "X-Form: " . $formedit->mForm );
 172+ header( "X-Target: " . $formedit->mTarget );
174173
175 - $msg = $wgParser -> recursiveTagParse( wfMsgReplaceArgs( $this -> mOptions[ 'ok text' ], array( $formedit -> mTarget, $formedit -> mForm ) ) );
 174+ $msg = $wgParser->recursiveTagParse( wfMsgReplaceArgs( $this->mOptions['ok text'], array( $formedit->mTarget, $formedit->mForm ) ) );
176175
177176 $result = new AjaxResponse( $msg );
178177 return $result;
@@ -180,26 +179,26 @@
181180
182181 private function parseDataFromHTMLFrag ( &$data, $html, $formID ) {
183182 $doc = new DOMDocument();
184 - @$doc -> loadHTML(
 183+ @$doc->loadHTML(
185184 '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body>'
186185 . $html
187186 . '</body></html>'
188187 );
189188
190 - $form = $doc -> getElementById( $formID );
 189+ $form = $doc->getElementById( $formID );
191190
192191 if ( !$form ) {
193192 return null;
194193 }
195194
196195 // Process input tags
197 - $inputs = $form -> getElementsByTagName( 'input' );
 196+ $inputs = $form->getElementsByTagName( 'input' );
198197
199 - for ( $i = 0; $i < $inputs -> length; $i++ ) {
 198+ for ( $i = 0; $i < $inputs->length; $i++ ) {
200199
201 - $input = $inputs -> item( $i );
202 - $type = $input -> getAttribute( 'type' );
203 - $name = trim( $input -> getAttribute( 'name' ) );
 200+ $input = $inputs->item( $i );
 201+ $type = $input->getAttribute( 'type' );
 202+ $name = trim( $input->getAttribute( 'name' ) );
204203
205204 if ( !$name )
206205 continue;
@@ -210,8 +209,8 @@
211210 switch ( $type ) {
212211 case 'checkbox':
213212 case 'radio':
214 - if ( $input -> getAttribute( 'checked' ) )
215 - $this -> addToArray( $data, $name, $input -> getAttribute( 'value' ) );
 213+ if ( $input->getAttribute( 'checked' ) )
 214+ $this->addToArray( $data, $name, $input->getAttribute( 'value' ) );
216215 break;
217216
218217 //case 'button':
@@ -221,51 +220,51 @@
222221 //case 'reset':
223222 //case 'submit':
224223 case 'text':
225 - $this -> addToArray( $data, $name, $input -> getAttribute( 'value' ) );
 224+ $this->addToArray( $data, $name, $input->getAttribute( 'value' ) );
226225 break;
227226
228227 case 'submit':
229228 if ( $name == "wpSave" )
230 - $this -> addToArray( $data, $name, $input -> getAttribute( 'value' ) );
 229+ $this->addToArray( $data, $name, $input->getAttribute( 'value' ) );
231230 }
232231 }
233232
234233 // Process select tags
235 - $selects = $form -> getElementsByTagName( 'select' );
 234+ $selects = $form->getElementsByTagName( 'select' );
236235
237 - for ( $i = 0; $i < $selects -> length; $i++ ) {
 236+ for ( $i = 0; $i < $selects->length; $i++ ) {
238237
239 - $select = $selects -> item( $i );
240 - $name = trim( $select -> getAttribute( 'name' ) );
 238+ $select = $selects->item( $i );
 239+ $name = trim( $select->getAttribute( 'name' ) );
241240
242241 if ( !$name )
243242 continue;
244243
245244 $values = array( );
246 - $options = $select -> getElementsByTagName( 'option' );
 245+ $options = $select->getElementsByTagName( 'option' );
247246
248 - if ( count( $options ) && (!$select -> hasAttribute( "multiple" ) || $options -> item( 0 ) -> hasAttribute( 'selected' ) ) ) {
249 - $this -> addToArray( $data, $name, $options -> item( 0 ) -> getAttribute( 'value' ) );
 247+ if ( count( $options ) && (!$select->hasAttribute( "multiple" ) || $options->item( 0 )->hasAttribute( 'selected' ) ) ) {
 248+ $this->addToArray( $data, $name, $options->item( 0 )->getAttribute( 'value' ) );
250249 }
251250
252 - for ( $o = 1; $o < $options -> length; $o++ ) {
253 - if ( $options -> item( $o ) -> hasAttribute( 'selected' ) )
254 - $this -> addToArray( $data, $name, $options -> item( $o ) -> getAttribute( 'value' ) );
 251+ for ( $o = 1; $o < $options->length; $o++ ) {
 252+ if ( $options->item( $o )->hasAttribute( 'selected' ) )
 253+ $this->addToArray( $data, $name, $options->item( $o )->getAttribute( 'value' ) );
255254 }
256255 }
257256
258257 // Process textarea tags
259 - $textareas = $form -> getElementsByTagName( 'textarea' );
 258+ $textareas = $form->getElementsByTagName( 'textarea' );
260259
261 - for ( $i = 0; $i < $textareas -> length; $i++ ) {
 260+ for ( $i = 0; $i < $textareas->length; $i++ ) {
262261
263 - $textarea = $textareas -> item( $i );
264 - $name = trim( $textarea -> getAttribute( 'name' ) );
 262+ $textarea = $textareas->item( $i );
 263+ $name = trim( $textarea->getAttribute( 'name' ) );
265264
266265 if ( !$name )
267266 continue;
268267
269 - $this -> addToArray( $data, $name, $textarea -> textContent );
 268+ $this->addToArray( $data, $name, $textarea->textContent );
270269 }
271270
272271 return $form;
@@ -278,11 +277,12 @@
279278 * @global $wgOut
280279 * @param Array $data
281280 * @param String $queryString
282 - * @param Boolean $expand If this is set to true, field values will get urldecoded and expanded
283 - * This allows to slip parser functions by the MW parser on page creation
284 - * (by urlencoding them) and to pass them to autoedit to expand them now.
285 - * Expanding parser functions on page creation already might lead to cache
286 - * issues, e.g. for the {{#time:}} parser function
 281+ * @param Boolean $expand If this is set to true, field values will get
 282+ * urldecoded and expanded. This allows us to have parser functions
 283+ * skip handling by the MW parser on page creation (since they will be
 284+ * URL-encoded), and to instead have them handled by #autoedit.
 285+ * This can prevent cache issues, such as when dealing with the
 286+ * {{#time:}} parser function.
287287 * @return <type>
288288 */
289289 private function parseDataFromQueryString ( &$data, $queryString, $expand = false ) {
@@ -291,15 +291,15 @@
292292 foreach ( $params as $i => $param ) {
293293 $elements = explode( '=', $param, 2 );
294294
295 - $key = trim( urldecode( $elements[ 0 ] ) );
296 - $value = count( $elements ) > 1 ? urldecode( $elements[ 1 ] ) : null;
 295+ $key = trim( urldecode( $elements[0] ) );
 296+ $value = count( $elements ) > 1 ? urldecode( $elements[1] ) : null;
297297
298298 if ( $key == "query string" ) {
299 - $this -> parseDataFromQueryString( $data, $value, $expand );
 299+ $this->parseDataFromQueryString( $data, $value, $expand );
300300 } elseif ( $expand ) {
301 - $this -> addToArray( $data, $key, $value );
 301+ $this->addToArray( $data, $key, $value );
302302 } else {
303 - $this -> addToArray( $data, $key, $value );
 303+ $this->addToArray( $data, $key, $value );
304304 }
305305 }
306306
@@ -316,20 +316,20 @@
317317
318318 if ( preg_match( '/^([^\[\]]*)\[([^\[\]]*)\](.*)/', $key, $matches ) ) {
319319
320 - $key = str_replace( ' ', '_', $matches[ 1 ] );
 320+ $key = str_replace( ' ', '_', $matches[1] );
321321 $key = str_replace( '.', '_', $key );
322322
323323 if ( !array_key_exists( $key, $array ) )
324 - $array[ $key ] = array( );
 324+ $array[$key] = array( );
325325
326 - $this -> addToArray( $array[ $key ], $matches[ 2 ] . $matches[ 3 ], $value );
 326+ $this->addToArray( $array[$key], $matches[2] . $matches[3], $value );
327327 } else {
328328
329329 if ( $key ) {
330330 // $key = str_replace( ' ', '_', $key );
331331 // $key = str_replace( '.', '_', $key );
332332 // var_dump($key);
333 - $array[ $key ] = $value;
 333+ $array[$key] = $value;
334334 } else {
335335 array_push( $array, $value );
336336 }
@@ -360,10 +360,10 @@
361361 $merged = $array1;
362362
363363 foreach ( $array2 as $key => &$value ) {
364 - if ( is_array( $value ) && isset( $merged [ $key ] ) && is_array( $merged [ $key ] ) ) {
365 - $merged [ $key ] = $this -> array_merge_recursive_distinct( $merged [ $key ], $value );
 364+ if ( is_array( $value ) && isset( $merged[$key] ) && is_array( $merged[$key] ) ) {
 365+ $merged[$key] = $this->array_merge_recursive_distinct( $merged[$key], $value );
366366 } else {
367 - $merged [ $key ] = $value;
 367+ $merged[$key] = $value;
368368 }
369369 }
370370