49 $resolveFilesViaParser = $this->mParser instanceof
Parser;
50 if ( $resolveFilesViaParser ) {
51 $parserOutput = $this->mParser->
getOutput();
53 $linkRenderer = $this->mParser->getLinkRenderer();
54 $badFileLookup = $this->mParser->getBadFileLookup();
57 $services = MediaWikiServices::getInstance();
58 $repoGroup = $services->getRepoGroup();
59 $linkRenderer = $services->getLinkRenderer();
60 $badFileLookup = $services->getBadFileLookup();
63 if ( $this->mPerRow > 0 ) {
64 $maxwidth = $this->mPerRow * ( $this->mWidths + $this->
getAllPadding() );
65 $oldStyle = $this->mAttribs[
'style'] ??
'';
66 $this->mAttribs[
'style'] =
"max-width: {$maxwidth}px;" . $oldStyle;
69 $attribs = Sanitizer::mergeAttributes(
70 [
'class' =>
'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
72 $parserOutput->addModules( $this->
getModules() );
73 $parserOutput->addModuleStyles( [
'mediawiki.page.gallery.styles' ] );
74 $output = Html::openElement(
'ul', $attribs );
75 if ( $this->mCaption ) {
76 $output .=
"\n\t" . Html::rawElement(
'li', [
'class' =>
'gallerycaption' ], $this->mCaption );
79 if ( $this->mShowFilename ) {
82 $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
83 $lb = $linkBatchFactory->newLinkBatch();
84 foreach ( $this->mImages as [ $title, ] ) {
85 $lb->addObj( $title );
91 $enableLegacyMediaDOM =
92 $this->
getConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
93 $hookRunner =
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
95 # Output each image...
96 foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading, $imageOptions ] ) {
101 if ( $nt->inNamespace(
NS_FILE ) && !$nt->isExternal() ) {
103 if ( $resolveFilesViaParser ) {
104 # Give extensions a chance to select the file revision for us
106 $hookRunner->onBeforeParserFetchFileAndTitle(
108 $this->mParser, $nt, $options, $descQuery );
109 # Fetch and register the file (file title may be different via hooks)
110 [ $img, $nt ] = $this->mParser->fetchFileAndTitle( $nt, $options );
112 $img = $repoGroup->findFile( $nt );
119 $thumb = $img ? $img->transform( $transformOptions ) :
false;
121 $rdfaType =
'mw:File';
123 $isBadFile = $img && $thumb && $this->mHideBadImages &&
124 $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() );
126 if ( !$img || !$thumb || ( !$enableLegacyMediaDOM && $thumb->isError() ) || $isBadFile ) {
127 $rdfaType =
'mw:Error ' . $rdfaType;
129 if ( $enableLegacyMediaDOM ) {
131 $thumbhtml = $linkRenderer->makeKnownLink( $nt, $nt->getText() );
133 $thumbhtml = htmlspecialchars( $img ? $img->getLastError() : $nt->getText() );
136 $currentExists = $img && $img->exists();
137 if ( $currentExists && !$thumb ) {
138 $label =
wfMessage(
'thumbnail_error',
'' )->text();
139 } elseif ( $thumb && $thumb->isError() ) {
142 'Unknown MediaTransformOutput: ' . get_class( $thumb )
144 $label = $thumb->toText();
148 $thumbhtml = Linker::makeBrokenImageLinkObj(
149 $nt, $label,
'',
'',
'',
false, $transformOptions, $currentExists
151 $thumbhtml = Html::rawElement(
'span', [
'typeof' => $rdfaType ], $thumbhtml );
154 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
158 'style' =>
'height: ' . ( $this->
getThumbPadding() + $this->mHeights ) .
'px;'
163 if ( !$img && $resolveFilesViaParser ) {
164 $this->mParser->addTrackingCategory(
'broken-file-category' );
168 $vpad = $this->
getVPad( $this->mHeights, $thumb->getHeight() );
171 if ( $imageOptions ===
null ) {
174 'desc-query' => $descQuery,
176 'custom-url-link' => $link
184 if ( $alt !==
null ) {
187 $params[
'title'] = $imageOptions[
'title'];
188 if ( !$enableLegacyMediaDOM ) {
189 $params[
'img-class'] =
'mw-file-element';
191 $imageParameters = Linker::getImageLinkMTOParams(
192 $imageOptions, $descQuery, $this->mParser
197 $imageParameters[
'loading'] =
'lazy';
202 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
204 $thumbhtml = $thumb->toHtml( $imageParameters );
206 if ( !$enableLegacyMediaDOM ) {
207 $thumbhtml = Html::rawElement(
208 'span', [
'typeof' => $rdfaType ], $thumbhtml
211 $thumbhtml = Html::rawElement(
'div', [
212 # Auto-margin centering
for block-level elements. Needed
213 # now that we have video handlers since they may emit block-
214 # level elements as opposed to simple <img> tags. ref
216 'style' =>
"margin:{$vpad}px auto;",
220 # Set both fixed width and min-height.
223 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
'div', [
225 'style' =>
"width: {$width}px;" .
226 ( !$enableLegacyMediaDOM && $this->mMode ===
'traditional' ?
227 " height: {$height}px;" :
'' ),
231 if ( $resolveFilesViaParser ) {
233 $handler = $img->getHandler();
235 $handler->parserTransformHook( $this->mParser, $img );
237 $this->mParser->modifyImageHtml(
238 $img, [
'handler' => $imageParameters ], $thumbhtml );
244 if ( $this->mShowDimensions ) {
245 $meta[] = htmlspecialchars( $img->getDimensionsString() );
247 if ( $this->mShowBytes ) {
248 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
250 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
251 $meta[] = $this->
msg(
'filemissing' )->escaped();
253 $meta = $lang->semicolonList( $meta );
255 $meta .= Html::rawElement(
'br', [] ) .
"\n";
258 $textlink = $this->mShowFilename ?
262 $galleryText = $this->
wrapGalleryText( $textlink . $text . $meta, $thumb );
265 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
266 # Can be safely removed if FF2 falls completely out of existence
267 $output .=
"\n\t\t" .
270 [
'class' =>
'gallerybox',
'style' =>
'width: ' . $gbWidth ],
271 ( $enableLegacyMediaDOM ? Html::openElement(
'div', [
'style' =>
'width: ' . $gbWidth ] ) :
'' )
275 . ( $enableLegacyMediaDOM ? Html::closeElement(
'div' ) :
'' )
278 $output .=
"\n" . Html::closeElement(
'ul' );