Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
45.95% |
17 / 37 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
OverridableConfigRequirement | |
45.95% |
17 / 37 |
|
0.00% |
0 / 4 |
44.96 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isMet | |
65.38% |
17 / 26 |
|
0.00% |
0 / 1 |
14.15 | |||
isVector2022BetaFeatureEnabled | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * http://www.gnu.org/copyleft/gpl.html |
18 | * |
19 | * @file |
20 | */ |
21 | |
22 | namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements; |
23 | |
24 | use ExtensionRegistry; |
25 | use MediaWiki\Config\Config; |
26 | use MediaWiki\Extension\BetaFeatures\BetaFeatures; |
27 | use MediaWiki\Request\WebRequest; |
28 | use MediaWiki\Skins\Vector\Constants; |
29 | use MediaWiki\Skins\Vector\FeatureManagement\Requirement; |
30 | use MediaWiki\User\UserIdentity; |
31 | |
32 | /** |
33 | * The `OverridableConfigRequirement` allows us to define requirements that can override |
34 | * configuration like querystring parameters, e.g. |
35 | * |
36 | * ```lang=php |
37 | * $featureManager->registerRequirement( |
38 | * new OverridableConfigRequirement( |
39 | * $config, |
40 | * $user, |
41 | * $request, |
42 | * MainConfigNames::Sitename, |
43 | * 'requirementName', |
44 | * 'overrideName', |
45 | * 'configTestName', |
46 | * ) |
47 | * ); |
48 | * ``` |
49 | * |
50 | * registers a requirement that will evaluate to true only when `mediawiki/includes/Setup.php` has |
51 | * finished executing (after all service wiring has executed). I.e., every call to |
52 | * `Requirement->isMet()` re-interrogates the request, user authentication status, |
53 | * and config object for the current state and returns it. Contrast to: |
54 | * |
55 | * ```lang=php |
56 | * $featureManager->registerSimpleRequirement( |
57 | * 'requirementName', |
58 | * (bool)$config->get( MainConfigNames::Sitename ) |
59 | * ); |
60 | * ``` |
61 | * |
62 | * wherein state is evaluated only once at registration time and permanently cached. |
63 | * |
64 | * NOTE: This API hasn't settled. It may change at any time without warning. Please don't bind to |
65 | * it unless you absolutely need to |
66 | * |
67 | * @package MediaWiki\Skins\Vector\FeatureManagement\Requirements |
68 | */ |
69 | class OverridableConfigRequirement implements Requirement { |
70 | |
71 | private Config $config; |
72 | |
73 | private UserIdentity $user; |
74 | |
75 | private string $configName; |
76 | |
77 | private string $requirementName; |
78 | |
79 | private OverrideableRequirementHelper $helper; |
80 | |
81 | /** |
82 | * This constructor accepts all dependencies needed to determine whether |
83 | * the overridable config is enabled for the current user and request. |
84 | * |
85 | * @param Config $config |
86 | * @param UserIdentity $user |
87 | * @param WebRequest $request |
88 | * @param string $configName Any `Config` key. This name is used to query `$config` state. |
89 | * @param string $requirementName The name of the requirement presented to FeatureManager. |
90 | */ |
91 | public function __construct( |
92 | Config $config, |
93 | UserIdentity $user, |
94 | WebRequest $request, |
95 | string $configName, |
96 | string $requirementName |
97 | ) { |
98 | $this->config = $config; |
99 | $this->user = $user; |
100 | $this->configName = $configName; |
101 | $this->requirementName = $requirementName; |
102 | $this->helper = new OverrideableRequirementHelper( $request, $requirementName ); |
103 | } |
104 | |
105 | /** |
106 | * @inheritDoc |
107 | */ |
108 | public function getName(): string { |
109 | return $this->requirementName; |
110 | } |
111 | |
112 | /** |
113 | * Check query parameter to override config or not. |
114 | * Then check for AB test value. |
115 | * Fallback to config value. |
116 | * |
117 | * @inheritDoc |
118 | */ |
119 | public function isMet(): bool { |
120 | $isMet = $this->helper->isMet(); |
121 | if ( $isMet !== null ) { |
122 | return $isMet; |
123 | } |
124 | |
125 | // If AB test is not enabled, fallback to checking config state. |
126 | $thisConfig = $this->config->get( $this->configName ); |
127 | |
128 | // Backwards compatibility with config variables that have been set in production. |
129 | if ( is_bool( $thisConfig ) ) { |
130 | $thisConfig = [ |
131 | 'logged_in' => $thisConfig, |
132 | 'logged_out' => $thisConfig, |
133 | 'beta' => $thisConfig, |
134 | ]; |
135 | } elseif ( array_key_exists( 'default', $thisConfig ) ) { |
136 | $thisConfig = [ |
137 | 'default' => $thisConfig['default'], |
138 | ]; |
139 | } else { |
140 | $thisConfig = [ |
141 | 'logged_in' => $thisConfig['logged_in'] ?? false, |
142 | 'logged_out' => $thisConfig['logged_out'] ?? false, |
143 | 'beta' => $thisConfig['beta'] ?? false, |
144 | ]; |
145 | } |
146 | |
147 | // Fallback to config. |
148 | $userConfig = array_key_exists( 'default', $thisConfig ) ? |
149 | $thisConfig[ 'default' ] : |
150 | $thisConfig[ $this->user->isRegistered() ? 'logged_in' : 'logged_out' ]; |
151 | // Check if use has enabled beta features |
152 | $betaFeatureConfig = array_key_exists( 'beta', $thisConfig ) && $thisConfig[ 'beta' ]; |
153 | $betaFeatureEnabled = in_array( $this->configName, Constants::VECTOR_BETA_FEATURES ) && |
154 | $betaFeatureConfig && $this->isVector2022BetaFeatureEnabled(); |
155 | // If user has enabled beta features, use beta config |
156 | return $betaFeatureEnabled ? $betaFeatureConfig : $userConfig; |
157 | } |
158 | |
159 | /** |
160 | * Check if user has enabled the Vector 2022 beta features |
161 | * @return bool |
162 | */ |
163 | public function isVector2022BetaFeatureEnabled(): bool { |
164 | return ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) && |
165 | /* @phan-suppress-next-line PhanUndeclaredClassMethod */ |
166 | BetaFeatures::isFeatureEnabled( |
167 | $this->user, |
168 | Constants::VECTOR_2022_BETA_KEY |
169 | ); |
170 | } |
171 | } |