Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| BetaFeatures | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 1 |
| isFeatureEnabled | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the MediaWiki extension BetaFeatures. |
| 4 | * |
| 5 | * BetaFeatures is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * BetaFeatures is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with BetaFeatures. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * BetaFeatures utilities |
| 19 | * |
| 20 | * @file |
| 21 | * @ingroup Extensions |
| 22 | * @copyright 2013 Mark Holmquist and others; see AUTHORS |
| 23 | * @license GPL-2.0-or-later |
| 24 | */ |
| 25 | |
| 26 | namespace MediaWiki\Extension\BetaFeatures; |
| 27 | |
| 28 | use MediaWiki\MediaWikiServices; |
| 29 | use MediaWiki\User\Options\StaticUserOptionsLookup; |
| 30 | use MediaWiki\User\UserIdentity; |
| 31 | |
| 32 | class BetaFeatures { |
| 33 | |
| 34 | /** |
| 35 | * Check if a user has a beta feature enabled. |
| 36 | * |
| 37 | * @param UserIdentity $user The user identity to check |
| 38 | * @param string $feature The key passed back to BetaFeatures |
| 39 | * from the GetBetaFeaturePreferences hook |
| 40 | * @param array|null $userOptions Specific state of user options, |
| 41 | * can be used to handle SaveUserOptions hook. |
| 42 | * @return bool |
| 43 | */ |
| 44 | public static function isFeatureEnabled( UserIdentity $user, $feature, $userOptions = null ) { |
| 45 | $services = MediaWikiServices::getInstance(); |
| 46 | $lookup = $services->getUserOptionsLookup(); |
| 47 | if ( is_array( $userOptions ) ) { |
| 48 | $defaults = $lookup->getDefaultOptions( $user ); |
| 49 | $lookup = new StaticUserOptionsLookup( [ $user->getName() => $userOptions ], $defaults ); |
| 50 | } |
| 51 | |
| 52 | $config = $services->getMainConfig(); |
| 53 | $allowlist = $config->get( 'BetaFeaturesAllowList' ); |
| 54 | |
| 55 | if ( is_array( $allowlist ) && !in_array( $feature, $allowlist ) ) { |
| 56 | // If there is an allow-list, and the feature is not in said list, |
| 57 | // it can't be enabled. |
| 58 | return false; |
| 59 | } |
| 60 | return $lookup->getBoolOption( $user, $feature ); |
| 61 | } |
| 62 | } |