Merge "EnhancedChangesList: Add missing </td></tr>"
[lhc/web/wiklou.git] / includes / gallery / TraditionalImageGallery.php
1 <?php
2 /**
3 * Image gallery.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 class TraditionalImageGallery extends ImageGalleryBase {
24
25 /**
26 * Return a HTML representation of the image gallery
27 *
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
33 *
34 * @return string
35 */
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;
42 }
43
44 $attribs = Sanitizer::mergeAttributes(
45 array( 'class' => 'gallery mw-gallery-' . $this->mMode ), $this->mAttribs );
46
47 $modules = $this->getModules();
48
49 if ( $this->mParser ) {
50 $this->mParser->getOutput()->addModules( $modules );
51 } else {
52 $this->getOutput()->addModules( $modules );
53 }
54 $output = Xml::openElement( 'ul', $attribs );
55 if ( $this->mCaption ) {
56 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
57 }
58
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];
66
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 );
79 }
80 } else {
81 $img = false;
82 }
83
84 $params = $this->getThumbParams( $img );
85 // $pair[4] is per image handler options
86 $transformOptions = $params + $pair[4];
87
88 $thumb = false;
89
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>';
94
95 if ( $this->mParser instanceof Parser ) {
96 $this->mParser->addTrackingCategory( 'broken-file-category' );
97 }
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' )
107 ) .
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() );
115
116 $imageParameters = array(
117 'desc-link' => true,
118 'desc-query' => $descQuery,
119 'alt' => $alt,
120 'custom-url-link' => $link
121 );
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();
125 }
126
127 $this->adjustImageParameters( $thumb, $imageParameters );
128
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>';
137
138 // Call parser transform hook
139 if ( $this->mParser && $img->getHandler() ) {
140 $img->getHandler()->parserTransformHook( $this->mParser, $img );
141 }
142 }
143
144 //TODO
145 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
146 // $ul = Linker::link( $linkTarget, $ut );
147
148 if ( $this->mShowBytes ) {
149 if ( $img ) {
150 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
151 } else {
152 $fileSize = $this->msg( 'filemissing' )->escaped();
153 }
154 $fileSize = "$fileSize<br />\n";
155 } else {
156 $fileSize = '';
157 }
158
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" :
167 '';
168
169 $galleryText = $textlink . $text . $fileSize;
170 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
171
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>";
180 }
181 $output .= "\n</ul>";
182
183 return $output;
184 }
185
186 /**
187 * Add the wrapper html around the thumb's caption
188 *
189 * @param String $galleryText The caption
190 * @param MediaTransformOutput|boolean $thumb The thumb this caption is for or false for bad image.
191 */
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
196
197 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
198 . $galleryText
199 . "\n\t\t\t</div>";
200 }
201
202 /**
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
207 */
208 protected function getThumbPadding() {
209 return 30;
210 }
211
212 /**
213 *
214 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
215 *
216 * @return int
217 */
218 protected function getGBPadding() {
219 return 5;
220 }
221
222 /**
223 * Get how much extra space the borders around the image takes up.
224 *
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
228 */
229 protected function getGBBorders() {
230 return 8;
231 }
232
233 /**
234 * Get total padding.
235 *
236 * @return int How many pixels of whitespace surround the thumbnail.
237 */
238 protected function getAllPadding() {
239 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
240 }
241
242 /**
243 * Get vertical padding for a thumbnail
244 *
245 * Generally this is the total height minus how high the thumb is.
246 *
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.
250 */
251 protected function getVPad( $boxHeight, $thumbHeight ) {
252 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
253 }
254
255 /**
256 * Get the transform parameters for a thumbnail.
257 *
258 * @param File $img The file in question. May be false for invalid image
259 */
260 protected function getThumbParams( $img ) {
261 return array(
262 'width' => $this->mWidths,
263 'height' => $this->mHeights
264 );
265 }
266
267 /**
268 * Get the width of the inner div that contains the thumbnail in
269 * question. This is the div with the class of "thumb".
270 *
271 * @param int $thumbWidth The width of the thumbnail.
272 * @return int Width of inner thumb div.
273 */
274 protected function getThumbDivWidth( $thumbWidth ) {
275 return $this->mWidths + $this->getThumbPadding();
276 }
277
278 /**
279 * Width of gallerybox <li>.
280 *
281 * Generally is the width of the image, plus padding on image
282 * plus padding on gallerybox.
283 *
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
287 */
288 protected function getGBWidth( $thumb ) {
289 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
290 }
291
292 /**
293 * Get a list of modules to include in the page.
294 *
295 * Primarily intended for subclasses.
296 *
297 * @return Array modules to include
298 */
299 protected function getModules() {
300 return array();
301 }
302
303 /**
304 * Adjust the image parameters for a thumbnail.
305 *
306 * Used by a subclass to insert extra high resolution images.
307 * @param MediaTransformOutput $thumb The thumbnail
308 * @param Array $imageParameters Array of options
309 */
310 protected function adjustImageParameters( $thumb, &$imageParameters ) { }
311 }
312
313 /**
314 * Backwards compatibility. This always uses traditional mode
315 * if called the old way, for extensions that may expect traditional
316 * mode.
317 *
318 * @deprecated 1.22 Use ImageGalleryBase::factory instead.
319 */
320 class ImageGallery extends TraditionalImageGallery {
321 function __construct( $mode = 'traditional' ) {
322 wfDeprecated( __METHOD__, '1.22' );
323 parent::__construct( $mode );
324 }
325 }