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    public function onUserGetDefaultOptions( &$defaultOptions ) {
28        // Normalize the default use option in case it's not a valid rendering mode. BUG 64844
29        $mode = $defaultOptions['math'] = MathConfig::normalizeRenderingMode( $defaultOptions['math'] );
30        if ( !$this->mathConfig->isValidRenderingMode( $mode ) ) {
31            $validModes = $this->mathConfig->getValidRenderingModes();
32            LoggerFactory::getInstance( 'Math' )
33                ->error( "Misconfiguration: wgDefaultUserOptions['math'] is not an enabled mode", [
34                    'valid_modes' => $validModes,
35                    'configured_default' => $mode,
36                ] );
37            $defaultOptions['math'] = $validModes[0];
38        }
39    }
40
41    public function onGetPreferences( $user, &$preferences ) {
42        $preferences['math'] = [
43            'type' => 'radio',
44            'options-messages' => array_flip( $this->mathConfig->getValidRenderingModeKeys() ),
45            'label' => '&#160;',
46            'section' => 'rendering/math',
47        ];
48
49        $preferences['math-popups'] = [
50            'type' => 'toggle',
51            'label-message' => 'popups-settings-option-math-tooltip',
52            'section' => 'rendering/math',
53            'help-message' => 'popups-settings-option-math-tooltip-description'
54        ];
55    }
56}