MediaWiki REL1_37
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 $enableLegacyMediaDOM = $this->getConfig()->get( 'ParserEnableLegacyMediaDOM' );
83
84 # Output each image...
85 foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading ] ) {
86 // "text" means "caption" here
89 $descQuery = false;
90 if ( $nt->getNamespace() === NS_FILE ) {
91 # Get the file...
92 if ( $resolveFilesViaParser ) {
93 # Give extensions a chance to select the file revision for us
94 $options = [];
95 Hooks::runner()->onBeforeParserFetchFileAndTitle(
96 $this->mParser, $nt, $options, $descQuery );
97 # Fetch and register the file (file title may be different via hooks)
98 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
99 } else {
100 $img = $repoGroup->findFile( $nt );
101 }
102 } else {
103 $img = false;
104 }
105
106 $params = $this->getThumbParams( $img );
107 $transformOptions = $params + $handlerOpts;
108
109 $thumb = false;
110
111 if ( !$img ) {
112 # We're dealing with a non-image, spit out the name and be done with it.
113 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
114 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
115 . htmlspecialchars( $nt->getText() ) . '</div>';
116
117 if ( $resolveFilesViaParser ) {
118 $this->mParser->addTrackingCategory( 'broken-file-category' );
119 }
120 } elseif ( $this->mHideBadImages &&
121 $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() )
122 ) {
123 # The image is bad, so just show it as a text link.
124 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
125 ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
126 $linkRenderer->makeKnownLink( $nt, $nt->getText() ) .
127 '</div>';
128 } else {
129 $thumb = $img->transform( $transformOptions );
130 if ( !$thumb ) {
131 # Error generating thumbnail.
132 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
133 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
134 . htmlspecialchars( $img->getLastError() ) . '</div>';
135 } else {
137 $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
138
139 $imageParameters = [
140 'desc-link' => true,
141 'desc-query' => $descQuery,
142 'alt' => $alt,
143 'custom-url-link' => $link
144 ];
145
146 // In the absence of both alt text and caption, fall back on
147 // providing screen readers with the filename as alt text
148 if ( $alt == '' && $text == '' ) {
149 $imageParameters['alt'] = $nt->getText();
150 }
151
152 if ( $loading === ImageGalleryBase::LOADING_LAZY ) {
153 $imageParameters['loading'] = 'lazy';
154 }
155
156 $this->adjustImageParameters( $thumb, $imageParameters );
157
158 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
159
160 switch ( $img->getMediaType() ) {
161 case 'AUDIO':
162 $rdfaType = 'mw:Audio';
163 break;
164 case 'VIDEO':
165 $rdfaType = 'mw:Video';
166 break;
167 default:
168 $rdfaType = 'mw:Image';
169 }
170
171 $thumbhtml = $thumb->toHtml( $imageParameters );
172
173 if ( !$enableLegacyMediaDOM ) {
174 $thumbhtml = Html::rawElement(
175 'span', [ 'typeof' => $rdfaType ], $thumbhtml
176 );
177 }
178
179 $thumbhtml = Html::rawElement( 'div', [
180 # Auto-margin centering for block-level elements. Needed
181 # now that we have video handlers since they may emit block-
182 # level elements as opposed to simple <img> tags. ref
183 # http://css-discuss.incutio.com/?page=CenteringBlockElement
184 'style' => "margin:{$vpad}px auto;",
185 ], $thumbhtml );
186
187 # Set both fixed width and min-height.
188 $width = $this->getThumbDivWidth( $thumb->getWidth() );
189 $thumbhtml = "\n\t\t\t" . Html::rawElement( 'div', [
190 'class' => 'thumb',
191 'style' => "width: {$width}px;",
192 ], $thumbhtml );
193
194 // Call parser transform hook
196 $handler = $img->getHandler();
197 if ( $resolveFilesViaParser && $handler ) {
198 $handler->parserTransformHook( $this->mParser, $img );
199 }
200 }
201 }
202
203 $meta = [];
204 if ( $img ) {
205 if ( $this->mShowDimensions ) {
206 $meta[] = htmlspecialchars( $img->getDimensionsString() );
207 }
208 if ( $this->mShowBytes ) {
209 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
210 }
211 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
212 $meta[] = $this->msg( 'filemissing' )->escaped();
213 }
214 $meta = $lang->semicolonList( $meta );
215 if ( $meta ) {
216 $meta .= "<br />\n";
217 }
218
219 $textlink = $this->mShowFilename ?
220 $this->getCaptionHtml( $nt, $lang, $linkRenderer ) :
221 '';
222
223 $galleryText = $textlink . $text . $meta;
224 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
225
226 $gbWidth = $this->getGBWidth( $thumb ) . 'px';
227 if ( $this->getGBWidthOverwrite( $thumb ) ) {
228 $gbWidth = $this->getGBWidthOverwrite( $thumb );
229 }
230 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
231 # Can be safely removed if FF2 falls completely out of existence
232 $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
233 . $gbWidth . '">'
234 . '<div style="width: ' . $gbWidth . '">'
235 . $thumbhtml
236 . $galleryText
237 . "\n\t\t</div></li>";
238 }
239 $output .= "\n</ul>";
240
241 return $output;
242 }
243
250 protected function getCaptionHtml( Title $nt, Language $lang, LinkRenderer $linkRenderer ) {
251 // Preloaded into LinkCache in toHTML
252 return $linkRenderer->makeKnownLink(
253 $nt,
254 // @phan-suppress-next-line SecurityCheck-DoubleEscaped Triggered by Language::truncateForVisual
255 is_int( $this->getCaptionLength() ) ?
256 $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
257 $nt->getText(),
258 [
259 'class' => 'galleryfilename' .
260 ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
261 ]
262 ) . "\n";
263 }
264
273 protected function wrapGalleryText( $galleryText, $thumb ) {
274 # ATTENTION: The newline after <div class="gallerytext"> is needed to
275 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
276 # its absence, see: https://phabricator.wikimedia.org/T3765
277 # -Ævar
278
279 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
280 . $galleryText
281 . "\n\t\t\t</div>";
282 }
283
290 protected function getThumbPadding() {
291 return 30;
292 }
293
299 protected function getGBPadding() {
300 return 5;
301 }
302
310 protected function getGBBorders() {
311 return 8;
312 }
313
321 protected function getCaptionLength() {
323 }
324
330 protected function getAllPadding() {
331 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
332 }
333
343 protected function getVPad( $boxHeight, $thumbHeight ) {
344 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
345 }
346
353 protected function getThumbParams( $img ) {
354 return [
355 'width' => $this->mWidths,
356 'height' => $this->mHeights
357 ];
358 }
359
367 protected function getThumbDivWidth( $thumbWidth ) {
368 return $this->mWidths + $this->getThumbPadding();
369 }
370
381 protected function getGBWidth( $thumb ) {
382 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
383 }
384
396 protected function getGBWidthOverwrite( $thumb ) {
397 return false;
398 }
399
407 protected function getModules() {
408 return [];
409 }
410
418 protected function adjustImageParameters( $thumb, &$imageParameters ) {
419 }
420}
const NS_FILE
Definition Defines.php:70
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:42
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
static processResponsiveImages( $file, $thumb, $hp)
Process responsive images: add 1.5x and 2x subimages to the thumbnail, where applicable.
Definition Linker.php:792
Base class for the output of MediaHandler::doTransform() and File::transform().
Class that generates HTML links for pages.
makeKnownLink( $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:91
getOutput()
Definition Parser.php:1090
Represents a title within MediaWiki.
Definition Title.php:48
getText()
Get the text form (spaces not underscores) of the main part.
Definition Title.php:1051
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