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  $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
74  $lb = $linkBatchFactory->newLinkBatch();
75  foreach ( $this->mImages as [ $title, /* see below */ ] ) {
76  $lb->addObj( $title );
77  }
78  $lb->execute();
79  }
80 
81  $lang = $this->getRenderLang();
82  # Output each image...
83  foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading ] ) {
84  // "text" means "caption" here
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  $transformOptions = $params + $handlerOpts;
106 
107  $thumb = false;
108 
109  if ( !$img ) {
110  # We're dealing with a non-image, spit out the name and be done with it.
111  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
112  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
113  . htmlspecialchars( $nt->getText() ) . '</div>';
114 
115  if ( $resolveFilesViaParser ) {
116  $this->mParser->addTrackingCategory( 'broken-file-category' );
117  }
118  } elseif ( $this->mHideBadImages &&
119  $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() )
120  ) {
121  # The image is blacklisted, just show it as a text link.
122  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
123  ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
124  $linkRenderer->makeKnownLink( $nt, $nt->getText() ) .
125  '</div>';
126  } else {
127  $thumb = $img->transform( $transformOptions );
128  if ( !$thumb ) {
129  # Error generating thumbnail.
130  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
131  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
132  . htmlspecialchars( $img->getLastError() ) . '</div>';
133  } else {
135  $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
136 
137  $imageParameters = [
138  'desc-link' => true,
139  'desc-query' => $descQuery,
140  'alt' => $alt,
141  'custom-url-link' => $link
142  ];
143 
144  // In the absence of both alt text and caption, fall back on
145  // providing screen readers with the filename as alt text
146  if ( $alt == '' && $text == '' ) {
147  $imageParameters['alt'] = $nt->getText();
148  }
149 
150  if ( $loading === ImageGalleryBase::LOADING_LAZY ) {
151  $imageParameters['loading'] = 'lazy';
152  }
153 
154  $this->adjustImageParameters( $thumb, $imageParameters );
155 
156  Linker::processResponsiveImages( $img, $thumb, $transformOptions );
157 
158  # Set both fixed width and min-height.
159  $thumbhtml = "\n\t\t\t"
160  . '<div class="thumb" style="width: '
161  . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
162  # Auto-margin centering for block-level elements. Needed
163  # now that we have video handlers since they may emit block-
164  # level elements as opposed to simple <img> tags. ref
165  # http://css-discuss.incutio.com/?page=CenteringBlockElement
166  . '<div style="margin:' . $vpad . 'px auto;">'
167  . $thumb->toHtml( $imageParameters ) . '</div></div>';
168 
169  // Call parser transform hook
171  $handler = $img->getHandler();
172  if ( $resolveFilesViaParser && $handler ) {
173  $handler->parserTransformHook( $this->mParser, $img );
174  }
175  }
176  }
177 
178  $meta = [];
179  if ( $img ) {
180  if ( $this->mShowDimensions ) {
181  $meta[] = $img->getDimensionsString();
182  }
183  if ( $this->mShowBytes ) {
184  $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
185  }
186  } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
187  $meta[] = $this->msg( 'filemissing' )->escaped();
188  }
189  $meta = $lang->semicolonList( $meta );
190  if ( $meta ) {
191  $meta .= "<br />\n";
192  }
193 
194  $textlink = $this->mShowFilename ?
195  $this->getCaptionHtml( $nt, $lang, $linkRenderer ) :
196  '';
197 
198  $galleryText = $textlink . $text . $meta;
199  $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
200 
201  $gbWidth = $this->getGBWidth( $thumb ) . 'px';
202  if ( $this->getGBWidthOverwrite( $thumb ) ) {
203  $gbWidth = $this->getGBWidthOverwrite( $thumb );
204  }
205  # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
206  # Can be safely removed if FF2 falls completely out of existence
207  $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
208  . $gbWidth . '">'
209  . '<div style="width: ' . $gbWidth . '">'
210  . $thumbhtml
211  . $galleryText
212  . "\n\t\t</div></li>";
213  }
214  $output .= "\n</ul>";
215 
216  return $output;
217  }
218 
225  protected function getCaptionHtml( Title $nt, Language $lang, LinkRenderer $linkRenderer ) {
226  // Preloaded into LinkCache in toHTML
227  return $linkRenderer->makeKnownLink(
228  $nt,
229  is_int( $this->getCaptionLength() ) ?
230  $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
231  $nt->getText(),
232  [
233  'class' => 'galleryfilename' .
234  ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
235  ]
236  ) . "\n";
237  }
238 
247  protected function wrapGalleryText( $galleryText, $thumb ) {
248  # ATTENTION: The newline after <div class="gallerytext"> is needed to
249  # accommodate htmltidy which in version 4.8.6 generated crackpot html in
250  # its absence, see: https://phabricator.wikimedia.org/T3765
251  # -Ævar
252 
253  return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
254  . $galleryText
255  . "\n\t\t\t</div>";
256  }
257 
264  protected function getThumbPadding() {
265  return 30;
266  }
267 
273  protected function getGBPadding() {
274  return 5;
275  }
276 
284  protected function getGBBorders() {
285  return 8;
286  }
287 
295  protected function getCaptionLength() {
296  return $this->mCaptionLength;
297  }
298 
304  protected function getAllPadding() {
305  return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
306  }
307 
317  protected function getVPad( $boxHeight, $thumbHeight ) {
318  return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
319  }
320 
327  protected function getThumbParams( $img ) {
328  return [
329  'width' => $this->mWidths,
330  'height' => $this->mHeights
331  ];
332  }
333 
341  protected function getThumbDivWidth( $thumbWidth ) {
342  return $this->mWidths + $this->getThumbPadding();
343  }
344 
355  protected function getGBWidth( $thumb ) {
356  return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
357  }
358 
370  protected function getGBWidthOverwrite( $thumb ) {
371  return false;
372  }
373 
381  protected function getModules() {
382  return [];
383  }
384 
392  protected function adjustImageParameters( $thumb, &$imageParameters ) {
393  }
394 }
TraditionalImageGallery\getThumbParams
getThumbParams( $img)
Get the transform parameters for a thumbnail.
Definition: TraditionalImageGallery.php:327
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:165
$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:3618
Sanitizer\mergeAttributes
static mergeAttributes( $a, $b)
Merge two sets of HTML attributes.
Definition: Sanitizer.php:541
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:295
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:370
TraditionalImageGallery\getGBBorders
getGBBorders()
Get how much extra space the borders around the image takes up.
Definition: TraditionalImageGallery.php:284
TraditionalImageGallery\getVPad
getVPad( $boxHeight, $thumbHeight)
Get vertical padding for a thumbnail.
Definition: TraditionalImageGallery.php:317
TraditionalImageGallery
Definition: TraditionalImageGallery.php:27
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:124
TraditionalImageGallery\getCaptionHtml
getCaptionHtml(Title $nt, Language $lang, LinkRenderer $linkRenderer)
Definition: TraditionalImageGallery.php:225
TraditionalImageGallery\wrapGalleryText
wrapGalleryText( $galleryText, $thumb)
Add the wrapper html around the thumb's caption.
Definition: TraditionalImageGallery.php:247
$title
$title
Definition: testCompression.php:38
TraditionalImageGallery\getModules
getModules()
Get a list of modules to include in the page.
Definition: TraditionalImageGallery.php:381
TraditionalImageGallery\getGBWidth
getGBWidth( $thumb)
Computed width of gallerybox .
Definition: TraditionalImageGallery.php:355
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:187
TraditionalImageGallery\adjustImageParameters
adjustImageParameters( $thumb, &$imageParameters)
Adjust the image parameters for a thumbnail.
Definition: TraditionalImageGallery.php:392
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:273
TraditionalImageGallery\getAllPadding
getAllPadding()
Get total padding.
Definition: TraditionalImageGallery.php:304
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:421
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:41
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:264
Parser\getOutput
getOutput()
Definition: Parser.php:1052
TraditionalImageGallery\getThumbDivWidth
getThumbDivWidth( $thumbWidth)
Get the width of the inner div that contains the thumbnail in question.
Definition: TraditionalImageGallery.php:341
Title\getText
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:1007
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42