MediaWiki  master
TraditionalImageGallery.php
Go to the documentation of this file.
1 <?php
2 
5 
39  public function toHTML() {
40  $resolveFilesViaParser = $this->mParser instanceof Parser;
41  if ( $resolveFilesViaParser ) {
42  $out = $this->mParser->getOutput();
43  $repoGroup = null;
44  $linkRenderer = $this->mParser->getLinkRenderer();
45  $badFileLookup = $this->mParser->getBadFileLookup();
46  } else {
47  $out = $this->getOutput();
48  $services = MediaWikiServices::getInstance();
49  $repoGroup = $services->getRepoGroup();
50  $linkRenderer = $services->getLinkRenderer();
51  $badFileLookup = $services->getBadFileLookup();
52  }
53 
54  if ( $this->mPerRow > 0 ) {
55  $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
56  $oldStyle = $this->mAttribs['style'] ?? '';
57  $this->mAttribs['style'] = "max-width: {$maxwidth}px;" . $oldStyle;
58  }
59 
60  $attribs = Sanitizer::mergeAttributes(
61  [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
62 
63  $out->addModules( $this->getModules() );
64  $out->addModuleStyles( 'mediawiki.page.gallery.styles' );
65  $output = Xml::openElement( 'ul', $attribs );
66  if ( $this->mCaption ) {
67  $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
68  }
69 
70  if ( $this->mShowFilename ) {
71  // Preload LinkCache info for when generating links
72  // of the filename below
73  $lb = new LinkBatch();
74  foreach ( $this->mImages as $img ) {
75  $lb->addObj( $img[0] );
76  }
77  $lb->execute();
78  }
79 
80  $lang = $this->getRenderLang();
81  # Output each image...
82  foreach ( $this->mImages as $pair ) {
83  // "text" means "caption" here
85  list( $nt, $text, $alt, $link, $handlerOpts, $loading ) = $pair;
86 
87  $descQuery = false;
88  if ( $nt->getNamespace() === NS_FILE ) {
89  # Get the file...
90  if ( $resolveFilesViaParser ) {
91  # Give extensions a chance to select the file revision for us
92  $options = [];
93  Hooks::runner()->onBeforeParserFetchFileAndTitle(
94  $this->mParser, $nt, $options, $descQuery );
95  # Fetch and register the file (file title may be different via hooks)
96  list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
97  } else {
98  $img = $repoGroup->findFile( $nt );
99  }
100  } else {
101  $img = false;
102  }
103 
104  $params = $this->getThumbParams( $img );
105  // $pair[4] is per image handler options
106  $transformOptions = $params + $pair[4];
107 
108  $thumb = false;
109 
110  if ( !$img ) {
111  # We're dealing with a non-image, spit out the name and be done with it.
112  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
113  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
114  . htmlspecialchars( $nt->getText() ) . '</div>';
115 
116  if ( $resolveFilesViaParser ) {
117  $this->mParser->addTrackingCategory( 'broken-file-category' );
118  }
119  } elseif ( $this->mHideBadImages &&
120  $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() )
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  if ( $loading === ImageGalleryBase::LOADING_LAZY ) {
152  $imageParameters['loading'] = 'lazy';
153  }
154 
155  $this->adjustImageParameters( $thumb, $imageParameters );
156 
157  Linker::processResponsiveImages( $img, $thumb, $transformOptions );
158 
159  # Set both fixed width and min-height.
160  $thumbhtml = "\n\t\t\t"
161  . '<div class="thumb" style="width: '
162  . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
163  # Auto-margin centering for block-level elements. Needed
164  # now that we have video handlers since they may emit block-
165  # level elements as opposed to simple <img> tags. ref
166  # http://css-discuss.incutio.com/?page=CenteringBlockElement
167  . '<div style="margin:' . $vpad . 'px auto;">'
168  . $thumb->toHtml( $imageParameters ) . '</div></div>';
169 
170  // Call parser transform hook
172  $handler = $img->getHandler();
173  if ( $resolveFilesViaParser && $handler ) {
174  $handler->parserTransformHook( $this->mParser, $img );
175  }
176  }
177  }
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, $linkRenderer ) :
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 
226  protected function getCaptionHtml( Title $nt, Language $lang, LinkRenderer $linkRenderer ) {
227  // Preloaded into LinkCache in toHTML
228  return $linkRenderer->makeKnownLink(
229  $nt,
230  is_int( $this->getCaptionLength() ) ?
231  $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
232  $nt->getText(),
233  [
234  'class' => 'galleryfilename' .
235  ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
236  ]
237  ) . "\n";
238  }
239 
248  protected function wrapGalleryText( $galleryText, $thumb ) {
249  # ATTENTION: The newline after <div class="gallerytext"> is needed to
250  # accommodate htmltidy which in version 4.8.6 generated crackpot html in
251  # its absence, see: https://phabricator.wikimedia.org/T3765
252  # -Ævar
253 
254  return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
255  . $galleryText
256  . "\n\t\t\t</div>";
257  }
258 
265  protected function getThumbPadding() {
266  return 30;
267  }
268 
274  protected function getGBPadding() {
275  return 5;
276  }
277 
285  protected function getGBBorders() {
286  return 8;
287  }
288 
296  protected function getCaptionLength() {
297  return $this->mCaptionLength;
298  }
299 
305  protected function getAllPadding() {
306  return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
307  }
308 
318  protected function getVPad( $boxHeight, $thumbHeight ) {
319  return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
320  }
321 
328  protected function getThumbParams( $img ) {
329  return [
330  'width' => $this->mWidths,
331  'height' => $this->mHeights
332  ];
333  }
334 
342  protected function getThumbDivWidth( $thumbWidth ) {
343  return $this->mWidths + $this->getThumbPadding();
344  }
345 
356  protected function getGBWidth( $thumb ) {
357  return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
358  }
359 
371  protected function getGBWidthOverwrite( $thumb ) {
372  return false;
373  }
374 
382  protected function getModules() {
383  return [];
384  }
385 
393  protected function adjustImageParameters( $thumb, &$imageParameters ) {
394  }
395 }
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:328
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:32
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:41
TraditionalImageGallery\toHTML
toHTML()
Return a HTML representation of the image gallery.
Definition: TraditionalImageGallery.php:39
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:3520
Sanitizer\mergeAttributes
static mergeAttributes( $a, $b)
Merge two sets of HTML attributes.
Definition: Sanitizer.php:798
ImageGalleryBase\LOADING_LAZY
const LOADING_LAZY
Definition: ImageGalleryBase.php:34
MediaTransformOutput\toHtml
toHtml( $options=[])
Fetch HTML for this transform output.
ImageGalleryBase\$mHeights
int $mHeights
Definition: ImageGalleryBase.php:102
NS_FILE
const NS_FILE
Definition: Defines.php:75
ImageGalleryBase\$mCaptionLength
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
Definition: ImageGalleryBase.php:74
TraditionalImageGallery\getCaptionLength
getCaptionLength()
Length (in characters) to truncate filename to in caption when using "showfilename" (if int).
Definition: TraditionalImageGallery.php:296
Linker\processResponsiveImages
static processResponsiveImages( $file, $thumb, $hp)
Process responsive images: add 1.5x and 2x subimages to the thumbnail, where applicable.
Definition: Linker.php:639
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:371
TraditionalImageGallery\getGBBorders
getGBBorders()
Get how much extra space the borders around the image takes up.
Definition: TraditionalImageGallery.php:285
TraditionalImageGallery\getVPad
getVPad( $boxHeight, $thumbHeight)
Get vertical padding for a thumbnail.
Definition: TraditionalImageGallery.php:318
TraditionalImageGallery
Definition: TraditionalImageGallery.php:27
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:121
TraditionalImageGallery\getCaptionHtml
getCaptionHtml(Title $nt, Language $lang, LinkRenderer $linkRenderer)
Definition: TraditionalImageGallery.php:226
TraditionalImageGallery\wrapGalleryText
wrapGalleryText( $galleryText, $thumb)
Add the wrapper html around the thumb's caption.
Definition: TraditionalImageGallery.php:248
TraditionalImageGallery\getModules
getModules()
Get a list of modules to include in the page.
Definition: TraditionalImageGallery.php:382
TraditionalImageGallery\getGBWidth
getGBWidth( $thumb)
Computed width of gallerybox .
Definition: TraditionalImageGallery.php:356
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:184
TraditionalImageGallery\adjustImageParameters
adjustImageParameters( $thumb, &$imageParameters)
Adjust the image parameters for a thumbnail.
Definition: TraditionalImageGallery.php:393
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:172
TraditionalImageGallery\getGBPadding
getGBPadding()
Definition: TraditionalImageGallery.php:274
TraditionalImageGallery\getAllPadding
getAllPadding()
Get total padding.
Definition: TraditionalImageGallery.php:305
Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:84
ImageGalleryBase\getRenderLang
getRenderLang()
Determines the correct language to be used for this image gallery.
Definition: ImageGalleryBase.php:422
MediaTransformOutput
Base class for the output of MediaHandler::doTransform() and File::transform().
Definition: MediaTransformOutput.php:30
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ImageGalleryBase\$mWidths
int $mWidths
Definition: ImageGalleryBase.php:99
MediaWiki\Linker\LinkRenderer\makeKnownLink
makeKnownLink(LinkTarget $target, $text=null, array $extraAttribs=[], array $query=[])
Definition: LinkRenderer.php:225
TraditionalImageGallery\getThumbPadding
getThumbPadding()
How much padding the thumb has between the image and the inner div that contains the border.
Definition: TraditionalImageGallery.php:265
Parser\getOutput
getOutput()
Definition: Parser.php:1061
TraditionalImageGallery\getThumbDivWidth
getThumbDivWidth( $thumbWidth)
Get the width of the inner div that contains the thumbnail in question.
Definition: TraditionalImageGallery.php:342
Title\getText
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:1005
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:41