MediaWiki REL1_34
MultimediaViewerHooks.php
Go to the documentation of this file.
1<?php
26 protected static $infoLink =
27 'https://mediawiki.org/wiki/Special:MyLanguage/Extension:Media_Viewer/About';
28
30 protected static $discussionLink =
31 'https://mediawiki.org/wiki/Special:MyLanguage/Extension_talk:Media_Viewer/About';
32
34 protected static $helpLink =
35 'https://mediawiki.org/wiki/Special:MyLanguage/Help:Extension:Media_Viewer';
36
41 public static function onUserGetDefaultOptions( array &$defaultOptions ) {
43
45 $defaultOptions['multimediaviewer-enable'] = 1;
46 }
47 }
48
54 protected static function shouldHandleClicks( User $user ) {
57
59 $enableByDefaultForAnons = $wgMediaViewerEnableByDefault;
60 } else {
61 $enableByDefaultForAnons = $wgMediaViewerEnableByDefaultForAnonymous;
62 }
63
64 if ( !$user->isLoggedIn() ) {
65 return (bool)$enableByDefaultForAnons;
66 } else {
67 return (bool)$user->getOption( 'multimediaviewer-enable' );
68 }
69 }
70
76 protected static function getModules( OutputPage $out ) {
77 $out->addModules( [ 'mmv.head', 'mmv.bootstrap.autostart' ] );
78 }
79
87 public static function onBeforePageDisplay( OutputPage $out, $skin ) {
88 $pageHasThumbnails = count( $out->getFileSearchOptions() ) > 0;
89 $pageIsFilePage = $out->getTitle()->inNamespace( NS_FILE );
90 // TODO: Have Flow work out if there are any images on the page
91 $pageIsFlowPage = ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) &&
92 // CONTENT_MODEL_FLOW_BOARD
93 $out->getTitle()->getContentModel() === 'flow-board';
94 $fileRelatedSpecialPages = [ 'NewFiles', 'ListFiles', 'MostLinkedFiles',
95 'MostGloballyLinkedFiles', 'UncategorizedFiles', 'UnusedFiles', 'Search' ];
96 $pageIsFileRelatedSpecialPage = $out->getTitle()->inNamespace( NS_SPECIAL )
97 && in_array( $out->getTitle()->getText(), $fileRelatedSpecialPages );
98
99 if ( $pageHasThumbnails || $pageIsFilePage || $pageIsFileRelatedSpecialPage || $pageIsFlowPage ) {
100 self::getModules( $out );
101 }
102 }
103
109 public static function onCategoryPageView( CategoryPage $catPage ) {
110 $title = $catPage->getTitle();
111 $cat = Category::newFromTitle( $title );
112 if ( $cat->getFileCount() > 0 ) {
113 $out = $catPage->getContext()->getOutput();
114 self::getModules( $out );
115 }
116 }
117
124 public static function onGetPreferences( $user, &$prefs ) {
125 $prefs['multimediaviewer-enable'] = [
126 'type' => 'toggle',
127 'label-message' => 'multimediaviewer-optin-pref',
128 'section' => 'rendering/files',
129 ];
130 }
131
137 public static function onResourceLoaderGetConfigVars( array &$vars ) {
144 $wgMediaViewerUseThumbnailGuessing, $wgMediaViewerExtensions,
146
147 $vars['wgMultimediaViewer'] = [
148 'infoLink' => self::$infoLink,
149 'discussionLink' => self::$discussionLink,
150 'helpLink' => self::$helpLink,
151 'useThumbnailGuessing' => (bool)$wgMediaViewerUseThumbnailGuessing,
152 'durationSamplingFactor' => $wgMediaViewerDurationLoggingSamplingFactor,
153 'durationSamplingFactorLoggedin' => $wgMediaViewerDurationLoggingLoggedinSamplingFactor,
154 'networkPerformanceSamplingFactor' => $wgMediaViewerNetworkPerformanceSamplingFactor,
155 'actionLoggingSamplingFactorMap' => $wgMediaViewerActionLoggingSamplingFactorMap,
156 'attributionSamplingFactor' => $wgMediaViewerAttributionLoggingSamplingFactor,
157 'dimensionSamplingFactor' => $wgMediaViewerDimensionLoggingSamplingFactor,
158 'imageQueryParameter' => $wgMediaViewerImageQueryParameter,
159 'recordVirtualViewBeaconURI' => $wgMediaViewerRecordVirtualViewBeaconURI,
160 'tooltipDelay' => 1000,
161 'extensions' => $wgMediaViewerExtensions,
162 ];
163 $vars['wgMediaViewer'] = true;
164 }
165
172 public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $out ) {
173 $defaultUserOptions = User::getDefaultOptions();
174
175 $user = $out->getUser();
176 $vars['wgMediaViewerOnClick'] = self::shouldHandleClicks( $user );
177 // needed because of T71942; could be different for anon and logged-in
178 $vars['wgMediaViewerEnabledByDefault'] =
179 !empty( $defaultUserOptions['multimediaviewer-enable'] );
180 }
181
189 public static function onThumbnailBeforeProduceHTML(
190 ThumbnailImage $thumbnail,
191 array &$attribs,
192 &$linkAttribs
193 ) {
194 $file = $thumbnail->getFile();
195
196 if ( $file ) {
197 // At the moment all classes that extend File have getWidth() and getHeight()
198 // but since the File class doesn't have these methods defined, this check
199 // is more future-proof
200
201 if ( method_exists( $file, 'getWidth' ) ) {
202 $attribs['data-file-width'] = $file->getWidth();
203 }
204
205 if ( method_exists( $file, 'getHeight' ) ) {
206 $attribs['data-file-height'] = $file->getHeight();
207 }
208 }
209 }
210}
int bool $wgMediaViewerDurationLoggingSamplingFactor
If set, records loading times via EventLogging.
int bool $wgMediaViewerAttributionLoggingSamplingFactor
If set, records whether image attribution data was available.
bool $wgMediaViewerEnableByDefault
If true, Media Viewer will be turned on by default.
bool null $wgMediaViewerEnableByDefaultForAnonymous
Overrides $wgMediaViewerEnableByDefault for anonymous users.
int bool $wgMediaViewerDurationLoggingLoggedinSamplingFactor
If set, records loading times via EventLogging with factor specific to loggedin users.
bool $wgMediaViewerUseThumbnailGuessing
When this is enabled, MediaViewer will try to guess image URLs instead of making an imageinfo API to ...
int bool $wgMediaViewerNetworkPerformanceSamplingFactor
If set, records image load network performance via EventLogging once per this many requests.
array bool $wgMediaViewerActionLoggingSamplingFactorMap
If set, records user actions via EventLogging and applies a sampling factor according to the map.
string bool $wgMediaViewerImageQueryParameter
If set, adds a query parameter to image requests made by Media Viewer.
string bool $wgMediaViewerRecordVirtualViewBeaconURI
If set, records a virtual view via the provided beacon URI.
int bool $wgMediaViewerDimensionLoggingSamplingFactor
If set, records whether image dimension data was available.
getContext()
Gets the context this Article is executed in.
Definition Article.php:2267
getTitle()
Get the title object of the article.
Definition Article.php:221
static shouldHandleClicks(User $user)
Checks the context for whether to load the viewer.
static onResourceLoaderGetConfigVars(array &$vars)
static $helpLink
Link to help about this module.
static getModules(OutputPage $out)
Handler for all places where we add the modules Could be on article pages or on Category pages.
static onThumbnailBeforeProduceHTML(ThumbnailImage $thumbnail, array &$attribs, &$linkAttribs)
static onCategoryPageView(CategoryPage $catPage)
static $discussionLink
Link to a page where this module can be discussed.
static onMakeGlobalVariablesScript(array &$vars, OutputPage $out)
static onBeforePageDisplay(OutputPage $out, $skin)
static onUserGetDefaultOptions(array &$defaultOptions)
static onGetPreferences( $user, &$prefs)
static $infoLink
Link to more information about this module.
This is one of the Core classes and should be read at least once by any new developers.
getFileSearchOptions()
Get the files used on this page.
addModules( $modules)
Load one or more ResourceLoader modules on this page.
Media transform output for images.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
getOption( $oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
Definition User.php:3022
isLoggedIn()
Get whether the user is logged in.
Definition User.php:3630
const NS_FILE
Definition Defines.php:75
const NS_SPECIAL
Definition Defines.php:58
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42