MediaWiki  master
TraditionalImageGallery.php
Go to the documentation of this file.
1 <?php
2 
6 
40  public function toHTML() {
41  $resolveFilesViaParser = $this->mParser instanceof Parser;
42  if ( $resolveFilesViaParser ) {
43  $parserOutput = $this->mParser->getOutput();
44  $repoGroup = null;
45  $linkRenderer = $this->mParser->getLinkRenderer();
46  $badFileLookup = $this->mParser->getBadFileLookup();
47  } else {
48  $parserOutput = $this->getOutput();
49  $services = MediaWikiServices::getInstance();
50  $repoGroup = $services->getRepoGroup();
51  $linkRenderer = $services->getLinkRenderer();
52  $badFileLookup = $services->getBadFileLookup();
53  }
54 
55  if ( $this->mPerRow > 0 ) {
56  $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
57  $oldStyle = $this->mAttribs['style'] ?? '';
58  $this->mAttribs['style'] = "max-width: {$maxwidth}px;" . $oldStyle;
59  }
60 
61  $attribs = Sanitizer::mergeAttributes(
62  [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
63 
64  $parserOutput->addModules( $this->getModules() );
65  $parserOutput->addModuleStyles( [ 'mediawiki.page.gallery.styles' ] );
66  $output = Xml::openElement( 'ul', $attribs );
67  if ( $this->mCaption ) {
68  $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
69  }
70 
71  if ( $this->mShowFilename ) {
72  // Preload LinkCache info for when generating links
73  // of the filename below
74  $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
75  $lb = $linkBatchFactory->newLinkBatch();
76  foreach ( $this->mImages as [ $title, /* see below */ ] ) {
77  $lb->addObj( $title );
78  }
79  $lb->execute();
80  }
81 
82  $lang = $this->getRenderLang();
83  $enableLegacyMediaDOM =
84  $this->getConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
85 
86  # Output each image...
87  foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading, $imageOptions ] ) {
88  // "text" means "caption" here
91  $descQuery = false;
92  if ( $nt->inNamespace( NS_FILE ) && !$nt->isExternal() ) {
93  # Get the file...
94  if ( $resolveFilesViaParser ) {
95  # Give extensions a chance to select the file revision for us
96  $options = [];
97  Hooks::runner()->onBeforeParserFetchFileAndTitle(
98  // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
99  $this->mParser, $nt, $options, $descQuery );
100  # Fetch and register the file (file title may be different via hooks)
101  [ $img, $nt ] = $this->mParser->fetchFileAndTitle( $nt, $options );
102  } else {
103  $img = $repoGroup->findFile( $nt );
104  }
105  } else {
106  $img = false;
107  }
108 
109  $transformOptions = $this->getThumbParams( $img ) + $handlerOpts;
110  $thumb = $img ? $img->transform( $transformOptions ) : false;
111 
112  $rdfaType = 'mw:File';
113 
114  $isBadFile = $img && $thumb && $this->mHideBadImages &&
115  $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() );
116 
117  if ( !$img || !$thumb || $isBadFile ) {
118  $rdfaType = 'mw:Error ' . $rdfaType;
119 
120  if ( $enableLegacyMediaDOM ) {
121  if ( $isBadFile ) {
122  $thumbhtml = $linkRenderer->makeKnownLink( $nt, $nt->getText() );
123  } else {
124  $thumbhtml = htmlspecialchars( $img ? $img->getLastError() : $nt->getText() );
125  }
126  } else {
127  // FIXME: BadFile is known
128  $thumbhtml = Linker::makeBrokenImageLinkObj(
129  $nt, '', '', '', '', false, $transformOptions
130  );
131  $thumbhtml = Html::rawElement( 'span', [ 'typeof' => $rdfaType ], $thumbhtml );
132  }
133 
134  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
135  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
136  . $thumbhtml . '</div>';
137 
138  if ( !$img && $resolveFilesViaParser ) {
139  $this->mParser->addTrackingCategory( 'broken-file-category' );
140  }
141  } else {
143  $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
144 
145  // Backwards compat before the $imageOptions existed
146  if ( $imageOptions === null ) {
147  $imageParameters = [
148  'desc-link' => true,
149  'desc-query' => $descQuery,
150  'alt' => $alt,
151  'custom-url-link' => $link
152  ];
153  } else {
154  $params = [
155  'alt' => $alt,
156  'title' => $imageOptions['title'],
157  ];
158  $imageParameters = Linker::getImageLinkMTOParams(
159  $imageOptions, $descQuery, $this->mParser
160  ) + $params;
161  }
162 
163  if ( $loading === ImageGalleryBase::LOADING_LAZY ) {
164  $imageParameters['loading'] = 'lazy';
165  }
166 
167  $this->adjustImageParameters( $thumb, $imageParameters );
168 
169  Linker::processResponsiveImages( $img, $thumb, $transformOptions );
170 
171  $thumbhtml = $thumb->toHtml( $imageParameters );
172 
173  if ( !$enableLegacyMediaDOM ) {
174  $thumbhtml = Html::rawElement(
175  'span', [ 'typeof' => $rdfaType ], $thumbhtml
176  );
177  } else {
178  $thumbhtml = Html::rawElement( 'div', [
179  # Auto-margin centering for block-level elements. Needed
180  # now that we have video handlers since they may emit block-
181  # level elements as opposed to simple <img> tags. ref
182  # http://css-discuss.incutio.com/?page=CenteringBlockElement
183  'style' => "margin:{$vpad}px auto;",
184  ], $thumbhtml );
185  }
186 
187  # Set both fixed width and min-height.
188  $width = $this->getThumbDivWidth( $thumb->getWidth() );
189  $height = $this->getThumbPadding() + $this->mHeights;
190  $thumbhtml = "\n\t\t\t" . Html::rawElement( 'div', [
191  'class' => 'thumb',
192  'style' => "width: {$width}px;" .
193  ( !$enableLegacyMediaDOM && $this->mMode === 'traditional' ?
194  " height: {$height}px;" : '' ),
195  ], $thumbhtml );
196 
197  // Call parser transform hook
198  if ( $resolveFilesViaParser ) {
200  $handler = $img->getHandler();
201  if ( $handler ) {
202  $handler->parserTransformHook( $this->mParser, $img );
203  }
204  $this->mParser->modifyImageHtml(
205  $img, [ 'handler' => $imageParameters ], $thumbhtml );
206  }
207  }
208 
209  $meta = [];
210  if ( $img ) {
211  if ( $this->mShowDimensions ) {
212  $meta[] = htmlspecialchars( $img->getDimensionsString() );
213  }
214  if ( $this->mShowBytes ) {
215  $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
216  }
217  } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
218  $meta[] = $this->msg( 'filemissing' )->escaped();
219  }
220  $meta = $lang->semicolonList( $meta );
221  if ( $meta ) {
222  $meta .= "<br />\n";
223  }
224 
225  $textlink = $this->mShowFilename ?
226  $this->getCaptionHtml( $nt, $lang, $linkRenderer ) :
227  '';
228 
229  $galleryText = $this->wrapGalleryText( $textlink . $text . $meta, $thumb );
230 
231  $gbWidth = $this->getGBWidthOverwrite( $thumb ) ?: $this->getGBWidth( $thumb ) . 'px';
232  # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
233  # Can be safely removed if FF2 falls completely out of existence
234  $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
235  . $gbWidth . '">'
236  . ( $enableLegacyMediaDOM ? '<div style="width: ' . $gbWidth . '">' : '' )
237  . $thumbhtml
238  . $galleryText
239  . "\n\t\t"
240  . ( $enableLegacyMediaDOM ? '</div>' : '' )
241  . "</li>";
242  }
243  $output .= "\n</ul>";
244 
245  return $output;
246  }
247 
254  protected function getCaptionHtml( Title $nt, Language $lang, LinkRenderer $linkRenderer ) {
255  // Preloaded into LinkCache in toHTML
256  return $linkRenderer->makeKnownLink(
257  $nt,
258  is_int( $this->getCaptionLength() ) ?
259  $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
260  $nt->getText(),
261  [
262  'class' => 'galleryfilename' .
263  ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
264  ]
265  ) . "\n";
266  }
267 
276  protected function wrapGalleryText( $galleryText, $thumb ) {
277  # ATTENTION: The newline after <div class="gallerytext"> is needed to
278  # accommodate htmltidy which in version 4.8.6 generated crackpot html in
279  # its absence, see: https://phabricator.wikimedia.org/T3765
280  # -Ævar
281 
282  return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
283  . $galleryText
284  . "\n\t\t\t</div>";
285  }
286 
293  protected function getThumbPadding() {
294  return 30;
295  }
296 
302  protected function getGBPadding() {
303  return 5;
304  }
305 
313  protected function getGBBorders() {
314  return 8;
315  }
316 
324  protected function getCaptionLength() {
325  return $this->mCaptionLength;
326  }
327 
333  protected function getAllPadding() {
334  return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
335  }
336 
346  protected function getVPad( $boxHeight, $thumbHeight ) {
347  return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
348  }
349 
356  protected function getThumbParams( $img ) {
357  return [
358  'width' => $this->mWidths,
359  'height' => $this->mHeights
360  ];
361  }
362 
370  protected function getThumbDivWidth( $thumbWidth ) {
371  return $this->mWidths + $this->getThumbPadding();
372  }
373 
384  protected function getGBWidth( $thumb ) {
385  return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
386  }
387 
399  protected function getGBWidthOverwrite( $thumb ) {
400  return false;
401  }
402 
410  protected function getModules() {
411  return [];
412  }
413 
421  protected function adjustImageParameters( $thumb, &$imageParameters ) {
422  }
423 }
const NS_FILE
Definition: Defines.php:70
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
Image gallery.
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
getRenderLang()
Determines the correct language to be used for this image gallery.
Base class for language-specific code.
Definition: Language.php:54
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
Definition: Language.php:3564
static processResponsiveImages( $file, $thumb, $hp)
Process responsive images: add 1.5x and 2x subimages to the thumbnail, where applicable.
Definition: Linker.php:779
static makeBrokenImageLinkObj( $title, $label='', $query='', $unused1='', $unused2='', $time=false, array $handlerParams=[])
Make a "broken" link to an image.
Definition: Linker.php:815
static getImageLinkMTOParams( $frameParams, $query='', $parser=null)
Get the link parameters for MediaTransformOutput::toHtml() from given frame parameters supplied by th...
Definition: Linker.php:521
Class that generates HTML anchor link elements for pages.
makeKnownLink( $target, $text=null, array $extraAttribs=[], array $query=[])
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:97
getOutput()
Definition: Parser.php:1109
static mergeAttributes( $a, $b)
Merge two sets of HTML attributes.
Definition: Sanitizer.php:675
Represents a title within MediaWiki.
Definition: Title.php:52
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:1042
getThumbPadding()
How much padding the thumb has between the image and the inner div that contains the border.
getCaptionLength()
Length (in characters) to truncate filename to in caption when using "showfilename" (if int).
getVPad( $boxHeight, $thumbHeight)
Get vertical padding for a thumbnail.
getCaptionHtml(Title $nt, Language $lang, LinkRenderer $linkRenderer)
getThumbParams( $img)
Get the transform parameters for a thumbnail.
toHTML()
Return a HTML representation of the image gallery.
getGBWidth( $thumb)
Computed width of gallerybox .
getModules()
Get a list of modules to include in the page.
getGBBorders()
Get how much extra space the borders around the image takes up.
wrapGalleryText( $galleryText, $thumb)
Add the wrapper html around the thumb's caption.
adjustImageParameters( $thumb, &$imageParameters)
Adjust the image parameters for a thumbnail.
getGBWidthOverwrite( $thumb)
Allows overwriting the computed width of the gallerybox with a string, like '100'.
getThumbDivWidth( $thumbWidth)
Get the width of the inner div that contains the thumbnail in question.
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:112
if(!isset( $args[0])) $lang