MaxSem has uploaded a new change for review.
https://gerrit.wikimedia.org/r/74667
Change subject: Simplify opt-in cookies
......................................................................
Simplify opt-in cookies
* Don't use two cookies where you can use one
* Simplifies accessors greatly
Change-Id: I4db3329d36ee3e5d3af276dae34fc38b4b1f70be
---
M includes/MobileContext.php
M includes/specials/SpecialMobileOptions.php
2 files changed, 60 insertions(+), 113 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend
refs/changes/67/74667/1
diff --git a/includes/MobileContext.php b/includes/MobileContext.php
index 934a957..58245a3 100644
--- a/includes/MobileContext.php
+++ b/includes/MobileContext.php
@@ -1,8 +1,7 @@
<?php
class MobileContext extends ContextSource {
- protected $betaGroupMember;
- protected $alphaGroupMember;
+ protected $optInGroup;
protected $contentFormat = '';
protected $useFormatCookieName;
protected $disableImages;
@@ -183,32 +182,66 @@
return true;
}
- public function isAlphaGroupMember() {
- if ( is_null( $this->alphaGroupMember ) ) {
- $this->checkUserStatus();
- if ( $this->getMobileAction() == 'alpha' ) {
- $this->setAlphaGroupMember( true );
+ /**
+ * Returns the testing group user has opted in: 'alpha', 'beta' or any
other value for no group
+ * @return string
+ */
+ protected function getOptInGroup() {
+ $req = $this->getRequest();
+ if ( is_null( $this->optInGroup ) ) {
+ // First check old cookie
+ // @todo: Remove in September
+ $alpha = $req->getCookie( 'mf_alpha', '' );
+ if ( $alpha == 1 ) {
+ $req->response()->setcookie( 'mf_alpha', '', 0,
'' );
+ $this->setOptInGroup( 'alpha' );
+ } else {
+ $this->optInGroup =
$this->getRequest()->getCookie( 'optin', '' );
+ // Old cookie format - handle it but no point
in overwriting the cookie
+ if ( $this->optInGroup == '1' ) {
+ $this->optInGroup = 'beta';
+ }
}
+
}
- return $this->alphaGroupMember;
+ return $this->optInGroup;
}
- public function setAlphaGroupMember( $value ) {
- $this->alphaGroupMember = $value;
+ /**
+ * Sets opt in group membership, both cookie and this class variables
+ * @param string $group: Group to set or empty string for no group
+ */
+ public function setOptInGroup( $group ) {
+ wfProfileIn( __METHOD__ );
+ if ( $group !== 'alpha' && $group !== 'beta' ) {
+ $group = '';
+ }
+ // Update statistics
+ if ( $group === 'alpha' && !is_null( $this->optInGroup ) ) {
+ wfIncrStats( 'mobile.alpha.opt_in_cookie_set' );
+ }
+ if ( $group === 'beta' ) {
+ if ( $this->optInGroup === 'alpha' ) {
+ wfIncrStats( 'mobile.alpha.opt_in_cookie_unset'
);
+ } else {
+ wfIncrStats( 'mobile.opt_in_cookie_set' );
+ }
+ }
+ if ( !$group ) {
+
+ }
+ $this->optInGroup = $group;
+ $this->getRequest()->response()->setcookie( 'optin', $group, 0,
'', $this->getBaseDomain() );
+ wfProfileOut( __METHOD__ );
+ }
+
+ public function isAlphaGroupMember() {
+ return $this->getOptInGroup() === 'alpha';
}
public function isBetaGroupMember() {
- if ( is_null( $this->betaGroupMember ) ) {
- $this->checkUserStatus();
- if ( $this->getMobileAction() == 'beta' ) {
- $this->setBetaGroupMember( true );
- }
- }
- return $this->betaGroupMember;
- }
-
- public function setBetaGroupMember( $value ) {
- $this->betaGroupMember = $value;
+ $group = $this->getOptInGroup();
+ return $group === 'beta' || $group === 'alpha';
}
public function getMobileToken() {
@@ -433,88 +466,6 @@
$useFormatFromCookie = $this->getRequest()->getCookie(
$this->getUseFormatCookieName(), '' );
return $useFormatFromCookie;
- }
-
- public function checkUserStatus() {
- wfProfileIn( __METHOD__ );
-
- $optInCookie = $this->getOptInOutCookie();
- $alpha = $this->getAlphaOptInOutCookie();
- if ( !empty( $optInCookie ) &&
- $optInCookie == 1 ) {
- $this->betaGroupMember = true;
- }
- if ( !empty( $alpha ) &&
- $alpha == 1 ) {
- $this->setAlphaGroupMember( true );
- } else {
- $this->setAlphaGroupMember( false );
- }
- wfProfileOut( __METHOD__ );
- return true;
- }
-
- /**
- * @param $value bool
- * @return bool
- */
- public function setOptInOutCookie( $value ) {
- global $wgCookieDomain, $wgCookiePrefix;
- wfProfileIn( __METHOD__ );
- if ( $value ) {
- wfIncrStats( 'mobile.opt_in_cookie_set' );
- }
- $tempWgCookieDomain = $wgCookieDomain;
- $wgCookieDomain = $this->getBaseDomain();
- $tempWgCookiePrefix = $wgCookiePrefix;
- $wgCookiePrefix = '';
- $this->getRequest()->response()->setcookie( 'optin', $value ?
'1' : '', 0, '' );
- $wgCookieDomain = $tempWgCookieDomain;
- $wgCookiePrefix = $tempWgCookiePrefix;
- wfProfileOut( __METHOD__ );
- return true;
- }
-
- /**
- * @param bool $value
- * @param bool $disabled
- *
- * @return bool
- */
- public function setAlphaOptInOutCookie( $value, $disabled = false ) {
- wfProfileIn( __METHOD__ );
- // track opt ins
- if ( $value ) {
- wfIncrStats( 'mobile.alpha.opt_in_cookie_set' );
- }
- // track opt outs
- if ( $disabled ) {
- wfIncrStats( 'mobie.alpha.opt_in_cookie_unset' );
- }
- $cookieDomain = $this->getBaseDomain();
- $this->getRequest()->response()->setcookie( 'mf_alpha', $value
? '1' : '', 0, '', $cookieDomain );
- wfProfileOut( __METHOD__ );
- return true;
- }
-
- /**
- * @return Mixed
- */
- private function getAlphaOptInOutCookie() {
- wfProfileIn( __METHOD__ );
- $optInCookie = $this->getRequest()->getCookie( 'mf_alpha', '' );
- wfProfileOut( __METHOD__ );
- return $optInCookie;
- }
-
- /**
- * @return Mixed
- */
- private function getOptInOutCookie() {
- wfProfileIn( __METHOD__ );
- $optInCookie = $this->getRequest()->getCookie( 'optin', '' );
- wfProfileOut( __METHOD__ );
- return $optInCookie;
}
/**
diff --git a/includes/specials/SpecialMobileOptions.php
b/includes/specials/SpecialMobileOptions.php
index 06a06d2..94a2192 100644
--- a/includes/specials/SpecialMobileOptions.php
+++ b/includes/specials/SpecialMobileOptions.php
@@ -194,20 +194,16 @@
wfDebug( __METHOD__ . "(): token mismatch\n" );
//return; // Display something here?
}
- $inBeta = $request->getBool( 'enableBeta' );
- $inAlpha = $request->getBool( 'enableAlpha' );
- // determine whether or not alpha was disabled
- if ( $context->isAlphaGroupMember() && !$inAlpha ) {
- $alphaDisabled = true;
+ if ( $request->getBool( 'enableAlpha' ) ) {
+ $group = 'alpha';
+ } elseif ( $request->getBool( 'enableBeta' ) ) {
+ $group = 'beta';
} else {
- $alphaDisabled = false;
+ $group = '';
}
+ $context->setOptInGroup( $group );
$imagesDisabled = !$request->getBool( 'enableImages' );
$context->setDisableImagesCookie( $imagesDisabled );
- $context->setOptInOutCookie( $inBeta ? '1' : '' );
- $context->setBetaGroupMember( $inBeta );
- $context->setAlphaOptInOutCookie( $inAlpha ? '1' : '',
$alphaDisabled );
- $context->setAlphaGroupMember( $inAlpha );
$returnToTitle = Title::newFromText( $request->getText(
'returnto' ) );
if ( $returnToTitle ) {
--
To view, visit https://gerrit.wikimedia.org/r/74667
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4db3329d36ee3e5d3af276dae34fc38b4b1f70be
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits