r110040 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110039‎ | r110040 | r110041 >
Date:00:32, 26 January 2012
Author:preilly
Status:ok (Comments)
Tags:mobile 
Comment:
create generic method for parsing WikiText to array of options
Modified paths:
  • /trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php
@@ -1,7 +1,7 @@
22 <?php
33
44 class ExtZeroRatedMobileAccess {
5 - const VERSION = '0.0.8';
 5+ const VERSION = '0.0.9';
66
77 public static $renderZeroRatedLandingPage;
88 public static $renderZeroRatedBanner;
@@ -326,12 +326,96 @@
327327 return true;
328328 }
329329
 330+ public function parseWikiTextToArray( Array $formatter, $wikiText, $nChild = false ) {
 331+ $options = array();
 332+ $data = explode( PHP_EOL, $wikiText );
 333+ if ( is_array( $formatter ) && !$nChild ) {
 334+ $arrayKeys = array_keys( $formatter );
 335+ $keyCount = count( $arrayKeys );
 336+ $index = 0;
 337+ foreach ( $data as $key => $rawData ) {
 338+ $index = ( intval( $key ) % $keyCount === 0 ) ? 0 : $index + 1;
 339+ if ( in_array( $index, $arrayKeys ) ) {
 340+ $data = trim( str_replace( '*', '', $rawData ) );
 341+ if ( is_array( $formatter[$index] ) ) {
 342+ $name = $formatter[$index]['name'];
 343+ if ( isset( $formatter[$index]['callback'] ) ) {
 344+ $callback = $formatter[$index]['callback'];
 345+ if ( method_exists( $this, $callback ) ) {
 346+ if ( isset( $formatter[$index]['parameters'] ) ) {
 347+ if ( is_array( $formatter[$index]['parameters'] ) ) {
 348+ $parameters = array();
 349+ foreach ( $formatter[$index]['parameters'] as $parameter ) {
 350+ if ( isset( $options[$prefixName][$parameter] ) ) {
 351+ $parameters[$parameter] = $options[$prefixName][$parameter];
 352+ }
 353+ }
 354+ $data = $this->$callback( $data, $parameters );
 355+ } else {
 356+ $parameter = $formatter[$index]['parameters'];
 357+ if ( isset( $options[$prefixName][$parameter] ) ) {
 358+ $parameterValue = $options[$prefixName][$parameter];
 359+ $data = $this->$callback( $data, $parameterValue );
 360+ }
 361+ }
 362+ } else {
 363+ $data = $this->$callback( $data );
 364+ }
 365+ }
 366+ }
 367+ } else {
 368+ $name = $formatter[$index];
 369+ }
 370+ if ( $index === 0 ) {
 371+ $prefixName = strtoupper( $data );
 372+ }
 373+ $options[$prefixName][$name] = $data;
 374+ }
 375+ }
 376+ } else if ( is_array( $formatter ) && $nChild ) {
 377+ foreach ( $data as $key => $rawData ) {
 378+ if ( strpos( $rawData, '*' ) === 0 && strpos( $rawData, '**' ) !== 0 && $key >= 0 ) {
 379+ $data = trim( str_replace( '*', '', $rawData ) );
 380+ $prefixName = strtoupper( $data );
 381+ $options[$prefixName] = '';
 382+ } else if ( strpos( $rawData, '**' ) === 0 && $key > 0 ) {
 383+ $data = trim( str_replace( '*', '', $rawData ) );
 384+ if ( is_array( $formatter ) ) {
 385+ if ( isset( $formatter[0]['callback'] ) ) {
 386+ $callback = $formatter[0]['callback'];
 387+ if ( method_exists( $this, $callback ) ) {
 388+ $data = $this->$callback( $data );
 389+ if ( $data ) {
 390+ $options[$prefixName][] = $data;
 391+ }
 392+ }
 393+ }
 394+ } else {
 395+ $options[$prefixName][] = $data;
 396+ }
 397+ }
 398+ }
 399+ }
 400+ return $options;
 401+ }
 402+
 403+ public function createUrlCallback( $url, $name ) {
 404+ $carrierLink = Html::rawElement( 'a',
 405+ array( 'href' => $url ),
 406+ $name );
 407+ return $carrierLink;
 408+ }
 409+
 410+ public function intValCallback( $int ) {
 411+ return intval( $int );
 412+ }
 413+
330414 /**
331415 * Returns the carrier options array parsed from a valid wiki page
332416 *
333417 * @return Array
334418 */
335 - private static function createCarrierOptionsFromWikiText() {
 419+ private function createCarrierOptionsFromWikiText() {
336420 global $wgMemc;
337421 wfProfileIn( __METHOD__ );
338422
@@ -347,33 +431,19 @@
348432 }
349433
350434 if ( !$carrierOptions ) {
351 - $carrierOptions = array();
352 - $lines = array();
353435 if ( $rev ) {
354 - $lines = explode( "\n", $rev );
 436+ $formatter = array(
 437+ 0 => 'name',
 438+ 1 => array( 'name' => 'link',
 439+ 'callback' => 'createUrlCallback',
 440+ 'parameters' => 'name',
 441+ ),
 442+ 2 => array( 'name' => 'partnerId',
 443+ 'callback' => 'intValCallback'
 444+ ),
 445+ );
 446+ $carrierOptions = $this->parseWikiTextToArray( $formatter, $rev );
355447 }
356 - if ( $lines && count( $lines ) > 0 ) {
357 - $sizeOfLines = sizeof( $lines );
358 - for ( $i = 0; $i < $sizeOfLines; $i++ ) {
359 - $line = $lines[$i];
360 - if ( strpos( $line, '*' ) === 0 && strpos( $line, '**' ) !== 0 && $i >= 0 ) {
361 - $carrierName = strtoupper( str_replace( '* ', '', $line ) );
362 - $carrierRaw = str_replace( '* ', '', $line );
363 - $carrierOptions[$carrierName] = '';
364 - $carrierOptions[$carrierName]['name'] = $carrierRaw;
365 - } elseif ( strpos( $line, '**' ) === 0 && $i > 0 ) {
366 - if ( $i % 3 === 1 ) {
367 - $carrierOptions[$carrierName]['url'] = trim( str_replace( '** ', '', $line ) );
368 - $carrierLink = Html::rawElement( 'a',
369 - array( 'href' => $carrierOptions[$carrierName]['url'] ),
370 - $carrierOptions[$carrierName]['name'] );
371 - $carrierOptions[$carrierName]['link'] = $carrierLink;
372 - } elseif ( $i % 3 === 2 ) {
373 - $carrierOptions[$carrierName]['partnerId'] = intval( trim( str_replace( '** ', '', $line ) ) );
374 - }
375 - }
376 - }
377 - }
378448 $wgMemc->set( $key, $carrierOptions, self::getMaxAge() );
379449 }
380450 wfProfileOut( __METHOD__ );
@@ -433,12 +503,25 @@
434504 return array( $key, $rev );
435505 }
436506
 507+ public function languagePercentageCallback( $data ) {
 508+ $languageArray = array();
 509+ $lineParts = explode( '#', $data );
 510+ $language = ( isset( $lineParts[0] ) ) ? trim( $lineParts[0] ) : trim( $data );
 511+ if ( $language !== 'portal' && $language !== 'other' ) {
 512+ $languageArray = ( isset( $lineParts[1] ) ) ?
 513+ array( 'language' => $language,
 514+ 'percentage' => intval( str_replace( '%', '', trim( $lineParts[1] ) ) ) ) :
 515+ $language;
 516+ }
 517+ return $languageArray;
 518+ }
 519+
437520 /**
438521 * Returns the language options array parsed from a valid wiki page
439522 *
440523 * @return Array
441524 */
442 - private static function createLanguageOptionsFromWikiText() {
 525+ private function createLanguageOptionsFromWikiText() {
443526 global $wgMemc;
444527 wfProfileIn( __METHOD__ );
445528 $languageOptionsWikiPage = wfMsgForContent( 'zero-rated-mobile-access-language-options-wiki-page' );
@@ -456,29 +539,13 @@
457540 $languageOptions = array();
458541 $lines = array();
459542 if ( $rev ) {
460 - $lines = explode( "\n", $rev );
 543+ $formatter = array(
 544+ 0 => array( 'name' => 'partnerId',
 545+ 'callback' => 'languagePercentageCallback'
 546+ ),
 547+ );
 548+ $languageOptions = $this->parseWikiTextToArray( $formatter, $rev, true );
461549 }
462 - if ( $lines && count( $lines ) > 0 ) {
463 - $sizeOfLines = sizeof( $lines );
464 - for ( $i = 0; $i < $sizeOfLines; $i++ ) {
465 - $line = $lines[$i];
466 - if ( strpos( $line, '*' ) === 0 && strpos( $line, '**' ) !== 0 && $i >= 0 ) {
467 - $countryName = strtoupper( str_replace( '* ', '', $line ) );
468 - $languageOptions[$countryName] = '';
469 - } elseif ( strpos( $line, '**' ) === 0 && $i > 0 ) {
470 - $lineParts = explode( '#', $line );
471 - $language = ( isset( $lineParts[0] ) ) ?
472 - trim( str_replace( '** ', '', $lineParts[0] ) ) :
473 - trim( str_replace( '** ', '', $line ) ) ;
474 - if ( $language !== 'portal' && $language !== 'other' ) {
475 - $languageOptions[$countryName][] = ( isset( $lineParts[1] ) ) ?
476 - array( 'language' => $language,
477 - 'percentage' => intval( str_replace( '%', '', trim( $lineParts[1] ) ) ) ) :
478 - $language;
479 - }
480 - }
481 - }
482 - }
483550 $wgMemc->set( $key, $languageOptions, self::getMaxAge() );
484551 }
485552 wfProfileOut( __METHOD__ );

Sign-offs

UserFlagDate
Awjrichardsinspected23:29, 27 January 2012

Follow-up revisions

RevisionCommit summaryAuthorDate
r110091Followup r110040, de-indent some of the code...reedy22:19, 26 January 2012
r110188fix for 110040 comment 30165preilly23:21, 27 January 2012

Comments

#Comment by Reedy (talk | contribs)   21:18, 26 January 2012

Would it be possible to indent this code any more?

#Comment by Awjrichards (talk | contribs)   23:19, 27 January 2012

You removed the static-ness of createLanguageOptionsFromWikiText(), however there is still a static call to the method on line 149:

$languageOptions = self::createLanguageOptionsFromWikiText();
#Comment by Preilly (talk | contribs)   23:22, 27 January 2012

This should now be fixed in r110188.

Status & tagging log