3 use MediaWiki\MediaWikiServices
;
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 class TraditionalImageGallery
extends ImageGalleryBase
{
28 * Return a HTML representation of the image gallery
30 * For each image in the gallery, display
33 * - the additional text provided when adding the image
34 * - the size of the image
39 if ( $this->mPerRow
> 0 ) {
40 $maxwidth = $this->mPerRow
* ( $this->mWidths +
$this->getAllPadding() );
41 $oldStyle = $this->mAttribs
['style'] ??
'';
42 # _width is ignored by any sane browser. IE6 doesn't know max-width
43 # so it uses _width instead
44 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" .
48 $attribs = Sanitizer
::mergeAttributes(
49 [ 'class' => 'gallery mw-gallery-' . $this->mMode
], $this->mAttribs
);
51 $modules = $this->getModules();
53 if ( $this->mParser
) {
54 $this->mParser
->getOutput()->addModules( $modules );
55 $this->mParser
->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
57 $this->getOutput()->addModules( $modules );
58 $this->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
60 $output = Xml
::openElement( 'ul', $attribs );
61 if ( $this->mCaption
) {
62 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
65 if ( $this->mShowFilename
) {
66 // Preload LinkCache info for when generating links
67 // of the filename below
68 $lb = new LinkBatch();
69 foreach ( $this->mImages
as $img ) {
70 $lb->addObj( $img[0] );
75 $lang = $this->getRenderLang();
76 # Output each image...
77 foreach ( $this->mImages
as $pair ) {
78 // "text" means "caption" here
80 list( $nt, $text, $alt, $link ) = $pair;
83 if ( $nt->getNamespace() === NS_FILE
) {
85 if ( $this->mParser
instanceof Parser
) {
86 # Give extensions a chance to select the file revision for us
88 Hooks
::run( 'BeforeParserFetchFileAndTitle',
89 [ $this->mParser
, $nt, &$options, &$descQuery ] );
90 # Fetch and register the file (file title may be different via hooks)
91 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $options );
93 $img = MediaWikiServices
::getInstance()->getRepoGroup()->findFile( $nt );
99 $params = $this->getThumbParams( $img );
100 // $pair[4] is per image handler options
101 $transformOptions = $params +
$pair[4];
106 # We're dealing with a non-image, spit out the name and be done with it.
107 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
108 . ( $this->getThumbPadding() +
$this->mHeights
) . 'px;">'
109 . htmlspecialchars( $nt->getText() ) . '</div>';
111 if ( $this->mParser
instanceof Parser
) {
112 $this->mParser
->addTrackingCategory( 'broken-file-category' );
114 } elseif ( $this->mHideBadImages
&& MediaWikiServices
::getInstance()->getBadFileLookup()
115 ->isBadFile( $nt->getDBkey(), $this->getContextTitle() )
117 # The image is blacklisted, just show it as a text link.
118 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
119 ( $this->getThumbPadding() +
$this->mHeights
) . 'px;">' .
122 htmlspecialchars( $nt->getText() )
126 $thumb = $img->transform( $transformOptions );
128 # Error generating thumbnail.
129 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
130 . ( $this->getThumbPadding() +
$this->mHeights
) . 'px;">'
131 . htmlspecialchars( $img->getLastError() ) . '</div>';
133 /** @var MediaTransformOutput $thumb */
134 $vpad = $this->getVPad( $this->mHeights
, $thumb->getHeight() );
138 'desc-query' => $descQuery,
140 'custom-url-link' => $link
143 // In the absence of both alt text and caption, fall back on
144 // providing screen readers with the filename as alt text
145 if ( $alt == '' && $text == '' ) {
146 $imageParameters['alt'] = $nt->getText();
149 $this->adjustImageParameters( $thumb, $imageParameters );
151 Linker
::processResponsiveImages( $img, $thumb, $transformOptions );
153 # Set both fixed width and min-height.
154 $thumbhtml = "\n\t\t\t"
155 . '<div class="thumb" style="width: '
156 . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
157 # Auto-margin centering for block-level elements. Needed
158 # now that we have video handlers since they may emit block-
159 # level elements as opposed to simple <img> tags. ref
160 # http://css-discuss.incutio.com/?page=CenteringBlockElement
161 . '<div style="margin:' . $vpad . 'px auto;">'
162 . $thumb->toHtml( $imageParameters ) . '</div></div>';
164 // Call parser transform hook
165 /** @var MediaHandler $handler */
166 $handler = $img->getHandler();
167 if ( $this->mParser
&& $handler ) {
168 $handler->parserTransformHook( $this->mParser
, $img );
173 // @todo Code is incomplete.
174 // $linkTarget = Title::newFromText( MediaWikiServices::getInstance()->
175 // getContentLanguage()->getNsText( MediaWikiServices::getInstance()->
176 // getNamespaceInfo()->getUser() ) . ":{$ut}" );
177 // $ul = Linker::link( $linkTarget, $ut );
181 if ( $this->mShowDimensions
) {
182 $meta[] = $img->getDimensionsString();
184 if ( $this->mShowBytes
) {
185 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
187 } elseif ( $this->mShowDimensions ||
$this->mShowBytes
) {
188 $meta[] = $this->msg( 'filemissing' )->escaped();
190 $meta = $lang->semicolonList( $meta );
195 $textlink = $this->mShowFilename ?
196 $this->getCaptionHtml( $nt, $lang ) :
199 $galleryText = $textlink . $text . $meta;
200 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
202 $gbWidth = $this->getGBWidth( $thumb ) . 'px';
203 if ( $this->getGBWidthOverwrite( $thumb ) ) {
204 $gbWidth = $this->getGBWidthOverwrite( $thumb );
206 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
207 # Can be safely removed if FF2 falls completely out of existence
208 $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
210 . '<div style="width: ' . $gbWidth . '">'
213 . "\n\t\t</div></li>";
215 $output .= "\n</ul>";
222 * @param Language $lang
223 * @return string HTML
225 protected function getCaptionHtml( Title
$nt, Language
$lang ) {
226 // Preloaded into LinkCache in toHTML
227 return Linker
::linkKnown(
230 is_int( $this->getCaptionLength() ) ?
231 $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
235 'class' => 'galleryfilename' .
236 ( $this->getCaptionLength() === true ?
' galleryfilename-truncate' : '' )
242 * Add the wrapper html around the thumb's caption
244 * @param string $galleryText The caption
245 * @param MediaTransformOutput|bool $thumb The thumb this caption is for
246 * or false for bad image.
249 protected function wrapGalleryText( $galleryText, $thumb ) {
250 # ATTENTION: The newline after <div class="gallerytext"> is needed to
251 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
252 # its absence, see: https://phabricator.wikimedia.org/T3765
255 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
261 * How much padding the thumb has between the image and the inner div
262 * that contains the border. This is for both vertical and horizontal
263 * padding. (However, it is cut in half in the vertical direction).
266 protected function getThumbPadding() {
271 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
275 protected function getGBPadding() {
280 * Get how much extra space the borders around the image takes up.
282 * For this mode, it is 2px borders on each side + 2px implied padding on
283 * each side from the stylesheet, giving us 2*2+2*2 = 8.
286 protected function getGBBorders() {
291 * Length (in characters) to truncate filename to in caption when using "showfilename" (if int).
292 * A value of 'true' will truncate the filename to one line using CSS, while
293 * 'false' will disable truncating.
297 protected function getCaptionLength() {
298 return $this->mCaptionLength
;
304 * @return int Number of pixels of whitespace surrounding the thumbnail.
306 protected function getAllPadding() {
307 return $this->getThumbPadding() +
$this->getGBPadding() +
$this->getGBBorders();
311 * Get vertical padding for a thumbnail
313 * Generally this is the total height minus how high the thumb is.
315 * @param int $boxHeight How high we want the box to be.
316 * @param int $thumbHeight How high the thumbnail is.
317 * @return int Vertical padding to add on each side.
319 protected function getVPad( $boxHeight, $thumbHeight ) {
320 return ( $this->getThumbPadding() +
$boxHeight - $thumbHeight ) / 2;
324 * Get the transform parameters for a thumbnail.
326 * @param File $img The file in question. May be false for invalid image
329 protected function getThumbParams( $img ) {
331 'width' => $this->mWidths
,
332 'height' => $this->mHeights
337 * Get the width of the inner div that contains the thumbnail in
338 * question. This is the div with the class of "thumb".
340 * @param int $thumbWidth The width of the thumbnail.
341 * @return int Width of inner thumb div.
343 protected function getThumbDivWidth( $thumbWidth ) {
344 return $this->mWidths +
$this->getThumbPadding();
348 * Computed width of gallerybox <li>.
350 * Generally is the width of the image, plus padding on image
351 * plus padding on gallerybox.
353 * @note Important: parameter will be false if no thumb used.
354 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
355 * @return int Width of gallerybox element
357 protected function getGBWidth( $thumb ) {
358 return $this->mWidths +
$this->getThumbPadding() +
$this->getGBPadding();
362 * Allows overwriting the computed width of the gallerybox <li> with a string,
365 * Generally is the width of the image, plus padding on image
366 * plus padding on gallerybox.
368 * @note Important: parameter will be false if no thumb used.
369 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
370 * @return bool|string Ignored if false.
372 protected function getGBWidthOverwrite( $thumb ) {
377 * Get a list of modules to include in the page.
379 * Primarily intended for subclasses.
381 * @return array Modules to include
383 protected function getModules() {
388 * Adjust the image parameters for a thumbnail.
390 * Used by a subclass to insert extra high resolution images.
391 * @param MediaTransformOutput $thumb The thumbnail
392 * @param array &$imageParameters Array of options
394 protected function adjustImageParameters( $thumb, &$imageParameters ) {