Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 91
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 91
0.00% covered (danger)
0.00%
0 / 8
380
0.00% covered (danger)
0.00%
0 / 1
 onGetPreferences
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 1
90
 onPreferencesGetIcon
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onIsUploadAllowedFromUrl
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getLicenseMessage
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 addListDefinedTags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onListDefinedTags
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onChangeTagsListActive
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onChangeTagsAllowedAdd
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\UploadWizard;
4
5use MediaWiki\ChangeTags\Hook\ChangeTagsAllowedAddHook;
6use MediaWiki\ChangeTags\Hook\ChangeTagsListActiveHook;
7use MediaWiki\ChangeTags\Hook\ListDefinedTagsHook;
8use MediaWiki\Hook\IsUploadAllowedFromUrlHook;
9use MediaWiki\Hook\PreferencesGetIconHook;
10use MediaWiki\Preferences\Hook\GetPreferencesHook;
11use MediaWiki\User\User;
12use RequestContext;
13
14class Hooks implements
15    GetPreferencesHook,
16    IsUploadAllowedFromUrlHook,
17    ListDefinedTagsHook,
18    ChangeTagsListActiveHook,
19    ChangeTagsAllowedAddHook,
20    PreferencesGetIconHook
21{
22
23    /**
24     * Adds the preferences of UploadWizard to the list of available ones.
25     * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
26     *
27     * @since 1.2
28     *
29     * @param User $user
30     * @param array &$preferences
31     */
32    public function onGetPreferences( $user, &$preferences ) {
33        $config = Config::getConfig();
34
35        // User preference to skip the licensing tutorial, provided it's not globally disabled
36        if ( Config::getSetting( 'tutorial' ) != [] ) {
37            $preferences['upwiz_skiptutorial'] = [
38                'type' => 'check',
39                'label-message' => 'mwe-upwiz-prefs-skiptutorial',
40                'section' => 'uploads/upwiz-interface'
41            ];
42        }
43
44        $preferences['upwiz_licensename'] = [
45            'type' => 'text',
46            'label-message' => 'mwe-upwiz-prefs-license-name',
47            'help-message' => 'mwe-upwiz-prefs-license-name-help',
48            'section' => 'uploads/upwiz-licensing'
49        ];
50
51        if ( Config::getSetting( 'enableLicensePreference' ) ) {
52            $licenseConfig = Config::getSetting( 'licenses' );
53
54            $licenses = [];
55
56            $licensingOptions = Config::getSetting( 'licensing' );
57
58            $ownWork = $licensingOptions['ownWork'];
59            foreach ( $ownWork['licenses'] as $license ) {
60                $licenseMessage = self::getLicenseMessage( $license, $licenseConfig );
61                $licenseKey = wfMessage( 'mwe-upwiz-prefs-license-own' )
62                    ->rawParams( $licenseMessage )->escaped();
63                $licenseValue = htmlspecialchars( 'ownwork-' . $license, ENT_QUOTES, 'UTF-8', false );
64                $licenses[$licenseKey] = $licenseValue;
65            }
66
67            $thirdParty = Config::getThirdPartyLicenses();
68            $hasCustom = false;
69            foreach ( $thirdParty as $license ) {
70                if ( $license !== 'custom' ) {
71                    $licenseMessage = self::getLicenseMessage( $license, $licenseConfig );
72                    $licenseKey = wfMessage( 'mwe-upwiz-prefs-license-thirdparty' )
73                        ->rawParams( $licenseMessage )->escaped();
74                    $licenseValue = htmlspecialchars( 'thirdparty-' . $license, ENT_QUOTES, 'UTF-8', false );
75                    $licenses[$licenseKey] = $licenseValue;
76                } else {
77                    $hasCustom = true;
78                }
79            }
80
81            $licenses = array_merge(
82                [
83                    wfMessage( 'mwe-upwiz-prefs-def-license-def' )->escaped() => 'default'
84                ],
85                $licenses
86            );
87
88            if ( $hasCustom ) {
89                // The "custom license" option must be last, otherwise the text referring to "following
90                // wikitext" and "last option above" makes no sense.
91                $licenseMessage = self::getLicenseMessage( 'custom', $licenseConfig );
92                $licenseKey = wfMessage( 'mwe-upwiz-prefs-license-thirdparty' )
93                    ->rawParams( $licenseMessage )->escaped();
94                $licenses[$licenseKey] = 'thirdparty-custom';
95            }
96
97            $preferences['upwiz_deflicense'] = [
98                'type' => 'radio',
99                'label-message' => 'mwe-upwiz-prefs-def-license',
100                'section' => 'uploads/upwiz-licensing',
101                'options' => $licenses
102            ];
103
104            if ( $hasCustom ) {
105                $preferences['upwiz_deflicense_custom'] = [
106                    'type' => 'text',
107                    'label-message' => 'mwe-upwiz-prefs-def-license-custom',
108                    'help-message' => 'mwe-upwiz-prefs-def-license-custom-help',
109                    'section' => 'uploads/upwiz-licensing',
110                ];
111            }
112        }
113
114        // Setting for maximum number of simultaneous uploads (always lower than the server-side config)
115        if ( $config[ 'maxSimultaneousConnections' ] > 1 ) {
116            // Hack to make the key and value the same otherwise options are added wrongly.
117            $range = range( 0, $config[ 'maxSimultaneousConnections' ] );
118            $range[0] = 'default';
119
120            $preferences['upwiz_maxsimultaneous'] = [
121                'type' => 'select',
122                'label-message' => 'mwe-upwiz-prefs-maxsimultaneous-upload',
123                'section' => 'uploads/upwiz-experimental',
124                'options' => $range
125            ];
126        }
127
128        // Store user dismissal of machine vision CTA on final step.
129        $preferences['upwiz_mv_cta_dismissed'] = [
130            'type' => 'api'
131        ];
132    }
133
134    /**
135     * Add icon for Special:Preferences mobile layout
136     *
137     * @param array &$iconNames Array of icon names for their respective sections.
138     */
139    public function onPreferencesGetIcon( &$iconNames ) {
140        $iconNames[ 'uploads' ] = 'upload';
141    }
142
143    /**
144     * Hook to blacklist flickr images by intercepting upload from url
145     * @param string $url
146     * @param bool &$allowed
147     */
148    public function onIsUploadAllowedFromUrl( $url, &$allowed ) {
149        if ( $allowed ) {
150            $flickrBlacklist = new FlickrBlacklist(
151                Config::getConfig(),
152                RequestContext::getMain()
153            );
154            if ( $flickrBlacklist->isBlacklisted( $url ) ) {
155                $allowed = false;
156            }
157        }
158    }
159
160    /**
161     * Helper function to get the message for a license.
162     *
163     * @since 1.2
164     *
165     * @param string $licenseName
166     * @param array $licenseConfig
167     *
168     * @return string
169     */
170    public static function getLicenseMessage( $licenseName, array $licenseConfig ) {
171        if ( array_key_exists( 'url', $licenseConfig[$licenseName] ) ) {
172            return wfMessage(
173                $licenseConfig[$licenseName]['msg'],
174                '',
175                $licenseConfig[$licenseName]['url'],
176                ''
177            )->parse();
178        }
179
180        return wfMessage( $licenseConfig[$licenseName]['msg'] )->escaped();
181    }
182
183    /**
184     * Lists tags used by UploadWizard (via ListDefinedTags,
185     * ListExplicitlyDefinedTags & ChangeTagsListActive hooks)
186     *
187     * @param string[] &$tags
188     */
189    public static function addListDefinedTags( &$tags ) {
190        $tags[] = 'uploadwizard';
191        $tags[] = 'uploadwizard-flickr';
192    }
193
194    /**
195     * @param string[] &$tags
196     */
197    public function onListDefinedTags( &$tags ) {
198        $this->addListDefinedTags( $tags );
199    }
200
201    /**
202     * @param string[] &$tags
203     */
204    public function onChangeTagsListActive( &$tags ) {
205        $this->addListDefinedTags( $tags );
206    }
207
208    /**
209     * @param string[] &$allowedTags
210     * @param string[] $addTags
211     * @param User|null $user
212     */
213    public function onChangeTagsAllowedAdd( &$allowedTags, $addTags, $user ) {
214        $this->addListDefinedTags( $allowedTags );
215    }
216}