Yurik has uploaded a new change for review. https://gerrit.wikimedia.org/r/58875
Change subject: Use JSON carrier settings for banner localization ...................................................................... Use JSON carrier settings for banner localization * Instead of taking carrier banner and name from the i18n settings, use JSON blob's carrier settings. Will use language fallbacks. * Remove CarrierId (no longer needed) Change-Id: I2871ff8c89fa729f0464cc5eda972622eb7054f0 --- M ZeroRatedMobileAccess.php M includes/CarrierConfig.php M includes/PageRenderingHooks.php 3 files changed, 85 insertions(+), 42 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ZeroRatedMobileAccess refs/changes/75/58875/1 diff --git a/ZeroRatedMobileAccess.php b/ZeroRatedMobileAccess.php index 857a96b..7274934 100644 --- a/ZeroRatedMobileAccess.php +++ b/ZeroRatedMobileAccess.php @@ -20,7 +20,7 @@ 'path' => __FILE__, 'name' => 'ZeroRatedMobileAccess', 'version' => '1.1.0', - 'author' => array( 'Patrick Reilly' ), + 'author' => array( 'Patrick Reilly', 'Yuri Astrakhan' ), 'descriptionmsg' => 'zero-rated-mobile-access-desc', 'url' => 'https://www.mediawiki.org/wiki/Extension:ZeroRatedMobileAccess', ); diff --git a/includes/CarrierConfig.php b/includes/CarrierConfig.php index 2aa44fc..e0005c7 100644 --- a/includes/CarrierConfig.php +++ b/includes/CarrierConfig.php @@ -42,22 +42,18 @@ * @return array|bool: Content array or false if irretrievable. */ public function get() { - global $wgZeroRatedMobileAccessDisableCache; - if ( $this->content ) { return $this->content; } - if ( !$wgZeroRatedMobileAccessDisableCache ) { - $this->content = $this->memcGet(); - if ( $this->content ) { - return $this->content; - } + $this->content = $this->memcGet(); + if ( $this->content ) { + return $this->content; } $this->content = $this->httpGetAndValidate(); - if ( !$wgZeroRatedMobileAccessDisableCache && $this->content ) { + if ( $this->content ) { $this->memcSet(); } @@ -70,6 +66,10 @@ * @return array|bool Carrier config or false if not in cache. */ private function memcGet() { + global $wgZeroRatedMobileAccessDisableCache; + if ( $wgZeroRatedMobileAccessDisableCache ) { + return false; + } return $this->cache->get( $this->key ); } @@ -78,6 +78,10 @@ * Store content in memcached. */ private function memcSet() { + global $wgZeroRatedMobileAccessDisableCache; + if ( $wgZeroRatedMobileAccessDisableCache ) { + return true; + } return $this->cache->set( $this->key, $this->content ); } @@ -86,6 +90,10 @@ * Delete any cached information related to this config */ public function resetCache() { + global $wgZeroRatedMobileAccessDisableCache; + if ( $wgZeroRatedMobileAccessDisableCache ) { + return; + } $this->cache->delete( $this->key ); $this->cache->delete( $this->key . ':lock' ); } @@ -96,6 +104,10 @@ * @return bool: Whether lock was successfully acquired. */ private function lock() { + global $wgZeroRatedMobileAccessDisableCache; + if ( $wgZeroRatedMobileAccessDisableCache ) { + return true; + } return $this->cache->add( $this->key . ':lock', 1, self::LOCK_TIMEOUT ); } @@ -108,7 +120,7 @@ global $wgZeroRatedMobileAccessConfigIndexUri; if ( $wgZeroRatedMobileAccessConfigIndexUri === false ) { - wfWarn( '$wgZeroRatedMobileAccessConfigIndexUri is not set' ); + wfLogWarning( '$wgZeroRatedMobileAccessConfigIndexUri is not set' ); return false; } @@ -174,6 +186,7 @@ return count( $arr ) === count( array_filter( $arr, $filter ) ); } + public function validateConfig( $raw ) { $config = array(); @@ -209,17 +222,6 @@ $this->check( $config, $json, 'partnerId', 0, $issues, function( $v ) { return is_int( $v ) ? null : 'Must be an integer (optional)'; - } ); - - // Integer Carrier ID - $this->check( $config, $json, 'messageId', null, $issues, - function( $v ) { - return is_string( $v ) - && strlen( $v ) > 0 - && strtolower( $v ) === $v - && strpos( $v, ' ' ) === false - ? null - : "Must be a nonempty lowercase string with no spaces"; } ); //'showLangs' => null, // List of language codes to show on Zero page diff --git a/includes/PageRenderingHooks.php b/includes/PageRenderingHooks.php index e64e6c7..31ca93e 100644 --- a/includes/PageRenderingHooks.php +++ b/includes/PageRenderingHooks.php @@ -8,6 +8,7 @@ use FormatJson; use Language; use OutputPage; +use RawMessage; use ResourceLoader; use Title; @@ -37,6 +38,7 @@ public static $useFormat; public static $renderWarning = false; + /** * @param $out OutputPage * @param $options array @@ -48,6 +50,7 @@ $ext = new PageRenderingHooks(); return $ext->beforePageDisplayHTML( $out, $options ); } + /** * @param $subdomainTokenReplacement string @@ -63,6 +66,7 @@ } return true; } + /** * Handler for the BeforePageDisplay hook @@ -82,7 +86,6 @@ list( $site, $lang ) = $wgConf->siteFromDB( $DBName ); if ( $site == 'wikipedia' || $wgEnableZeroRatedMobileAccessTesting ) { - $xDevice = isset( $_SERVER['HTTP_X_DEVICE'] ) ? $_SERVER['HTTP_X_DEVICE'] : ''; self::$useFormat = $wgRequest->getText( 'useformat' ); @@ -142,15 +145,8 @@ self::$isFilePage = true; } - if ( self::$acceptBilling === 'no' ) { - $targetUrl = $wgRequest->getVal( 'returnto' ); - if ( $targetUrl ) { - $out->redirect( $targetUrl, '301' ); - $out->output(); - } - } - - if ( self::$acceptBilling === 'yes' ) { + // @BUG: weird code, need to check what was originally intended + if ( self::$acceptBilling === 'no' || self::$acceptBilling === 'yes' ) { $targetUrl = $wgRequest->getVal( 'returnto' ); if ( $targetUrl ) { $out->redirect( $targetUrl, '301' ); @@ -242,8 +238,7 @@ $parsedHtml = $this->parseLinksForZeroQueryString( $html ); $out->clearHTML(); $out->addHTML( $parsedHtml ); - $carrierLink = self::getCarrierLink(); - + $carrierLink = self::getCarrierLink( $lang ); if ( !self::$carrier['bannerWarning'] && !empty( $carrierLink ) ) { preg_match( '/<a href="(.+)">/', $carrierLink, $match ); @@ -353,16 +348,27 @@ return true; } - private static function getCarrierLink() { - global $wgRequest; - $url = self::$carrier['bannerUrl']; - $messageId = self::$carrier['messageId']; - $name = ucwords( - wfMessage( 'zero-rated-mobile-access-banner-carrier-name-' . $messageId )->escaped() - ); - $linkText = wfMessage( 'zero-rated-mobile-access-banner-text-' . $messageId ) - ->rawParams( $name )->escaped(); + private static function getCarrierLink( $lang ) { + global $wgRequest; + $names = self::$carrier['name']; + $banners = self::$carrier['banner']; + $url = self::$carrier['bannerUrl']; + + $fallbacks = null; + $name = self::pickLocalizedString( $names, $lang, $fallbacks ); + if ( $name === false ) { + return ''; + } + $name = new RawMessage( $name ); + $name = $name->escaped(); + $linkText = self::pickLocalizedString( $banners, $lang, $fallbacks ); + if ( $linkText === false ) { + return ''; + } + $linkText = new RawMessage( $linkText ); + $linkText = $linkText->rawParams( $name )->escaped(); + $billingURL = $wgRequest->appendQuery( 'renderZeroRatedBanner=true&renderwarning=yes&returnto=' . urlencode( $url ) ); @@ -371,6 +377,7 @@ $linkText ); return $carrierLink; } + /** * ResourceLoaderTestModules hook handler @@ -411,6 +418,7 @@ wfProfileOut( __METHOD__ ); return $data; } + /** * Returns the Html of a page with the various links appended with zero partner parameter @@ -492,6 +500,39 @@ return $output; } + + /** + * Find a message in a dictionary for the given language, + * or use language fallbacks if message is not defined. + * @param array $map Dictionary of languageCode => string + * @param string $lang language code + * @param array $fallbacks array of fallback messages (could be null, will be initialized) + * @return string|bool message from the dictionary or false if nothing found + */ + private static function pickLocalizedString( $map, $lang, &$fallbacks ) { + if ( empty( $lang ) ) { + // FIXME: Might need a better way to determine default language + global $wgLanguageCode; + $lang = $wgLanguageCode; + } + if ( array_key_exists( $lang, $map ) ) { + return $map[$lang]; + } + if ( $fallbacks === null ) { + $fallbacks = Language::getFallbacksFor( $lang ); + if ( count( $fallbacks ) === 0 ) { + $fallbacks = array( 'en' ); + } + } + foreach ( $fallbacks as $l ) { + if ( array_key_exists( $l, $map ) ) { + return $map[$l]; + } + } + return false; + } + + public function getVersion() { return __CLASS__ . ': $Id$'; } -- To view, visit https://gerrit.wikimedia.org/r/58875 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2871ff8c89fa729f0464cc5eda972622eb7054f0 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ZeroRatedMobileAccess Gerrit-Branch: master Gerrit-Owner: Yurik <yu...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits