Merge "Update comment in DefaultSettings for bug 17180"
[mediawiki.git] / includes / gallery / TraditionalImageGallery.php
blobc6e6dd36d9bb8b499c15e09356348e4de21a9685
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 {
25 /**
26 * Return a HTML representation of the image gallery
28 * For each image in the gallery, display
29 * - a thumbnail
30 * - the image name
31 * - the additional text provided when adding the image
32 * - the size of the image
34 * @return string
36 function toHTML() {
37 if ( $this->mPerRow > 0 ) {
38 $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
39 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : '';
40 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
41 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
44 $attribs = Sanitizer::mergeAttributes(
45 array( 'class' => 'gallery mw-gallery-' . $this->mMode ), $this->mAttribs );
47 $modules = $this->getModules();
49 if ( $this->mParser ) {
50 $this->mParser->getOutput()->addModules( $modules );
51 } else {
52 $this->getOutput()->addModules( $modules );
54 $output = Xml::openElement( 'ul', $attribs );
55 if ( $this->mCaption ) {
56 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
59 $lang = $this->getRenderLang();
60 # Output each image...
61 foreach ( $this->mImages as $pair ) {
62 $nt = $pair[0];
63 $text = $pair[1]; # "text" means "caption" here
64 $alt = $pair[2];
65 $link = $pair[3];
67 $descQuery = false;
68 if ( $nt->getNamespace() === NS_FILE ) {
69 # Get the file...
70 if ( $this->mParser instanceof Parser ) {
71 # Give extensions a chance to select the file revision for us
72 $options = array();
73 wfRunHooks( 'BeforeParserFetchFileAndTitle',
74 array( $this->mParser, $nt, &$options, &$descQuery ) );
75 # Fetch and register the file (file title may be different via hooks)
76 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
77 } else {
78 $img = wfFindFile( $nt );
80 } else {
81 $img = false;
84 $params = $this->getThumbParams( $img );
85 // $pair[4] is per image handler options
86 $transformOptions = $params + $pair[4];
88 $thumb = false;
90 if ( !$img ) {
91 # We're dealing with a non-image, spit out the name and be done with it.
92 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
93 . htmlspecialchars( $nt->getText() ) . '</div>';
95 if ( $this->mParser instanceof Parser ) {
96 $this->mParser->addTrackingCategory( 'broken-file-category' );
98 } elseif ( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
99 # The image is blacklisted, just show it as a text link.
100 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
101 Linker::link(
102 $nt,
103 htmlspecialchars( $nt->getText() ),
104 array(),
105 array(),
106 array( 'known', 'noclasses' )
108 '</div>';
109 } elseif ( !( $thumb = $img->transform( $transformOptions ) ) ) {
110 # Error generating thumbnail.
111 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
112 . htmlspecialchars( $img->getLastError() ) . '</div>';
113 } else {
114 $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
116 $imageParameters = array(
117 'desc-link' => true,
118 'desc-query' => $descQuery,
119 'alt' => $alt,
120 'custom-url-link' => $link
122 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
123 if ( $alt == '' && $text == '' ) {
124 $imageParameters['alt'] = $nt->getText();
127 $this->adjustImageParameters( $thumb, $imageParameters );
129 # Set both fixed width and min-height.
130 $thumbhtml = "\n\t\t\t" .
131 '<div class="thumb" style="width: ' . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
132 # Auto-margin centering for block-level elements. Needed now that we have video
133 # handlers since they may emit block-level elements as opposed to simple <img> tags.
134 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
135 . '<div style="margin:' . $vpad . 'px auto;">'
136 . $thumb->toHtml( $imageParameters ) . '</div></div>';
138 // Call parser transform hook
139 if ( $this->mParser && $img->getHandler() ) {
140 $img->getHandler()->parserTransformHook( $this->mParser, $img );
144 //TODO
145 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
146 // $ul = Linker::link( $linkTarget, $ut );
148 if ( $this->mShowBytes ) {
149 if ( $img ) {
150 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
151 } else {
152 $fileSize = $this->msg( 'filemissing' )->escaped();
154 $fileSize = "$fileSize<br />\n";
155 } else {
156 $fileSize = '';
159 $textlink = $this->mShowFilename ?
160 Linker::link(
161 $nt,
162 htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) ),
163 array(),
164 array(),
165 array( 'known', 'noclasses' )
166 ) . "<br />\n" :
169 $galleryText = $textlink . $text . $fileSize;
170 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
172 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
173 # Can be safely removed if FF2 falls completely out of existence
174 $output .=
175 "\n\t\t" . '<li class="gallerybox" style="width: ' . $this->getGBWidth( $thumb ) . 'px">'
176 . '<div style="width: ' . $this->getGBWidth( $thumb ) . 'px">'
177 . $thumbhtml
178 . $galleryText
179 . "\n\t\t</div></li>";
181 $output .= "\n</ul>";
183 return $output;
187 * Add the wrapper html around the thumb's caption
189 * @param String $galleryText The caption
190 * @param MediaTransformOutput|boolean $thumb The thumb this caption is for or false for bad image.
192 protected function wrapGalleryText( $galleryText, $thumb ) {
193 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
194 # in version 4.8.6 generated crackpot html in its absence, see:
195 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
197 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
198 . $galleryText
199 . "\n\t\t\t</div>";
203 * How much padding such the thumb have between image and inner div that
204 * that contains the border. This is both for verical and horizontal
205 * padding. (However, it is cut in half in the vertical direction).
206 * @return int
208 protected function getThumbPadding() {
209 return 30;
214 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
216 * @return int
218 protected function getGBPadding() {
219 return 5;
223 * Get how much extra space the borders around the image takes up.
225 * For this mode, it is 2px borders on each side + 2px implied padding on
226 * each side from the stylesheet, giving us 2*2+2*2 = 8.
227 * @return int
229 protected function getGBBorders() {
230 return 8;
234 * Get total padding.
236 * @return int How many pixels of whitespace surround the thumbnail.
238 protected function getAllPadding() {
239 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
243 * Get vertical padding for a thumbnail
245 * Generally this is the total height minus how high the thumb is.
247 * @param int $boxHeight How high we want the box to be.
248 * @param int $thumbHeight How high the thumbnail is.
249 * @return int How many vertical padding to add on each side.
251 protected function getVPad( $boxHeight, $thumbHeight ) {
252 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
256 * Get the transform parameters for a thumbnail.
258 * @param File $img The file in question. May be false for invalid image
260 protected function getThumbParams( $img ) {
261 return array(
262 'width' => $this->mWidths,
263 'height' => $this->mHeights
268 * Get the width of the inner div that contains the thumbnail in
269 * question. This is the div with the class of "thumb".
271 * @param int $thumbWidth The width of the thumbnail.
272 * @return int Width of inner thumb div.
274 protected function getThumbDivWidth( $thumbWidth ) {
275 return $this->mWidths + $this->getThumbPadding();
279 * Width of gallerybox <li>.
281 * Generally is the width of the image, plus padding on image
282 * plus padding on gallerybox.
284 * @note Important: parameter will be false if no thumb used.
285 * @param Mixed $thumb MediaTransformObject object or false.
286 * @return int width of gallerybox element
288 protected function getGBWidth( $thumb ) {
289 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
293 * Get a list of modules to include in the page.
295 * Primarily intended for subclasses.
297 * @return Array modules to include
299 protected function getModules() {
300 return array();
304 * Adjust the image parameters for a thumbnail.
306 * Used by a subclass to insert extra high resolution images.
307 * @param MediaTransformOutput $thumb The thumbnail
308 * @param Array $imageParameters Array of options
310 protected function adjustImageParameters( $thumb, &$imageParameters ) { }
314 * Backwards compatibility. This always uses traditional mode
315 * if called the old way, for extensions that may expect traditional
316 * mode.
318 * @deprecated 1.22 Use ImageGalleryBase::factory instead.
320 class ImageGallery extends TraditionalImageGallery {
321 function __construct( $mode = 'traditional' ) {
322 wfDeprecated( __METHOD__, '1.22' );
323 parent::__construct( $mode );