50 $resolveFilesViaParser = $this->mParser instanceof
Parser;
51 if ( $resolveFilesViaParser ) {
52 $parserOutput = $this->mParser->
getOutput();
54 $linkRenderer = $this->mParser->getLinkRenderer();
55 $badFileLookup = $this->mParser->getBadFileLookup();
58 $services = MediaWikiServices::getInstance();
59 $repoGroup = $services->getRepoGroup();
60 $linkRenderer = $services->getLinkRenderer();
61 $badFileLookup = $services->getBadFileLookup();
64 if ( $this->mPerRow > 0 ) {
65 $maxwidth = $this->mPerRow * ( $this->mWidths + $this->
getAllPadding() );
66 $oldStyle = $this->mAttribs[
'style'] ??
'';
67 $this->mAttribs[
'style'] =
"max-width: {$maxwidth}px;" . $oldStyle;
70 $attribs = Sanitizer::mergeAttributes(
71 [
'class' =>
'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
73 $parserOutput->addModules( $this->
getModules() );
74 $parserOutput->addModuleStyles( [
'mediawiki.page.gallery.styles' ] );
75 $output = Html::openElement(
'ul', $attribs );
76 if ( $this->mCaption ) {
77 $output .=
"\n\t" . Html::rawElement(
'li', [
'class' =>
'gallerycaption' ], $this->mCaption );
80 if ( $this->mShowFilename ) {
83 $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
84 $lb = $linkBatchFactory->newLinkBatch();
85 foreach ( $this->mImages as [ $title, ] ) {
86 $lb->addObj( $title );
92 $enableLegacyMediaDOM =
93 $this->
getConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
94 $hookRunner =
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
96 # Output each image...
97 foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading, $imageOptions ] ) {
102 if ( $nt->inNamespace(
NS_FILE ) && !$nt->isExternal() ) {
104 if ( $resolveFilesViaParser ) {
105 # Give extensions a chance to select the file revision for us
107 $hookRunner->onBeforeParserFetchFileAndTitle(
109 $this->mParser, $nt, $options, $descQuery );
110 # Fetch and register the file (file title may be different via hooks)
111 [ $img, $nt ] = $this->mParser->fetchFileAndTitle( $nt, $options );
113 $img = $repoGroup->findFile( $nt );
120 $thumb = $img ? $img->transform( $transformOptions ) :
false;
122 $rdfaType =
'mw:File';
124 $isBadFile = $img && $thumb && $this->mHideBadImages &&
125 $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() );
127 if ( !$img || !$thumb || ( !$enableLegacyMediaDOM && $thumb->isError() ) || $isBadFile ) {
128 $rdfaType =
'mw:Error ' . $rdfaType;
130 if ( $enableLegacyMediaDOM ) {
132 $thumbhtml = $linkRenderer->makeKnownLink( $nt, $nt->getText() );
134 $thumbhtml = htmlspecialchars( $img ? $img->getLastError() : $nt->getText() );
137 $currentExists = $img && $img->exists();
138 if ( $currentExists && !$thumb ) {
139 $label =
wfMessage(
'thumbnail_error',
'' )->text();
140 } elseif ( $thumb && $thumb->isError() ) {
143 'Unknown MediaTransformOutput: ' . get_class( $thumb )
145 $label = $thumb->toText();
149 $thumbhtml = Linker::makeBrokenImageLinkObj(
150 $nt, $label,
'',
'',
'',
false, $transformOptions, $currentExists
152 $thumbhtml = Html::rawElement(
'span', [
'typeof' => $rdfaType ], $thumbhtml );
155 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
159 'style' =>
'height: ' . ( $this->
getThumbPadding() + $this->mHeights ) .
'px;'
164 if ( !$img && $resolveFilesViaParser ) {
165 $this->mParser->addTrackingCategory(
'broken-file-category' );
169 $vpad = $this->
getVPad( $this->mHeights, $thumb->getHeight() );
172 if ( $imageOptions ===
null ) {
175 'desc-query' => $descQuery,
177 'custom-url-link' => $link
185 if ( $alt !==
null ) {
186 $params[
'alt'] = $alt;
188 $params[
'title'] = $imageOptions[
'title'];
189 if ( !$enableLegacyMediaDOM ) {
190 $params[
'img-class'] =
'mw-file-element';
192 $imageParameters = Linker::getImageLinkMTOParams(
193 $imageOptions, $descQuery, $this->mParser
198 $imageParameters[
'loading'] =
'lazy';
203 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
205 $thumbhtml = $thumb->toHtml( $imageParameters );
207 if ( !$enableLegacyMediaDOM ) {
208 $thumbhtml = Html::rawElement(
209 'span', [
'typeof' => $rdfaType ], $thumbhtml
212 $thumbhtml = Html::rawElement(
'div', [
213 # Auto-margin centering
for block-level elements. Needed
214 # now that we have video handlers since they may emit block-
215 # level elements as opposed to simple <img> tags. ref
217 'style' =>
"margin:{$vpad}px auto;",
221 # Set both fixed width and min-height.
224 $thumbhtml =
"\n\t\t\t" . Html::rawElement(
'div', [
226 'style' =>
"width: {$width}px;" .
227 ( !$enableLegacyMediaDOM && $this->mMode ===
'traditional' ?
228 " height: {$height}px;" :
'' ),
232 if ( $resolveFilesViaParser ) {
234 $handler = $img->getHandler();
236 $handler->parserTransformHook( $this->mParser, $img );
238 $this->mParser->modifyImageHtml(
239 $img, [
'handler' => $imageParameters ], $thumbhtml );
245 if ( $this->mShowDimensions ) {
246 $meta[] = htmlspecialchars( $img->getDimensionsString() );
248 if ( $this->mShowBytes ) {
249 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
251 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
252 $meta[] = $this->
msg(
'filemissing' )->escaped();
254 $meta = $lang->semicolonList( $meta );
256 $meta .= Html::rawElement(
'br', [] ) .
"\n";
259 $textlink = $this->mShowFilename ?
263 $galleryText = $this->
wrapGalleryText( $textlink . $text . $meta, $thumb );
266 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
267 # Can be safely removed if FF2 falls completely out of existence
268 $output .=
"\n\t\t" .
271 [
'class' =>
'gallerybox',
'style' =>
'width: ' . $gbWidth ],
272 ( $enableLegacyMediaDOM ? Html::openElement(
'div', [
'style' =>
'width: ' . $gbWidth ] ) :
'' )
276 . ( $enableLegacyMediaDOM ? Html::closeElement(
'div' ) :
'' )
279 $output .=
"\n" . Html::closeElement(
'ul' );