42 $resolveFilesViaParser = $this->mParser instanceof
Parser;
43 if ( $resolveFilesViaParser ) {
44 $parserOutput = $this->mParser->
getOutput();
46 $linkRenderer = $this->mParser->getLinkRenderer();
47 $badFileLookup = $this->mParser->getBadFileLookup();
50 $services = MediaWikiServices::getInstance();
51 $repoGroup = $services->getRepoGroup();
52 $linkRenderer = $services->getLinkRenderer();
53 $badFileLookup = $services->getBadFileLookup();
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;
62 $attribs = Sanitizer::mergeAttributes(
63 [
'class' =>
'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
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>";
72 if ( $this->mShowFilename ) {
75 $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
76 $lb = $linkBatchFactory->newLinkBatch();
77 foreach ( $this->mImages as [
$title, ] ) {
84 $enableLegacyMediaDOM =
85 $this->
getConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
87 # Output each image...
88 foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading, $imageOptions ] ) {
93 if ( $nt->inNamespace(
NS_FILE ) && !$nt->isExternal() ) {
95 if ( $resolveFilesViaParser ) {
96 # Give extensions a chance to select the file revision for us
98 Hooks::runner()->onBeforeParserFetchFileAndTitle(
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 );
104 $img = $repoGroup->findFile( $nt );
111 $thumb = $img ? $img->transform( $transformOptions ) :
false;
113 $rdfaType =
'mw:File';
115 $isBadFile = $img && $thumb && $this->mHideBadImages &&
116 $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() );
118 if ( !$img || !$thumb || ( !$enableLegacyMediaDOM && $thumb->isError() ) || $isBadFile ) {
119 $rdfaType =
'mw:Error ' . $rdfaType;
121 if ( $enableLegacyMediaDOM ) {
123 $thumbhtml = $linkRenderer->makeKnownLink( $nt, $nt->getText() );
125 $thumbhtml = htmlspecialchars( $img ? $img->getLastError() : $nt->getText() );
128 $currentExists = $img && $img->exists();
129 if ( $currentExists && !$thumb ) {
130 $label =
wfMessage(
'thumbnail_error',
'' )->text();
131 } elseif ( $thumb && $thumb->isError() ) {
134 'Unknown MediaTransformOutput: ' . get_class( $thumb )
136 $label = $thumb->toText();
141 $nt, $label,
'',
'',
'',
false, $transformOptions, $currentExists
143 $thumbhtml = Html::rawElement(
'span', [
'typeof' => $rdfaType ], $thumbhtml );
146 $thumbhtml =
"\n\t\t\t" .
'<div class="thumb" style="height: '
148 . $thumbhtml .
'</div>';
150 if ( !$img && $resolveFilesViaParser ) {
151 $this->mParser->addTrackingCategory(
'broken-file-category' );
155 $vpad = $this->
getVPad( $this->mHeights, $thumb->getHeight() );
158 if ( $imageOptions ===
null ) {
161 'desc-query' => $descQuery,
163 'custom-url-link' => $link
168 'title' => $imageOptions[
'title'],
171 $imageOptions, $descQuery, $this->mParser
176 $imageParameters[
'loading'] =
'lazy';
183 $thumbhtml = $thumb->toHtml( $imageParameters );
185 if ( !$enableLegacyMediaDOM ) {
186 $thumbhtml = Html::rawElement(
187 'span', [
'typeof' => $rdfaType ], $thumbhtml
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
195 'style' =>
"margin:{$vpad}px auto;",
199 # Set both fixed width and min-height.
202 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
'div', [
204 'style' =>
"width: {$width}px;" .
205 ( !$enableLegacyMediaDOM && $this->mMode ===
'traditional' ?
206 " height: {$height}px;" :
'' ),
210 if ( $resolveFilesViaParser ) {
212 $handler = $img->getHandler();
214 $handler->parserTransformHook( $this->mParser, $img );
216 $this->mParser->modifyImageHtml(
217 $img, [
'handler' => $imageParameters ], $thumbhtml );
223 if ( $this->mShowDimensions ) {
224 $meta[] = htmlspecialchars( $img->getDimensionsString() );
226 if ( $this->mShowBytes ) {
227 $meta[] = htmlspecialchars(
$lang->formatSize( $img->getSize() ) );
229 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
230 $meta[] = $this->
msg(
'filemissing' )->escaped();
232 $meta =
$lang->semicolonList( $meta );
237 $textlink = $this->mShowFilename ?
241 $galleryText = $this->
wrapGalleryText( $textlink . $text . $meta, $thumb );
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: '
248 . ( $enableLegacyMediaDOM ?
'<div style="width: ' . $gbWidth .
'">' :
'' )
252 . ( $enableLegacyMediaDOM ?
'</div>' :
'' )
255 $output .=
"\n</ul>";