r110091 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110090‎ | r110091 | r110092 >
Date:22:19, 26 January 2012
Author:reedy
Status:ok
Tags:mobile 
Comment:
Followup r110040, de-indent some of the code

Inverted a few inner ifs, if $formatter isn't an array, earlier

else if to elseif

Moved some code around to flip the indenting
Modified paths:
  • /trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php
@@ -326,76 +326,89 @@
327327 return true;
328328 }
329329
 330+ /**
 331+ * @param $formatter array
 332+ * @param $wikiText string
 333+ * @param $nChild bool
 334+ * @return array
 335+ */
330336 public function parseWikiTextToArray( Array $formatter, $wikiText, $nChild = false ) {
 337+ $options = array();
 338+ if ( !is_array( $formatter ) ) {
 339+ return $options;
 340+ }
331341 wfProfileIn( __METHOD__ );
332 - $options = array();
333342 $data = explode( PHP_EOL, $wikiText );
334 - if ( is_array( $formatter ) && !$nChild ) {
335 - $arrayKeys = array_keys( $formatter );
336 - $keyCount = count( $arrayKeys );
337 - $index = 0;
 343+ if( $nChild ) {
338344 foreach ( $data as $key => $rawData ) {
339 - $index = ( intval( $key ) % $keyCount === 0 ) ? 0 : $index + 1;
340 - if ( in_array( $index, $arrayKeys ) ) {
 345+ if ( strpos( $rawData, '*' ) === 0 && strpos( $rawData, '**' ) !== 0 && $key >= 0 ) {
341346 $data = trim( str_replace( '*', '', $rawData ) );
342 - if ( is_array( $formatter[$index] ) ) {
343 - $name = $formatter[$index]['name'];
344 - if ( isset( $formatter[$index]['callback'] ) ) {
345 - $callback = $formatter[$index]['callback'];
346 - if ( method_exists( $this, $callback ) ) {
347 - if ( isset( $formatter[$index]['parameters'] ) ) {
348 - if ( is_array( $formatter[$index]['parameters'] ) ) {
349 - $parameters = array();
350 - foreach ( $formatter[$index]['parameters'] as $parameter ) {
351 - if ( isset( $options[$prefixName][$parameter] ) ) {
352 - $parameters[$parameter] = $options[$prefixName][$parameter];
353 - }
354 - }
355 - $data = $this->$callback( $data, $parameters );
356 - } else {
357 - $parameter = $formatter[$index]['parameters'];
358 - if ( isset( $options[$prefixName][$parameter] ) ) {
359 - $parameterValue = $options[$prefixName][$parameter];
360 - $data = $this->$callback( $data, $parameterValue );
361 - }
362 - }
363 - } else {
364 - $data = $this->$callback( $data );
365 - }
366 - }
 347+ $prefixName = strtoupper( $data );
 348+ $options[$prefixName] = '';
 349+ } elseif ( strpos( $rawData, '**' ) === 0 && $key > 0 ) {
 350+ $data = trim( str_replace( '*', '', $rawData ) );
 351+ if ( !is_array( $formatter ) ) {
 352+ $options[$prefixName][] = $data;
 353+ continue;
 354+ }
 355+ if ( !isset( $formatter[0]['callback'] ) ) {
 356+ continue;
 357+ }
 358+ $callback = $formatter[0]['callback'];
 359+ if ( method_exists( $this, $callback ) ) {
 360+ $data = $this->$callback( $data );
 361+ if ( $data ) {
 362+ $options[$prefixName][] = $data;
367363 }
368 - } else {
369 - $name = $formatter[$index];
370364 }
371 - if ( $index === 0 ) {
372 - $prefixName = strtoupper( $data );
373 - }
374 - $options[$prefixName][$name] = $data;
375365 }
376366 }
377 - } else if ( is_array( $formatter ) && $nChild ) {
378 - foreach ( $data as $key => $rawData ) {
379 - if ( strpos( $rawData, '*' ) === 0 && strpos( $rawData, '**' ) !== 0 && $key >= 0 ) {
380 - $data = trim( str_replace( '*', '', $rawData ) );
381 - $prefixName = strtoupper( $data );
382 - $options[$prefixName] = '';
383 - } else if ( strpos( $rawData, '**' ) === 0 && $key > 0 ) {
384 - $data = trim( str_replace( '*', '', $rawData ) );
385 - if ( is_array( $formatter ) ) {
386 - if ( isset( $formatter[0]['callback'] ) ) {
387 - $callback = $formatter[0]['callback'];
388 - if ( method_exists( $this, $callback ) ) {
389 - $data = $this->$callback( $data );
390 - if ( $data ) {
391 - $options[$prefixName][] = $data;
 367+ wfProfileOut( __METHOD__ );
 368+ return $options;
 369+ }
 370+
 371+ $arrayKeys = array_keys( $formatter );
 372+ $keyCount = count( $arrayKeys );
 373+ $index = 0;
 374+ foreach ( $data as $key => $rawData ) {
 375+ $index = ( intval( $key ) % $keyCount === 0 ) ? 0 : $index + 1;
 376+ if ( !in_array( $index, $arrayKeys ) ) {
 377+ continue;
 378+ }
 379+ $data = trim( str_replace( '*', '', $rawData ) );
 380+ if ( is_array( $formatter[$index] ) ) {
 381+ $name = $formatter[$index]['name'];
 382+ if ( isset( $formatter[$index]['callback'] ) ) {
 383+ $callback = $formatter[$index]['callback'];
 384+ if ( method_exists( $this, $callback ) ) {
 385+ if ( isset( $formatter[$index]['parameters'] ) ) {
 386+ if ( is_array( $formatter[$index]['parameters'] ) ) {
 387+ $parameters = array();
 388+ foreach ( $formatter[$index]['parameters'] as $parameter ) {
 389+ if ( isset( $options[$prefixName][$parameter] ) ) {
 390+ $parameters[$parameter] = $options[$prefixName][$parameter];
 391+ }
392392 }
 393+ $data = $this->$callback( $data, $parameters );
 394+ } else {
 395+ $parameter = $formatter[$index]['parameters'];
 396+ if ( isset( $options[$prefixName][$parameter] ) ) {
 397+ $parameterValue = $options[$prefixName][$parameter];
 398+ $data = $this->$callback( $data, $parameterValue );
 399+ }
393400 }
 401+ } else {
 402+ $data = $this->$callback( $data );
394403 }
395 - } else {
396 - $options[$prefixName][] = $data;
397404 }
398405 }
 406+ } else {
 407+ $name = $formatter[$index];
399408 }
 409+ if ( $index === 0 ) {
 410+ $prefixName = strtoupper( $data );
 411+ }
 412+ $options[$prefixName][$name] = $data;
400413 }
401414 wfProfileOut( __METHOD__ );
402415 return $options;
@@ -435,12 +448,12 @@
436449 if ( !$carrierOptions ) {
437450 if ( $rev ) {
438451 $formatter = array(
439 - 0 => 'name',
440 - 1 => array( 'name' => 'link',
 452+ 0 => 'name',
 453+ 1 => array( 'name' => 'link',
441454 'callback' => 'createUrlCallback',
442455 'parameters' => 'name',
443456 ),
444 - 2 => array( 'name' => 'partnerId',
 457+ 2 => array( 'name' => 'partnerId',
445458 'callback' => 'intValCallback'
446459 ),
447460 );

Sign-offs

UserFlagDate
😂inspected16:32, 27 January 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r110040create generic method for parsing WikiText to array of optionspreilly00:32, 26 January 2012

Status & tagging log