MediaWiki REL1_35
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[] = htmlspecialchars( $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() {
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}
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.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:41
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:35
static processResponsiveImages( $file, $thumb, $hp)
Process responsive images: add 1.5x and 2x subimages to the thumbnail, where applicable.
Definition Linker.php:649
Base class for the output of MediaHandler::doTransform() and File::transform().
toHtml( $options=[])
Fetch HTML for this transform output.
Class that generates HTML links for pages.
makeKnownLink(LinkTarget $target, $text=null, array $extraAttribs=[], array $query=[])
MediaWikiServices is the service locator for the application scope of MediaWiki.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:85
getOutput()
Definition Parser.php:1062
Represents a title within MediaWiki.
Definition Title.php:42
getText()
Get the text form (spaces not underscores) of the main part.
Definition Title.php:1014
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.
const NS_FILE
Definition Defines.php:76
if(!isset( $args[0])) $lang