2022-10-21 17:39:16 +00:00
|
|
|
<?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;
|
2023-08-19 04:23:07 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-10-21 17:39:16 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
|
|
|
use User;
|
2023-10-02 18:15:18 +00:00
|
|
|
use WebRequest;
|
2022-10-21 17:39:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
|
|
|
|
*/
|
2022-12-01 20:05:06 +00:00
|
|
|
final class UserPreferenceRequirement implements Requirement {
|
2022-10-21 17:39:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var UserOptionsLookup
|
|
|
|
*/
|
|
|
|
private $userOptionsLookup;
|
|
|
|
|
2022-12-01 20:05:06 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $optionName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $requirementName;
|
|
|
|
|
2022-12-15 22:52:24 +00:00
|
|
|
/**
|
|
|
|
* @var Title|null
|
|
|
|
*/
|
|
|
|
private $title;
|
|
|
|
|
2023-10-02 18:15:18 +00:00
|
|
|
/**
|
|
|
|
* @var OverrideableRequirementHelper
|
|
|
|
*/
|
|
|
|
private $helper;
|
|
|
|
|
2022-10-21 17:39:16 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2022-12-01 20:05:06 +00:00
|
|
|
* @param string $optionName The name of the user preference.
|
|
|
|
* @param string $requirementName The name of the requirement presented to FeatureManager.
|
2023-10-02 18:15:18 +00:00
|
|
|
* @param WebRequest $request
|
2022-10-21 17:39:16 +00:00
|
|
|
* @param Title|null $title
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
User $user,
|
|
|
|
UserOptionsLookup $userOptionsLookup,
|
2022-12-01 20:05:06 +00:00
|
|
|
string $optionName,
|
|
|
|
string $requirementName,
|
2023-10-02 18:15:18 +00:00
|
|
|
WebRequest $request,
|
2022-10-21 17:39:16 +00:00
|
|
|
$title = null
|
|
|
|
) {
|
|
|
|
$this->user = $user;
|
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2022-12-01 20:05:06 +00:00
|
|
|
$this->optionName = $optionName;
|
|
|
|
$this->requirementName = $requirementName;
|
2022-10-21 17:39:16 +00:00
|
|
|
$this->title = $title;
|
2023-10-02 18:15:18 +00:00
|
|
|
$this->helper = new OverrideableRequirementHelper( $request, $requirementName );
|
2022-10-21 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getName(): string {
|
2022-12-01 20:05:06 +00:00
|
|
|
return $this->requirementName;
|
2022-10-21 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-01 20:05:06 +00:00
|
|
|
* Checks whether the user preference is enabled or not. Returns true if
|
|
|
|
* enabled AND title is not null.
|
|
|
|
*
|
2022-10-21 17:39:16 +00:00
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-12-01 20:05:06 +00:00
|
|
|
public function isPreferenceEnabled() {
|
2022-10-21 17:39:16 +00:00
|
|
|
$user = $this->user;
|
|
|
|
$userOptionsLookup = $this->userOptionsLookup;
|
2023-10-02 21:03:21 +00:00
|
|
|
$optionValue = $userOptionsLookup->getOption(
|
2022-10-21 17:39:16 +00:00
|
|
|
$user,
|
2022-12-01 20:05:06 +00:00
|
|
|
$this->optionName
|
2022-10-21 17:39:16 +00:00
|
|
|
);
|
2023-10-02 21:03:21 +00:00
|
|
|
// Check for 0, '0' or 'disabled'.
|
|
|
|
// Any other value will be handled as enabled.
|
|
|
|
$isEnabled = (bool)$optionValue && $optionValue !== 'disabled';
|
2022-12-01 20:05:06 +00:00
|
|
|
|
|
|
|
return $this->title && $isEnabled;
|
2022-10-21 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isMet(): bool {
|
2023-10-02 18:15:18 +00:00
|
|
|
$override = $this->helper->isMet();
|
|
|
|
return $override ?? $this->isPreferenceEnabled();
|
2022-10-21 17:39:16 +00:00
|
|
|
}
|
|
|
|
}
|