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 
48  $attribs = Sanitizer::mergeAttributes(
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  }
60  $output = Xml::openElement( 'ul', $attribs );
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 && MediaWikiServices::getInstance()->getBadFileLookup()
115  ->isBadFile( $nt->getDBkey(), $this->getContextTitle() )
116  ) {
117  if ( $this->mParser instanceof Parser ) {
118  $linkRenderer = $this->mParser->getLinkRenderer();
119  } else {
120  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
121  }
122  # The image is blacklisted, just show it as a text link.
123  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
124  ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
125  $linkRenderer->makeKnownLink( $nt, $nt->getText() ) .
126  '</div>';
127  } else {
128  $thumb = $img->transform( $transformOptions );
129  if ( !$thumb ) {
130  # Error generating thumbnail.
131  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
132  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
133  . htmlspecialchars( $img->getLastError() ) . '</div>';
134  } else {
136  $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
137 
138  $imageParameters = [
139  'desc-link' => true,
140  'desc-query' => $descQuery,
141  'alt' => $alt,
142  'custom-url-link' => $link
143  ];
144 
145  // In the absence of both alt text and caption, fall back on
146  // providing screen readers with the filename as alt text
147  if ( $alt == '' && $text == '' ) {
148  $imageParameters['alt'] = $nt->getText();
149  }
150 
151  $this->adjustImageParameters( $thumb, $imageParameters );
152 
153  Linker::processResponsiveImages( $img, $thumb, $transformOptions );
154 
155  # Set both fixed width and min-height.
156  $thumbhtml = "\n\t\t\t"
157  . '<div class="thumb" style="width: '
158  . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
159  # Auto-margin centering for block-level elements. Needed
160  # now that we have video handlers since they may emit block-
161  # level elements as opposed to simple <img> tags. ref
162  # http://css-discuss.incutio.com/?page=CenteringBlockElement
163  . '<div style="margin:' . $vpad . 'px auto;">'
164  . $thumb->toHtml( $imageParameters ) . '</div></div>';
165 
166  // Call parser transform hook
168  $handler = $img->getHandler();
169  if ( $this->mParser && $handler ) {
170  $handler->parserTransformHook( $this->mParser, $img );
171  }
172  }
173  }
174 
175  // @todo Code is incomplete.
176  // $linkTarget = Title::newFromText( MediaWikiServices::getInstance()->
177  // getContentLanguage()->getNsText( MediaWikiServices::getInstance()->
178  // getNamespaceInfo()->getUser() ) . ":{$ut}" );
179  // $ul = Linker::link( $linkTarget, $ut );
180 
181  $meta = [];
182  if ( $img ) {
183  if ( $this->mShowDimensions ) {
184  $meta[] = $img->getDimensionsString();
185  }
186  if ( $this->mShowBytes ) {
187  $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
188  }
189  } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
190  $meta[] = $this->msg( 'filemissing' )->escaped();
191  }
192  $meta = $lang->semicolonList( $meta );
193  if ( $meta ) {
194  $meta .= "<br />\n";
195  }
196 
197  $textlink = $this->mShowFilename ?
198  $this->getCaptionHtml( $nt, $lang ) :
199  '';
200 
201  $galleryText = $textlink . $text . $meta;
202  $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
203 
204  $gbWidth = $this->getGBWidth( $thumb ) . 'px';
205  if ( $this->getGBWidthOverwrite( $thumb ) ) {
206  $gbWidth = $this->getGBWidthOverwrite( $thumb );
207  }
208  # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
209  # Can be safely removed if FF2 falls completely out of existence
210  $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
211  . $gbWidth . '">'
212  . '<div style="width: ' . $gbWidth . '">'
213  . $thumbhtml
214  . $galleryText
215  . "\n\t\t</div></li>";
216  }
217  $output .= "\n</ul>";
218 
219  return $output;
220  }
221 
227  protected function getCaptionHtml( Title $nt, Language $lang ) {
228  if ( $this->mParser instanceof Parser ) {
229  $linkRenderer = $this->mParser->getLinkRenderer();
230  } else {
231  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
232  }
233  // Preloaded into LinkCache in toHTML
234  return $linkRenderer->makeKnownLink(
235  $nt,
236  is_int( $this->getCaptionLength() ) ?
237  $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
238  $nt->getText(),
239  [
240  'class' => 'galleryfilename' .
241  ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
242  ]
243  ) . "\n";
244  }
245 
254  protected function wrapGalleryText( $galleryText, $thumb ) {
255  # ATTENTION: The newline after <div class="gallerytext"> is needed to
256  # accommodate htmltidy which in version 4.8.6 generated crackpot html in
257  # its absence, see: https://phabricator.wikimedia.org/T3765
258  # -Ævar
259 
260  return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
261  . $galleryText
262  . "\n\t\t\t</div>";
263  }
264 
271  protected function getThumbPadding() {
272  return 30;
273  }
274 
280  protected function getGBPadding() {
281  return 5;
282  }
283 
291  protected function getGBBorders() {
292  return 8;
293  }
294 
302  protected function getCaptionLength() {
303  return $this->mCaptionLength;
304  }
305 
311  protected function getAllPadding() {
312  return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
313  }
314 
324  protected function getVPad( $boxHeight, $thumbHeight ) {
325  return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
326  }
327 
334  protected function getThumbParams( $img ) {
335  return [
336  'width' => $this->mWidths,
337  'height' => $this->mHeights
338  ];
339  }
340 
348  protected function getThumbDivWidth( $thumbWidth ) {
349  return $this->mWidths + $this->getThumbPadding();
350  }
351 
362  protected function getGBWidth( $thumb ) {
363  return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
364  }
365 
377  protected function getGBWidthOverwrite( $thumb ) {
378  return false;
379  }
380 
388  protected function getModules() {
389  return [];
390  }
391 
399  protected function adjustImageParameters( $thumb, &$imageParameters ) {
400  }
401 }
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:35
TraditionalImageGallery\getThumbParams
getThumbParams( $img)
Get the transform parameters for a thumbnail.
Definition: TraditionalImageGallery.php:334
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:130
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:35
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:32
TraditionalImageGallery\toHTML
toHTML()
Return a HTML representation of the image gallery.
Definition: TraditionalImageGallery.php:38
Language\truncateForVisual
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
Definition: Language.php:3431
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:388
MediaTransformOutput\toHtml
toHtml( $options=[])
Fetch HTML for this transform output.
ImageGalleryBase\$mHeights
int $mHeights
Definition: ImageGalleryBase.php:99
NS_FILE
const NS_FILE
Definition: Defines.php:66
ImageGalleryBase\$mCaptionLength
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
Definition: ImageGalleryBase.php:71
TraditionalImageGallery\getCaptionLength
getCaptionLength()
Length (in characters) to truncate filename to in caption when using "showfilename" (if int).
Definition: TraditionalImageGallery.php:302
Linker\processResponsiveImages
static processResponsiveImages( $file, $thumb, $hp)
Process responsive images: add 1.5x and 2x subimages to the thumbnail, where applicable.
Definition: Linker.php:643
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
TraditionalImageGallery\getGBWidthOverwrite
getGBWidthOverwrite( $thumb)
Allows overwriting the computed width of the gallerybox with a string, like '100'.
Definition: TraditionalImageGallery.php:377
TraditionalImageGallery\getGBBorders
getGBBorders()
Get how much extra space the borders around the image takes up.
Definition: TraditionalImageGallery.php:291
TraditionalImageGallery\getVPad
getVPad( $boxHeight, $thumbHeight)
Get vertical padding for a thumbnail.
Definition: TraditionalImageGallery.php:324
TraditionalImageGallery\getCaptionHtml
getCaptionHtml(Title $nt, Language $lang)
Definition: TraditionalImageGallery.php:227
TraditionalImageGallery
Definition: TraditionalImageGallery.php:26
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
$modules
$modules
Definition: HTMLFormElement.php:13
TraditionalImageGallery\wrapGalleryText
wrapGalleryText( $galleryText, $thumb)
Add the wrapper html around the thumb's caption.
Definition: TraditionalImageGallery.php:254
TraditionalImageGallery\getModules
getModules()
Get a list of modules to include in the page.
Definition: TraditionalImageGallery.php:388
TraditionalImageGallery\getGBWidth
getGBWidth( $thumb)
Computed width of gallerybox .
Definition: TraditionalImageGallery.php:362
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
TraditionalImageGallery\adjustImageParameters
adjustImageParameters( $thumb, &$imageParameters)
Adjust the image parameters for a thumbnail.
Definition: TraditionalImageGallery.php:399
TraditionalImageGallery\getGBPadding
getGBPadding()
Definition: TraditionalImageGallery.php:280
TraditionalImageGallery\getAllPadding
getAllPadding()
Get total padding.
Definition: TraditionalImageGallery.php:311
ImageGalleryBase\getRenderLang
getRenderLang()
Determines the correct language to be used for this image gallery.
Definition: ImageGalleryBase.php:396
MediaTransformOutput
Base class for the output of MediaHandler::doTransform() and File::transform().
Definition: MediaTransformOutput.php:29
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ImageGalleryBase\$mWidths
int $mWidths
Definition: ImageGalleryBase.php:96
TraditionalImageGallery\getThumbPadding
getThumbPadding()
How much padding the thumb has between the image and the inner div that contains the border.
Definition: TraditionalImageGallery.php:271
TraditionalImageGallery\getThumbDivWidth
getThumbDivWidth( $thumbWidth)
Get the width of the inner div that contains the thumbnail in question.
Definition: TraditionalImageGallery.php:348
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
Title\getText
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:996
Language
Internationalisation code.
Definition: Language.php:39