MediaWiki  1.34.0
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 }
ThumbnailImage
Media transform output for images.
Definition: ThumbnailImage.php:29
MediaTransformOutput\getFile
getFile()
Definition: MediaTransformOutput.php:76
$wgMediaViewerUseThumbnailGuessing
bool $wgMediaViewerUseThumbnailGuessing
When this is enabled, MediaViewer will try to guess image URLs instead of making an imageinfo API to ...
Definition: MultimediaViewer.php:100
MultimediaViewerHooks\$infoLink
static $infoLink
Link to more information about this module.
Definition: MultimediaViewerHooks.php:26
$wgMediaViewerAttributionLoggingSamplingFactor
int bool $wgMediaViewerAttributionLoggingSamplingFactor
If set, records whether image attribution data was available.
Definition: MultimediaViewer.php:71
$wgMediaViewerDurationLoggingLoggedinSamplingFactor
int bool $wgMediaViewerDurationLoggingLoggedinSamplingFactor
If set, records loading times via EventLogging with factor specific to loggedin users.
Definition: MultimediaViewer.php:63
CategoryPage
Definition: CategoryPage.php:30
$wgMediaViewerDimensionLoggingSamplingFactor
int bool $wgMediaViewerDimensionLoggingSamplingFactor
If set, records whether image dimension data was available.
Definition: MultimediaViewer.php:79
$wgMediaViewerImageQueryParameter
string bool $wgMediaViewerImageQueryParameter
If set, adds a query parameter to image requests made by Media Viewer.
Definition: MultimediaViewer.php:119
NS_FILE
const NS_FILE
Definition: Defines.php:66
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
$wgMediaViewerNetworkPerformanceSamplingFactor
int bool $wgMediaViewerNetworkPerformanceSamplingFactor
If set, records image load network performance via EventLogging once per this many requests.
Definition: MultimediaViewer.php:47
$wgMediaViewerDurationLoggingSamplingFactor
int bool $wgMediaViewerDurationLoggingSamplingFactor
If set, records loading times via EventLogging.
Definition: MultimediaViewer.php:55
MultimediaViewerHooks\$helpLink
static $helpLink
Link to help about this module.
Definition: MultimediaViewerHooks.php:34
MultimediaViewerHooks\onUserGetDefaultOptions
static onUserGetDefaultOptions(array &$defaultOptions)
Definition: MultimediaViewerHooks.php:41
User\getDefaultOptions
static getDefaultOptions()
Combine the language default options with any site-specific options and add the default language vari...
Definition: User.php:1651
MultimediaViewerHooks\onThumbnailBeforeProduceHTML
static onThumbnailBeforeProduceHTML(ThumbnailImage $thumbnail, array &$attribs, &$linkAttribs)
Definition: MultimediaViewerHooks.php:189
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:106
MultimediaViewerHooks\onMakeGlobalVariablesScript
static onMakeGlobalVariablesScript(array &$vars, OutputPage $out)
Definition: MultimediaViewerHooks.php:172
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:49
Article\getTitle
getTitle()
Get the title object of the article.
Definition: Article.php:221
Article\getContext
getContext()
Gets the context this Article is executed in.
Definition: Article.php:2267
$title
$title
Definition: testCompression.php:34
Category\newFromTitle
static newFromTitle( $title)
Factory function.
Definition: Category.php:146
MultimediaViewerHooks
Definition: MultimediaViewerHooks.php:24
$wgMediaViewerEnableByDefaultForAnonymous
bool null $wgMediaViewerEnableByDefaultForAnonymous
Overrides $wgMediaViewerEnableByDefault for anonymous users.
Definition: MultimediaViewer.php:113
MultimediaViewerHooks\onBeforePageDisplay
static onBeforePageDisplay(OutputPage $out, $skin)
Definition: MultimediaViewerHooks.php:87
User\getOption
getOption( $oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
Definition: User.php:2918
$wgMediaViewerActionLoggingSamplingFactorMap
array bool $wgMediaViewerActionLoggingSamplingFactorMap
If set, records user actions via EventLogging and applies a sampling factor according to the map.
Definition: MultimediaViewer.php:87
MultimediaViewerHooks\getModules
static getModules(OutputPage $out)
Handler for all places where we add the modules Could be on article pages or on Category pages.
Definition: MultimediaViewerHooks.php:76
MultimediaViewerHooks\shouldHandleClicks
static shouldHandleClicks(User $user)
Checks the context for whether to load the viewer.
Definition: MultimediaViewerHooks.php:54
User\isLoggedIn
isLoggedIn()
Get whether the user is logged in.
Definition: User.php:3524
MultimediaViewerHooks\onGetPreferences
static onGetPreferences( $user, &$prefs)
Definition: MultimediaViewerHooks.php:124
$wgMediaViewerEnableByDefault
bool $wgMediaViewerEnableByDefault
If true, Media Viewer will be turned on by default.
Definition: MultimediaViewer.php:106
MultimediaViewerHooks\onCategoryPageView
static onCategoryPageView(CategoryPage $catPage)
Definition: MultimediaViewerHooks.php:109
MultimediaViewerHooks\onResourceLoaderGetConfigVars
static onResourceLoaderGetConfigVars(array &$vars)
Definition: MultimediaViewerHooks.php:137
MultimediaViewerHooks\$discussionLink
static $discussionLink
Link to a page where this module can be discussed.
Definition: MultimediaViewerHooks.php:30
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
$wgMediaViewerRecordVirtualViewBeaconURI
string bool $wgMediaViewerRecordVirtualViewBeaconURI
If set, records a virtual view via the provided beacon URI.
Definition: MultimediaViewer.php:125