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