Allow user preferences to be overriden by query string parameter

Currently only features that are associated with LocalSettings configuration
can be overriden by query string. Going forward we'd like to expand this to apply
to user preference too.

A generic OverrideableRequirement is created.
The existing OverridableConfigRequirement is refactored to extend OverrideableRequirement
The /UserPreferenceRequirement now extends it

This allows http://localhost:8888/wiki/Spain?vectortypographysurvey=1
to work

Bug: T347900
Change-Id: I11efd6b07192d5d2333f4506e9d87a8c0638d657
This commit is contained in:
Jon Robson 2023-10-02 11:15:18 -07:00 committed by Jdlrobson
parent 0e37765afb
commit 411f9ce69a
6 changed files with 119 additions and 25 deletions

View file

@ -75,11 +75,6 @@ final class OverridableConfigRequirement implements Requirement {
*/
private $user;
/**
* @var WebRequest
*/
private $request;
/**
* @var string
*/
@ -91,9 +86,9 @@ final class OverridableConfigRequirement implements Requirement {
private $requirementName;
/**
* @var string
* @var OverrideableRequirementHelper
*/
private $overrideName;
private $helper;
/**
* This constructor accepts all dependencies needed to determine whether
@ -114,10 +109,9 @@ final class OverridableConfigRequirement implements Requirement {
) {
$this->config = $config;
$this->user = $user;
$this->request = $request;
$this->configName = $configName;
$this->requirementName = $requirementName;
$this->overrideName = strtolower( $configName );
$this->helper = new OverrideableRequirementHelper( $request, $requirementName );
}
/**
@ -135,12 +129,9 @@ final class OverridableConfigRequirement implements Requirement {
* @inheritDoc
*/
public function isMet(): bool {
// Check query parameter.
if ( $this->request->getCheck( $this->overrideName ) ) {
return $this->request->getBool( $this->overrideName );
}
if ( $this->request->getCheck( $this->configName ) ) {
return $this->request->getBool( $this->configName );
$isMet = $this->helper->isMet();
if ( $isMet !== null ) {
return $isMet;
}
// If AB test is not enabled, fallback to checking config state.

View file

@ -0,0 +1,84 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements;
use WebRequest;
/**
* The `OverridableConfigRequirement` allows us to define requirements that can override
* requirements with querystring parameters.
*
* NOTE: This API hasn't settled. It may change at any time without warning. Please don't bind to
* it unless you absolutely need to
*
* @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
*/
class OverrideableRequirementHelper {
/**
* @var WebRequest
*/
private $request;
/**
* @var string
*/
private $requirementName;
/**
* @var string
*/
private $overrideName;
/**
* This constructor accepts all dependencies needed to determine whether
* the overridable config is enabled for the current user and request.
*
* @param WebRequest $request
* @param string $requirementName The name of the requirement presented to FeatureManager.
*/
public function __construct(
WebRequest $request,
string $requirementName
) {
$this->request = $request;
$this->overrideName = 'vector' . strtolower( $requirementName );
$this->requirementName = $requirementName;
}
/**
* Check query parameter to override config or not.
* Then check for AB test value.
* Fallback to config value.
*
* @return bool|null
*/
public function isMet() {
// Check query parameter.
if ( $this->request->getCheck( $this->overrideName ) ) {
return $this->request->getBool( $this->overrideName );
}
$vectorReq = 'Vector' . $this->requirementName;
if ( $this->request->getCheck( $vectorReq ) ) {
return $this->request->getBool( $vectorReq );
}
return null;
}
}

View file

@ -25,6 +25,7 @@ use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
use MediaWiki\Title\Title;
use MediaWiki\User\UserOptionsLookup;
use User;
use WebRequest;
/**
* @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
@ -56,6 +57,11 @@ final class UserPreferenceRequirement implements Requirement {
*/
private $title;
/**
* @var OverrideableRequirementHelper
*/
private $helper;
/**
* This constructor accepts all dependencies needed to determine whether
* the overridable config is enabled for the current user and request.
@ -64,6 +70,7 @@ final class UserPreferenceRequirement implements Requirement {
* @param UserOptionsLookup $userOptionsLookup
* @param string $optionName The name of the user preference.
* @param string $requirementName The name of the requirement presented to FeatureManager.
* @param WebRequest $request
* @param Title|null $title
*/
public function __construct(
@ -71,6 +78,7 @@ final class UserPreferenceRequirement implements Requirement {
UserOptionsLookup $userOptionsLookup,
string $optionName,
string $requirementName,
WebRequest $request,
$title = null
) {
$this->user = $user;
@ -78,6 +86,7 @@ final class UserPreferenceRequirement implements Requirement {
$this->optionName = $optionName;
$this->requirementName = $requirementName;
$this->title = $title;
$this->helper = new OverrideableRequirementHelper( $request, $requirementName );
}
/**
@ -113,6 +122,7 @@ final class UserPreferenceRequirement implements Requirement {
* @inheritDoc
*/
public function isMet(): bool {
return $this->isPreferenceEnabled();
$override = $this->helper->isMet();
return $override ?? $this->isPreferenceEnabled();
}
}

View file

@ -50,6 +50,7 @@ return [
);
$context = RequestContext::getMain();
$request = $context->getRequest();
// Feature: Languages in sidebar
// ================================
@ -57,7 +58,7 @@ return [
new OverridableConfigRequirement(
$services->getMainConfig(),
$context->getUser(),
$context->getRequest(),
$request,
Constants::CONFIG_KEY_LANGUAGE_IN_HEADER,
Constants::REQUIREMENT_LANGUAGE_IN_HEADER
)
@ -89,7 +90,7 @@ return [
new OverridableConfigRequirement(
$config,
$context->getUser(),
$context->getRequest(),
$request,
Constants::CONFIG_KEY_LANGUAGE_IN_HEADER,
$requirementName
)
@ -111,7 +112,7 @@ return [
new OverridableConfigRequirement(
$services->getMainConfig(),
$context->getUser(),
$context->getRequest(),
$request,
Constants::CONFIG_LANGUAGE_IN_MAIN_PAGE_HEADER,
Constants::REQUIREMENT_LANGUAGE_IN_MAIN_PAGE_HEADER
)
@ -138,7 +139,7 @@ return [
new OverridableConfigRequirement(
$services->getMainConfig(),
$context->getUser(),
$context->getRequest(),
$request,
Constants::CONFIG_STICKY_HEADER,
Constants::REQUIREMENT_STICKY_HEADER
)
@ -167,6 +168,7 @@ return [
$services->getUserOptionsLookup(),
Constants::PREF_KEY_PAGE_TOOLS_PINNED,
Constants::REQUIREMENT_PAGE_TOOLS_PINNED,
$request,
$context->getTitle()
)
);
@ -188,6 +190,7 @@ return [
$services->getUserOptionsLookup(),
Constants::PREF_KEY_TOC_PINNED,
Constants::REQUIREMENT_TOC_PINNED,
$request,
$context->getTitle()
)
);
@ -208,6 +211,7 @@ return [
$services->getUserOptionsLookup(),
Constants::PREF_KEY_MAIN_MENU_PINNED,
Constants::REQUIREMENT_MAIN_MENU_PINNED,
$request,
$context->getTitle()
)
);
@ -229,6 +233,7 @@ return [
$services->getUserOptionsLookup(),
Constants::PREF_KEY_LIMITED_WIDTH,
Constants::REQUIREMENT_LIMITED_WIDTH,
$request,
$context->getTitle()
)
);
@ -245,7 +250,7 @@ return [
$featureManager->registerRequirement(
new LimitedWidthContentRequirement(
$services->getMainConfig(),
$context->getRequest(),
$request,
$context->getTitle()
)
);
@ -263,7 +268,7 @@ return [
new OverridableConfigRequirement(
$services->getMainConfig(),
$context->getUser(),
$context->getRequest(),
$request,
Constants::CONFIG_ZEBRA_DESIGN,
Constants::REQUIREMENT_ZEBRA_DESIGN
)
@ -285,6 +290,7 @@ return [
$services->getUserOptionsLookup(),
Constants::PREF_KEY_FONT_SIZE,
Constants::REQUIREMENT_FONT_SIZE,
$request,
$context->getTitle()
)
);
@ -304,7 +310,7 @@ return [
new OverridableConfigRequirement(
$services->getMainConfig(),
$context->getUser(),
$context->getRequest(),
$request,
Constants::CONFIG_KEY_CLIENT_PREFERENCES,
Constants::REQUIREMENT_CLIENT_PREFERENCES
)
@ -326,6 +332,7 @@ return [
$services->getUserOptionsLookup(),
Constants::PREF_KEY_TYPOGRAPHY_SURVEY,
Constants::REQUIREMENT_TYPOGRAPHY_SURVEY,
$request,
$context->getTitle()
)
);

View file

@ -133,8 +133,7 @@ class OverridableConfigRequirementTest extends \MediaWikiUnitTestCase {
$user,
$request,
'VectorLanguageInHeader',
'LanguageInHeader',
'languageinheader'
'LanguageInHeader'
);
$this->assertSame( $expected, $requirement->isMet(), $msg );

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Skins\Vector\Tests\Unit\FeatureManagement\Requirements;
use FauxRequest;
use MediaWiki\Skins\Vector\FeatureManagement\Requirements\UserPreferenceRequirement;
use MediaWiki\Title\Title;
use MediaWiki\User\UserOptionsLookup;
@ -85,6 +86,7 @@ final class UserPreferenceRequirementTest extends \MediaWikiUnitTestCase {
) {
$user = $this->createMock( User::class );
$title = $isTitlePresent ? $this->createMock( Title::class ) : null;
$request = new FauxRequest();
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
$userOptionsLookup->method( 'getOption' )->willReturn( $isEnabled );
@ -94,6 +96,7 @@ final class UserPreferenceRequirementTest extends \MediaWikiUnitTestCase {
$userOptionsLookup,
'userOption',
'userRequirement',
$request,
$title
);