Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| PreferenceHelper | |
0.00% |
0 / 27 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| isBetaFeatureEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| isEnabledForUser | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| setGlobalPreference | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getGlobalPreference | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| isCXEntrypointDisabled | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * User preference helper methods for ContentTranslation extension. |
| 4 | * |
| 5 | * @copyright See AUTHORS.txt |
| 6 | * @license GPL-2.0-or-later |
| 7 | */ |
| 8 | namespace ContentTranslation; |
| 9 | |
| 10 | use GlobalPreferences\GlobalPreferencesFactory; |
| 11 | use GlobalPreferences\Storage; |
| 12 | use MediaWiki\Config\ServiceOptions; |
| 13 | use MediaWiki\Context\RequestContext; |
| 14 | use MediaWiki\Extension\BetaFeatures\BetaFeatures; |
| 15 | use MediaWiki\Preferences\PreferencesFactory; |
| 16 | use MediaWiki\User\Options\UserOptionsLookup; |
| 17 | use MediaWiki\User\User; |
| 18 | use MediaWiki\User\UserIdentity; |
| 19 | |
| 20 | class PreferenceHelper { |
| 21 | |
| 22 | /** @var bool */ |
| 23 | private bool $contentTranslationAsBetaFeature; |
| 24 | |
| 25 | /** |
| 26 | * @internal For use by ServiceWiring |
| 27 | */ |
| 28 | public const CONSTRUCTOR_OPTIONS = [ 'ContentTranslationAsBetaFeature' ]; |
| 29 | |
| 30 | public function __construct( |
| 31 | private readonly PreferencesFactory $preferencesFactory, |
| 32 | private readonly UserOptionsLookup $userOptionsLookup, |
| 33 | ServiceOptions $options, |
| 34 | private readonly bool $isBetaFeaturesLoaded, |
| 35 | private readonly bool $isGlobalPreferencesLoaded |
| 36 | ) { |
| 37 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 38 | |
| 39 | $this->contentTranslationAsBetaFeature = $options->get( 'ContentTranslationAsBetaFeature' ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param UserIdentity $user |
| 44 | * |
| 45 | * @return bool |
| 46 | */ |
| 47 | public function isBetaFeatureEnabled( UserIdentity $user ) { |
| 48 | return $this->isBetaFeaturesLoaded && BetaFeatures::isFeatureEnabled( $user, 'cx' ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Utility function that checks whether CX is enabled for a given user. |
| 53 | * Currently, it checks that if CX is a beta feature, whether the user has |
| 54 | * enabled it. Otherwise, it is always enabled. |
| 55 | * |
| 56 | * @param User $user |
| 57 | * @return bool |
| 58 | */ |
| 59 | public function isEnabledForUser( User $user ) { |
| 60 | // CX is currently restricted to only logged-in users. For now treat temp users as anonymous. |
| 61 | if ( !$user->isNamed() ) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if ( !$this->contentTranslationAsBetaFeature ) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | return $this->isBetaFeatureEnabled( $user ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Set a global preference for the user. |
| 74 | * @param User $user |
| 75 | * @param string $preference |
| 76 | * @param string $value |
| 77 | */ |
| 78 | public function setGlobalPreference( User $user, $preference, $value ) { |
| 79 | if ( !$this->isGlobalPreferencesLoaded ) { |
| 80 | // Need GlobalPreferences extension. |
| 81 | wfLogWarning( __METHOD__ . ': Need GlobalPreferences extension. Not setting preference.' ); |
| 82 | return; |
| 83 | } |
| 84 | /** @var GlobalPreferencesFactory $globalPref */ |
| 85 | $globalPref = $this->preferencesFactory; |
| 86 | '@phan-var GlobalPreferencesFactory $globalPref'; |
| 87 | $prefs = $globalPref->getGlobalPreferencesValues( $user, Storage::SKIP_CACHE ); |
| 88 | if ( $prefs === false ) { |
| 89 | $prefs = []; |
| 90 | } |
| 91 | $prefs[$preference] = $value; |
| 92 | $globalPref->setGlobalPreferences( $user, $prefs, RequestContext::getMain() ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get a global preference for the user. |
| 97 | * @param User $user |
| 98 | * @param string $preference |
| 99 | * @return string|null Preference value |
| 100 | */ |
| 101 | public function getGlobalPreference( $user, $preference ) { |
| 102 | if ( !$this->isGlobalPreferencesLoaded ) { |
| 103 | // Need GlobalPreferences extension. |
| 104 | wfLogWarning( __METHOD__ . ': Need GlobalPreferences extension. Not getting preference.' ); |
| 105 | return null; |
| 106 | } |
| 107 | /** @var GlobalPreferencesFactory $globalPref */ |
| 108 | $globalPref = $this->preferencesFactory; |
| 109 | '@phan-var GlobalPreferencesFactory $globalPref'; |
| 110 | $prefs = $globalPref->getGlobalPreferencesValues( $user, Storage::SKIP_CACHE ); |
| 111 | if ( $prefs === false ) { |
| 112 | $prefs = []; |
| 113 | } |
| 114 | return $prefs[ $preference ] ?? null; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * If CX is not beta feature and user unchecked the preference |
| 119 | * to avoid seeing entry points, disable all entrypoints |
| 120 | * @param UserIdentity $user |
| 121 | * @return bool |
| 122 | */ |
| 123 | public function isCXEntrypointDisabled( UserIdentity $user ) { |
| 124 | return !$this->contentTranslationAsBetaFeature && |
| 125 | !$this->userOptionsLookup->getBoolOption( $user, 'cx-enable-entrypoints' ); |
| 126 | } |
| 127 | } |