Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.76% covered (warning)
71.76%
61 / 85
55.56% covered (warning)
55.56%
5 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
FeatureManager
71.76% covered (warning)
71.76%
61 / 85
55.56% covered (warning)
55.56%
5 / 9
60.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 registerFeature
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 getUserPreferenceValue
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getFeatureBodyClass
81.08% covered (warning)
81.08%
30 / 37
0.00% covered (danger)
0.00%
0 / 1
12.98
 isFeatureEnabled
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 registerRequirement
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 registerSimpleRequirement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRequirementMet
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 resolveNightModeQueryValue
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
56
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 * @since 1.35
21 */
22
23namespace MediaWiki\Skins\Vector\FeatureManagement;
24
25use MediaWiki\Context\IContextSource;
26use MediaWiki\Skins\Vector\ConfigHelper;
27use MediaWiki\Skins\Vector\Constants;
28use MediaWiki\Skins\Vector\FeatureManagement\Requirements\SimpleRequirement;
29use MediaWiki\User\Options\UserOptionsLookup;
30use Wikimedia\Assert\Assert;
31
32/**
33 * A simple feature manager.
34 *
35 * NOTE: This API hasn't settled. It may change at any time without warning. Please don't bind to
36 * it unless you absolutely need to.
37 *
38 * @unstable
39 *
40 * @package MediaWiki\Skins\Vector\FeatureManagement
41 * @internal
42 * @final
43 */
44class FeatureManager {
45
46    /**
47     * A map of feature name to the array of requirements (referenced by name). A feature is only
48     * considered enabled when all of its requirements are met.
49     *
50     * See FeatureManager::registerFeature for additional detail.
51     *
52     * @var Array<string,string[]>
53     */
54    private $features = [];
55
56    /**
57     * A map of requirement name to the Requirement instance that represents it.
58     *
59     * The names of requirements are assumed to be static for the lifetime of the request. Therefore
60     * we can use them to look up Requirement instances quickly.
61     *
62     * @var Array<string,Requirement>
63     */
64    private $requirements = [];
65
66    private UserOptionsLookup $userOptionsLookup;
67    private IContextSource $context;
68
69    public function __construct(
70        UserOptionsLookup $userOptionsLookup,
71        IContextSource $context
72    ) {
73        $this->userOptionsLookup = $userOptionsLookup;
74        $this->context = $context;
75    }
76
77    /**
78     * Register a feature and its requirements.
79     *
80     * Essentially, a "feature" is a friendly (hopefully) name for some component, however big or
81     * small, that has some requirements. A feature manager allows us to decouple the component's
82     * logic from its requirements, allowing them to vary independently. Moreover, the use of
83     * friendly names wherever possible allows us to define a common language with our non-technical
84     * colleagues.
85     *
86     * ```php
87     * $featureManager->registerFeature( 'featureA', 'requirementA' );
88     * ```
89     *
90     * defines the "featureA" feature, which is enabled when the "requirementA" requirement is met.
91     *
92     * ```php
93     * $featureManager->registerFeature( 'featureB', [ 'requirementA', 'requirementB' ] );
94     * ```
95     *
96     * defines the "featureB" feature, which is enabled when the "requirementA" and "requirementB"
97     * requirements are met. Note well that the feature is only enabled when _all_ requirements are
98     * met, i.e. the requirements are evaluated in order and logically `AND`ed together.
99     *
100     * @param string $feature The name of the feature
101     * @param string|array $requirements The feature's requirements. As above, you can define a
102     * feature that requires a single requirement via the shorthand
103     *
104     *  ```php
105     *  $featureManager->registerFeature( 'feature', 'requirementA' );
106     *  // Equivalent to $featureManager->registerFeature( 'feature', [ 'requirementA' ] );
107     *  ```
108     *
109     * @throws \LogicException If the feature is already registered
110     * @throws \Wikimedia\Assert\ParameterAssertionException If the feature's requirements aren't
111     *  the name of a single requirement or a list of requirements
112     * @throws \InvalidArgumentException If the feature references a requirement that isn't
113     *  registered
114     */
115    public function registerFeature( string $feature, $requirements ) {
116        //
117        // Validation
118        if ( array_key_exists( $feature, $this->features ) ) {
119            throw new \LogicException( sprintf(
120                'Feature "%s" is already registered.',
121                $feature
122            ) );
123        }
124
125        Assert::parameterType( 'string|array', $requirements, 'requirements' );
126
127        $requirements = (array)$requirements;
128
129        Assert::parameterElementType( 'string', $requirements, 'requirements' );
130
131        foreach ( $requirements as $name ) {
132            if ( !array_key_exists( $name, $this->requirements ) ) {
133                throw new \InvalidArgumentException( sprintf(
134                    'Feature "%s" references requirement "%s", which hasn\'t been registered',
135                    $feature,
136                    $name
137                ) );
138            }
139        }
140
141        // Mutation
142        $this->features[$feature] = $requirements;
143    }
144
145    /**
146     * Gets user's preference value
147     *
148     * If user preference is not set or did not appear in config
149     * set it to default value we go back to defualt suffix value
150     * that will ensure that the feature will be enabled when requirements are met
151     *
152     * @param string $preferenceKey User preference key
153     * @return string
154     */
155    public function getUserPreferenceValue( $preferenceKey ) {
156        return $this->userOptionsLookup->getOption(
157            $this->context->getUser(),
158            $preferenceKey
159            // For client preferences, this should be the same as `preferenceKey`
160            // in 'resources/skins.vector.js/clientPreferences.json'
161        );
162    }
163
164    /**
165     * Return a list of classes that should be added to the body tag
166     *
167     * @return array
168     */
169    public function getFeatureBodyClass() {
170        return array_map( function ( $featureName ) {
171            // switch to lower case and switch from camel case to hyphens
172            $featureClass = ltrim( strtolower( preg_replace( '/[A-Z]([A-Z](?![a-z]))*/', '-$0', $featureName ) ), '-' );
173            $prefix = 'vector-feature-' . $featureClass . '-';
174
175            // some features (eg night mode) will require request context to determine status
176            $request = $this->context->getRequest();
177            $config = $this->context->getConfig();
178            $title = $this->context->getTitle();
179
180            // Client side preferences
181            switch ( $featureName ) {
182                case CONSTANTS::FEATURE_FONT_SIZE:
183                    if ( ConfigHelper::shouldDisable(
184                        $config->get( 'VectorFontSizeConfigurableOptions' ), $request, $title
185                    ) ) {
186                        return $prefix . 'clientpref--excluded';
187                    }
188                    $suffixEnabled = 'clientpref-' . $this->getUserPreferenceValue( CONSTANTS::PREF_KEY_FONT_SIZE );
189                    $suffixDisabled = 'clientpref-0';
190                    break;
191                case CONSTANTS::PREF_NIGHT_MODE:
192                    // if night mode is disabled for the page, add the exclude class instead and return early
193                    if ( ConfigHelper::shouldDisable( $config->get( 'VectorNightModeOptions' ), $request, $title ) ) {
194                        // The additional "-" prefix, makes this an invalid client preference for anonymous users.
195                        return 'skin-theme-clientpref--excluded';
196                    }
197
198                    $prefix = '';
199                    $valueRequest = $request->getRawVal( 'vectornightmode' );
200                    // If night mode query string is used, hardcode pref value to the night mode value
201                    // NOTE: The query string parameter only works for logged in users.
202                    // IF you have set a cookie locally this will be overriden.
203                    $value = $valueRequest !== null ? self::resolveNightModeQueryValue( $valueRequest ) :
204                        $this->getUserPreferenceValue( CONSTANTS::PREF_KEY_NIGHT_MODE );
205                    $suffixEnabled = 'clientpref-' . $value;
206                    $suffixDisabled = 'clientpref-day';
207                    // Must be hardcoded to 'skin-theme-' to be consistent with Minerva
208                    // So that editors can target the same class across skins
209                    $prefix .= 'skin-theme-';
210                    break;
211                case CONSTANTS::FEATURE_LIMITED_WIDTH:
212                    if ( ConfigHelper::shouldDisable( $config->get( 'VectorMaxWidthOptions' ), $request, $title ) ) {
213                        return 'vector-feature-limited-width-clientpref--excluded';
214                    }
215
216                    $suffixEnabled = 'clientpref-1';
217                    $suffixDisabled = 'clientpref-0';
218
219                    break;
220                case CONSTANTS::FEATURE_TOC_PINNED:
221                case CONSTANTS::FEATURE_APPEARANCE_PINNED:
222                    $suffixEnabled = 'clientpref-1';
223                    $suffixDisabled = 'clientpref-0';
224                    break;
225                default:
226                    // FIXME: Eventually this should not be necessary.
227                    $suffixEnabled = 'enabled';
228                    $suffixDisabled = 'disabled';
229                    break;
230            }
231            return $this->isFeatureEnabled( $featureName ) ?
232                $prefix . $suffixEnabled : $prefix . $suffixDisabled;
233        }, array_keys( $this->features ) );
234    }
235
236    /**
237     * Gets whether the feature's requirements are met.
238     *
239     * @param string $feature
240     * @return bool
241     *
242     * @throws \InvalidArgumentException If the feature isn't registered
243     */
244    public function isFeatureEnabled( string $feature ): bool {
245        if ( !array_key_exists( $feature, $this->features ) ) {
246            throw new \InvalidArgumentException( "The feature \"{$feature}\" isn't registered." );
247        }
248
249        $requirements = $this->features[$feature];
250
251        foreach ( $requirements as $name ) {
252            if ( !$this->requirements[$name]->isMet() ) {
253                return false;
254            }
255        }
256
257        return true;
258    }
259
260    /**
261     * Register a complex {@see Requirement}.
262     *
263     * A complex requirement is one that depends on object that may or may not be fully loaded
264     * while the application is booting, e.g. see `User::isSafeToLoad`.
265     *
266     * Such requirements are expected to be registered during a hook that is run early on in the
267     * application lifecycle, e.g. the `BeforePerformAction` and `APIBeforeMain` hooks.
268     *
269     * @param Requirement $requirement
270     *
271     * @throws \LogicException If the requirement has already been registered
272     */
273    public function registerRequirement( Requirement $requirement ) {
274        $name = $requirement->getName();
275
276        if ( array_key_exists( $name, $this->requirements ) ) {
277            throw new \LogicException( "The requirement \"{$name}\" is already registered." );
278        }
279
280        $this->requirements[$name] = $requirement;
281    }
282
283    /**
284     * Register a {@see SimpleRequirement}.
285     *
286     * A requirement is some condition of the application state that a feature requires to be true
287     * or false.
288     *
289     * @param string $name The name of the requirement
290     * @param bool $isMet Whether the requirement is met
291     *
292     * @throws \LogicException If the requirement has already been registered
293     */
294    public function registerSimpleRequirement( string $name, bool $isMet ) {
295        $this->registerRequirement( new SimpleRequirement( $name, $isMet ) );
296    }
297
298    /**
299     * Gets whether the requirement is met.
300     *
301     * @param string $name The name of the requirement
302     * @return bool
303     *
304     * @throws \InvalidArgumentException If the requirement isn't registered
305     */
306    public function isRequirementMet( string $name ): bool {
307        if ( !array_key_exists( $name, $this->requirements ) ) {
308            throw new \InvalidArgumentException( "Requirement \"{$name}\" isn't registered." );
309        }
310
311        return $this->requirements[$name]->isMet();
312    }
313
314    /**
315     * Converts "1", "2", and "0" to equivalent values.
316     *
317     * @return string
318     */
319    private static function resolveNightModeQueryValue( string $value ) {
320        switch ( $value ) {
321            case 'day':
322            case 'night':
323            case 'os':
324                return $value;
325            case '1':
326                return 'night';
327            case '2':
328                return 'os';
329            default:
330                return 'day';
331        }
332    }
333}