Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | class ExtZeroRatedMobileAccess { |
5 | | - const VERSION = '0.0.8'; |
| 5 | + const VERSION = '0.0.9'; |
6 | 6 | |
7 | 7 | public static $renderZeroRatedLandingPage; |
8 | 8 | public static $renderZeroRatedBanner; |
— | — | @@ -326,12 +326,96 @@ |
327 | 327 | return true; |
328 | 328 | } |
329 | 329 | |
| 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 | + |
330 | 414 | /** |
331 | 415 | * Returns the carrier options array parsed from a valid wiki page |
332 | 416 | * |
333 | 417 | * @return Array |
334 | 418 | */ |
335 | | - private static function createCarrierOptionsFromWikiText() { |
| 419 | + private function createCarrierOptionsFromWikiText() { |
336 | 420 | global $wgMemc; |
337 | 421 | wfProfileIn( __METHOD__ ); |
338 | 422 | |
— | — | @@ -347,33 +431,19 @@ |
348 | 432 | } |
349 | 433 | |
350 | 434 | if ( !$carrierOptions ) { |
351 | | - $carrierOptions = array(); |
352 | | - $lines = array(); |
353 | 435 | 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 ); |
355 | 447 | } |
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 | | - } |
378 | 448 | $wgMemc->set( $key, $carrierOptions, self::getMaxAge() ); |
379 | 449 | } |
380 | 450 | wfProfileOut( __METHOD__ ); |
— | — | @@ -433,12 +503,25 @@ |
434 | 504 | return array( $key, $rev ); |
435 | 505 | } |
436 | 506 | |
| 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 | + |
437 | 520 | /** |
438 | 521 | * Returns the language options array parsed from a valid wiki page |
439 | 522 | * |
440 | 523 | * @return Array |
441 | 524 | */ |
442 | | - private static function createLanguageOptionsFromWikiText() { |
| 525 | + private function createLanguageOptionsFromWikiText() { |
443 | 526 | global $wgMemc; |
444 | 527 | wfProfileIn( __METHOD__ ); |
445 | 528 | $languageOptionsWikiPage = wfMsgForContent( 'zero-rated-mobile-access-language-options-wiki-page' ); |
— | — | @@ -456,29 +539,13 @@ |
457 | 540 | $languageOptions = array(); |
458 | 541 | $lines = array(); |
459 | 542 | 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 ); |
461 | 549 | } |
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 | | - } |
483 | 550 | $wgMemc->set( $key, $languageOptions, self::getMaxAge() ); |
484 | 551 | } |
485 | 552 | wfProfileOut( __METHOD__ ); |