11 use Wikimedia\Assert\Assert;
47 $resolveFilesViaParser = $this->mParser instanceof
Parser;
48 if ( $resolveFilesViaParser ) {
49 $parserOutput = $this->mParser->
getOutput();
51 $linkRenderer = $this->mParser->getLinkRenderer();
52 $badFileLookup = $this->mParser->getBadFileLookup();
55 $services = MediaWikiServices::getInstance();
56 $repoGroup = $services->getRepoGroup();
57 $linkRenderer = $services->getLinkRenderer();
58 $badFileLookup = $services->getBadFileLookup();
61 if ( $this->mPerRow > 0 ) {
62 $maxwidth = $this->mPerRow * ( $this->mWidths + $this->
getAllPadding() );
63 $oldStyle = $this->mAttribs[
'style'] ??
'';
64 $this->mAttribs[
'style'] =
"max-width: {$maxwidth}px;" . $oldStyle;
67 $attribs = Sanitizer::mergeAttributes(
68 [
'class' =>
'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
70 $parserOutput->addModules( $this->
getModules() );
71 $parserOutput->addModuleStyles( [
'mediawiki.page.gallery.styles' ] );
73 if ( $this->mCaption ) {
74 $output .=
"\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
77 if ( $this->mShowFilename ) {
80 $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
81 $lb = $linkBatchFactory->newLinkBatch();
82 foreach ( $this->mImages as [ $title, ] ) {
83 $lb->addObj( $title );
89 $enableLegacyMediaDOM =
90 $this->
getConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
91 $hookRunner =
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
93 # Output each image...
94 foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading, $imageOptions ] ) {
99 if ( $nt->inNamespace(
NS_FILE ) && !$nt->isExternal() ) {
101 if ( $resolveFilesViaParser ) {
102 # Give extensions a chance to select the file revision for us
104 $hookRunner->onBeforeParserFetchFileAndTitle(
106 $this->mParser, $nt, $options, $descQuery );
107 # Fetch and register the file (file title may be different via hooks)
108 [ $img, $nt ] = $this->mParser->fetchFileAndTitle( $nt, $options );
110 $img = $repoGroup->findFile( $nt );
117 $thumb = $img ? $img->transform( $transformOptions ) :
false;
119 $rdfaType =
'mw:File';
121 $isBadFile = $img && $thumb && $this->mHideBadImages &&
122 $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() );
124 if ( !$img || !$thumb || ( !$enableLegacyMediaDOM && $thumb->isError() ) || $isBadFile ) {
125 $rdfaType =
'mw:Error ' . $rdfaType;
127 if ( $enableLegacyMediaDOM ) {
129 $thumbhtml = $linkRenderer->makeKnownLink( $nt, $nt->getText() );
131 $thumbhtml = htmlspecialchars( $img ? $img->getLastError() : $nt->getText() );
134 $currentExists = $img && $img->exists();
135 if ( $currentExists && !$thumb ) {
136 $label =
wfMessage(
'thumbnail_error',
'' )->text();
137 } elseif ( $thumb && $thumb->isError() ) {
140 'Unknown MediaTransformOutput: ' . get_class( $thumb )
142 $label = $thumb->toText();
146 $thumbhtml = Linker::makeBrokenImageLinkObj(
147 $nt, $label,
'',
'',
'',
false, $transformOptions, $currentExists
149 $thumbhtml = Html::rawElement(
'span', [
'typeof' => $rdfaType ], $thumbhtml );
152 $thumbhtml =
"\n\t\t\t" .
'<div class="thumb" style="height: '
154 . $thumbhtml .
'</div>';
156 if ( !$img && $resolveFilesViaParser ) {
157 $this->mParser->addTrackingCategory(
'broken-file-category' );
161 $vpad = $this->
getVPad( $this->mHeights, $thumb->getHeight() );
164 if ( $imageOptions ===
null ) {
167 'desc-query' => $descQuery,
169 'custom-url-link' => $link
177 if ( $alt !==
null ) {
178 $params[
'alt'] = $alt;
180 $params[
'title'] = $imageOptions[
'title'];
181 if ( !$enableLegacyMediaDOM ) {
182 $params[
'img-class'] =
'mw-file-element';
184 $imageParameters = Linker::getImageLinkMTOParams(
185 $imageOptions, $descQuery, $this->mParser
190 $imageParameters[
'loading'] =
'lazy';
195 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
197 $thumbhtml = $thumb->toHtml( $imageParameters );
199 if ( !$enableLegacyMediaDOM ) {
200 $thumbhtml = Html::rawElement(
201 'span', [
'typeof' => $rdfaType ], $thumbhtml
204 $thumbhtml = Html::rawElement(
'div', [
205 # Auto-margin centering
for block-level elements. Needed
206 # now that we have video handlers since they may emit block-
207 # level elements as opposed to simple <img> tags. ref
209 'style' =>
"margin:{$vpad}px auto;",
213 # Set both fixed width and min-height.
216 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
'div', [
218 'style' =>
"width: {$width}px;" .
219 ( !$enableLegacyMediaDOM && $this->mMode ===
'traditional' ?
220 " height: {$height}px;" :
'' ),
224 if ( $resolveFilesViaParser ) {
226 $handler = $img->getHandler();
228 $handler->parserTransformHook( $this->mParser, $img );
230 $this->mParser->modifyImageHtml(
231 $img, [
'handler' => $imageParameters ], $thumbhtml );
237 if ( $this->mShowDimensions ) {
238 $meta[] = htmlspecialchars( $img->getDimensionsString() );
240 if ( $this->mShowBytes ) {
241 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
243 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
244 $meta[] = $this->
msg(
'filemissing' )->escaped();
246 $meta = $lang->semicolonList( $meta );
251 $textlink = $this->mShowFilename ?
255 $galleryText = $this->
wrapGalleryText( $textlink . $text . $meta, $thumb );
258 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
259 # Can be safely removed if FF2 falls completely out of existence
260 $output .=
"\n\t\t" .
'<li class="gallerybox" style="width: '
262 . ( $enableLegacyMediaDOM ?
'<div style="width: ' . $gbWidth .
'">' :
'' )
266 . ( $enableLegacyMediaDOM ?
'</div>' :
'' )
269 $output .=
"\n</ul>";
288 'class' =>
'galleryfilename' .
289 ( $this->getCaptionLength() ===
true ?
' galleryfilename-truncate' :
'' )
303 # ATTENTION: The newline after <div class="gallerytext"> is needed to
304 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
305 # its absence, see: https:
308 return "\n\t\t\t" .
'<div class="gallerytext">' .
"\n"
372 protected function getVPad( $boxHeight, $thumbHeight ) {
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.
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
A class containing constants representing the names of configuration variables.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
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'.
getAllPadding()
Get total padding.
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.