jenkins-bot has submitted this change and it was merged.

Change subject: Consolidated config, fixed bug in addWarning()
......................................................................


Consolidated config, fixed bug in addWarning()

* One method to determine if config is applicable to the current request
* Incorrect placement of the return statement in the addWarning()
* Spacing in a few places

Change-Id: I05a099db2f23f17f2aab27389e89e7bf1ab503ff
---
M includes/PageRenderingHooks.php
1 file changed, 88 insertions(+), 77 deletions(-)

Approvals:
  Dr0ptp4kt: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/PageRenderingHooks.php b/includes/PageRenderingHooks.php
index d538878..299460e 100644
--- a/includes/PageRenderingHooks.php
+++ b/includes/PageRenderingHooks.php
@@ -43,37 +43,38 @@
                global $wgRequest;
 
                $config = self::getConfig();
+               if ( $config === null ) {
+                       return true;
+               }
 
-               if ( $config !== null ) {
-                       $skin = new SkinTemplate();
-                       $redirectWarning = 
'&renderZeroRatedRedirect=true&returnto=';
-                       $pattern = '/href=[\'"](.*?)[\'"]/';
+               $skin = new SkinTemplate();
+               $redirectWarning = '&renderZeroRatedRedirect=true&returnto=';
+               $pattern = '/href=[\'"](.*?)[\'"]/';
 
-                       $licenseText = wfMessage( 
'mobile-frontend-footer-license' )->parse();
-                       $privacyText = $skin->footerLink( 
'mobile-frontend-privacy-link-text', 'privacypage' );
-                       $termsText = wfMessage( 
'mobile-frontend-terms-use-text' )->parse();
+               $licenseText = wfMessage( 'mobile-frontend-footer-license' 
)->parse();
+               $privacyText = $skin->footerLink( 
'mobile-frontend-privacy-link-text', 'privacypage' );
+               $termsText = wfMessage( 'mobile-frontend-terms-use-text' 
)->parse();
 
-                       $licenseText = self::addWarning( $wgRequest, $pattern, 
$licenseText, $redirectWarning ) ;
-                       $privacyText = self::addWarning( $wgRequest, $pattern, 
$privacyText, $redirectWarning );
-                       $termsText = self::addWarning( $wgRequest, $pattern, 
$termsText, $redirectWarning );
+               $licenseText = self::addWarning( $wgRequest, $pattern, 
$licenseText, $redirectWarning ) ;
+               $privacyText = self::addWarning( $wgRequest, $pattern, 
$privacyText, $redirectWarning );
+               $termsText = self::addWarning( $wgRequest, $pattern, 
$termsText, $redirectWarning );
 
-                       $template->set( 'mobile-license', $licenseText );
-                       $template->set( 'privacy', $privacyText );
-                       $template->set( 'terms-use', $termsText );
+               $template->set( 'mobile-license', $licenseText );
+               $template->set( 'privacy', $privacyText );
+               $template->set( 'terms-use', $termsText );
 
-                       $redirectWarningQPS = 
'?renderZeroRatedRedirect=true&returnto=';
+               $redirectWarningQPS = '?renderZeroRatedRedirect=true&returnto=';
 
-                       if (isset( $template->data['language_urls'] )
-                               && 0 < count( $template->data['language_urls'] )
-                       ) {
-                               $languageUrls = 
$template->data['language_urls'];
+               if ( isset( $template->data['language_urls'] )
+                       && 0 < count( $template->data['language_urls'] )
+               ) {
+                       $languageUrls = $template->data['language_urls'];
 
-                               foreach ($languageUrls as &$lang ) {
-                                       $lang['href'] = $redirectWarningQPS . 
urlencode( $lang['href'] );
-                               }
-
-                               $template->set( 'language_urls', $languageUrls 
);
+                       foreach ( $languageUrls as &$lang ) {
+                               $lang['href'] = $redirectWarningQPS . 
urlencode( $lang['href'] );
                        }
+
+                       $template->set( 'language_urls', $languageUrls );
                }
 
                return true;
@@ -88,32 +89,18 @@
         * @return bool
         */
        public static function onBeforePageDisplay( &$out, &$options ) {
-               global $wgRequest, $wgConf, 
$wgEnableZeroRatedMobileAccessTesting;
-               wfProfileIn( __METHOD__ );
+               global $wgRequest;
 
-               // FIXME BUG -- why do we need master on every requests?!
-               $DB = wfGetDB( DB_MASTER );
-               $DBName = $DB->getDBname();
-               /** @noinspection PhpUnusedLocalVariableInspection */
-               list( $site, $lang ) = $wgConf->siteFromDB( $DBName );
-
-               // Determine if this is a Zero-related site shown with a mobile 
device
-               $isZero = ( $site === 'wikipedia' || 
$wgEnableZeroRatedMobileAccessTesting );
-               if ( $isZero ) {
-                       $useFormat = $wgRequest->getText( 'useformat' );
-                       $isZero = !empty( $_SERVER['HTTP_X_DEVICE'] ) || 
$useFormat === 'mobile' || $useFormat === 'mobile-wap';
-               }
-               if ( !$isZero ) {
-                       wfProfileOut( __METHOD__ );
+               $config = self::getConfig();
+               if ( $config === null ) {
                        return true;
                }
 
+               wfProfileIn( __METHOD__ );
                $out->addModuleStyles( 'mobile.zero.styles' );
                $out->addModules( 'mobile.zero.scripts' );
 
                $isFilePage = $out->getTitle()->inNamespace( NS_FILE );
-               $config = self::getConfig();
-
                $showBanner = $config !== null || $wgRequest->getFuzzyBool( 
'renderZeroRatedBanner' );
                if ( !$showBanner && self::isZeroRequest() ) {
                        $out->clearHTML();
@@ -267,15 +254,15 @@
                $carrierLink = str_replace(
                        '<a href="',
                        '<a style="color:' . $config['foreground'] . ';" 
href="',
-                       $carrierLink);
+                       $carrierLink );
                $customStyleNotifyClose = 'background-color:' . 
$config['background'] .
                        ';border: 2px solid ' . $config['foreground'] . ';';
-               $bannerText = Html::rawElement('span',
+               $bannerText = Html::rawElement( 'span',
                        array(
                                'class' => 'mw-mf-message',
-                               'id' => 'zero-rated-banner-text', 'style' => 
$customStyleFontSize),
-                       $carrierLink);
-               $banner = Html::rawElement('div',
+                               'id' => 'zero-rated-banner-text', 'style' => 
$customStyleFontSize ),
+                       $carrierLink );
+               $banner = Html::rawElement( 'div',
                        array(
                                'class' => 'mw-mf-banner',
                                'style' => ( $lang === null ? 'display:none;' : 
'' ) . $customStyle,
@@ -283,8 +270,8 @@
                        ),
                        '<button style="padding:0.5em;' . 
$customStyleNotifyClose . $customStyle .
                                '" class="notify-close" title="' .
-                               
wfMessage('zero-rated-mobile-access-dismiss-notification')->escaped() .
-                               '">×</button>' . $bannerText);
+                               wfMessage( 
'zero-rated-mobile-access-dismiss-notification' )->escaped() .
+                               '">×</button>' . $bannerText );
                return $banner;
        }
 
@@ -462,44 +449,68 @@
                }
 
                wfProfileIn( __METHOD__ );
-               global $wgRequest;
+               global $wgRequest, $wgConf, $wgDBname, 
$wgEnableZeroRatedMobileAccessTesting;
 
                $config = null;
+               list( $site, $langCode ) = $wgConf->siteFromDB( $wgDBname );
 
-               // Allow URL override of the X-CS parameter for testing purposes
-               $id = $wgRequest->getVal( 'X-CS' );
-               if ( $id === null ) {
-                       $id = $wgRequest->getHeader( 'X-CS' );
+               // Determine if this is a Zero-related site shown with a mobile 
device
+               $isZero = ( $site === 'wikipedia' || 
$wgEnableZeroRatedMobileAccessTesting );
+               if ( $isZero ) {
+                       $useFormat = $wgRequest->getText( 'useformat' );
+                       $isZero = !empty( $_SERVER['HTTP_X_DEVICE'] ) || 
$useFormat === 'mobile' || $useFormat === 'mobile-wap';
                }
-               if ( $id === '(null)' || !$id ) {
-                       $id = null;
-               }
-               if ( $id !== null ) {
-                       $store = new CarrierConfigStore( $id );
-                       $text = $store->get();
-                       if ( $text !== false ) {
-                               $conf = new CarrierConfig( $text );
-                               if ( !$conf->isError() ) {
-                                       $config = $conf->getData();
-                                       if ( !$config['enabled'] ) {
-                                               // If the configuration is 
disabled, pretend like it doesn't exist
-                                               $config = null;
-                                       } else {
-                                               // Check if the wiki is in the 
allowed list
-                                               // @TODO: expand to allow 
non-wikipedia sites
-                                               $site = strtolower( 
$wgRequest->getHeader( 'X-SUBDOMAIN' ) ) . '.wiki';
-                                               if ( !in_array( $site, 
$config['sites'] ) ) {
+               if ( $isZero ) {
+                       // Allow URL override of the X-CS parameter for testing 
purposes
+                       $id = $wgRequest->getVal( 'X-CS' );
+                       if ( $id === null ) {
+                               $id = $wgRequest->getHeader( 'X-CS' );
+                       }
+                       if ( $id === '(null)' || !$id ) {
+                               $id = null;
+                       }
+                       if ( $id !== null ) {
+                               $store = new CarrierConfigStore( $id );
+                               $text = $store->get();
+                               if ( $text !== false ) {
+                                       $conf = new CarrierConfig( $text );
+                                       if ( !$conf->isError() ) {
+                                               $config = $conf->getData();
+                                               $subdomain = 
$wgRequest->getHeader( 'X-SUBDOMAIN' );
+                                               if ( !self::isApplicable( 
$config, $subdomain, $langCode ) ) {
                                                        $config = null;
                                                }
                                        }
                                }
                        }
                }
-
                wfProfileOut( __METHOD__ );
                return $config;
        }
 
+
+       static function isApplicable( $config, $subdomain, $langCode ) {
+               if ( !$config['enabled'] ) {
+                       // If the configuration is disabled, pretend like it 
doesn't exist
+                       return false;
+               }
+               // Check if the wiki is in the allowed list
+               // @TODO: expand to allow non-wikipedia sites
+               $site = strtolower( $subdomain ) . '.wiki';
+               if ( !in_array( $site, $config['sites'] ) ) {
+                       return false;
+               }
+               $freeLangs = $config['whitelistedLangs'];
+               if ( 0 !== count( $freeLangs ) ) {
+                       if ( $langCode === '' || $langCode === 'test' ) {
+                               $langCode = 'en'; // Useful for debugging, 
should never be the case in production
+                       }
+                       if ( !in_array( $langCode, $freeLangs ) ) {
+                               return false;
+                       }
+               }
+               return true;
+       }
 
        /**
         * Returns the Html of a page with the various links appended with zero 
partner parameter
@@ -635,20 +646,20 @@
        /**
         * Adds parameters to URLs. Helper for onMinervaPreRender( 
\BaseTemplate &$template )
         * @param \WebRequest $request The request object that will contain the 
request path.
-        * @param $pattern The regular expression string capturing the URI.
-        * @param $link The full link, usually parsed wikitext.
-        * @param $redirectQuery The name-value query param string to add to 
the URI.
+        * @param string $pattern The regular expression string capturing the 
URI.
+        * @param string $link The full link, usually parsed wikitext.
+        * @param string $redirectQuery The name-value query param string to 
add to the URI.
         * @return mixed The link with URIs rewritten.
         */
        private static function addWarning( \WebRequest $request, $pattern, 
$link, $redirectQuery ) {
                if ( preg_match( $pattern, $link, $match ) ) {
                        $link = str_replace(
                                $match[1],
-                               $request->appendQuery( $redirectQuery ) . 
urlencode($match[1]),
+                               $request->appendQuery( $redirectQuery ) . 
urlencode( $match[1] ),
                                $link
                        );
-                       return $link;
                }
+               return $link;
        }
 
 

-- 
To view, visit https://gerrit.wikimedia.org/r/65164
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I05a099db2f23f17f2aab27389e89e7bf1ab503ff
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/ZeroRatedMobileAccess
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>
Gerrit-Reviewer: Dr0ptp4kt <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to