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

Change subject: Moved special page rendering to dedicated class
......................................................................


Moved special page rendering to dedicated class

Change-Id: If72ace549e4029c204d1deff9d915367fcaf3895
---
M includes/PageRenderingHooks.php
M includes/ZeroSpecialPage.php
2 files changed, 101 insertions(+), 103 deletions(-)

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



diff --git a/includes/PageRenderingHooks.php b/includes/PageRenderingHooks.php
index 1363055..9ba87cb 100644
--- a/includes/PageRenderingHooks.php
+++ b/includes/PageRenderingHooks.php
@@ -30,8 +30,6 @@
 
        // TODO: Clean up HTML concatenation. Review for any non-escaped user 
input.
 
-       private static $formatMobileUrl = '//%s.m.wikipedia.org/wiki/Main_Page';
-       private static $formatZeroUrl = 
'//%s.zero.wikipedia.org/wiki/Main_Page';
        private static $originParent = '.wikipedia.org/';
        private static $zerodotParent = '.zero.wikipedia.org/';
        private static $mdotParent = '.m.wikipedia.org/';
@@ -224,18 +222,6 @@
                        $out->addHTML( $parsedHtml );
                }
 
-               if ( $out->getTitle()->isSpecial( 'ZeroRatedMobileAccess' ) ) {
-                       $out->clearHTML();
-                       $out->setPageTitle( null );
-                       $output = self::renderSpecialPage( $config );
-                       if ( $output ) { // necessary in bizarre case of 
misconfiguraton
-                               // TODO: Remove unnecessary 'id' attributes, if 
applicable
-                               $output = Html::rawElement( 'div',
-                                       array( 'id' => 'zero-landing-page' ), 
$output );
-                               $out->addHTML( $output );
-                       }
-               }
-
                wfProfileOut( __METHOD__ );
                return true;
        }
@@ -351,6 +337,7 @@
         * @param $config
         * @param $request
         * @param $out
+        * @param bool $wap
         * @param bool $wap
         * @return null|string Warning banner if applicable, else null
         */
@@ -486,76 +473,6 @@
 
        /**
         * @param $config
-        * @return string
-        */
-       private static function renderSpecialPage( $config ) {
-               global $wgRequest;
-               $output = '';
-               $languageNames = Language::fetchLanguageNames();
-
-               if ( $config ) {
-                       $overrides = $config['langNameOverrides'];
-                       // Allow language name overrides, as requested by 
Orange Congo ('kg' => 'Kikongo')
-                       // Do it one at a time to keep the original order of 
the array
-                       foreach ( $overrides as $l => $n ) {
-                               $languageNames[$l] = $n;
-                       }
-
-                       foreach ( $config['showLangs'] as $languageCode ) {
-                               if ( !array_key_exists( $languageCode, 
$languageNames ) ) {
-                                       continue;
-                               }
-                               $languageLink = Html::element( 'a',
-                                       array( 'href' => self::getMainPage( 
$languageCode ) ),
-                                       wfMessage( 
'zero-rated-mobile-access-home-page-selection',
-                                               ucfirst( 
$languageNames[$languageCode] ) )->inLanguage( $languageCode )
-                               );
-                               $langDir = Language::factory( $languageCode 
)->getDir();
-                               $output .= Html::rawElement( 'h3', array( 'dir' 
=> $langDir ), $languageLink );
-                       }
-               }
-               $output .= Html::element( 'hr' );
-               $output .= wfMessage( 
'zero-rated-mobile-access-home-page-selection-text' )->escaped();
-               // TODO: Remove unnecessary 'id' attributes
-               $output .= Html::openElement( 'select',
-                       array( 'id' => 'languageselection',
-                               'onchange' => 'javascript:window.location = 
this.options[this.selectedIndex].value;',
-                       )
-               );
-               $output .= Html::element( 'option',
-                       array( 'value' => '' ),
-                       wfMessage( 
'zero-rated-mobile-access-language-selection' )->text()
-               );
-
-               $freeLangs = $config['whitelistedLangs'];
-               foreach ( $languageNames as $languageCode => $languageName ) {
-                       $isFree = ( 0 === count( $freeLangs ) || in_array( 
$languageCode, $freeLangs ) );
-                       if ( !$isFree || !self::disableImages() ) {
-                               // send user to the m subdomain
-                               $languageUrl = sprintf( self::$formatMobileUrl, 
$languageCode );
-                               if ( !$isFree ) {
-                                       // offer upgraded ux
-                                       $languageUrl = $wgRequest->appendQuery(
-                                               
'renderZeroRatedBanner=true&renderwarning=yes&returnto='
-                                               . urlencode( $languageUrl ) );
-                               }
-                       } else {
-                               // send low bandwidth user to whitelisted zero 
subdomain
-                               $languageUrl = sprintf( self::$formatZeroUrl, 
$languageCode );
-                       }
-
-                       $output .= Html::element( 'option',
-                               array( 'value' => $languageUrl ),
-                               $languageName
-                       );
-               }
-               $output .= Html::closeElement( 'select' );
-               return $output;
-       }
-
-
-       /**
-        * @param $config
         * @param Language $lang
         * @param string $sitename
         * @return bool|String
@@ -612,20 +529,6 @@
                        'scripts' => array( 'tests/js/test_banner.js' ),
                );
                return true;
-       }
-
-       /**
-        * Format URL to the zero or m main page for the specific language (or 
current if null)
-        * @param null|string $langCode
-        * @return string
-        */
-       public static function getMainPage( $langCode = null ) {
-               if ( $langCode === null ) {
-                       global $wgLang;
-                       $langCode = $wgLang->getCode();
-               }
-               $langUrlFormat = self::isZeroSubdomain() ? self::$formatZeroUrl 
: self::$formatMobileUrl;
-               return sprintf( $langUrlFormat, $langCode );
        }
 
        /**
@@ -706,7 +609,7 @@
         * Returns true if X-SUBDOMAIN header is set to 'ZERO'
         * @return bool
         */
-       private static function isZeroSubdomain() {
+       public static function isZeroSubdomain() {
                static $isZero = null;
                if ( $isZero === null ) {
                        global $wgRequest;
@@ -730,8 +633,8 @@
                if ( $isZero === null ) {
                        global $wgConf, $wgDBname, $wgRequest, 
$wgEnableZeroRatedMobileAccessTesting;
                        /** @noinspection PhpUnusedLocalVariableInspection */
+                       // @FIXME: remove this check - there should only be a 
global setting - enable/disable, no check for "wikipedia"
                        list( $site, $langCode ) = $wgConf->siteFromDB( 
$wgDBname );
-                       // @FIXME: expand to allow non-wikipedia sites
                        $isZero = ( $site === 'wikipedia' || 
$wgEnableZeroRatedMobileAccessTesting );
                        // To be zero must have either X-Device or useformat
                        if ( $isZero && $wgRequest->getHeader( 'X-DEVICE' ) === 
false ) {
@@ -861,7 +764,7 @@
         * This is a placeholder function to replace $wgZeroDisableImages which 
seems to always be 1 for Zero
         * @return bool
         */
-       private static function disableImages() {
+       public static function disableImages() {
                // @FIXME: Probably need to check the config as well
                return self::isZeroSubdomain();
        }
diff --git a/includes/ZeroSpecialPage.php b/includes/ZeroSpecialPage.php
index 2647aa6..675b927 100644
--- a/includes/ZeroSpecialPage.php
+++ b/includes/ZeroSpecialPage.php
@@ -1,7 +1,9 @@
 <?php
 
 namespace Extensions\ZeroRatedMobileAccess;
+use Language;
 use UnlistedSpecialPage;
+use Html;
 
 /**
  * Default startup page for ZeroRatedMobileAccess extension that shows the 
list of available languages.
@@ -17,6 +19,9 @@
                parent::__construct( 'ZeroRatedMobileAccess' );
        }
 
+       private static $formatMobileUrl = '//%s.m.wikipedia.org/wiki/Main_Page';
+       private static $formatZeroUrl = 
'//%s.zero.wikipedia.org/wiki/Main_Page';
+
        /**
         * Show the special page
         *
@@ -26,9 +31,99 @@
                $this->setHeaders();
                $config = PageRenderingHooks::getConfig();
 
-               if ( $config !== null && !$config['showZeroPage'] ) {
-                       $this->getOutput()->redirect( 
PageRenderingHooks::getMainPage(), '301' );
+               if ( $config === null ) {
                        return;
                }
+
+               if ( !$config['showZeroPage'] ) {
+                       $this->getOutput()->redirect( self::getMainPage(), 
'301' );
+                       return;
+               }
+
+               $out = $this->getOutput();
+               $out->setPageTitle( null );
+               $output = $this->renderSpecialPage( $config );
+               $out->addHTML( $output );
+       }
+
+       /**
+        * @param $config
+        * @return string
+        */
+       private function renderSpecialPage( $config ) {
+               global $wgRequest;
+               $output = '';
+               $languageNames = Language::fetchLanguageNames();
+
+               $overrides = $config['langNameOverrides'];
+               // Allow language name overrides, as requested by Orange Congo 
('kg' => 'Kikongo')
+               // Do it one at a time to keep the original order of the array
+               foreach ( $overrides as $l => $n ) {
+                       $languageNames[$l] = $n;
+               }
+
+               foreach ( $config['showLangs'] as $languageCode ) {
+                       if ( !array_key_exists( $languageCode, $languageNames ) 
) {
+                               continue;
+                       }
+                       $languageLink = Html::element( 'a',
+                               array( 'href' => self::getMainPage( 
$languageCode ) ),
+                               wfMessage( 
'zero-rated-mobile-access-home-page-selection',
+                                       ucfirst( $languageNames[$languageCode] 
) )->inLanguage( $languageCode )
+                       );
+                       $langDir = Language::factory( $languageCode )->getDir();
+                       $output .= Html::rawElement( 'h3', array( 'dir' => 
$langDir ), $languageLink );
+               }
+               $output .= Html::element( 'hr' );
+               $output .= wfMessage( 
'zero-rated-mobile-access-home-page-selection-text' )->escaped();
+               // TODO: Remove unnecessary 'id' attributes
+               $output .= Html::openElement( 'select',
+                       array( 'id' => 'languageselection',
+                               'onchange' => 'javascript:window.location = 
this.options[this.selectedIndex].value;',
+                       )
+               );
+               $output .= Html::element( 'option',
+                       array( 'value' => '' ),
+                       wfMessage( 
'zero-rated-mobile-access-language-selection' )->text()
+               );
+
+               $freeLangs = $config['whitelistedLangs'];
+               foreach ( $languageNames as $languageCode => $languageName ) {
+                       $isFree = ( 0 === count( $freeLangs ) || in_array( 
$languageCode, $freeLangs ) );
+                       if ( !$isFree || !PageRenderingHooks::disableImages() ) 
{
+                               // send user to the m subdomain
+                               $languageUrl = sprintf( self::$formatMobileUrl, 
$languageCode );
+                               if ( !$isFree ) {
+                                       // offer upgraded ux
+                                       $languageUrl = $wgRequest->appendQuery(
+                                               
'renderZeroRatedBanner=true&renderwarning=yes&returnto='
+                                               . urlencode( $languageUrl ) );
+                               }
+                       } else {
+                               // send low bandwidth user to whitelisted zero 
subdomain
+                               $languageUrl = sprintf( self::$formatZeroUrl, 
$languageCode );
+                       }
+
+                       $output .= Html::element( 'option',
+                               array( 'value' => $languageUrl ),
+                               $languageName
+                       );
+               }
+               $output .= Html::closeElement( 'select' );
+               return $output;
+       }
+
+       /**
+        * Format URL to the zero or m main page for the specific language (or 
current if null)
+        * @param null|string $langCode
+        * @return string
+        */
+       private static function getMainPage( $langCode = null ) {
+               if ( $langCode === null ) {
+                       global $wgLang;
+                       $langCode = $wgLang->getCode();
+               }
+               $langUrlFormat = PageRenderingHooks::isZeroSubdomain() ? 
self::$formatZeroUrl : self::$formatMobileUrl;
+               return sprintf( $langUrlFormat, $langCode );
        }
 }

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

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

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

Reply via email to