5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 class TraditionalImageGallery
extends ImageGalleryBase
{
25 * Return a HTML representation of the image gallery
27 * For each image in the gallery, display
30 * - the additional text provided when adding the image
31 * - the size of the image
36 if ( $this->mPerRow
> 0 ) {
37 $maxwidth = $this->mPerRow
* ( $this->mWidths +
$this->getAllPadding() );
38 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : '';
39 # _width is ignored by any sane browser. IE6 doesn't know max-width
40 # so it uses _width instead
41 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" .
45 $attribs = Sanitizer
::mergeAttributes(
46 [ 'class' => 'gallery mw-gallery-' . $this->mMode
], $this->mAttribs
);
48 $modules = $this->getModules();
50 if ( $this->mParser
) {
51 $this->mParser
->getOutput()->addModules( $modules );
52 $this->mParser
->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
54 $this->getOutput()->addModules( $modules );
55 $this->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
57 $output = Xml
::openElement( 'ul', $attribs );
58 if ( $this->mCaption
) {
59 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
62 if ( $this->mShowFilename
) {
63 // Preload LinkCache info for when generating links
64 // of the filename below
65 $lb = new LinkBatch();
66 foreach ( $this->mImages
as $img ) {
67 $lb->addObj( $img[0] );
72 $lang = $this->getRenderLang();
73 # Output each image...
74 foreach ( $this->mImages
as $pair ) {
77 $text = $pair[1]; # "text" means "caption" here
82 if ( $nt->getNamespace() === NS_FILE
) {
84 if ( $this->mParser
instanceof Parser
) {
85 # Give extensions a chance to select the file revision for us
87 Hooks
::run( 'BeforeParserFetchFileAndTitle',
88 [ $this->mParser
, $nt, &$options, &$descQuery ] );
89 # Fetch and register the file (file title may be different via hooks)
90 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $options );
92 $img = wfFindFile( $nt );
98 $params = $this->getThumbParams( $img );
99 // $pair[4] is per image handler options
100 $transformOptions = $params +
$pair[4];
105 # We're dealing with a non-image, spit out the name and be done with it.
106 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
107 . ( $this->getThumbPadding() +
$this->mHeights
) . 'px;">'
108 . htmlspecialchars( $nt->getText() ) . '</div>';
110 if ( $this->mParser
instanceof Parser
) {
111 $this->mParser
->addTrackingCategory( 'broken-file-category' );
113 } elseif ( $this->mHideBadImages
114 && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
116 # The image is blacklisted, just show it as a text link.
117 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
118 ( $this->getThumbPadding() +
$this->mHeights
) . 'px;">' .
121 htmlspecialchars( $nt->getText() )
125 $thumb = $img->transform( $transformOptions );
127 # Error generating thumbnail.
128 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
129 . ( $this->getThumbPadding() +
$this->mHeights
) . 'px;">'
130 . htmlspecialchars( $img->getLastError() ) . '</div>';
132 /** @var MediaTransformOutput $thumb */
133 $vpad = $this->getVPad( $this->mHeights
, $thumb->getHeight() );
137 'desc-query' => $descQuery,
139 'custom-url-link' => $link
142 // In the absence of both alt text and caption, fall back on
143 // providing screen readers with the filename as alt text
144 if ( $alt == '' && $text == '' ) {
145 $imageParameters['alt'] = $nt->getText();
148 $this->adjustImageParameters( $thumb, $imageParameters );
150 Linker
::processResponsiveImages( $img, $thumb, $transformOptions );
152 # Set both fixed width and min-height.
153 $thumbhtml = "\n\t\t\t"
154 . '<div class="thumb" style="width: '
155 . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
156 # Auto-margin centering for block-level elements. Needed
157 # now that we have video handlers since they may emit block-
158 # level elements as opposed to simple <img> tags. ref
159 # http://css-discuss.incutio.com/?page=CenteringBlockElement
160 . '<div style="margin:' . $vpad . 'px auto;">'
161 . $thumb->toHtml( $imageParameters ) . '</div></div>';
163 // Call parser transform hook
164 /** @var MediaHandler $handler */
165 $handler = $img->getHandler();
166 if ( $this->mParser
&& $handler ) {
167 $handler->parserTransformHook( $this->mParser
, $img );
172 // @todo Code is incomplete.
173 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) .
175 // $ul = Linker::link( $linkTarget, $ut );
177 if ( $this->mShowBytes
) {
179 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
181 $fileSize = $this->msg( 'filemissing' )->escaped();
183 $fileSize = "$fileSize<br />\n";
188 $textlink = $this->mShowFilename ?
189 // Preloaded into LinkCache above
193 $this->mCaptionLength
!== true ?
194 $lang->truncate( $nt->getText(), $this->mCaptionLength
) :
198 'class' => 'galleryfilename' .
199 ( $this->mCaptionLength
=== true ?
' galleryfilename-truncate' : '' )
204 $galleryText = $textlink . $text . $fileSize;
205 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
207 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
208 # Can be safely removed if FF2 falls completely out of existence
209 $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
210 . $this->getGBWidth( $thumb ) . 'px">'
211 . '<div style="width: ' . $this->getGBWidth( $thumb ) . 'px">'
214 . "\n\t\t</div></li>";
216 $output .= "\n</ul>";
222 * Add the wrapper html around the thumb's caption
224 * @param string $galleryText The caption
225 * @param MediaTransformOutput|bool $thumb The thumb this caption is for
226 * or false for bad image.
229 protected function wrapGalleryText( $galleryText, $thumb ) {
230 # ATTENTION: The newline after <div class="gallerytext"> is needed to
231 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
232 # its absence, see: https://phabricator.wikimedia.org/T3765
235 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
241 * How much padding the thumb has between the image and the inner div
242 * that contains the border. This is for both vertical and horizontal
243 * padding. (However, it is cut in half in the vertical direction).
246 protected function getThumbPadding() {
251 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
255 protected function getGBPadding() {
260 * Get how much extra space the borders around the image takes up.
262 * For this mode, it is 2px borders on each side + 2px implied padding on
263 * each side from the stylesheet, giving us 2*2+2*2 = 8.
266 protected function getGBBorders() {
273 * @return int Number of pixels of whitespace surrounding the thumbnail.
275 protected function getAllPadding() {
276 return $this->getThumbPadding() +
$this->getGBPadding() +
$this->getGBBorders();
280 * Get vertical padding for a thumbnail
282 * Generally this is the total height minus how high the thumb is.
284 * @param int $boxHeight How high we want the box to be.
285 * @param int $thumbHeight How high the thumbnail is.
286 * @return int Vertical padding to add on each side.
288 protected function getVPad( $boxHeight, $thumbHeight ) {
289 return ( $this->getThumbPadding() +
$boxHeight - $thumbHeight ) / 2;
293 * Get the transform parameters for a thumbnail.
295 * @param File $img The file in question. May be false for invalid image
298 protected function getThumbParams( $img ) {
300 'width' => $this->mWidths
,
301 'height' => $this->mHeights
306 * Get the width of the inner div that contains the thumbnail in
307 * question. This is the div with the class of "thumb".
309 * @param int $thumbWidth The width of the thumbnail.
310 * @return int Width of inner thumb div.
312 protected function getThumbDivWidth( $thumbWidth ) {
313 return $this->mWidths +
$this->getThumbPadding();
317 * Width of gallerybox <li>.
319 * Generally is the width of the image, plus padding on image
320 * plus padding on gallerybox.
322 * @note Important: parameter will be false if no thumb used.
323 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
324 * @return int Width of gallerybox element
326 protected function getGBWidth( $thumb ) {
327 return $this->mWidths +
$this->getThumbPadding() +
$this->getGBPadding();
331 * Get a list of modules to include in the page.
333 * Primarily intended for subclasses.
335 * @return array Modules to include
337 protected function getModules() {
342 * Adjust the image parameters for a thumbnail.
344 * Used by a subclass to insert extra high resolution images.
345 * @param MediaTransformOutput $thumb The thumbnail
346 * @param array $imageParameters Array of options
348 protected function adjustImageParameters( $thumb, &$imageParameters ) {