https://www.mediawiki.org/wiki/Special:Code/MediaWiki/113439

Revision: 113439
Author:   awjrichards
Date:     2012-03-09 00:40:30 +0000 (Fri, 09 Mar 2012)
Log Message:
-----------
Makes useformat=mobile sticky with a cookie. Changese 
ExtMobileFrontend::useFormat to protected var accessible with get/set methods. 
Updated tests to reflect changes

Modified Paths:
--------------
    trunk/extensions/MobileFrontend/MobileFrontend.body.php
    trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php

Modified: trunk/extensions/MobileFrontend/MobileFrontend.body.php
===================================================================
--- trunk/extensions/MobileFrontend/MobileFrontend.body.php     2012-03-09 
00:38:35 UTC (rev 113438)
+++ trunk/extensions/MobileFrontend/MobileFrontend.body.php     2012-03-09 
00:40:30 UTC (rev 113439)
@@ -20,7 +20,6 @@
        public static $format;
        public static $search;
        public static $callback;
-       public static $useFormat;
        public static $disableImages;
        public static $enableImages;
        public static $isMainPage = false;
@@ -46,6 +45,8 @@
        public static $loginHtml;
        public static $zeroRatedBanner;
        
+       protected $useFormat;
+       
        /**
         * @var string xDevice header information
         */
@@ -350,8 +351,9 @@
                // This is stated to be intended behavior, as per the 
following: [http://bugs.php.net/bug.php?id=40104]
 
                $xDevice = $this->getXDevice();
-               self::$useFormat = $wgRequest->getText( 'useformat' );
-               $this->wmlContext->setUseFormat( self::$useFormat );
+               $this->checkUseFormatCookie();
+               $useFormat = $this->getUseFormat();
+               $this->wmlContext->setUseFormat( $useFormat );
                $mobileAction = $this->getMobileAction();               
 
                if ( !$this->shouldDisplayMobileView() ) {
@@ -1371,12 +1373,12 @@
                if ( !$this->isFauxMobileDevice() ) {
                        return;
                }
-               
+               $useFormat = $this->getUseFormat();
                if ( !isset( $parsedUrl[ 'query' ] )) {
-                       $parsedUrl[ 'query' ] = 'useformat=' . urlencode( 
self::$useFormat );
+                       $parsedUrl[ 'query' ] = 'useformat=' . urlencode( 
$useFormat );
                } else {
                        $query = wfCgiToArray( $parsedUrl[ 'query' ] );
-                       $query[ 'useformat' ] = urlencode( self::$useFormat );
+                       $query[ 'useformat' ] = urlencode( $useFormat );
                        $parsedUrl[ 'query' ] = wfArrayToCgi( $query );
                }
        }
@@ -1427,7 +1429,8 @@
        }
        
        protected function isFauxMobileDevice() {
-               if ( self::$useFormat !== 'mobile' && self::$useFormat !== 
'mobile-wap') {
+               $useFormat = $this->getUseFormat();
+               if ( $useFormat !== 'mobile' && $useFormat !== 'mobile-wap') {
                        return false;
                } 
 
@@ -1477,6 +1480,47 @@
                return $this->action;
        }
        
+       public function getUseFormat() {
+               global $wgRequest;
+               if ( !isset( $this->useFormat ) ) {
+                       $useFormat = $wgRequest->getText( 'useformat' );
+                       $this->setUseFormat( $useFormat );
+               }
+               return $this->useFormat;
+       }
+       
+       public function setUseFormat( $useFormat ) {
+               $this->useFormat = $useFormat;
+       }
+       
+       public function checkUseFormatCookie() {
+               global $wgRequest;
+               
+               $useFormat = $this->getUseFormat();
+               $useFormatFromCookie = $wgRequest->getCookie( 'mf_useformat' );
+               if( !strlen( $useFormat ) && !is_null( $useFormatFromCookie ) ) 
{
+                       $this->setUseFormat( $useFormatFromCookie );
+               }
+               
+               // if we should not be displaying the mobile view, make sure 
cookies are unset etc.
+               if ( !$this->shouldDisplayMobileView() ) {
+                       // make sure cookie is unset for appropriate mobile 
actions
+                       $mobileAction = $this->getMobileAction();
+                       if ( in_array( $mobileAction, array( 
'view_normal_site', 'disable_mobile_site' ) ) ) {
+                               $wgRequest->response()->setCookie( 
'mf_useformat', false, time() - 3600 );
+                       }
+                       
+                       // make sure useformat is unset
+                       $this->setUseFormat( '' );
+                       return;
+               }
+               
+               // if getUseFormat and no cookie set, set the cookie
+               if ( is_null( $useFormatFromCookie ) && strlen( $useFormat ) ) {
+                       $wgRequest->response()->setCookie( 'mf_useformat', 
$useFormat, 0 );
+               }
+       }
+       
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }

Modified: trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php
===================================================================
--- trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php        
2012-03-09 00:38:35 UTC (rev 113438)
+++ trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php        
2012-03-09 00:40:30 UTC (rev 113439)
@@ -27,7 +27,6 @@
                global $wgExtMobileFrontend;
                unset( $wgExtMobileFrontend );
                unset( $_SERVER[ 'HTTP_X_DEVICE' ] );
-               ExtMobileFrontend::$useFormat = null;
                parent::tearDown();
        }
 
@@ -114,7 +113,7 @@
                $testMethod = ( $assert ) ? 'assertTrue' : 'assertFalse';
                $url = 
'http://en.wikipedia.org/wiki/Article/?something=bananas';
                if ( !empty( $useFormat ) ) $url .= "&useformat=" . $useFormat;
-               ExtMobileFrontend::$useFormat = $useFormat;
+               $wgExtMobileFrontend->setUseFormat( $useFormat );
                
                $parsedUrl = wfParseUrl( $url );
                
@@ -165,7 +164,7 @@
                
                $testMethod = ( $isFauxDevice ) ? 'assertTrue' : 'assertFalse';
                
-               ExtMobileFrontend::$useFormat = $useformat;
+               $wgExtMobileFrontend->setUseFormat( $useformat );
                $this->$testMethod( $isFauxMobileDevice->invokeArgs( 
$wgExtMobileFrontend, array() ), $msg );
        }
        
@@ -190,7 +189,7 @@
                if ( count( $requestVal )) {
                        foreach ( $requestVal as $key => $val ) {
                                if ( $key == 'useformat' ) {
-                                       ExtMobileFrontend::$useFormat = $val;
+                                       $wgExtMobileFrontend->setUseFormat( 
$val );
                                } else {
                                        $wgRequest->setVal( $key, $val );
                                }


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

Reply via email to