mediawiki-skins-Vector/includes/FeatureManagement/Requirements/UserPreferenceRequirement.php
Jon Robson 411f9ce69a 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
2023-10-12 19:05:10 +00:00

129 lines
3.1 KiB
PHP

<?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 MediaWiki\Skins\Vector\FeatureManagement\Requirement;
use MediaWiki\Title\Title;
use MediaWiki\User\UserOptionsLookup;
use User;
use WebRequest;
/**
* @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
*/
final class UserPreferenceRequirement implements Requirement {
/**
* @var User
*/
private $user;
/**
* @var UserOptionsLookup
*/
private $userOptionsLookup;
/**
* @var string
*/
private $optionName;
/**
* @var string
*/
private $requirementName;
/**
* @var Title|null
*/
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.
*
* @param User $user
* @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(
User $user,
UserOptionsLookup $userOptionsLookup,
string $optionName,
string $requirementName,
WebRequest $request,
$title = null
) {
$this->user = $user;
$this->userOptionsLookup = $userOptionsLookup;
$this->optionName = $optionName;
$this->requirementName = $requirementName;
$this->title = $title;
$this->helper = new OverrideableRequirementHelper( $request, $requirementName );
}
/**
* @inheritDoc
*/
public function getName(): string {
return $this->requirementName;
}
/**
* Checks whether the user preference is enabled or not. Returns true if
* enabled AND title is not null.
*
* @internal
*
* @return bool
*/
public function isPreferenceEnabled() {
$user = $this->user;
$userOptionsLookup = $this->userOptionsLookup;
$optionValue = $userOptionsLookup->getOption(
$user,
$this->optionName
);
// Check for 0, '0' or 'disabled'.
// Any other value will be handled as enabled.
$isEnabled = (bool)$optionValue && $optionValue !== 'disabled';
return $this->title && $isEnabled;
}
/**
* @inheritDoc
*/
public function isMet(): bool {
$override = $this->helper->isMet();
return $override ?? $this->isPreferenceEnabled();
}
}