48 $resolveFilesViaParser = $this->mParser instanceof
Parser;
49 if ( $resolveFilesViaParser ) {
50 $parserOutput = $this->mParser->
getOutput();
52 $linkRenderer = $this->mParser->getLinkRenderer();
53 $badFileLookup = $this->mParser->getBadFileLookup();
56 $services = MediaWikiServices::getInstance();
57 $repoGroup = $services->getRepoGroup();
58 $linkRenderer = $services->getLinkRenderer();
59 $badFileLookup = $services->getBadFileLookup();
62 if ( $this->mPerRow > 0 ) {
63 $maxwidth = $this->mPerRow * ( $this->mWidths + $this->
getAllPadding() );
64 $oldStyle = $this->mAttribs[
'style'] ??
'';
65 $this->mAttribs[
'style'] =
"max-width: {$maxwidth}px;" . $oldStyle;
68 $attribs = Sanitizer::mergeAttributes(
69 [
'class' =>
'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
71 $parserOutput->addModules( $this->
getModules() );
72 $parserOutput->addModuleStyles( [
'mediawiki.page.gallery.styles' ] );
73 $output = Html::openElement(
'ul', $attribs );
74 if ( $this->mCaption ) {
75 $output .=
"\n\t" . Html::rawElement(
'li', [
'class' =>
'gallerycaption' ], $this->mCaption );
78 if ( $this->mShowFilename ) {
81 $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
82 $lb = $linkBatchFactory->newLinkBatch();
83 foreach ( $this->mImages as [ $title, ] ) {
84 $lb->addObj( $title );
90 $enableLegacyMediaDOM =
91 $this->
getConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
92 $hookRunner =
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
94 # Output each image...
95 foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading, $imageOptions ] ) {
100 if ( $nt->inNamespace(
NS_FILE ) && !$nt->isExternal() ) {
102 if ( $resolveFilesViaParser ) {
103 # Give extensions a chance to select the file revision for us
105 $hookRunner->onBeforeParserFetchFileAndTitle(
107 $this->mParser, $nt, $options, $descQuery );
108 # Fetch and register the file (file title may be different via hooks)
109 [ $img, $nt ] = $this->mParser->fetchFileAndTitle( $nt, $options );
111 $img = $repoGroup->findFile( $nt );
118 $thumb = $img ? $img->transform( $transformOptions ) :
false;
120 $rdfaType =
'mw:File';
122 $isBadFile = $img && $thumb && $this->mHideBadImages &&
123 $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() );
125 if ( !$img || !$thumb || ( !$enableLegacyMediaDOM && $thumb->isError() ) || $isBadFile ) {
126 $rdfaType =
'mw:Error ' . $rdfaType;
128 if ( $enableLegacyMediaDOM ) {
130 $thumbhtml = $linkRenderer->makeKnownLink( $nt, $nt->getText() );
132 $thumbhtml = htmlspecialchars( $img ? $img->getLastError() : $nt->getText() );
135 $currentExists = $img && $img->exists();
136 if ( $currentExists && !$thumb ) {
137 $label =
wfMessage(
'thumbnail_error',
'' )->text();
138 } elseif ( $thumb && $thumb->isError() ) {
141 'Unknown MediaTransformOutput: ' . get_class( $thumb )
143 $label = $thumb->toText();
147 $thumbhtml = Linker::makeBrokenImageLinkObj(
148 $nt, $label,
'',
'',
'',
false, $transformOptions, $currentExists
150 $thumbhtml = Html::rawElement(
'span', [
'typeof' => $rdfaType ], $thumbhtml );
153 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
157 'style' =>
'height: ' . ( $this->
getThumbPadding() + $this->mHeights ) .
'px;'
162 if ( !$img && $resolveFilesViaParser ) {
163 $this->mParser->addTrackingCategory(
'broken-file-category' );
167 $vpad = $this->
getVPad( $this->mHeights, $thumb->getHeight() );
170 if ( $imageOptions ===
null ) {
173 'desc-query' => $descQuery,
175 'custom-url-link' => $link
183 if ( $alt !==
null ) {
186 $params[
'title'] = $imageOptions[
'title'];
187 if ( !$enableLegacyMediaDOM ) {
188 $params[
'img-class'] =
'mw-file-element';
190 $imageParameters = Linker::getImageLinkMTOParams(
191 $imageOptions, $descQuery, $this->mParser
196 $imageParameters[
'loading'] =
'lazy';
201 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
203 $thumbhtml = $thumb->toHtml( $imageParameters );
205 if ( !$enableLegacyMediaDOM ) {
206 $thumbhtml = Html::rawElement(
207 'span', [
'typeof' => $rdfaType ], $thumbhtml
210 $thumbhtml = Html::rawElement(
'div', [
211 # Auto-margin centering
for block-level elements. Needed
212 # now that we have video handlers since they may emit block-
213 # level elements as opposed to simple <img> tags. ref
215 'style' =>
"margin:{$vpad}px auto;",
219 # Set both fixed width and min-height.
222 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
'div', [
224 'style' =>
"width: {$width}px;" .
225 ( !$enableLegacyMediaDOM && $this->mMode ===
'traditional' ?
226 " height: {$height}px;" :
'' ),
230 if ( $resolveFilesViaParser ) {
232 $handler = $img->getHandler();
234 $handler->parserTransformHook( $this->mParser, $img );
236 $this->mParser->modifyImageHtml(
237 $img, [
'handler' => $imageParameters ], $thumbhtml );
243 if ( $this->mShowDimensions ) {
244 $meta[] = htmlspecialchars( $img->getDimensionsString() );
246 if ( $this->mShowBytes ) {
247 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
249 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
250 $meta[] = $this->
msg(
'filemissing' )->escaped();
252 $meta = $lang->semicolonList( $meta );
254 $meta .= Html::rawElement(
'br', [] ) .
"\n";
257 $textlink = $this->mShowFilename ?
261 $galleryText = $this->
wrapGalleryText( $textlink . $text . $meta, $thumb );
264 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
265 # Can be safely removed if FF2 falls completely out of existence
266 $output .=
"\n\t\t" .
269 [
'class' =>
'gallerybox',
'style' =>
'width: ' . $gbWidth ],
270 ( $enableLegacyMediaDOM ? Html::openElement(
'div', [
'style' =>
'width: ' . $gbWidth ] ) :
'' )
274 . ( $enableLegacyMediaDOM ? Html::closeElement(
'div' ) :
'' )
277 $output .=
"\n" . Html::closeElement(
'ul' );