MediaWiki REL1_39
TraditionalImageGallery.php
Go to the documentation of this file.
1<?php
2
6use Wikimedia\Assert\Assert;
7
41 public function toHTML() {
42 $resolveFilesViaParser = $this->mParser instanceof Parser;
43 if ( $resolveFilesViaParser ) {
44 $parserOutput = $this->mParser->getOutput();
45 $repoGroup = null;
46 $linkRenderer = $this->mParser->getLinkRenderer();
47 $badFileLookup = $this->mParser->getBadFileLookup();
48 } else {
49 $parserOutput = $this->getOutput();
50 $services = MediaWikiServices::getInstance();
51 $repoGroup = $services->getRepoGroup();
52 $linkRenderer = $services->getLinkRenderer();
53 $badFileLookup = $services->getBadFileLookup();
54 }
55
56 if ( $this->mPerRow > 0 ) {
57 $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
58 $oldStyle = $this->mAttribs['style'] ?? '';
59 $this->mAttribs['style'] = "max-width: {$maxwidth}px;" . $oldStyle;
60 }
61
62 $attribs = Sanitizer::mergeAttributes(
63 [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
64
65 $parserOutput->addModules( $this->getModules() );
66 $parserOutput->addModuleStyles( [ 'mediawiki.page.gallery.styles' ] );
67 $output = Xml::openElement( 'ul', $attribs );
68 if ( $this->mCaption ) {
69 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
70 }
71
72 if ( $this->mShowFilename ) {
73 // Preload LinkCache info for when generating links
74 // of the filename below
75 $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
76 $lb = $linkBatchFactory->newLinkBatch();
77 foreach ( $this->mImages as [ $title, /* see below */ ] ) {
78 $lb->addObj( $title );
79 }
80 $lb->execute();
81 }
82
83 $lang = $this->getRenderLang();
84 $enableLegacyMediaDOM =
85 $this->getConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
86
87 # Output each image...
88 foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading, $imageOptions ] ) {
89 // "text" means "caption" here
92 $descQuery = false;
93 if ( $nt->inNamespace( NS_FILE ) && !$nt->isExternal() ) {
94 # Get the file...
95 if ( $resolveFilesViaParser ) {
96 # Give extensions a chance to select the file revision for us
97 $options = [];
98 Hooks::runner()->onBeforeParserFetchFileAndTitle(
99 // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
100 $this->mParser, $nt, $options, $descQuery );
101 # Fetch and register the file (file title may be different via hooks)
102 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
103 } else {
104 $img = $repoGroup->findFile( $nt );
105 }
106 } else {
107 $img = false;
108 }
109
110 $transformOptions = $this->getThumbParams( $img ) + $handlerOpts;
111 $thumb = $img ? $img->transform( $transformOptions ) : false;
112
113 $rdfaType = 'mw:File';
114
115 $isBadFile = $img && $thumb && $this->mHideBadImages &&
116 $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() );
117
118 if ( !$img || !$thumb || ( !$enableLegacyMediaDOM && $thumb->isError() ) || $isBadFile ) {
119 $rdfaType = 'mw:Error ' . $rdfaType;
120
121 if ( $enableLegacyMediaDOM ) {
122 if ( $isBadFile ) {
123 $thumbhtml = $linkRenderer->makeKnownLink( $nt, $nt->getText() );
124 } else {
125 $thumbhtml = htmlspecialchars( $img ? $img->getLastError() : $nt->getText() );
126 }
127 } else {
128 $currentExists = $img && $img->exists();
129 if ( $currentExists && !$thumb ) {
130 $label = wfMessage( 'thumbnail_error', '' )->text();
131 } elseif ( $thumb && $thumb->isError() ) {
132 Assert::invariant(
133 $thumb instanceof MediaTransformError,
134 'Unknown MediaTransformOutput: ' . get_class( $thumb )
135 );
136 $label = $thumb->toText();
137 } else {
138 $label = '';
139 }
141 $nt, $label, '', '', '', false, $transformOptions, $currentExists
142 );
143 $thumbhtml = Html::rawElement( 'span', [ 'typeof' => $rdfaType ], $thumbhtml );
144 }
145
146 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
147 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
148 . $thumbhtml . '</div>';
149
150 if ( !$img && $resolveFilesViaParser ) {
151 $this->mParser->addTrackingCategory( 'broken-file-category' );
152 }
153 } else {
155 $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
156
157 // Backwards compat before the $imageOptions existed
158 if ( $imageOptions === null ) {
159 $imageParameters = [
160 'desc-link' => true,
161 'desc-query' => $descQuery,
162 'alt' => $alt,
163 'custom-url-link' => $link
164 ];
165 } else {
166 $params = [
167 'alt' => $alt,
168 'title' => $imageOptions['title'],
169 ];
170 $imageParameters = Linker::getImageLinkMTOParams(
171 $imageOptions, $descQuery, $this->mParser
172 ) + $params;
173 }
174
175 if ( $loading === ImageGalleryBase::LOADING_LAZY ) {
176 $imageParameters['loading'] = 'lazy';
177 }
178
179 $this->adjustImageParameters( $thumb, $imageParameters );
180
181 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
182
183 $thumbhtml = $thumb->toHtml( $imageParameters );
184
185 if ( !$enableLegacyMediaDOM ) {
186 $thumbhtml = Html::rawElement(
187 'span', [ 'typeof' => $rdfaType ], $thumbhtml
188 );
189 } else {
190 $thumbhtml = Html::rawElement( 'div', [
191 # Auto-margin centering for block-level elements. Needed
192 # now that we have video handlers since they may emit block-
193 # level elements as opposed to simple <img> tags. ref
194 # http://css-discuss.incutio.com/?page=CenteringBlockElement
195 'style' => "margin:{$vpad}px auto;",
196 ], $thumbhtml );
197 }
198
199 # Set both fixed width and min-height.
200 $width = $this->getThumbDivWidth( $thumb->getWidth() );
201 $height = $this->getThumbPadding() + $this->mHeights;
202 $thumbhtml = "\n\t\t\t" . Html::rawElement( 'div', [
203 'class' => 'thumb',
204 'style' => "width: {$width}px;" .
205 ( !$enableLegacyMediaDOM && $this->mMode === 'traditional' ?
206 " height: {$height}px;" : '' ),
207 ], $thumbhtml );
208
209 // Call parser transform hook
210 if ( $resolveFilesViaParser ) {
212 $handler = $img->getHandler();
213 if ( $handler ) {
214 $handler->parserTransformHook( $this->mParser, $img );
215 }
216 $this->mParser->modifyImageHtml(
217 $img, [ 'handler' => $imageParameters ], $thumbhtml );
218 }
219 }
220
221 $meta = [];
222 if ( $img ) {
223 if ( $this->mShowDimensions ) {
224 $meta[] = htmlspecialchars( $img->getDimensionsString() );
225 }
226 if ( $this->mShowBytes ) {
227 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
228 }
229 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
230 $meta[] = $this->msg( 'filemissing' )->escaped();
231 }
232 $meta = $lang->semicolonList( $meta );
233 if ( $meta ) {
234 $meta .= "<br />\n";
235 }
236
237 $textlink = $this->mShowFilename ?
238 $this->getCaptionHtml( $nt, $lang, $linkRenderer ) :
239 '';
240
241 $galleryText = $this->wrapGalleryText( $textlink . $text . $meta, $thumb );
242
243 $gbWidth = $this->getGBWidthOverwrite( $thumb ) ?: $this->getGBWidth( $thumb ) . 'px';
244 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
245 # Can be safely removed if FF2 falls completely out of existence
246 $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
247 . $gbWidth . '">'
248 . ( $enableLegacyMediaDOM ? '<div style="width: ' . $gbWidth . '">' : '' )
249 . $thumbhtml
250 . $galleryText
251 . "\n\t\t"
252 . ( $enableLegacyMediaDOM ? '</div>' : '' )
253 . "</li>";
254 }
255 $output .= "\n</ul>";
256
257 return $output;
258 }
259
266 protected function getCaptionHtml( Title $nt, Language $lang, LinkRenderer $linkRenderer ) {
267 // Preloaded into LinkCache in toHTML
268 return $linkRenderer->makeKnownLink(
269 $nt,
270 is_int( $this->getCaptionLength() ) ?
271 $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
272 $nt->getText(),
273 [
274 'class' => 'galleryfilename' .
275 ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
276 ]
277 ) . "\n";
278 }
279
288 protected function wrapGalleryText( $galleryText, $thumb ) {
289 # ATTENTION: The newline after <div class="gallerytext"> is needed to
290 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
291 # its absence, see: https://phabricator.wikimedia.org/T3765
292 # -Ævar
293
294 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
295 . $galleryText
296 . "\n\t\t\t</div>";
297 }
298
305 protected function getThumbPadding() {
306 return 30;
307 }
308
314 protected function getGBPadding() {
315 return 5;
316 }
317
325 protected function getGBBorders() {
326 return 8;
327 }
328
336 protected function getCaptionLength() {
338 }
339
345 protected function getAllPadding() {
346 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
347 }
348
358 protected function getVPad( $boxHeight, $thumbHeight ) {
359 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
360 }
361
368 protected function getThumbParams( $img ) {
369 return [
370 'width' => $this->mWidths,
371 'height' => $this->mHeights
372 ];
373 }
374
382 protected function getThumbDivWidth( $thumbWidth ) {
383 return $this->mWidths + $this->getThumbPadding();
384 }
385
396 protected function getGBWidth( $thumb ) {
397 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
398 }
399
411 protected function getGBWidthOverwrite( $thumb ) {
412 return false;
413 }
414
422 protected function getModules() {
423 return [];
424 }
425
433 protected function adjustImageParameters( $thumb, &$imageParameters ) {
434 }
435}
const NS_FILE
Definition Defines.php:70
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
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:53
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
static makeBrokenImageLinkObj( $title, $label='', $query='', $unused1='', $unused2='', $time=false, array $handlerParams=[], bool $currentExists=false)
Make a "broken" link to an image.
Definition Linker.php:870
static processResponsiveImages( $file, $thumb, $hp)
Process responsive images: add 1.5x and 2x subimages to the thumbnail, where applicable.
Definition Linker.php:833
static getImageLinkMTOParams( $frameParams, $query='', $parser=null)
Get the link parameters for MediaTransformOutput::toHtml() from given frame parameters supplied by th...
Definition Linker.php:551
Basic media transform error class.
Base class for the output of MediaHandler::doTransform() and File::transform().
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:96
getOutput()
Definition Parser.php:1118
Represents a title within MediaWiki.
Definition Title.php:49
getText()
Get the text form (spaces not underscores) of the main part.
Definition Title.php:1039
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.
if(!isset( $args[0])) $lang