MediaWiki  master
TraditionalImageGallery.php
Go to the documentation of this file.
1 <?php
2 
4 
38  function toHTML() {
39  if ( $this->mPerRow > 0 ) {
40  $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
41  $oldStyle = $this->mAttribs['style'] ?? '';
42  # _width is ignored by any sane browser. IE6 doesn't know max-width
43  # so it uses _width instead
44  $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" .
45  $oldStyle;
46  }
47 
49  [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
50 
51  $modules = $this->getModules();
52 
53  if ( $this->mParser ) {
54  $this->mParser->getOutput()->addModules( $modules );
55  $this->mParser->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
56  } else {
57  $this->getOutput()->addModules( $modules );
58  $this->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
59  }
61  if ( $this->mCaption ) {
62  $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
63  }
64 
65  if ( $this->mShowFilename ) {
66  // Preload LinkCache info for when generating links
67  // of the filename below
68  $lb = new LinkBatch();
69  foreach ( $this->mImages as $img ) {
70  $lb->addObj( $img[0] );
71  }
72  $lb->execute();
73  }
74 
75  $lang = $this->getRenderLang();
76  # Output each image...
77  foreach ( $this->mImages as $pair ) {
78  // "text" means "caption" here
80  list( $nt, $text, $alt, $link ) = $pair;
81 
82  $descQuery = false;
83  if ( $nt->getNamespace() === NS_FILE ) {
84  # Get the file...
85  if ( $this->mParser instanceof Parser ) {
86  # Give extensions a chance to select the file revision for us
87  $options = [];
88  Hooks::run( 'BeforeParserFetchFileAndTitle',
89  [ $this->mParser, $nt, &$options, &$descQuery ] );
90  # Fetch and register the file (file title may be different via hooks)
91  list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
92  } else {
93  $img = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $nt );
94  }
95  } else {
96  $img = false;
97  }
98 
99  $params = $this->getThumbParams( $img );
100  // $pair[4] is per image handler options
101  $transformOptions = $params + $pair[4];
102 
103  $thumb = false;
104 
105  if ( !$img ) {
106  # We're dealing with a non-image, spit out the name and be done with it.
107  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
108  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
109  . htmlspecialchars( $nt->getText() ) . '</div>';
110 
111  if ( $this->mParser instanceof Parser ) {
112  $this->mParser->addTrackingCategory( 'broken-file-category' );
113  }
114  } elseif ( $this->mHideBadImages
115  && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
116  ) {
117  # The image is blacklisted, just show it as a text link.
118  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
119  ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
121  $nt,
122  htmlspecialchars( $nt->getText() )
123  ) .
124  '</div>';
125  } else {
126  $thumb = $img->transform( $transformOptions );
127  if ( !$thumb ) {
128  # Error generating thumbnail.
129  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
130  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
131  . htmlspecialchars( $img->getLastError() ) . '</div>';
132  } else {
134  $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
135 
136  $imageParameters = [
137  'desc-link' => true,
138  'desc-query' => $descQuery,
139  'alt' => $alt,
140  'custom-url-link' => $link
141  ];
142 
143  // In the absence of both alt text and caption, fall back on
144  // providing screen readers with the filename as alt text
145  if ( $alt == '' && $text == '' ) {
146  $imageParameters['alt'] = $nt->getText();
147  }
148 
149  $this->adjustImageParameters( $thumb, $imageParameters );
150 
151  Linker::processResponsiveImages( $img, $thumb, $transformOptions );
152 
153  # Set both fixed width and min-height.
154  $thumbhtml = "\n\t\t\t"
155  . '<div class="thumb" style="width: '
156  . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
157  # Auto-margin centering for block-level elements. Needed
158  # now that we have video handlers since they may emit block-
159  # level elements as opposed to simple <img> tags. ref
160  # http://css-discuss.incutio.com/?page=CenteringBlockElement
161  . '<div style="margin:' . $vpad . 'px auto;">'
162  . $thumb->toHtml( $imageParameters ) . '</div></div>';
163 
164  // Call parser transform hook
166  $handler = $img->getHandler();
167  if ( $this->mParser && $handler ) {
168  $handler->parserTransformHook( $this->mParser, $img );
169  }
170  }
171  }
172 
173  // @todo Code is incomplete.
174  // $linkTarget = Title::newFromText( MediaWikiServices::getInstance()->
175  // getContentLanguage()->getNsText( MediaWikiServices::getInstance()->
176  // getNamespaceInfo()->getUser() ) . ":{$ut}" );
177  // $ul = Linker::link( $linkTarget, $ut );
178 
179  $meta = [];
180  if ( $img ) {
181  if ( $this->mShowDimensions ) {
182  $meta[] = $img->getDimensionsString();
183  }
184  if ( $this->mShowBytes ) {
185  $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
186  }
187  } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
188  $meta[] = $this->msg( 'filemissing' )->escaped();
189  }
190  $meta = $lang->semicolonList( $meta );
191  if ( $meta ) {
192  $meta .= "<br />\n";
193  }
194 
195  $textlink = $this->mShowFilename ?
196  $this->getCaptionHtml( $nt, $lang ) :
197  '';
198 
199  $galleryText = $textlink . $text . $meta;
200  $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
201 
202  $gbWidth = $this->getGBWidth( $thumb ) . 'px';
203  if ( $this->getGBWidthOverwrite( $thumb ) ) {
204  $gbWidth = $this->getGBWidthOverwrite( $thumb );
205  }
206  # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
207  # Can be safely removed if FF2 falls completely out of existence
208  $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
209  . $gbWidth . '">'
210  . '<div style="width: ' . $gbWidth . '">'
211  . $thumbhtml
212  . $galleryText
213  . "\n\t\t</div></li>";
214  }
215  $output .= "\n</ul>";
216 
217  return $output;
218  }
219 
225  protected function getCaptionHtml( Title $nt, Language $lang ) {
226  // Preloaded into LinkCache in toHTML
227  return Linker::linkKnown(
228  $nt,
229  htmlspecialchars(
230  is_int( $this->getCaptionLength() ) ?
231  $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
232  $nt->getText()
233  ),
234  [
235  'class' => 'galleryfilename' .
236  ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
237  ]
238  ) . "\n";
239  }
240 
249  protected function wrapGalleryText( $galleryText, $thumb ) {
250  # ATTENTION: The newline after <div class="gallerytext"> is needed to
251  # accommodate htmltidy which in version 4.8.6 generated crackpot html in
252  # its absence, see: https://phabricator.wikimedia.org/T3765
253  # -Ævar
254 
255  return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
256  . $galleryText
257  . "\n\t\t\t</div>";
258  }
259 
266  protected function getThumbPadding() {
267  return 30;
268  }
269 
275  protected function getGBPadding() {
276  return 5;
277  }
278 
286  protected function getGBBorders() {
287  return 8;
288  }
289 
297  protected function getCaptionLength() {
298  return $this->mCaptionLength;
299  }
300 
306  protected function getAllPadding() {
307  return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
308  }
309 
319  protected function getVPad( $boxHeight, $thumbHeight ) {
320  return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
321  }
322 
329  protected function getThumbParams( $img ) {
330  return [
331  'width' => $this->mWidths,
332  'height' => $this->mHeights
333  ];
334  }
335 
343  protected function getThumbDivWidth( $thumbWidth ) {
344  return $this->mWidths + $this->getThumbPadding();
345  }
346 
357  protected function getGBWidth( $thumb ) {
358  return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
359  }
360 
372  protected function getGBWidthOverwrite( $thumb ) {
373  return false;
374  }
375 
383  protected function getModules() {
384  return [];
385  }
386 
394  protected function adjustImageParameters( $thumb, &$imageParameters ) {
395  }
396 }
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if that
Definition: deferred.txt:11
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
getThumbParams( $img)
Get the transform parameters for a thumbnail.
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:984
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfIsBadImage( $name, $contextTitle=false, $blacklist=null)
Determine if an image exists on the &#39;bad image list&#39;.
if(!isset( $args[0])) $lang
getGBBorders()
Get how much extra space the borders around the image takes up.
getVPad( $boxHeight, $thumbHeight)
Get vertical padding for a thumbnail.
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 MediaWikiServices
Definition: injection.txt:23
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
toHTML()
Return a HTML representation of the image gallery.
Apache License January http
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2217
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3051
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 modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:780
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
getCaptionLength()
Length (in characters) to truncate filename to in caption when using "showfilename" (if int)...
getGBWidth( $thumb)
Computed width of gallerybox .
getRenderLang()
Determines the correct language to be used for this image gallery.
$modules
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging a wrapping ErrorException block
Definition: hooks.txt:2159
getModules()
Get a list of modules to include in the page.
getGBWidthOverwrite( $thumb)
Allows overwriting the computed width of the gallerybox with a string, like &#39;100&#39;.
static mergeAttributes( $a, $b)
Merge two sets of HTML attributes.
Definition: Sanitizer.php:929
$params
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 & $options
Definition: hooks.txt:1982
getAllPadding()
Get total padding.
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:1982
getCaptionHtml(Title $nt, Language $lang)
const NS_FILE
Definition: Defines.php:66
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getContextTitle()
Get the contextual title, if applicable.
wrapGalleryText( $galleryText, $thumb)
Add the wrapper html around the thumb&#39;s caption.
getThumbPadding()
How much padding the thumb has between the image and the inner div that contains the border...
Image gallery.
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
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
static linkKnown( $target, $html=null, $customAttribs=[], $query=[], $options=[ 'known'])
Identical to link(), except $options defaults to &#39;known&#39;.
Definition: Linker.php:146
adjustImageParameters( $thumb, &$imageParameters)
Adjust the image parameters for a thumbnail.
static processResponsiveImages( $file, $thumb, $hp)
Process responsive images: add 1.5x and 2x subimages to the thumbnail, where applicable.
Definition: Linker.php:647
getThumbDivWidth( $thumbWidth)
Get the width of the inner div that contains the thumbnail in question.
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e...
Definition: Language.php:3542
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200