MediaWiki  1.31.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 
37  public static function onUserGetDefaultOptions( &$defaultOptions ) {
39 
41  $defaultOptions['multimediaviewer-enable'] = 1;
42  }
43 
44  return true;
45  }
46 
47  public static function onExtensionFunctions() {
49 
50  if ( isset( $wgResourceModules['ext.eventLogging'] ) ) {
51  $wgResourceModules['mmv.lightboxinterface']['dependencies'][] = 'ext.eventLogging';
52  $wgResourceModules['mmv']['dependencies'][] = 'ext.eventLogging';
53  $wgResourceModules['mmv.bootstrap.autostart']['dependencies'][] = 'ext.eventLogging';
54  }
55  }
56 
57  public static function onEventLoggingRegisterSchemas( array &$schemas ) {
58  $schemas += [
59  'MediaViewer' => 10867062,
60  'MultimediaViewerNetworkPerformance' => 15573630,
61  'MultimediaViewerDuration' => 10427980,
62  'MultimediaViewerAttribution' => 9758179,
63  'MultimediaViewerDimensions' => 10014238,
64  ];
65  }
66 
72  protected static function shouldHandleClicks( $user ) {
75 
76  if ( $wgMediaViewerIsInBeta && class_exists( 'BetaFeatures' ) ) {
77  return BetaFeatures::isFeatureEnabled( $user, 'multimedia-viewer' );
78  }
79 
81  $enableByDefaultForAnons = $wgMediaViewerEnableByDefault;
82  } else {
83  $enableByDefaultForAnons = $wgMediaViewerEnableByDefaultForAnonymous;
84  }
85 
86  if ( !$user->isLoggedIn() ) {
87  return (bool)$enableByDefaultForAnons;
88  } else {
89  return (bool)$user->getOption( 'multimediaviewer-enable' );
90  }
91  }
92 
99  protected static function getModules( &$out ) {
100  $out->addModules( [ 'mmv.head', 'mmv.bootstrap.autostart' ] );
101 
102  return true;
103  }
104 
113  public static function getModulesForArticle( &$out, &$skin ) {
114  $pageHasThumbnails = count( $out->getFileSearchOptions() ) > 0;
115  $pageIsFilePage = $out->getTitle()->inNamespace( NS_FILE );
116  $fileRelatedSpecialPages = [ 'NewFiles', 'ListFiles', 'MostLinkedFiles',
117  'MostGloballyLinkedFiles', 'UncategorizedFiles', 'UnusedFiles', 'Search' ];
118  $pageIsFileRelatedSpecialPage = $out->getTitle()->inNamespace( NS_SPECIAL )
119  && in_array( $out->getTitle()->getText(), $fileRelatedSpecialPages );
120 
121  if ( $pageHasThumbnails || $pageIsFilePage || $pageIsFileRelatedSpecialPage ) {
122  return self::getModules( $out );
123  }
124 
125  return true;
126  }
127 
134  public static function getModulesForCategory( &$catPage ) {
135  $title = $catPage->getTitle();
136  $cat = Category::newFromTitle( $title );
137  if ( $cat->getFileCount() > 0 ) {
138  $out = $catPage->getContext()->getOutput();
139  return self::getModules( $out );
140  }
141 
142  return true;
143  }
144 
151  public static function getBetaPreferences( $user, &$prefs ) {
153 
154  if ( !$wgMediaViewerIsInBeta ) {
155  return true;
156  }
157 
158  $prefs['multimedia-viewer'] = [
159  'label-message' => 'multimediaviewer-pref',
160  'desc-message' => 'multimediaviewer-pref-desc',
161  'info-link' => self::$infoLink,
162  'discussion-link' => self::$discussionLink,
163  'help-link' => self::$helpLink,
164  'screenshot' => [
165  'ltr' => "$wgExtensionAssetsPath/MultimediaViewer/viewer-ltr.svg",
166  'rtl' => "$wgExtensionAssetsPath/MultimediaViewer/viewer-rtl.svg",
167  ],
168  ];
169 
170  return true;
171  }
172 
179  public static function getPreferences( $user, &$prefs ) {
181 
182  if ( !$wgMediaViewerIsInBeta ) {
183  $prefs['multimediaviewer-enable'] = [
184  'type' => 'toggle',
185  'label-message' => 'multimediaviewer-optin-pref',
186  'section' => 'rendering/files',
187  ];
188  }
189 
190  return true;
191  }
192 
198  public static function resourceLoaderGetConfigVars( &$vars ) {
207 
208  $vars['wgMultimediaViewer'] = [
209  'infoLink' => self::$infoLink,
210  'discussionLink' => self::$discussionLink,
211  'helpLink' => self::$helpLink,
212  'useThumbnailGuessing' => (bool)$wgMediaViewerUseThumbnailGuessing,
213  'durationSamplingFactor' => $wgMediaViewerDurationLoggingSamplingFactor,
214  'durationSamplingFactorLoggedin' => $wgMediaViewerDurationLoggingLoggedinSamplingFactor,
215  'networkPerformanceSamplingFactor' => $wgMediaViewerNetworkPerformanceSamplingFactor,
216  'actionLoggingSamplingFactorMap' => $wgMediaViewerActionLoggingSamplingFactorMap,
217  'attributionSamplingFactor' => $wgMediaViewerAttributionLoggingSamplingFactor,
218  'dimensionSamplingFactor' => $wgMediaViewerDimensionLoggingSamplingFactor,
219  'imageQueryParameter' => $wgMediaViewerImageQueryParameter,
220  'recordVirtualViewBeaconURI' => $wgMediaViewerRecordVirtualViewBeaconURI,
221  'tooltipDelay' => 1000,
222  'extensions' => $wgMediaViewerExtensions,
223  ];
224  $vars['wgMediaViewer'] = true;
225  $vars['wgMediaViewerIsInBeta'] = $wgMediaViewerIsInBeta;
226 
227  return true;
228  }
229 
235  public static function makeGlobalVariablesScript( &$vars, OutputPage $out ) {
236  $defaultUserOptions = User::getDefaultOptions();
237 
238  $user = $out->getUser();
239  $vars['wgMediaViewerOnClick'] = self::shouldHandleClicks( $user );
240  // needed because of bug 69942; could be different for anon and logged-in
241  $vars['wgMediaViewerEnabledByDefault'] =
242  !empty( $defaultUserOptions['multimediaviewer-enable'] );
243  }
244 
251  public static function getTestModules( array &$testModules, ResourceLoader &$resourceLoader ) {
252  $testModules['qunit']['mmv.tests'] = [
253  'scripts' => [
254  'tests/qunit/mmv/mmv.bootstrap.test.js',
255  'tests/qunit/mmv/mmv.test.js',
256  'tests/qunit/mmv/mmv.lightboxinterface.test.js',
257  'tests/qunit/mmv/mmv.lightboximage.test.js',
258  'tests/qunit/mmv/mmv.ThumbnailWidthCalculator.test.js',
259  'tests/qunit/mmv/mmv.EmbedFileFormatter.test.js',
260  'tests/qunit/mmv/mmv.Config.test.js',
261  'tests/qunit/mmv/mmv.HtmlUtils.test.js',
262  'tests/qunit/mmv/logging/mmv.logging.DurationLogger.test.js',
263  'tests/qunit/mmv/logging/mmv.logging.PerformanceLogger.test.js',
264  'tests/qunit/mmv/logging/mmv.logging.ActionLogger.test.js',
265  'tests/qunit/mmv/logging/mmv.logging.AttributionLogger.test.js',
266  'tests/qunit/mmv/logging/mmv.logging.DimensionLogger.test.js',
267  'tests/qunit/mmv/logging/mmv.logging.ViewLogger.test.js',
268  'tests/qunit/mmv/model/mmv.model.test.js',
269  'tests/qunit/mmv/model/mmv.model.IwTitle.test.js',
270  'tests/qunit/mmv/model/mmv.model.TaskQueue.test.js',
271  'tests/qunit/mmv/model/mmv.model.License.test.js',
272  'tests/qunit/mmv/model/mmv.model.Image.test.js',
273  'tests/qunit/mmv/model/mmv.model.Repo.test.js',
274  'tests/qunit/mmv/model/mmv.model.EmbedFileInfo.test.js',
275  'tests/qunit/mmv/provider/mmv.provider.Api.test.js',
276  'tests/qunit/mmv/provider/mmv.provider.ImageInfo.test.js',
277  'tests/qunit/mmv/provider/mmv.provider.FileRepoInfo.test.js',
278  'tests/qunit/mmv/provider/mmv.provider.ThumbnailInfo.test.js',
279  'tests/qunit/mmv/provider/mmv.provider.GuessedThumbnailInfo.test.js',
280  'tests/qunit/mmv/provider/mmv.provider.Image.test.js',
281  'tests/qunit/mmv/routing/mmv.routing.MainFileRoute.test.js',
282  'tests/qunit/mmv/routing/mmv.routing.ThumbnailRoute.test.js',
283  'tests/qunit/mmv/routing/mmv.routing.Router.test.js',
284  'tests/qunit/mmv/ui/mmv.ui.test.js',
285  'tests/qunit/mmv/ui/mmv.ui.canvas.test.js',
286  'tests/qunit/mmv/ui/mmv.ui.canvasButtons.test.js',
287  'tests/qunit/mmv/ui/mmv.ui.description.test.js',
288  'tests/qunit/mmv/ui/mmv.ui.download.pane.test.js',
289  'tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js',
290  'tests/qunit/mmv/ui/mmv.ui.metadataPanelScroller.test.js',
291  'tests/qunit/mmv/ui/mmv.ui.progressBar.test.js',
292  'tests/qunit/mmv/ui/mmv.ui.permission.test.js',
293  'tests/qunit/mmv/ui/mmv.ui.stripeButtons.test.js',
294  'tests/qunit/mmv/ui/mmv.ui.reuse.dialog.test.js',
295  'tests/qunit/mmv/ui/mmv.ui.reuse.embed.test.js',
296  'tests/qunit/mmv/ui/mmv.ui.reuse.share.test.js',
297  'tests/qunit/mmv/ui/mmv.ui.reuse.tab.test.js',
298  'tests/qunit/mmv/ui/mmv.ui.reuse.utils.test.js',
299  'tests/qunit/mmv/ui/mmv.ui.tipsyDialog.test.js',
300  'tests/qunit/mmv/ui/mmv.ui.truncatableTextField.test.js',
301  'tests/qunit/mmv/ui/mmv.ui.viewingOptions.test.js',
302  'tests/qunit/mmv/mmv.testhelpers.js',
303  ],
304  'dependencies' => [
305  'mmv.head',
306  'mmv.bootstrap',
307  'mmv',
308  'mmv.ui.ondemandshareddependencies',
309  'mmv.ui.reuse.shareembed',
310  'mmv.ui.download.pane',
311  'mmv.ui.tipsyDialog',
312  'moment',
313  ],
314  'localBasePath' => dirname( __DIR__ ),
315  'remoteExtPath' => 'MultimediaViewer',
316  ];
317 
318  return true;
319  }
320 
328  public static function thumbnailBeforeProduceHTML( ThumbnailImage $thumbnail, array &$attribs,
329  &$linkAttribs
330  ) {
331  $file = $thumbnail->getFile();
332 
333  if ( $file ) {
334  // At the moment all classes that extend File have getWidth() and getHeight()
335  // but since the File class doesn't have these methods defined, this check
336  // is more future-proof
337 
338  if ( method_exists( $file, 'getWidth' ) ) {
339  $attribs['data-file-width'] = $file->getWidth();
340  }
341 
342  if ( method_exists( $file, 'getHeight' ) ) {
343  $attribs['data-file-height'] = $file->getHeight();
344  }
345  }
346 
347  return true;
348  }
349 }
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
ThumbnailImage
Media transform output for images.
Definition: MediaTransformOutput.php:277
MediaTransformOutput\getFile
getFile()
Definition: MediaTransformOutput.php:76
$wgMediaViewerIsInBeta
bool $wgMediaViewerIsInBeta
If set, Media Viewer will try to use BetaFeatures.
Definition: MultimediaViewer.php:93
MultimediaViewerHooks\getModulesForCategory
static getModulesForCategory(&$catPage)
Handler for CategoryPageView hook Add JavaScript to the page if there are images in the category.
Definition: MultimediaViewerHooks.php:134
$wgMediaViewerUseThumbnailGuessing
bool $wgMediaViewerUseThumbnailGuessing
When this is enabled, MediaViewer will try to guess image URLs instead of making an imageinfo API to ...
Definition: MultimediaViewer.php:106
MultimediaViewerHooks\$infoLink
static $infoLink
Link to more information about this module.
Definition: MultimediaViewerHooks.php:26
captcha-old.count
count
Definition: captcha-old.py:249
MultimediaViewerHooks\getBetaPreferences
static getBetaPreferences( $user, &$prefs)
Add a beta preference to gate the feature.
Definition: MultimediaViewerHooks.php:151
$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
$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:126
NS_FILE
const NS_FILE
Definition: Defines.php:71
$wgExtensionAssetsPath
$wgExtensionAssetsPath
The URL path of the extensions directory.
Definition: DefaultSettings.php:215
MultimediaViewerHooks\onEventLoggingRegisterSchemas
static onEventLoggingRegisterSchemas(array &$schemas)
Definition: MultimediaViewerHooks.php:57
$wgMediaViewerNetworkPerformanceSamplingFactor
int bool $wgMediaViewerNetworkPerformanceSamplingFactor
If set, records image load network performance via EventLogging once per this many requests.
Definition: MultimediaViewer.php:47
$resourceLoader
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext such as when responding to a resource loader request or generating HTML output & $resourceLoader
Definition: hooks.txt:2604
$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
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MultimediaViewerHooks\getPreferences
static getPreferences( $user, &$prefs)
Adds a default-enabled preference to gate the feature on non-beta sites.
Definition: MultimediaViewerHooks.php:179
User\getDefaultOptions
static getDefaultOptions()
Combine the language default options with any site-specific options and add the default language vari...
Definition: User.php:1722
MultimediaViewerHooks\thumbnailBeforeProduceHTML
static thumbnailBeforeProduceHTML(ThumbnailImage $thumbnail, array &$attribs, &$linkAttribs)
Modify thumbnail DOM.
Definition: MultimediaViewerHooks.php:328
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:54
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
MultimediaViewerHooks\shouldHandleClicks
static shouldHandleClicks( $user)
Checks the context for whether to load the viewer.
Definition: MultimediaViewerHooks.php:72
MultimediaViewerHooks\makeGlobalVariablesScript
static makeGlobalVariablesScript(&$vars, OutputPage $out)
Export variables which depend on the current user.
Definition: MultimediaViewerHooks.php:235
$attribs
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition: hooks.txt:1987
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$vars
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
Definition: hooks.txt:2220
OutputPage
This class should be covered by a general architecture document which does not exist as of January 20...
Definition: OutputPage.php:45
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:120
MultimediaViewerHooks\getModulesForArticle
static getModulesForArticle(&$out, &$skin)
Handler for BeforePageDisplay hook Add JavaScript to the page when an image is on it and the user has...
Definition: MultimediaViewerHooks.php:113
$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\resourceLoaderGetConfigVars
static resourceLoaderGetConfigVars(&$vars)
Export variables used in both PHP and JS to keep DRY.
Definition: MultimediaViewerHooks.php:198
MultimediaViewerHooks\getTestModules
static getTestModules(array &$testModules, ResourceLoader &$resourceLoader)
Get modules for testing our JavaScript.
Definition: MultimediaViewerHooks.php:251
$wgResourceModules
$wgResourceModules['ext.ReplaceText']
Definition: ReplaceText.php:66
$wgMediaViewerEnableByDefault
bool $wgMediaViewerEnableByDefault
If trueish, and $wgMediaViewerIsInBeta is unset, Media Viewer will be turned on by default.
Definition: MultimediaViewer.php:113
$skin
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned $skin
Definition: hooks.txt:1987
MultimediaViewerHooks\onUserGetDefaultOptions
static onUserGetDefaultOptions(&$defaultOptions)
Definition: MultimediaViewerHooks.php:37
MultimediaViewerHooks\onExtensionFunctions
static onExtensionFunctions()
Definition: MultimediaViewerHooks.php:47
MultimediaViewerHooks\$discussionLink
static $discussionLink
Link to a page where this module can be discussed.
Definition: MultimediaViewerHooks.php:30
$wgMediaViewerRecordVirtualViewBeaconURI
string bool $wgMediaViewerRecordVirtualViewBeaconURI
If set, records a virtual view via the provided beacon URI.
Definition: MultimediaViewer.php:132
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MultimediaViewerHooks\getModules
static getModules(&$out)
Handler for all places where we add the modules Could be on article pages or on Category pages.
Definition: MultimediaViewerHooks.php:99
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:783