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 Html::addClass( $this->mAttribs[
'class'],
'gallery' );
63 Html::addClass( $this->mAttribs[
'class'],
'mw-gallery-' . $this->mMode );
65 if ( $this->mPerRow > 0 ) {
66 $maxwidth = $this->mPerRow * ( $this->mWidths + $this->
getAllPadding() );
67 $oldStyle = $this->mAttribs[
'style'] ??
'';
68 $this->mAttribs[
'style'] =
"max-width: {$maxwidth}px;" . $oldStyle;
71 $parserOutput->addModules( $this->
getModules() );
72 $parserOutput->addModuleStyles( [
'mediawiki.page.gallery.styles' ] );
73 $output = Html::openElement(
'ul', $this->mAttribs );
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()->setCaller( __METHOD__ );
83 foreach ( $this->mImages as [ $title, ] ) {
84 $lb->addObj( $title );
90 $hookRunner =
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
92 # Output each image...
93 foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading, $imageOptions ] ) {
98 if ( $nt->inNamespace(
NS_FILE ) && !$nt->isExternal() ) {
100 if ( $resolveFilesViaParser ) {
101 # Give extensions a chance to select the file revision for us
103 $hookRunner->onBeforeParserFetchFileAndTitle(
105 $this->mParser, $nt, $options, $descQuery );
106 # Fetch and register the file (file title may be different via hooks)
107 [ $img, $nt ] = $this->mParser->fetchFileAndTitle( $nt, $options );
109 $img = $repoGroup->findFile( $nt );
116 $thumb = $img ? $img->transform( $transformOptions ) :
false;
118 $rdfaType =
'mw:File';
120 $isBadFile = $img && $thumb && $this->mHideBadImages &&
121 $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() );
123 if ( !$img || !$thumb || $thumb->isError() || $isBadFile ) {
124 $rdfaType =
'mw:Error ' . $rdfaType;
126 $currentExists = $img && $img->exists();
127 if ( $currentExists && !$thumb ) {
128 $label =
wfMessage(
'thumbnail_error',
'' )->text();
129 } elseif ( $thumb && $thumb->isError() ) {
132 'Unknown MediaTransformOutput: ' . get_class( $thumb )
134 $label = $thumb->toText();
138 $thumbhtml = Linker::makeBrokenImageLinkObj(
139 $nt, $label,
'',
'',
'',
false, $transformOptions, $currentExists
141 $thumbhtml = Html::rawElement(
'span', [
'typeof' => $rdfaType ], $thumbhtml );
143 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
147 'style' =>
'height: ' . ( $this->
getThumbPadding() + $this->mHeights ) .
'px;'
152 if ( !$img && $resolveFilesViaParser ) {
153 $this->mParser->addTrackingCategory(
'broken-file-category' );
157 $vpad = $this->
getVPad( $this->mHeights, $thumb->getHeight() );
160 if ( $imageOptions ===
null ) {
163 'desc-query' => $descQuery,
165 'custom-url-link' => $link
173 if ( $alt !==
null ) {
174 $params[
'alt'] = $alt;
176 $params[
'title'] = $imageOptions[
'title'];
177 $params[
'img-class'] =
'mw-file-element';
178 $imageParameters = Linker::getImageLinkMTOParams(
179 $imageOptions, $descQuery, $this->mParser
184 $imageParameters[
'loading'] =
'lazy';
189 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
191 $thumbhtml = $thumb->toHtml( $imageParameters );
192 $thumbhtml = Html::rawElement(
193 'span', [
'typeof' => $rdfaType ], $thumbhtml
196 # Set both fixed width and min-height.
199 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
'div', [
201 'style' =>
"width: {$width}px;" .
202 ( $this->mMode ===
'traditional' ?
" height: {$height}px;" :
'' ),
206 if ( $resolveFilesViaParser ) {
208 $handler = $img->getHandler();
210 $handler->parserTransformHook( $this->mParser, $img );
212 $this->mParser->modifyImageHtml(
213 $img, [
'handler' => $imageParameters ], $thumbhtml );
219 if ( $this->mShowDimensions ) {
220 $meta[] = htmlspecialchars( $img->getDimensionsString() );
222 if ( $this->mShowBytes ) {
223 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
225 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
226 $meta[] = $this->
msg(
'filemissing' )->escaped();
228 $meta = $lang->semicolonList( $meta );
230 $meta .= Html::rawElement(
'br', [] ) .
"\n";
233 $textlink = $this->mShowFilename ?
237 $galleryText = $this->
wrapGalleryText( $textlink . $text . $meta, $thumb );
240 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
241 # Can be safely removed if FF2 falls completely out of existence
242 $output .=
"\n\t\t" .
245 [
'class' =>
'gallerybox',
'style' =>
'width: ' . $gbWidth ],
251 $output .=
"\n" . Html::closeElement(
'ul' );