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;
|
|
|
|
|
2024-01-02 19:54:08 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2022-10-21 17:39:16 +00:00
|
|
|
use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
|
2023-08-19 04:23:07 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-11-29 12:42:02 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2024-03-13 20:59:40 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
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
|
|
|
|
2024-03-13 20:59:40 +00:00
|
|
|
private UserIdentity $user;
|
2022-10-21 17:39:16 +00:00
|
|
|
|
2024-02-05 00:15:55 +00:00
|
|
|
private UserOptionsLookup $userOptionsLookup;
|
2022-10-21 17:39:16 +00:00
|
|
|
|
2024-02-05 00:15:55 +00:00
|
|
|
private string $optionName;
|
2022-12-01 20:05:06 +00:00
|
|
|
|
2024-02-05 00:15:55 +00:00
|
|
|
private string $requirementName;
|
2022-12-01 20:05:06 +00:00
|
|
|
|
2024-02-05 00:15:55 +00:00
|
|
|
private ?Title $title;
|
2022-12-15 22:52:24 +00:00
|
|
|
|
2024-02-05 00:15:55 +00:00
|
|
|
private OverrideableRequirementHelper $helper;
|
2023-10-02 18:15:18 +00:00
|
|
|
|
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.
|
|
|
|
*
|
2024-03-13 20:59:40 +00:00
|
|
|
* @param UserIdentity $user
|
2022-10-21 17:39:16 +00:00
|
|
|
* @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(
|
2024-03-13 20:59:40 +00:00
|
|
|
UserIdentity $user,
|
2022-10-21 17:39:16 +00:00
|
|
|
UserOptionsLookup $userOptionsLookup,
|
2022-12-01 20:05:06 +00:00
|
|
|
string $optionName,
|
|
|
|
string $requirementName,
|
2023-10-02 18:15:18 +00:00
|
|
|
WebRequest $request,
|
2024-02-05 00:15:55 +00:00
|
|
|
Title $title = null
|
2022-10-21 17:39:16 +00:00
|
|
|
) {
|
|
|
|
$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
|
|
|
|
*/
|
2024-02-05 00:15:55 +00:00
|
|
|
public function isPreferenceEnabled(): bool {
|
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.
|
2024-02-05 00:15:55 +00:00
|
|
|
$isEnabled = $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
|
|
|
}
|
|
|
|
}
|