Debian Codegraph @

extensions/MultimediaViewer/includes/Hooks.php

raw symbols SCIP
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 <[email protected]>
21 * @copyright Copyright © 2013, Mark Holmquist
22 */
23
24 namespace MediaWiki\Extension\MultimediaViewer;
25
26 use CategoryPage;
27 use MediaWiki\Category\Category;
28 use MediaWiki\Config\Config;
29 use MediaWiki\Hook\ThumbnailBeforeProduceHTMLHook;
30 use MediaWiki\Output\Hook\BeforePageDisplayHook;
31 use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook;
32 use MediaWiki\Output\OutputPage;
33 use MediaWiki\Page\Hook\CategoryPageViewHook;
34 use MediaWiki\Preferences\Hook\GetPreferencesHook;
35 use MediaWiki\Registration\ExtensionRegistry;
36 use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
37 use MediaWiki\SpecialPage\SpecialPageFactory;
38 use MediaWiki\User\Hook\UserGetDefaultOptionsHook;
39 use MediaWiki\User\Options\UserOptionsLookup;
40 use MediaWiki\User\User;
41 use MobileContext;
42 use Skin;
43 use ThumbnailImage;
44
45 class Hooks implements
46 MakeGlobalVariablesScriptHook,
47 UserGetDefaultOptionsHook,
48 GetPreferencesHook,
49 BeforePageDisplayHook,
50 CategoryPageViewHook,
51 ResourceLoaderGetConfigVarsHook,
52 ThumbnailBeforeProduceHTMLHook
53 {
54 private Config $config;
55 private SpecialPageFactory $specialPageFactory;
56 private UserOptionsLookup $userOptionsLookup;
57 private ?MobileContext $mobileContext;
58
59 /**
60 * @param Config $config
61 * @param SpecialPageFactory $specialPageFactory
62 * @param UserOptionsLookup $userOptionsLookup
63 */
64 public function __construct(
65 Config $config,
66 SpecialPageFactory $specialPageFactory,
67 UserOptionsLookup $userOptionsLookup,
68 ?MobileContext $mobileContext
69 ) {
70 $this->config = $config;
71 $this->specialPageFactory = $specialPageFactory;
72 $this->userOptionsLookup = $userOptionsLookup;
73 $this->mobileContext = $mobileContext;
74 }
75
76 /**
77 * @see https://www.mediawiki.org/wiki/Manual:Hooks/UserGetDefaultOptions
78 * @param array &$defaultOptions
79 */
80 public function onUserGetDefaultOptions( &$defaultOptions ) {
81 if ( $this->config->get( 'MediaViewerEnableByDefault' ) ) {
82 $defaultOptions['multimediaviewer-enable'] = 1;
83 }
84 }
85
86 /**
87 * Checks the context for whether to load the viewer.
88 * @param User $performer
89 * @return bool
90 */
91 protected function shouldHandleClicks( User $performer ): bool {
92 if ( $performer->isNamed() ) {
93 return (bool)$this->userOptionsLookup->getOption( $performer, 'multimediaviewer-enable' );
94 }
95
96 return (bool)(
97 $this->config->get( 'MediaViewerEnableByDefaultForAnonymous' ) ??
98 $this->config->get( 'MediaViewerEnableByDefault' )
99 );
100 }
101
102 /**
103 * Handler for all places where we add the modules
104 * Could be on article pages or on Category pages
105 * @param OutputPage $out
106 */
107 protected function getModules( OutputPage $out ) {
108 // The MobileFrontend extension provides its own implementation of MultimediaViewer.
109 // See https://phabricator.wikimedia.org/T65504 and subtasks for more details.
110 // To avoid loading MMV twice, we check the environment we are running in.
111 $isMobileFrontendView = ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) &&
112 $this->mobileContext && $this->mobileContext->shouldDisplayMobileView();
113 if ( !$isMobileFrontendView ) {
114 $out->addModules( [ 'mmv.bootstrap' ] );
115 }
116 }
117
118 /**
119 * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
120 * Add JavaScript to the page when an image is on it
121 * and the user has enabled the feature
122 * @param OutputPage $out
123 * @param Skin $skin
124 */
125 public function onBeforePageDisplay( $out, $skin ): void {
126 $pageHasThumbnails = count( $out->getFileSearchOptions() ) > 0;
127 $pageIsFilePage = $out->getTitle()->inNamespace( NS_FILE );
128 // TODO: Have Flow work out if there are any images on the page
129 $pageIsFlowPage = ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) &&
130 // CONTENT_MODEL_FLOW_BOARD
131 $out->getTitle()->getContentModel() === 'flow-board';
132 $fileRelatedSpecialPages = [ 'Newimages', 'Listfiles', 'Mostimages',
133 'MostGloballyLinkedFiles', 'Uncategorizedimages', 'Unusedimages', 'Search' ];
134 $pageIsFileRelatedSpecialPage = $out->getTitle()->inNamespace( NS_SPECIAL )
135 && in_array( $this->specialPageFactory->resolveAlias( $out->getTitle()->getDBkey() )[0],
136 $fileRelatedSpecialPages );
137
138 if ( $pageHasThumbnails || $pageIsFilePage || $pageIsFileRelatedSpecialPage || $pageIsFlowPage ) {
139 $this->getModules( $out );
140 }
141 }
142
143 /**
144 * @see https://www.mediawiki.org/wiki/Manual:Hooks/CategoryPageView
145 * Add JavaScript to the page if there are images in the category
146 * @param CategoryPage $catPage
147 */
148 public function onCategoryPageView( $catPage ) {
149 $title = $catPage->getTitle();
150 $cat = Category::newFromTitle( $title );
151 if ( $cat->getFileCount() > 0 ) {
152 $out = $catPage->getContext()->getOutput();
153 $this->getModules( $out );
154 }
155 }
156
157 /**
158 * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
159 * Adds a default-enabled preference to gate the feature
160 * @param User $user
161 * @param array &$prefs
162 */
163 public function onGetPreferences( $user, &$prefs ) {
164 $prefs['multimediaviewer-enable'] = [
165 'type' => 'toggle',
166 'label-message' => 'multimediaviewer-optin-pref',
167 'section' => 'rendering/files',
168 ];
169 }
170
171 /**
172 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
173 * Export variables used in both PHP and JS to keep DRY
174 * @param array &$vars
175 * @param string $skin
176 * @param Config $config
177 */
178 public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
179 $vars['wgMediaViewer'] = true;
180 }
181
182 /**
183 * @see https://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
184 * Export variables which depend on the current user
185 * @param array &$vars
186 * @param OutputPage $out
187 * @return void
188 */
189 public function onMakeGlobalVariablesScript( &$vars, $out ): void {
190 $user = $out->getUser();
191 $isMultimediaViewerEnable = $this->userOptionsLookup->getDefaultOption(
192 'multimediaviewer-enable',
193 $user
194 );
195
196 $vars['wgMediaViewerOnClick'] = $this->shouldHandleClicks( $user );
197 // needed because of T71942; could be different for anon and logged-in
198 $vars['wgMediaViewerEnabledByDefault'] = (bool)$isMultimediaViewerEnable;
199 }
200
201 /**
202 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ThumbnailBeforeProduceHTML
203 * Modify thumbnail DOM
204 * @param ThumbnailImage $thumbnail
205 * @param array &$attribs Attributes of the <img> element
206 * @param array|bool &$linkAttribs Attributes of the wrapping <a> element
207 */
208 public function onThumbnailBeforeProduceHTML(
209 $thumbnail,
210 &$attribs,
211 &$linkAttribs
212 ) {
213 $file = $thumbnail->getFile();
214
215 if ( $file ) {
216 $attribs['data-file-width'] = $file->getWidth();
217 $attribs['data-file-height'] = $file->getHeight();
218 }
219 }
220 }