Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.18% covered (warning)
68.18%
15 / 22
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PreferencesHooksHandler
68.18% covered (warning)
68.18%
15 / 22
66.67% covered (warning)
66.67%
2 / 3
4.52
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onUserGetDefaultOptions
22.22% covered (danger)
22.22%
2 / 9
0.00% covered (danger)
0.00%
0 / 1
3.88
 onGetPreferences
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\Math\HookHandlers;
4
5use MediaWiki\Extension\Math\MathConfig;
6use MediaWiki\Logger\LoggerFactory;
7use MediaWiki\Preferences\Hook\GetPreferencesHook;
8use MediaWiki\User\Hook\UserGetDefaultOptionsHook;
9
10class PreferencesHooksHandler implements
11    UserGetDefaultOptionsHook,
12    GetPreferencesHook
13{
14
15    /** @var MathConfig */
16    private $mathConfig;
17
18    /**
19     * @param MathConfig $mathConfig
20     */
21    public function __construct(
22        MathConfig $mathConfig
23    ) {
24        $this->mathConfig = $mathConfig;
25    }
26
27    /** @inheritDoc */
28    public function onUserGetDefaultOptions( &$defaultOptions ) {
29        // Normalize the default use option in case it's not a valid rendering mode. BUG 64844
30        $mode = $defaultOptions['math'] = MathConfig::normalizeRenderingMode( $defaultOptions['math'] );
31        if ( !$this->mathConfig->isValidRenderingMode( $mode ) ) {
32            $validModes = $this->mathConfig->getValidRenderingModes();
33            LoggerFactory::getInstance( 'Math' )
34                ->error( "Misconfiguration: wgDefaultUserOptions['math'] is not an enabled mode", [
35                    'valid_modes' => $validModes,
36                    'configured_default' => $mode,
37                ] );
38            $defaultOptions['math'] = $validModes[0];
39        }
40    }
41
42    /** @inheritDoc */
43    public function onGetPreferences( $user, &$preferences ) {
44        $preferences['math'] = [
45            'type' => 'radio',
46            'options-messages' => array_flip( $this->mathConfig->getValidRenderingModeKeys() ),
47            'label' => '&#160;',
48            'section' => 'rendering/math',
49        ];
50
51        $preferences['math-popups'] = [
52            'type' => 'toggle',
53            'label-message' => 'popups-settings-option-math-tooltip',
54            'section' => 'rendering/math',
55            'help-message' => 'popups-settings-option-math-tooltip-description'
56        ];
57    }
58}