r113987 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113986‎ | r113987 | r113988 >
Date:01:27, 16 March 2012
Author:awjrichards
Status:ok
Tags:
Comment:
* 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 (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/application.js (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/application.min.js (modified) (history)
  • /trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php (modified) (history)
  • /trunk/extensions/MobileFrontend/templates/FooterTemplate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/MobileFrontend.body.php
@@ -43,6 +43,7 @@
4444 public static $logoutHtml;
4545 public static $loginHtml;
4646 public static $zeroRatedBanner;
 47+ public static $useFormatCookieName;
4748
4849 protected $useFormat;
4950
@@ -190,7 +191,7 @@
191192 * @return bool
192193 */
193194 public function addMobileFooter( &$obj, &$tpl ) {
194 - global $wgRequest;
 195+ global $wgRequest, $wgServer;
195196 wfProfileIn( __METHOD__ );
196197
197198 $title = $obj->getTitle();
@@ -202,6 +203,7 @@
203204 $this->removeQueryStringParameter( $wgRequest->appendQuery( 'useformat=mobile' ), 'mobileaction' )
204205 );
205206
 207+ $mobileViewUrl = $this->getMobileUrl( $wgServer . $mobileViewUrl );
206208 $tpl->set( 'mobileview', "<a href='{$mobileViewUrl}' class='noprint'>" . wfMsg( 'mobile-frontend-view' ) . "</a>" );
207209 $footerlinks['places'][] = 'mobileview';
208210 $tpl->set( 'footerlinks', $footerlinks );
@@ -1164,6 +1166,8 @@
11651167 'zeroRatedBanner' => self::$zeroRatedBanner,
11661168 'showText' => self::$messages[ 'mobile-frontend-show-button' ],
11671169 'hideText' => self::$messages[ 'mobile-frontend-hide-button' ],
 1170+ 'useFormatCookieName' => self::$useFormatCookieName,
 1171+ 'useFormatCookieDuration' => $this->getUseFormatCookieDuration(),
11681172 );
11691173 $applicationTemplate->setByArray( $options );
11701174 wfProfileOut( __METHOD__ );
@@ -1446,8 +1450,12 @@
14471451 }
14481452
14491453 public function checkUseFormatCookie() {
1450 - global $wgRequest;
 1454+ global $wgRequest, $wgCookiePrefix;
14511455
 1456+ if ( !isset( self::$useFormatCookieName )) {
 1457+ self::$useFormatCookieName = $wgCookiePrefix . 'mf_useformat';
 1458+ }
 1459+
14521460 $useFormat = $this->getUseFormat();
14531461 $useFormatFromCookie = $wgRequest->getCookie( 'mf_useformat' );
14541462
@@ -1472,27 +1480,44 @@
14731481 * @param string The format to store in the cookie
14741482 */
14751483 protected function setUseFormatCookie( $useFormat ) {
1476 - global $wgRequest;
 1484+ global $wgRequest, $wgCookiePath, $wgCookieSecure, $wgCookieDomain;
14771485 $expiry = $this->getUseFormatCookieExpiry();
1478 - $wgRequest->response()->setCookie( 'mf_useformat', $useFormat, $expiry );
 1486+
 1487+ // use regular php setcookie() rather than WebResponse::setCookie
 1488+ // so we can ignore $wgCookieHttpOnly since the protection it provides
 1489+ // is irrelevant for this cookie.
 1490+ setcookie( self::$useFormatCookieName, $useFormat, $expiry, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
14791491 }
14801492
14811493 /**
14821494 * Get the expiration time for the mf_useformat cookie
14831495 *
1484 - * If $wgMobileFrontendFormatCookieExpiry as a non-0 value,
14851496 * @param int The base time (in seconds since Epoch) from which to calculate
14861497 * cookie expiration. If null, time() is used.
 1498+ * @return int The time (in seconds since Epoch) that the cookie should expire
14871499 */
14881500 protected function getUseFormatCookieExpiry( $startTime=null ) {
1489 - global $wgCookieExpiration, $wgMobileFrontendFormatCookieExpiry;
1490 - $cookieDuration = ( abs( intval( $wgMobileFrontendFormatCookieExpiry ) ) > 0 ) ?
1491 - $wgMobileFrontendFormatCookieExpiry : $wgCookieExpiration;
 1501+ $cookieDuration = $this->getUseFormatCookieDuration();
14921502 if ( intval( $startTime ) === 0 ) $startTime = time();
14931503 $expiry = $startTime + $cookieDuration;
14941504 return $expiry;
14951505 }
14961506
 1507+ /**
 1508+ * Determine the duration the cookie should last.
 1509+ *
 1510+ * If $wgMobileFrontendFormatcookieExpiry has a non-0 value, use that
 1511+ * for the duration. Otherwise, fall back to $wgCookieExpiration.
 1512+ *
 1513+ * @return int The number of seconds for which the cookie should last.
 1514+ */
 1515+ protected function getUseFormatCookieDuration() {
 1516+ global $wgMobileFrontendFormatCookieExpiry, $wgCookieExpiration;
 1517+ $cookieDuration = ( abs( intval( $wgMobileFrontendFormatCookieExpiry ) ) > 0 ) ?
 1518+ $wgMobileFrontendFormatCookieExpiry : $wgCookieExpiration;
 1519+ return $cookieDuration;
 1520+ }
 1521+
14971522 public function getVersion() {
14981523 return __CLASS__ . ': $Id$';
14991524 }
Index: trunk/extensions/MobileFrontend/javascripts/application.min.js
@@ -1 +1 @@
2 -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
 2+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
Index: trunk/extensions/MobileFrontend/javascripts/application.js
@@ -26,6 +26,15 @@
2727 }
2828 utilities( document.getElementById( 'logo' ) ).bind( 'click', logoClick );
2929
 30+ function desktopViewClick() {
 31+ var cookieName = MobileFrontend.setting( 'useFormatCookieName' );
 32+ var cookieDuration = MobileFrontend.setting( 'useFormatCookieDuration' );
 33+ // convert from seconds to days
 34+ cookieDuration = cookieDuration / ( 24 * 60 * 60 );
 35+ MobileFrontend.banner.writeCookie( cookieName, 'desktop', cookieDuration );
 36+ }
 37+ utilities( document.getElementById( 'mf-display-toggle' ) ).bind( 'click', desktopViewClick );
 38+
3039 // Try to scroll and hide URL bar
3140 window.scrollTo( 0, 1 );
3241 }
Index: trunk/extensions/MobileFrontend/templates/FooterTemplate.php
@@ -36,7 +36,7 @@
3737 <div id='footer' {$footerDisplayNone}>
3838 <div class='nav' id='footmenu'>
3939 <div class='mwm-notice'>
40 - <a href="{$viewNormalSiteURL}">{$regularSite}</a> | <a href="{$imagesURL}">{$imagesToggle}</a> {$feedbackLink} {$logoutLink}
 40+ <a href="{$viewNormalSiteURL}" id="mf-display-toggle">{$regularSite}</a> | <a href="{$imagesURL}">{$imagesToggle}</a> {$feedbackLink} {$logoutLink}
4141 </div>
4242 </div>
4343 <div id='copyright'>{$copyright}</div>
Index: trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php
@@ -51,6 +51,8 @@
5252 ),
5353 'settings' => array(
5454 'scriptPath' => ( $this->data['wgScriptPath'] ),
 55+ 'useFormatCookieName' => ( $this->data['useFormatCookieName'] ),
 56+ 'useFormatCookieDuration' => ( $this->data['useFormatCookieDuration'] ),
5557 ),
5658 );
5759 $configuration = FormatJSON::encode( $jsconfig );

Follow-up revisions

RevisionCommit summaryAuthorDate
r114205MFT r113942, r113971, r113987, r114005, r114025, r114100awjrichards22:25, 19 March 2012

Status & tagging log