Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / gallery / TraditionalImageGallery.php
blobd2f741735166a72052d81bfa346e32f426f7c107
1 <?php
2 /**
3 * Image gallery.
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
20 * @file
23 class TraditionalImageGallery extends ImageGalleryBase {
24 /**
25 * Return a HTML representation of the image gallery
27 * For each image in the gallery, display
28 * - a thumbnail
29 * - the image name
30 * - the additional text provided when adding the image
31 * - the size of the image
33 * @return string
35 function toHTML() {
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;" .
42 $oldStyle;
45 $attribs = Sanitizer::mergeAttributes(
46 array( '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' );
53 } else {
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 $lang = $this->getRenderLang();
63 # Output each image...
64 foreach ( $this->mImages as $pair ) {
65 /** @var Title $nt */
66 $nt = $pair[0];
67 $text = $pair[1]; # "text" means "caption" here
68 $alt = $pair[2];
69 $link = $pair[3];
71 $descQuery = false;
72 if ( $nt->getNamespace() === NS_FILE ) {
73 # Get the file...
74 if ( $this->mParser instanceof Parser ) {
75 # Give extensions a chance to select the file revision for us
76 $options = array();
77 Hooks::run( 'BeforeParserFetchFileAndTitle',
78 array( $this->mParser, $nt, &$options, &$descQuery ) );
79 # Fetch and register the file (file title may be different via hooks)
80 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
81 } else {
82 $img = wfFindFile( $nt );
84 } else {
85 $img = false;
88 $params = $this->getThumbParams( $img );
89 // $pair[4] is per image handler options
90 $transformOptions = $params + $pair[4];
92 $thumb = false;
94 if ( !$img ) {
95 # We're dealing with a non-image, spit out the name and be done with it.
96 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
97 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
98 . htmlspecialchars( $nt->getText() ) . '</div>';
100 if ( $this->mParser instanceof Parser ) {
101 $this->mParser->addTrackingCategory( 'broken-file-category' );
103 } elseif ( $this->mHideBadImages
104 && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
106 # The image is blacklisted, just show it as a text link.
107 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
108 ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
109 Linker::linkKnown(
110 $nt,
111 htmlspecialchars( $nt->getText() )
113 '</div>';
114 } else {
115 $thumb = $img->transform( $transformOptions );
116 if ( !$thumb ) {
117 # Error generating thumbnail.
118 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
119 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
120 . htmlspecialchars( $img->getLastError() ) . '</div>';
121 } else {
122 /** @var MediaTransformOutput $thumb */
123 $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
125 $imageParameters = array(
126 'desc-link' => true,
127 'desc-query' => $descQuery,
128 'alt' => $alt,
129 'custom-url-link' => $link
132 // In the absence of both alt text and caption, fall back on
133 // providing screen readers with the filename as alt text
134 if ( $alt == '' && $text == '' ) {
135 $imageParameters['alt'] = $nt->getText();
138 $this->adjustImageParameters( $thumb, $imageParameters );
140 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
142 # Set both fixed width and min-height.
143 $thumbhtml = "\n\t\t\t"
144 . '<div class="thumb" style="width: '
145 . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
146 # Auto-margin centering for block-level elements. Needed
147 # now that we have video handlers since they may emit block-
148 # level elements as opposed to simple <img> tags. ref
149 # http://css-discuss.incutio.com/?page=CenteringBlockElement
150 . '<div style="margin:' . $vpad . 'px auto;">'
151 . $thumb->toHtml( $imageParameters ) . '</div></div>';
153 // Call parser transform hook
154 /** @var MediaHandler $handler */
155 $handler = $img->getHandler();
156 if ( $this->mParser && $handler ) {
157 $handler->parserTransformHook( $this->mParser, $img );
162 // @todo Code is incomplete.
163 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) .
164 // ":{$ut}" );
165 // $ul = Linker::link( $linkTarget, $ut );
167 if ( $this->mShowBytes ) {
168 if ( $img ) {
169 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
170 } else {
171 $fileSize = $this->msg( 'filemissing' )->escaped();
173 $fileSize = "$fileSize<br />\n";
174 } else {
175 $fileSize = '';
178 $textlink = $this->mShowFilename ?
179 Linker::linkKnown(
180 $nt,
181 htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) )
182 ) . "<br />\n" :
185 $galleryText = $textlink . $text . $fileSize;
186 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
188 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
189 # Can be safely removed if FF2 falls completely out of existence
190 $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
191 . $this->getGBWidth( $thumb ) . 'px">'
192 . '<div style="width: ' . $this->getGBWidth( $thumb ) . 'px">'
193 . $thumbhtml
194 . $galleryText
195 . "\n\t\t</div></li>";
197 $output .= "\n</ul>";
199 return $output;
203 * Add the wrapper html around the thumb's caption
205 * @param string $galleryText The caption
206 * @param MediaTransformOutput|bool $thumb The thumb this caption is for
207 * or false for bad image.
208 * @return string
210 protected function wrapGalleryText( $galleryText, $thumb ) {
211 # ATTENTION: The newline after <div class="gallerytext"> is needed to
212 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
213 # its absence, see: https://phabricator.wikimedia.org/T3765
214 # -Ævar
216 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
217 . $galleryText
218 . "\n\t\t\t</div>";
222 * How much padding such the thumb have between image and inner div that
223 * that contains the border. This is both for verical and horizontal
224 * padding. (However, it is cut in half in the vertical direction).
225 * @return int
227 protected function getThumbPadding() {
228 return 30;
232 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
234 * @return int
236 protected function getGBPadding() {
237 return 5;
241 * Get how much extra space the borders around the image takes up.
243 * For this mode, it is 2px borders on each side + 2px implied padding on
244 * each side from the stylesheet, giving us 2*2+2*2 = 8.
245 * @return int
247 protected function getGBBorders() {
248 return 8;
252 * Get total padding.
254 * @return int Number of pixels of whitespace surrounding the thumbnail.
256 protected function getAllPadding() {
257 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
261 * Get vertical padding for a thumbnail
263 * Generally this is the total height minus how high the thumb is.
265 * @param int $boxHeight How high we want the box to be.
266 * @param int $thumbHeight How high the thumbnail is.
267 * @return int Vertical padding to add on each side.
269 protected function getVPad( $boxHeight, $thumbHeight ) {
270 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
274 * Get the transform parameters for a thumbnail.
276 * @param File $img The file in question. May be false for invalid image
277 * @return array
279 protected function getThumbParams( $img ) {
280 return array(
281 'width' => $this->mWidths,
282 'height' => $this->mHeights
287 * Get the width of the inner div that contains the thumbnail in
288 * question. This is the div with the class of "thumb".
290 * @param int $thumbWidth The width of the thumbnail.
291 * @return int Width of inner thumb div.
293 protected function getThumbDivWidth( $thumbWidth ) {
294 return $this->mWidths + $this->getThumbPadding();
298 * Width of gallerybox <li>.
300 * Generally is the width of the image, plus padding on image
301 * plus padding on gallerybox.
303 * @note Important: parameter will be false if no thumb used.
304 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
305 * @return int Width of gallerybox element
307 protected function getGBWidth( $thumb ) {
308 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
312 * Get a list of modules to include in the page.
314 * Primarily intended for subclasses.
316 * @return array Modules to include
318 protected function getModules() {
319 return array();
323 * Adjust the image parameters for a thumbnail.
325 * Used by a subclass to insert extra high resolution images.
326 * @param MediaTransformOutput $thumb The thumbnail
327 * @param array $imageParameters Array of options
329 protected function adjustImageParameters( $thumb, &$imageParameters ) {
334 * Backwards compatibility. This always uses traditional mode
335 * if called the old way, for extensions that may expect traditional
336 * mode.
338 * @deprecated since 1.22 Use ImageGalleryBase::factory instead.
340 class ImageGallery extends TraditionalImageGallery {
341 function __construct( $mode = 'traditional' ) {
342 wfDeprecated( __METHOD__, '1.22' );
343 parent::__construct( $mode );