Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 3
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onGetPreferences
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 1
110
 getLicenseMessage
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\MediaUploader\Hooks;
4
5use MediaWiki\Extension\MediaUploader\Config\ConfigBase;
6use MediaWiki\Extension\MediaUploader\Config\RawConfig;
7use MediaWiki\Preferences\Hook\GetPreferencesHook;
8use User;
9
10/**
11 * General MediaUploader hooks.
12 */
13class Hooks implements GetPreferencesHook {
14    /** @var RawConfig */
15    private $config;
16
17    /**
18     * @param RawConfig $rawConfig
19     */
20    public function __construct( RawConfig $rawConfig ) {
21        $this->config = $rawConfig;
22    }
23
24    /**
25     * @param User $user
26     * @param array &$preferences
27     *
28     * @return true
29     */
30    public function onGetPreferences( $user, &$preferences ) {
31        // User preference to skip the licensing tutorial, provided it's not globally disabled
32        if ( $this->config->getSetting( 'tutorial' ) ) {
33            $preferences['upwiz_skiptutorial'] = [
34                'type' => 'check',
35                'label-message' => 'mediauploader-prefs-skiptutorial',
36                'section' => 'uploads/mediauploader-interface'
37            ];
38        }
39
40        $preferences['upwiz_licensename'] = [
41            'type' => 'text',
42            'label-message' => 'mediauploader-prefs-license-name',
43            'help-message' => 'mediauploader-prefs-license-name-help',
44            'section' => 'uploads/mediauploader-licensing'
45        ];
46
47        $licenses = [];
48        $licenseTypes = [
49            [ 'configKey' => ConfigBase::LIC_OWN_WORK, 'msgKey' => 'ownwork' ],
50            [ 'configKey' => ConfigBase::LIC_THIRD_PARTY, 'msgKey' => 'thirdparty' ],
51        ];
52        $hasCustom = false;
53
54        foreach ( $licenseTypes as $lType ) {
55            foreach ( $this->config->getAvailableLicenses( $lType['configKey'] ) as $license ) {
56                if ( $license === 'custom' ) {
57                    $hasCustom = true;
58                    continue;
59                }
60
61                $lMsg = $this->getLicenseMessage( $license ) ?: '';
62                $lKey = wfMessage( 'mediauploader-prefs-license-' . $lType['msgKey'] )
63                    ->rawParams( $lMsg )->escaped();
64                $lValue = htmlspecialchars(
65                    $lType['configKey'] . '-' . $license, ENT_QUOTES, 'UTF-8', false
66                );
67                $licenses[$lKey] = $lValue;
68            }
69        }
70
71        $licenses = array_merge(
72            [ wfMessage( 'mediauploader-prefs-def-license-def' )->escaped() => 'default' ],
73            $licenses
74        );
75
76        if ( $hasCustom ) {
77            // The "custom license" option must be last, otherwise the text referring to "following
78            // wikitext" and "last option above" makes no sense.
79            $licenseMessage = $this->getLicenseMessage( 'custom' ) ?: '';
80            $licenseKey = wfMessage( 'mediauploader-prefs-license-thirdparty' )
81                ->rawParams( $licenseMessage )->escaped();
82            $licenses[$licenseKey] = 'thirdparty-custom';
83        }
84
85        $preferences['upwiz_deflicense'] = [
86            'type' => 'radio',
87            'label-message' => 'mediauploader-prefs-def-license',
88            'section' => 'uploads/mediauploader-licensing',
89            'options' => $licenses
90        ];
91
92        if ( $hasCustom ) {
93            $preferences['upwiz_deflicense_custom'] = [
94                'type' => 'text',
95                'label-message' => 'mediauploader-prefs-def-license-custom',
96                'help-message' => 'mediauploader-prefs-def-license-custom-help',
97                'section' => 'uploads/mediauploader-licensing',
98            ];
99        }
100
101        // Setting for maximum number of simultaneous uploads (always lower than the server-side config)
102        if ( ( $this->config->getSetting( 'maxSimultaneousConnections', 0 ) ) > 1 ) {
103            // Hack to make the key and value the same otherwise options are added wrongly.
104            $range = range( 0, $this->config->getSetting( 'maxSimultaneousConnections' ) );
105            $range[0] = 'default';
106
107            $preferences['upwiz_maxsimultaneous'] = [
108                'type' => 'select',
109                'label-message' => 'mediauploader-prefs-maxsimultaneous-upload',
110                'section' => 'uploads/mediauploader-experimental',
111                'options' => $range
112            ];
113        }
114
115        return true;
116    }
117
118    /**
119     * Helper to return the parsed text of a license message.
120     *
121     * @param string $licenseName
122     *
123     * @return string|null
124     */
125    private function getLicenseMessage( string $licenseName ): ?string {
126        $license = $this->config->getSetting( 'licenses', [] )[$licenseName] ?? null;
127        if ( $license === null ) {
128            return null;
129        }
130
131        if ( array_key_exists( 'url', $license ) ) {
132            return wfMessage( $license['msg'], '', $license['url'] )->parse();
133        } else {
134            return wfMessage( $license['msg'] )->parse();
135        }
136    }
137}