2021-07-12 23:04:09 +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
|
|
|
|
*/
|
|
|
|
|
2022-05-23 07:32:40 +00:00
|
|
|
namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements;
|
2021-07-12 23:04:09 +00:00
|
|
|
|
2023-10-18 17:32:16 +00:00
|
|
|
use ExtensionRegistry;
|
2024-01-02 19:54:08 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2023-10-18 17:32:16 +00:00
|
|
|
use MediaWiki\Extension\BetaFeatures\BetaFeatures;
|
2024-01-02 19:54:08 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2023-10-18 17:32:16 +00:00
|
|
|
use MediaWiki\Skins\Vector\Constants;
|
2022-05-23 07:32:40 +00:00
|
|
|
use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
|
2024-01-02 19:54:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2021-07-12 23:04:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The `OverridableConfigRequirement` allows us to define requirements that can override
|
|
|
|
* configuration like querystring parameters, e.g.
|
|
|
|
*
|
|
|
|
* ```lang=php
|
|
|
|
* $featureManager->registerRequirement(
|
|
|
|
* new OverridableConfigRequirement(
|
|
|
|
* $config,
|
|
|
|
* $user,
|
|
|
|
* $request,
|
2023-03-03 09:40:44 +00:00
|
|
|
* MainConfigNames::Sitename,
|
2021-07-12 23:04:09 +00:00
|
|
|
* 'requirementName',
|
|
|
|
* 'overrideName',
|
|
|
|
* 'configTestName',
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* registers a requirement that will evaluate to true only when `mediawiki/includes/Setup.php` has
|
|
|
|
* finished executing (after all service wiring has executed). I.e., every call to
|
|
|
|
* `Requirement->isMet()` re-interrogates the request, user authentication status,
|
|
|
|
* and config object for the current state and returns it. Contrast to:
|
|
|
|
*
|
|
|
|
* ```lang=php
|
2023-03-03 09:40:44 +00:00
|
|
|
* $featureManager->registerSimpleRequirement(
|
|
|
|
* 'requirementName',
|
|
|
|
* (bool)$config->get( MainConfigNames::Sitename )
|
2021-07-12 23:04:09 +00:00
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* wherein state is evaluated only once at registration time and permanently cached.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2022-05-23 07:32:40 +00:00
|
|
|
* @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
|
2021-07-12 23:04:09 +00:00
|
|
|
*/
|
2023-10-18 17:32:16 +00:00
|
|
|
class OverridableConfigRequirement implements Requirement {
|
2021-07-12 23:04:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $configName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $requirementName;
|
|
|
|
|
|
|
|
/**
|
2023-10-02 18:15:18 +00:00
|
|
|
* @var OverrideableRequirementHelper
|
2021-07-12 23:04:09 +00:00
|
|
|
*/
|
2023-10-02 18:15:18 +00:00
|
|
|
private $helper;
|
2021-07-12 23:04:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This constructor accepts all dependencies needed to determine whether
|
|
|
|
* the overridable config is enabled for the current user and request.
|
|
|
|
*
|
|
|
|
* @param Config $config
|
|
|
|
* @param User $user
|
|
|
|
* @param WebRequest $request
|
|
|
|
* @param string $configName Any `Config` key. This name is used to query `$config` state.
|
|
|
|
* @param string $requirementName The name of the requirement presented to FeatureManager.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Config $config,
|
|
|
|
User $user,
|
|
|
|
WebRequest $request,
|
|
|
|
string $configName,
|
2023-05-03 16:55:40 +00:00
|
|
|
string $requirementName
|
2021-07-12 23:04:09 +00:00
|
|
|
) {
|
|
|
|
$this->config = $config;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->configName = $configName;
|
|
|
|
$this->requirementName = $requirementName;
|
2023-10-02 18:15:18 +00:00
|
|
|
$this->helper = new OverrideableRequirementHelper( $request, $requirementName );
|
2021-07-12 23:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2021-07-23 19:53:14 +00:00
|
|
|
public function getName(): string {
|
2021-07-12 23:04:09 +00:00
|
|
|
return $this->requirementName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check query parameter to override config or not.
|
|
|
|
* Then check for AB test value.
|
|
|
|
* Fallback to config value.
|
|
|
|
*
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2021-07-23 19:53:14 +00:00
|
|
|
public function isMet(): bool {
|
2023-10-02 18:15:18 +00:00
|
|
|
$isMet = $this->helper->isMet();
|
|
|
|
if ( $isMet !== null ) {
|
|
|
|
return $isMet;
|
2023-05-03 16:55:40 +00:00
|
|
|
}
|
2021-07-12 23:04:09 +00:00
|
|
|
|
|
|
|
// If AB test is not enabled, fallback to checking config state.
|
|
|
|
$thisConfig = $this->config->get( $this->configName );
|
|
|
|
|
|
|
|
// Backwards compatibility with config variables that have been set in production.
|
|
|
|
if ( is_bool( $thisConfig ) ) {
|
|
|
|
$thisConfig = [
|
|
|
|
'logged_in' => $thisConfig,
|
|
|
|
'logged_out' => $thisConfig,
|
2023-10-18 17:32:16 +00:00
|
|
|
'beta' => $thisConfig,
|
2021-07-12 23:04:09 +00:00
|
|
|
];
|
2021-12-15 17:51:44 +00:00
|
|
|
} elseif ( array_key_exists( 'default', $thisConfig ) ) {
|
|
|
|
$thisConfig = [
|
|
|
|
'default' => $thisConfig['default'],
|
|
|
|
];
|
2021-07-12 23:04:09 +00:00
|
|
|
} else {
|
|
|
|
$thisConfig = [
|
|
|
|
'logged_in' => $thisConfig['logged_in'] ?? false,
|
|
|
|
'logged_out' => $thisConfig['logged_out'] ?? false,
|
2023-10-18 17:32:16 +00:00
|
|
|
'beta' => $thisConfig['beta'] ?? false,
|
2021-07-12 23:04:09 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to config.
|
2023-10-18 17:32:16 +00:00
|
|
|
$userConfig = array_key_exists( 'default', $thisConfig ) ?
|
2021-12-15 17:51:44 +00:00
|
|
|
$thisConfig[ 'default' ] :
|
|
|
|
$thisConfig[ $this->user->isRegistered() ? 'logged_in' : 'logged_out' ];
|
2023-10-18 17:32:16 +00:00
|
|
|
// Check if use has enabled beta features
|
|
|
|
$betaFeatureConfig = array_key_exists( 'beta', $thisConfig ) && $thisConfig[ 'beta' ];
|
|
|
|
$betaFeatureEnabled = in_array( $this->configName, Constants::VECTOR_BETA_FEATURES ) &&
|
|
|
|
$betaFeatureConfig && $this->isVector2022BetaFeatureEnabled();
|
|
|
|
// If user has enabled beta features, use beta config
|
|
|
|
return $betaFeatureEnabled ? $betaFeatureConfig : $userConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if user has enabled the Vector 2022 beta features
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isVector2022BetaFeatureEnabled(): bool {
|
|
|
|
return ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) &&
|
|
|
|
/* @phan-suppress-next-line PhanUndeclaredClassMethod */
|
|
|
|
BetaFeatures::isFeatureEnabled(
|
|
|
|
$this->user,
|
|
|
|
Constants::VECTOR_2022_BETA_KEY
|
|
|
|
);
|
2021-07-12 23:04:09 +00:00
|
|
|
}
|
|
|
|
}
|