MediaWiki  1.33.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 
52  protected static function shouldHandleClicks( $user ) {
55 
56  if ( $wgMediaViewerIsInBeta && ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) ) {
57  return BetaFeatures::isFeatureEnabled( $user, 'multimedia-viewer' );
58  }
59 
61  $enableByDefaultForAnons = $wgMediaViewerEnableByDefault;
62  } else {
63  $enableByDefaultForAnons = $wgMediaViewerEnableByDefaultForAnonymous;
64  }
65 
66  if ( !$user->isLoggedIn() ) {
67  return (bool)$enableByDefaultForAnons;
68  } else {
69  return (bool)$user->getOption( 'multimediaviewer-enable' );
70  }
71  }
72 
79  protected static function getModules( &$out ) {
80  $out->addModules( [ 'mmv.head', 'mmv.bootstrap.autostart' ] );
81 
82  return true;
83  }
84 
93  public static function getModulesForArticle( &$out, &$skin ) {
94  $pageHasThumbnails = count( $out->getFileSearchOptions() ) > 0;
95  $pageIsFilePage = $out->getTitle()->inNamespace( NS_FILE );
96  // TODO: Have Flow work out if there are any images on the page
97  $pageIsFlowPage = ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) &&
98  // CONTENT_MODEL_FLOW_BOARD
99  $out->getTitle()->getContentModel() === 'flow-board';
100  $fileRelatedSpecialPages = [ 'NewFiles', 'ListFiles', 'MostLinkedFiles',
101  'MostGloballyLinkedFiles', 'UncategorizedFiles', 'UnusedFiles', 'Search' ];
102  $pageIsFileRelatedSpecialPage = $out->getTitle()->inNamespace( NS_SPECIAL )
103  && in_array( $out->getTitle()->getText(), $fileRelatedSpecialPages );
104 
105  if ( $pageHasThumbnails || $pageIsFilePage || $pageIsFileRelatedSpecialPage || $pageIsFlowPage ) {
106  return self::getModules( $out );
107  }
108 
109  return true;
110  }
111 
118  public static function getModulesForCategory( &$catPage ) {
119  $title = $catPage->getTitle();
120  $cat = Category::newFromTitle( $title );
121  if ( $cat->getFileCount() > 0 ) {
122  $out = $catPage->getContext()->getOutput();
123  return self::getModules( $out );
124  }
125 
126  return true;
127  }
128 
135  public static function getBetaPreferences( $user, &$prefs ) {
137 
138  if ( !$wgMediaViewerIsInBeta ) {
139  return true;
140  }
141 
142  $prefs['multimedia-viewer'] = [
143  'label-message' => 'multimediaviewer-pref',
144  'desc-message' => 'multimediaviewer-pref-desc',
145  'info-link' => self::$infoLink,
146  'discussion-link' => self::$discussionLink,
147  'help-link' => self::$helpLink,
148  'screenshot' => [
149  'ltr' => "$wgExtensionAssetsPath/MultimediaViewer/viewer-ltr.svg",
150  'rtl' => "$wgExtensionAssetsPath/MultimediaViewer/viewer-rtl.svg",
151  ],
152  ];
153 
154  return true;
155  }
156 
163  public static function getPreferences( $user, &$prefs ) {
164  global $wgMediaViewerIsInBeta;
165 
166  if ( !$wgMediaViewerIsInBeta ) {
167  $prefs['multimediaviewer-enable'] = [
168  'type' => 'toggle',
169  'label-message' => 'multimediaviewer-optin-pref',
170  'section' => 'rendering/files',
171  ];
172  }
173 
174  return true;
175  }
176 
182  public static function resourceLoaderGetConfigVars( &$vars ) {
191 
192  $vars['wgMultimediaViewer'] = [
193  'infoLink' => self::$infoLink,
194  'discussionLink' => self::$discussionLink,
195  'helpLink' => self::$helpLink,
196  'useThumbnailGuessing' => (bool)$wgMediaViewerUseThumbnailGuessing,
197  'durationSamplingFactor' => $wgMediaViewerDurationLoggingSamplingFactor,
198  'durationSamplingFactorLoggedin' => $wgMediaViewerDurationLoggingLoggedinSamplingFactor,
199  'networkPerformanceSamplingFactor' => $wgMediaViewerNetworkPerformanceSamplingFactor,
200  'actionLoggingSamplingFactorMap' => $wgMediaViewerActionLoggingSamplingFactorMap,
201  'attributionSamplingFactor' => $wgMediaViewerAttributionLoggingSamplingFactor,
202  'dimensionSamplingFactor' => $wgMediaViewerDimensionLoggingSamplingFactor,
203  'imageQueryParameter' => $wgMediaViewerImageQueryParameter,
204  'recordVirtualViewBeaconURI' => $wgMediaViewerRecordVirtualViewBeaconURI,
205  'tooltipDelay' => 1000,
206  'extensions' => $wgMediaViewerExtensions,
207  ];
208  $vars['wgMediaViewer'] = true;
209  $vars['wgMediaViewerIsInBeta'] = $wgMediaViewerIsInBeta;
210 
211  return true;
212  }
213 
219  public static function makeGlobalVariablesScript( &$vars, OutputPage $out ) {
220  $defaultUserOptions = User::getDefaultOptions();
221 
222  $user = $out->getUser();
223  $vars['wgMediaViewerOnClick'] = self::shouldHandleClicks( $user );
224  // needed because of bug 69942; could be different for anon and logged-in
225  $vars['wgMediaViewerEnabledByDefault'] =
226  !empty( $defaultUserOptions['multimediaviewer-enable'] );
227  }
228 
236  public static function thumbnailBeforeProduceHTML( ThumbnailImage $thumbnail, array &$attribs,
237  &$linkAttribs
238  ) {
239  $file = $thumbnail->getFile();
240 
241  if ( $file ) {
242  // At the moment all classes that extend File have getWidth() and getHeight()
243  // but since the File class doesn't have these methods defined, this check
244  // is more future-proof
245 
246  if ( method_exists( $file, 'getWidth' ) ) {
247  $attribs['data-file-width'] = $file->getWidth();
248  }
249 
250  if ( method_exists( $file, 'getHeight' ) ) {
251  $attribs['data-file-height'] = $file->getHeight();
252  }
253  }
254 
255  return true;
256  }
257 }
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
ThumbnailImage
Media transform output for images.
Definition: ThumbnailImage.php:29
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
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:118
$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:135
$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
$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 When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:780
$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:70
$wgExtensionAssetsPath
$wgExtensionAssetsPath
The URL path of the extensions directory.
Definition: DefaultSettings.php:216
$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
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:163
User\getDefaultOptions
static getDefaultOptions()
Combine the language default options with any site-specific options and add the default language vari...
Definition: User.php:1776
MultimediaViewerHooks\thumbnailBeforeProduceHTML
static thumbnailBeforeProduceHTML(ThumbnailImage $thumbnail, array &$attribs, &$linkAttribs)
Modify thumbnail DOM.
Definition: MultimediaViewerHooks.php:236
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:98
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
MultimediaViewerHooks\shouldHandleClicks
static shouldHandleClicks( $user)
Checks the context for whether to load the viewer.
Definition: MultimediaViewerHooks.php:52
MultimediaViewerHooks\makeGlobalVariablesScript
static makeGlobalVariablesScript(&$vars, OutputPage $out)
Export variables which depend on the current user.
Definition: MultimediaViewerHooks.php:219
$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:1985
$vars
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
Definition: hooks.txt:2220
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
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:93
$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:182
$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:1985
MultimediaViewerHooks\onUserGetDefaultOptions
static onUserGetDefaultOptions(&$defaultOptions)
Definition: MultimediaViewerHooks.php:37
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
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:79