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

Revision: 113987
Author:   awjrichards
Date:     2012-03-16 01:27:26 +0000 (Fri, 16 Mar 2012)
Log Message:
-----------
* Adds ExtMobileFrontend::$useFormatCookieName property so we can pass full 
mf_useformat cookie name to application.js
* Makes 'Mobile view' URL generation generate a full URL using the mobile URL 
template
* Refactors determining cookie's duration logic into its own method
* Uses native PHP setcookie() to set mf_useformat cookie
* Makes clicking on 'Desktop view' link use Javascript to change the cookie 
value to 'desktop' before redirecting the user (specifically to bypass WMF 
mobile varnish servers once squid config changes are complete [see rt #2645])

Modified Paths:
--------------
    trunk/extensions/MobileFrontend/MobileFrontend.body.php
    trunk/extensions/MobileFrontend/javascripts/application.js
    trunk/extensions/MobileFrontend/javascripts/application.min.js
    trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php
    trunk/extensions/MobileFrontend/templates/FooterTemplate.php

Modified: trunk/extensions/MobileFrontend/MobileFrontend.body.php
===================================================================
--- trunk/extensions/MobileFrontend/MobileFrontend.body.php     2012-03-16 
01:26:34 UTC (rev 113986)
+++ trunk/extensions/MobileFrontend/MobileFrontend.body.php     2012-03-16 
01:27:26 UTC (rev 113987)
@@ -43,6 +43,7 @@
        public static $logoutHtml;
        public static $loginHtml;
        public static $zeroRatedBanner;
+       public static $useFormatCookieName;
        
        protected $useFormat;
        
@@ -190,7 +191,7 @@
         * @return bool
         */
        public function addMobileFooter( &$obj, &$tpl ) {
-               global $wgRequest;
+               global $wgRequest, $wgServer;
                wfProfileIn( __METHOD__ );
 
                $title = $obj->getTitle();
@@ -202,6 +203,7 @@
                                        $this->removeQueryStringParameter( 
$wgRequest->appendQuery( 'useformat=mobile' ), 'mobileaction' )
                                        );
 
+                       $mobileViewUrl = $this->getMobileUrl( $wgServer . 
$mobileViewUrl );
                        $tpl->set( 'mobileview', "<a href='{$mobileViewUrl}' 
class='noprint'>" . wfMsg( 'mobile-frontend-view' ) . "</a>" );
                        $footerlinks['places'][] = 'mobileview';
                        $tpl->set( 'footerlinks', $footerlinks );
@@ -1164,6 +1166,8 @@
                                                'zeroRatedBanner' => 
self::$zeroRatedBanner,
                                                'showText' => self::$messages[ 
'mobile-frontend-show-button' ],
                                                'hideText' => self::$messages[ 
'mobile-frontend-hide-button' ],
+                                               'useFormatCookieName' => 
self::$useFormatCookieName,
+                                               'useFormatCookieDuration' => 
$this->getUseFormatCookieDuration(),
                                                );
                $applicationTemplate->setByArray( $options );
                wfProfileOut( __METHOD__ );
@@ -1446,8 +1450,12 @@
        }
        
        public function checkUseFormatCookie() {
-               global $wgRequest;
+               global $wgRequest, $wgCookiePrefix;
                
+               if ( !isset( self::$useFormatCookieName )) {
+                       self::$useFormatCookieName = $wgCookiePrefix . 
'mf_useformat';
+               }
+               
                $useFormat = $this->getUseFormat();
                $useFormatFromCookie = $wgRequest->getCookie( 'mf_useformat' );
                
@@ -1472,27 +1480,44 @@
         * @param string The format to store in the cookie
         */
        protected function setUseFormatCookie( $useFormat ) {
-               global $wgRequest;
+               global $wgRequest, $wgCookiePath, $wgCookieSecure, 
$wgCookieDomain;
                $expiry = $this->getUseFormatCookieExpiry();
-               $wgRequest->response()->setCookie( 'mf_useformat', $useFormat, 
$expiry );
+               
+               // use regular php setcookie() rather than 
WebResponse::setCookie
+               // so we can ignore $wgCookieHttpOnly since the protection it 
provides
+               // is irrelevant for this cookie.
+               setcookie( self::$useFormatCookieName, $useFormat, $expiry, 
$wgCookiePath, $wgCookieDomain, $wgCookieSecure );
        }
        
        /**
         * Get the expiration time for the mf_useformat cookie
         *
-        * If $wgMobileFrontendFormatCookieExpiry as a non-0 value, 
         * @param int The base time (in seconds since Epoch) from which to 
calculate
         *              cookie expiration. If null, time() is used.
+        * @return int The time (in seconds since Epoch) that the cookie should 
expire
         */
        protected function getUseFormatCookieExpiry( $startTime=null ) {
-               global $wgCookieExpiration, $wgMobileFrontendFormatCookieExpiry;
-               $cookieDuration = ( abs( intval( 
$wgMobileFrontendFormatCookieExpiry ) ) > 0 ) ? 
-                               $wgMobileFrontendFormatCookieExpiry : 
$wgCookieExpiration;
+               $cookieDuration = $this->getUseFormatCookieDuration();
                if ( intval( $startTime ) === 0 ) $startTime = time();
                $expiry = $startTime + $cookieDuration;
                return $expiry;
        }
        
+       /**
+        * Determine the duration the cookie should last.
+        * 
+        * If $wgMobileFrontendFormatcookieExpiry has a non-0 value, use that
+        * for the duration. Otherwise, fall back to $wgCookieExpiration.
+        * 
+        * @return int The number of seconds for which the cookie should last.
+        */
+       protected function getUseFormatCookieDuration() {
+               global $wgMobileFrontendFormatCookieExpiry, $wgCookieExpiration;
+               $cookieDuration = ( abs( intval( 
$wgMobileFrontendFormatCookieExpiry ) ) > 0 ) ? 
+                               $wgMobileFrontendFormatCookieExpiry : 
$wgCookieExpiration;
+               return $cookieDuration;
+       }
+       
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }

Modified: trunk/extensions/MobileFrontend/javascripts/application.js
===================================================================
--- trunk/extensions/MobileFrontend/javascripts/application.js  2012-03-16 
01:26:34 UTC (rev 113986)
+++ trunk/extensions/MobileFrontend/javascripts/application.js  2012-03-16 
01:27:26 UTC (rev 113987)
@@ -26,6 +26,15 @@
                }
                utilities( document.getElementById( 'logo' ) ).bind( 'click', 
logoClick );
 
+               function desktopViewClick() {
+                       var cookieName = MobileFrontend.setting( 
'useFormatCookieName' );
+                       var cookieDuration = MobileFrontend.setting( 
'useFormatCookieDuration' );
+                       // convert from seconds to days
+                       cookieDuration = cookieDuration / ( 24 * 60 * 60 );
+                       MobileFrontend.banner.writeCookie( cookieName, 
'desktop', cookieDuration );
+               }
+               utilities( document.getElementById( 'mf-display-toggle' ) 
).bind( 'click', desktopViewClick );
+
                // Try to scroll and hide URL bar
                window.scrollTo( 0, 1 );
        }

Modified: trunk/extensions/MobileFrontend/javascripts/application.min.js
===================================================================
--- trunk/extensions/MobileFrontend/javascripts/application.min.js      
2012-03-16 01:26:34 UTC (rev 113986)
+++ trunk/extensions/MobileFrontend/javascripts/application.min.js      
2012-03-16 01:27:26 UTC (rev 113987)
@@ -1 +1 @@
-MobileFrontend=(function(){var a;function b(){var 
e;a(document.body).addClass("jsEnabled");e=document.getElementById("languageselection");function
 c(){var 
f;if(e){f=e.options[e.selectedIndex].value;if(f){location.href=f}}}a(e).bind("change",c);function
 d(){var 
f=document.getElementById("nav").style;f.display=f.display==="block"?"none":"block"}a(document.getElementById("logo")).bind("click",d);window.scrollTo(0,1)}a=typeof
 
jQuery!=="undefined"?jQuery:function(e){if(typeof(e)==="string"){if(document.querySelectorAll){return[].slice.call(document.querySelectorAll(e))}}function
 d(i){var j=e.className.split("");return j.indexOf(i)>-1}function f(i){var 
j=e.className,k=j.split(" ");k.push(i);e.className=k.join(" ")}function 
g(j){var l=e.className,m=l.split(" 
"),n=[],k;for(k=0;k<m.length;k++){if(m[k]!==j){n.push(m[k])}}e.className=n.join("
 ")}function h(j,i){e.addEventListener(j,i,false)}function 
c(){e.parentNode.removeChild(e)}return{addClass:f,bind:h,hasClass:d,remove:c,removeClass:g}};a.ajax=a.ajax||function(e){var
 c,d;if(window.XMLHttpRequest){c=new XMLHttpRequest()}else{c=new 
ActiveXObject("Microsoft.XMLHTTP")}if(c.overrideMimeType){c.overrideMimeType("text/xml")}c.onreadystatechange=function(){if(c.readyState===4&&c.status===200){e.success(c.responseXML)}};c.open("GET",e.url,true);c.send()};b();return{init:b,message:function(c){return
 mwMobileFrontendConfig.messages[c]||""},setting:function(c){return 
mwMobileFrontendConfig.settings[c]||""},utils:a}}());
\ No newline at end of file
+MobileFrontend=(function(){var a;function b(){var 
e;a(document.body).addClass("jsEnabled");e=document.getElementById("languageselection");function
 c(){var 
g;if(e){g=e.options[e.selectedIndex].value;if(g){location.href=g}}}a(e).bind("change",c);function
 d(){var 
g=document.getElementById("nav").style;g.display=g.display==="block"?"none":"block"}a(document.getElementById("logo")).bind("click",d);function
 f(){var h=MobileFrontend.setting("useFormatCookieName");var 
g=MobileFrontend.setting("useFormatCookieDuration");g=g/(24*60*60);MobileFrontend.banner.writeCookie(h,"desktop",g)}a(document.getElementById("mf-display-toggle")).bind("click",f);window.scrollTo(0,1)}a=typeof
 
jQuery!=="undefined"?jQuery:function(e){if(typeof(e)==="string"){if(document.querySelectorAll){return[].slice.call(document.querySelectorAll(e))}}else{if(!e){e=document.createElement("div")}}function
 d(i){var j=e.className.split(" ");return j.indexOf(i)>-1}function f(i){var 
j=e.className,k=j.split(" ");k.push(i);e.className=k.join(" ")}function 
g(j){var l=e.className,m=l.split(" 
"),n=[],k;for(k=0;k<m.length;k++){if(m[k]!==j){n.push(m[k])}}e.className=n.join("
 ")}function h(j,i){e.addEventListener(j,i,false)}function 
c(){e.parentNode.removeChild(e)}return{addClass:f,bind:h,hasClass:d,remove:c,removeClass:g}};a.ajax=a.ajax||function(e){var
 c,d;if(window.XMLHttpRequest){c=new XMLHttpRequest()}else{c=new 
ActiveXObject("Microsoft.XMLHTTP")}if(c.overrideMimeType){c.overrideMimeType("text/xml")}c.onreadystatechange=function(){if(c.readyState===4&&c.status===200){e.success(c.responseXML)}};c.open("GET",e.url,true);c.send()};b();return{init:b,message:function(c){return
 mwMobileFrontendConfig.messages[c]||""},setting:function(c){return 
mwMobileFrontendConfig.settings[c]||""},utils:a}}());
\ No newline at end of file

Modified: trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php
===================================================================
--- trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php   
2012-03-16 01:26:34 UTC (rev 113986)
+++ trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php   
2012-03-16 01:27:26 UTC (rev 113987)
@@ -51,6 +51,8 @@
                                ),
                        'settings' => array(
                                'scriptPath' => ( $this->data['wgScriptPath'] ),
+                               'useFormatCookieName' => ( 
$this->data['useFormatCookieName'] ),
+                               'useFormatCookieDuration' => ( 
$this->data['useFormatCookieDuration'] ),
                        ),
                );
                $configuration = FormatJSON::encode( $jsconfig );

Modified: trunk/extensions/MobileFrontend/templates/FooterTemplate.php
===================================================================
--- trunk/extensions/MobileFrontend/templates/FooterTemplate.php        
2012-03-16 01:26:34 UTC (rev 113986)
+++ trunk/extensions/MobileFrontend/templates/FooterTemplate.php        
2012-03-16 01:27:26 UTC (rev 113987)
@@ -36,7 +36,7 @@
                        <div id='footer' {$footerDisplayNone}>
                          <div class='nav' id='footmenu'>
                                <div class='mwm-notice'>
-                                 <a 
href="{$viewNormalSiteURL}">{$regularSite}</a> | <a 
href="{$imagesURL}">{$imagesToggle}</a> {$feedbackLink} {$logoutLink}
+                                 <a href="{$viewNormalSiteURL}" 
id="mf-display-toggle">{$regularSite}</a> | <a 
href="{$imagesURL}">{$imagesToggle}</a> {$feedbackLink} {$logoutLink}
                                </div>
                          </div>
                          <div id='copyright'>{$copyright}</div>


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

Reply via email to