Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php |
— | — | @@ -336,16 +336,21 @@ |
337 | 337 | return true; |
338 | 338 | } |
339 | 339 | |
| 340 | + /** |
| 341 | + * Returns the carrier options array parsed from a valid wiki page |
| 342 | + * |
| 343 | + * @return Array |
| 344 | + */ |
340 | 345 | private static function createCarrierOptionsFromWikiText() { |
341 | 346 | global $wgMemc; |
342 | 347 | wfProfileIn( __METHOD__ ); |
343 | | - $carrierOptionsWikiPage = wfMsg( 'zero-rated-mobile-access-carrier-options-wiki-page' ); |
344 | | - $title = Title::newFromText( $carrierOptionsWikiPage, NS_MEDIAWIKI ); |
345 | | - // Use the revision directly to prevent other hooks to be called |
346 | | - $rev = Revision::newFromTitle( $title ); |
| 348 | + |
| 349 | + $carrierOptionsWikiPage = wfMsgForContent( 'zero-rated-mobile-access-carrier-options-wiki-page' ); |
| 350 | + |
| 351 | + list( $revId, $rev ) = self::getOptionsFromForeignWiki( $carrierOptionsWikiPage ); |
| 352 | + |
347 | 353 | if ( $rev ) { |
348 | | - $sha1OfRev = $rev->getSize(); |
349 | | - $key = wfMemcKey( 'zero-rated-mobile-access-carrier-options', $sha1OfRev ); |
| 354 | + $key = wfMemcKey( 'zero-rated-mobile-access-carrier-options', $revId ); |
350 | 355 | $carrierOptions = $wgMemc->get( $key ); |
351 | 356 | } else { |
352 | 357 | $carrierOptions = null; |
— | — | @@ -355,7 +360,7 @@ |
356 | 361 | $carrierOptions = array(); |
357 | 362 | $lines = array(); |
358 | 363 | if ( $rev ) { |
359 | | - $lines = explode( "\n", $rev->getRawText() ); |
| 364 | + $lines = explode( "\n", $rev ); |
360 | 365 | } |
361 | 366 | if ( $lines && count( $lines ) > 0 ) { |
362 | 367 | $sizeOfLines = sizeof( $lines ); |
— | — | @@ -386,20 +391,79 @@ |
387 | 392 | } |
388 | 393 | |
389 | 394 | /** |
390 | | - * Returns the language options array parsed from a valid Wiki page |
| 395 | + * Returns the foreign wiki options array from a valid wiki page |
391 | 396 | * |
392 | 397 | * @return Array |
393 | 398 | */ |
| 399 | + private static function getOptionsFromForeignWiki( $pageName ) { |
| 400 | + global $wgMemc; |
| 401 | + wfProfileIn( __METHOD__ ); |
| 402 | + |
| 403 | + $key = null; |
| 404 | + $rev = null; |
| 405 | + |
| 406 | + if ( $pageName ) { |
| 407 | + |
| 408 | + $memcKey = wfMemcKey( 'zero-rated-mobile-access-foreign-options-', md5( $pageName ) ); |
| 409 | + $foreignOptions = $wgMemc->get( $memcKey ); |
| 410 | + |
| 411 | + if ( !$foreignOptions ) { |
| 412 | + $options = array(); |
| 413 | + $options['method'] = 'GET'; |
| 414 | + $url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&&rvlimit=1&rvprop=content&format=json&titles=MediaWiki:' . $pageName; |
| 415 | + $req = ( class_exists( 'MWHttpRequest' ) ) ? MWHttpRequest::factory( $url, $options ) : HttpRequest::factory( $url, $options ); |
| 416 | + |
| 417 | + $status = $req->execute(); |
| 418 | + |
| 419 | + if ( !$status->isOK() ) { |
| 420 | + $error = $req->getContent(); |
| 421 | + wfProfileOut( __METHOD__ ); |
| 422 | + return array( $key, $rev ); |
| 423 | + } |
| 424 | + |
| 425 | + $ret = $req->getContent(); |
| 426 | + |
| 427 | + $jsonData = FormatJson::decode( $ret, true ); |
| 428 | + |
| 429 | + if ( isset( $jsonData['query']['pages'] ) ) { |
| 430 | + $key = key( $jsonData['query']['pages'] ); |
| 431 | + if ( !is_int( $key ) ) { |
| 432 | + $key = null; |
| 433 | + } |
| 434 | + |
| 435 | + foreach( $jsonData['query']['pages'] as $pages ) { |
| 436 | + if ( isset( $pages['revisions'][0]['*'] ) ) { |
| 437 | + $rev = $pages['revisions'][0]['*']; |
| 438 | + } |
| 439 | + } |
| 440 | + } |
| 441 | + |
| 442 | + if ( $key && $rev ) { |
| 443 | + $wgMemc->set( $memcKey, array( $key, $rev ), self::getMaxAge() ); |
| 444 | + } |
| 445 | + } else { |
| 446 | + list ( $key, $rev ) = $foreignOptions; |
| 447 | + } |
| 448 | + } |
| 449 | + |
| 450 | + wfProfileOut( __METHOD__ ); |
| 451 | + return array( $key, $rev ); |
| 452 | + } |
| 453 | + |
| 454 | + /** |
| 455 | + * Returns the language options array parsed from a valid wiki page |
| 456 | + * |
| 457 | + * @return Array |
| 458 | + */ |
394 | 459 | private static function createLanguageOptionsFromWikiText() { |
395 | 460 | global $wgMemc; |
396 | 461 | wfProfileIn( __METHOD__ ); |
397 | 462 | $languageOptionsWikiPage = wfMsgForContent( 'zero-rated-mobile-access-language-options-wiki-page' ); |
398 | | - $title = Title::newFromText( $languageOptionsWikiPage, NS_MEDIAWIKI ); |
399 | | - // Use the revision directly to prevent other hooks to be called |
400 | | - $rev = Revision::newFromTitle( $title ); |
| 463 | + |
| 464 | + list( $revId, $rev ) = self::getOptionsFromForeignWiki( $languageOptionsWikiPage ); |
| 465 | + |
401 | 466 | if ( $rev ) { |
402 | | - $sha1OfRev = $rev->getSize(); |
403 | | - $key = wfMemcKey( 'zero-rated-mobile-access-language-options', $sha1OfRev ); |
| 467 | + $key = wfMemcKey( 'zero-rated-mobile-access-language-options', $revId ); |
404 | 468 | $languageOptions = $wgMemc->get( $key ); |
405 | 469 | } else { |
406 | 470 | $languageOptions = null; |
— | — | @@ -409,7 +473,7 @@ |
410 | 474 | $languageOptions = array(); |
411 | 475 | $lines = array(); |
412 | 476 | if ( $rev ) { |
413 | | - $lines = explode( "\n", $rev->getRawText() ); |
| 477 | + $lines = explode( "\n", $rev ); |
414 | 478 | } |
415 | 479 | if ( $lines && count( $lines ) > 0 ) { |
416 | 480 | $sizeOfLines = sizeof( $lines ); |