Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
32.76% covered (danger)
32.76%
19 / 58
30.00% covered (danger)
30.00%
3 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
32.76% covered (danger)
32.76%
19 / 58
30.00% covered (danger)
30.00%
3 / 10
183.83
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 onUserGetDefaultOptions
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 shouldHandleClicks
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getModules
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 onBeforePageDisplay
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
7
 onCategoryPageView
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 onGetPreferences
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 onResourceLoaderGetConfigVars
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 onMakeGlobalVariablesScript
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 onThumbnailBeforeProduceHTML
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This file is part of the MediaWiki extension MultimediaViewer.
4 *
5 * MultimediaViewer is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * MultimediaViewer is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with MultimediaViewer.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @file
19 * @ingroup extensions
20 * @author Mark Holmquist <mtraceur@member.fsf.org>
21 * @copyright Copyright © 2013, Mark Holmquist
22 */
23
24namespace MediaWiki\Extension\MultimediaViewer;
25
26use CategoryPage;
27use ExtensionRegistry;
28use MediaWiki\Category\Category;
29use MediaWiki\Config\Config;
30use MediaWiki\Hook\BeforePageDisplayHook;
31use MediaWiki\Hook\MakeGlobalVariablesScriptHook;
32use MediaWiki\Hook\ThumbnailBeforeProduceHTMLHook;
33use MediaWiki\Output\OutputPage;
34use MediaWiki\Page\Hook\CategoryPageViewHook;
35use MediaWiki\Preferences\Hook\GetPreferencesHook;
36use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
37use MediaWiki\SpecialPage\SpecialPageFactory;
38use MediaWiki\User\Hook\UserGetDefaultOptionsHook;
39use MediaWiki\User\Options\UserOptionsLookup;
40use MediaWiki\User\User;
41use MobileContext;
42use Skin;
43use ThumbnailImage;
44
45class Hooks implements
46    MakeGlobalVariablesScriptHook,
47    UserGetDefaultOptionsHook,
48    GetPreferencesHook,
49    BeforePageDisplayHook,
50    CategoryPageViewHook,
51    ResourceLoaderGetConfigVarsHook,
52    ThumbnailBeforeProduceHTMLHook
53{
54    /** @var string Link to more information about this module */
55    protected static $infoLink =
56        'https://mediawiki.org/wiki/Special:MyLanguage/Extension:Media_Viewer/About';
57
58    /** @var string Link to a page where this module can be discussed */
59    protected static $discussionLink =
60        'https://mediawiki.org/wiki/Special:MyLanguage/Extension_talk:Media_Viewer/About';
61
62    /** @var string Link to help about this module */
63    protected static $helpLink =
64        'https://mediawiki.org/wiki/Special:MyLanguage/Help:Extension:Media_Viewer';
65
66    private Config $config;
67    private SpecialPageFactory $specialPageFactory;
68    private UserOptionsLookup $userOptionsLookup;
69    private ?MobileContext $mobileContext;
70
71    /**
72     * @param Config $config
73     * @param SpecialPageFactory $specialPageFactory
74     * @param UserOptionsLookup $userOptionsLookup
75     */
76    public function __construct(
77        Config $config,
78        SpecialPageFactory $specialPageFactory,
79        UserOptionsLookup $userOptionsLookup,
80        ?MobileContext $mobileContext
81    ) {
82        $this->config = $config;
83        $this->specialPageFactory = $specialPageFactory;
84        $this->userOptionsLookup = $userOptionsLookup;
85        $this->mobileContext = $mobileContext;
86    }
87
88    /**
89     * @see https://www.mediawiki.org/wiki/Manual:Hooks/UserGetDefaultOptions
90     * @param array &$defaultOptions
91     */
92    public function onUserGetDefaultOptions( &$defaultOptions ) {
93        if ( $this->config->get( 'MediaViewerEnableByDefault' ) ) {
94            $defaultOptions['multimediaviewer-enable'] = 1;
95        }
96    }
97
98    /**
99     * Checks the context for whether to load the viewer.
100     * @param User $performer
101     * @return bool
102     */
103    protected function shouldHandleClicks( User $performer ): bool {
104        if ( $performer->isNamed() ) {
105            return (bool)$this->userOptionsLookup->getOption( $performer, 'multimediaviewer-enable' );
106        }
107
108        return (bool)(
109            $this->config->get( 'MediaViewerEnableByDefaultForAnonymous' ) ??
110            $this->config->get( 'MediaViewerEnableByDefault' )
111        );
112    }
113
114    /**
115     * Handler for all places where we add the modules
116     * Could be on article pages or on Category pages
117     * @param OutputPage $out
118     */
119    protected function getModules( OutputPage $out ) {
120        // The MobileFrontend extension provides its own implementation of MultimediaViewer.
121        // See https://phabricator.wikimedia.org/T65504 and subtasks for more details.
122        // To avoid loading MMV twice, we check the environment we are running in.
123        $isMobileFrontendView = ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) &&
124            $this->mobileContext && $this->mobileContext->shouldDisplayMobileView();
125        if ( !$isMobileFrontendView ) {
126            $out->addModules( [ 'mmv.head', 'mmv.bootstrap.autostart' ] );
127        }
128    }
129
130    /**
131     * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
132     * Add JavaScript to the page when an image is on it
133     * and the user has enabled the feature
134     * @param OutputPage $out
135     * @param Skin $skin
136     */
137    public function onBeforePageDisplay( $out, $skin ): void {
138        $pageHasThumbnails = count( $out->getFileSearchOptions() ) > 0;
139        $pageIsFilePage = $out->getTitle()->inNamespace( NS_FILE );
140        // TODO: Have Flow work out if there are any images on the page
141        $pageIsFlowPage = ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) &&
142            // CONTENT_MODEL_FLOW_BOARD
143            $out->getTitle()->getContentModel() === 'flow-board';
144        $fileRelatedSpecialPages = [ 'Newimages', 'Listfiles', 'Mostimages',
145            'MostGloballyLinkedFiles', 'Uncategorizedimages', 'Unusedimages', 'Search' ];
146        $pageIsFileRelatedSpecialPage = $out->getTitle()->inNamespace( NS_SPECIAL )
147            && in_array( $this->specialPageFactory->resolveAlias( $out->getTitle()->getDBkey() )[0],
148                $fileRelatedSpecialPages );
149
150        if ( $pageHasThumbnails || $pageIsFilePage || $pageIsFileRelatedSpecialPage || $pageIsFlowPage ) {
151            $this->getModules( $out );
152        }
153    }
154
155    /**
156     * @see https://www.mediawiki.org/wiki/Manual:Hooks/CategoryPageView
157     * Add JavaScript to the page if there are images in the category
158     * @param CategoryPage $catPage
159     */
160    public function onCategoryPageView( $catPage ) {
161        $title = $catPage->getTitle();
162        $cat = Category::newFromTitle( $title );
163        if ( $cat->getFileCount() > 0 ) {
164            $out = $catPage->getContext()->getOutput();
165            $this->getModules( $out );
166        }
167    }
168
169    /**
170     * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
171     * Adds a default-enabled preference to gate the feature
172     * @param User $user
173     * @param array &$prefs
174     */
175    public function onGetPreferences( $user, &$prefs ) {
176        $prefs['multimediaviewer-enable'] = [
177            'type' => 'toggle',
178            'label-message' => 'multimediaviewer-optin-pref',
179            'section' => 'rendering/files',
180        ];
181    }
182
183    /**
184     * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
185     * Export variables used in both PHP and JS to keep DRY
186     * @param array &$vars
187     * @param string $skin
188     * @param Config $config
189     */
190    public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
191        $vars['wgMultimediaViewer'] = [
192            'infoLink' => self::$infoLink,
193            'discussionLink' => self::$discussionLink,
194            'helpLink' => self::$helpLink,
195            'useThumbnailGuessing' => (bool)$this->config->get( 'MediaViewerUseThumbnailGuessing' ),
196            'imageQueryParameter' => $this->config->get( 'MediaViewerImageQueryParameter' ),
197            'recordVirtualViewBeaconURI' => $this->config->get( 'MediaViewerRecordVirtualViewBeaconURI' ),
198            'extensions' => $this->config->get( 'MediaViewerExtensions' ),
199        ];
200        $vars['wgMediaViewer'] = true;
201    }
202
203    /**
204     * @see https://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
205     * Export variables which depend on the current user
206     * @param array &$vars
207     * @param OutputPage $out
208     * @return void
209     */
210    public function onMakeGlobalVariablesScript( &$vars, $out ): void {
211        $user = $out->getUser();
212        $isMultimediaViewerEnable = $this->userOptionsLookup->getDefaultOption(
213            'multimediaviewer-enable',
214            $user
215        );
216
217        $vars['wgMediaViewerOnClick'] = $this->shouldHandleClicks( $user );
218        // needed because of T71942; could be different for anon and logged-in
219        $vars['wgMediaViewerEnabledByDefault'] = (bool)$isMultimediaViewerEnable;
220    }
221
222    /**
223     * @see https://www.mediawiki.org/wiki/Manual:Hooks/ThumbnailBeforeProduceHTML
224     * Modify thumbnail DOM
225     * @param ThumbnailImage $thumbnail
226     * @param array &$attribs Attributes of the <img> element
227     * @param array|bool &$linkAttribs Attributes of the wrapping <a> element
228     */
229    public function onThumbnailBeforeProduceHTML(
230        $thumbnail,
231        &$attribs,
232        &$linkAttribs
233    ) {
234        $file = $thumbnail->getFile();
235
236        if ( $file ) {
237            $attribs['data-file-width'] = $file->getWidth();
238            $attribs['data-file-height'] = $file->getHeight();
239        }
240    }
241}