r108696 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108695‎ | r108696 | r108697 >
Date:23:31, 11 January 2012
Author:preilly
Status:resolved (Comments)
Tags:mobile 
Comment:
load options directly from a foreign wiki
Modified paths:
  • /trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php
@@ -336,16 +336,21 @@
337337 return true;
338338 }
339339
 340+ /**
 341+ * Returns the carrier options array parsed from a valid wiki page
 342+ *
 343+ * @return Array
 344+ */
340345 private static function createCarrierOptionsFromWikiText() {
341346 global $wgMemc;
342347 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+
347353 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 );
350355 $carrierOptions = $wgMemc->get( $key );
351356 } else {
352357 $carrierOptions = null;
@@ -355,7 +360,7 @@
356361 $carrierOptions = array();
357362 $lines = array();
358363 if ( $rev ) {
359 - $lines = explode( "\n", $rev->getRawText() );
 364+ $lines = explode( "\n", $rev );
360365 }
361366 if ( $lines && count( $lines ) > 0 ) {
362367 $sizeOfLines = sizeof( $lines );
@@ -386,20 +391,79 @@
387392 }
388393
389394 /**
390 - * Returns the language options array parsed from a valid Wiki page
 395+ * Returns the foreign wiki options array from a valid wiki page
391396 *
392397 * @return Array
393398 */
 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+ */
394459 private static function createLanguageOptionsFromWikiText() {
395460 global $wgMemc;
396461 wfProfileIn( __METHOD__ );
397462 $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+
401466 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 );
404468 $languageOptions = $wgMemc->get( $key );
405469 } else {
406470 $languageOptions = null;
@@ -409,7 +473,7 @@
410474 $languageOptions = array();
411475 $lines = array();
412476 if ( $rev ) {
413 - $lines = explode( "\n", $rev->getRawText() );
 477+ $lines = explode( "\n", $rev );
414478 }
415479 if ( $lines && count( $lines ) > 0 ) {
416480 $sizeOfLines = sizeof( $lines );

Follow-up revisions

RevisionCommit summaryAuthorDate
r109625Greatly simplify http request from r108696demon16:12, 20 January 2012

Comments

#Comment by MaxSem (talk | contribs)   11:45, 19 January 2012

Why not just Http::get()?

#Comment by Preilly (talk | contribs)   23:50, 19 January 2012

Well, what's wrong with doing it this way?

#Comment by MaxSem (talk | contribs)   07:39, 20 January 2012

1 line instead of 4 and much smaller chance that it will break in the future.

Status & tagging log