Update documentation for gallery related files
[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 * Return a HTML representation of the image gallery
26 *
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
32 *
33 * @return string
34 */
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;
43 }
44
45 $attribs = Sanitizer::mergeAttributes(
46 array( 'class' => 'gallery mw-gallery-' . $this->mMode ), $this->mAttribs );
47
48 $modules = $this->getModules();
49
50 if ( $this->mParser ) {
51 $this->mParser->getOutput()->addModules( $modules );
52 } else {
53 $this->getOutput()->addModules( $modules );
54 }
55 $output = Xml::openElement( 'ul', $attribs );
56 if ( $this->mCaption ) {
57 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
58 }
59
60 $lang = $this->getRenderLang();
61 # Output each image...
62 foreach ( $this->mImages as $pair ) {
63 $nt = $pair[0];
64 $text = $pair[1]; # "text" means "caption" here
65 $alt = $pair[2];
66 $link = $pair[3];
67
68 $descQuery = false;
69 if ( $nt->getNamespace() === NS_FILE ) {
70 # Get the file...
71 if ( $this->mParser instanceof Parser ) {
72 # Give extensions a chance to select the file revision for us
73 $options = array();
74 wfRunHooks( 'BeforeParserFetchFileAndTitle',
75 array( $this->mParser, $nt, &$options, &$descQuery ) );
76 # Fetch and register the file (file title may be different via hooks)
77 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
78 } else {
79 $img = wfFindFile( $nt );
80 }
81 } else {
82 $img = false;
83 }
84
85 $params = $this->getThumbParams( $img );
86 // $pair[4] is per image handler options
87 $transformOptions = $params + $pair[4];
88
89 $thumb = false;
90
91 if ( !$img ) {
92 # We're dealing with a non-image, spit out the name and be done with it.
93 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
94 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
95 . htmlspecialchars( $nt->getText() ) . '</div>';
96
97 if ( $this->mParser instanceof Parser ) {
98 $this->mParser->addTrackingCategory( 'broken-file-category' );
99 }
100 } elseif ( $this->mHideBadImages
101 && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
102 ) {
103 # The image is blacklisted, just show it as a text link.
104 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
105 ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
106 Linker::linkKnown(
107 $nt,
108 htmlspecialchars( $nt->getText() )
109 ) .
110 '</div>';
111 } elseif ( !( $thumb = $img->transform( $transformOptions ) ) ) {
112 # Error generating thumbnail.
113 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
114 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
115 . htmlspecialchars( $img->getLastError() ) . '</div>';
116 } else {
117 $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
118
119 $imageParameters = array(
120 'desc-link' => true,
121 'desc-query' => $descQuery,
122 'alt' => $alt,
123 'custom-url-link' => $link
124 );
125
126 // In the absence of both alt text and caption, fall back on
127 // providing screen readers with the filename as alt text
128 if ( $alt == '' && $text == '' ) {
129 $imageParameters['alt'] = $nt->getText();
130 }
131
132 $this->adjustImageParameters( $thumb, $imageParameters );
133
134 # Set both fixed width and min-height.
135 $thumbhtml = "\n\t\t\t"
136 . '<div class="thumb" style="width: '
137 . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
138 # Auto-margin centering for block-level elements. Needed
139 # now that we have video handlers since they may emit block-
140 # level elements as opposed to simple <img> tags. ref
141 # http://css-discuss.incutio.com/?page=CenteringBlockElement
142 . '<div style="margin:' . $vpad . 'px auto;">'
143 . $thumb->toHtml( $imageParameters ) . '</div></div>';
144
145 // Call parser transform hook
146 if ( $this->mParser && $img->getHandler() ) {
147 $img->getHandler()->parserTransformHook( $this->mParser, $img );
148 }
149 }
150
151 // @todo Code is incomplete.
152 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) .
153 // ":{$ut}" );
154 // $ul = Linker::link( $linkTarget, $ut );
155
156 if ( $this->mShowBytes ) {
157 if ( $img ) {
158 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
159 } else {
160 $fileSize = $this->msg( 'filemissing' )->escaped();
161 }
162 $fileSize = "$fileSize<br />\n";
163 } else {
164 $fileSize = '';
165 }
166
167 $textlink = $this->mShowFilename ?
168 Linker::linkKnown(
169 $nt,
170 htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) )
171 ) . "<br />\n" :
172 '';
173
174 $galleryText = $textlink . $text . $fileSize;
175 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
176
177 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
178 # Can be safely removed if FF2 falls completely out of existence
179 $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
180 . $this->getGBWidth( $thumb ) . 'px">'
181 . '<div style="width: ' . $this->getGBWidth( $thumb ) . 'px">'
182 . $thumbhtml
183 . $galleryText
184 . "\n\t\t</div></li>";
185 }
186 $output .= "\n</ul>";
187
188 return $output;
189 }
190
191 /**
192 * Add the wrapper html around the thumb's caption
193 *
194 * @param string $galleryText The caption
195 * @param MediaTransformOutput|bool $thumb The thumb this caption is for
196 * or false for bad image.
197 * @return string
198 */
199 protected function wrapGalleryText( $galleryText, $thumb ) {
200 # ATTENTION: The newline after <div class="gallerytext"> is needed to
201 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
202 # its absence, see: http://bugzilla.wikimedia.org/show_bug.cgi?id=1765
203 # -Ævar
204
205 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
206 . $galleryText
207 . "\n\t\t\t</div>";
208 }
209
210 /**
211 * How much padding such the thumb have between image and inner div that
212 * that contains the border. This is both for verical and horizontal
213 * padding. (However, it is cut in half in the vertical direction).
214 * @return int
215 */
216 protected function getThumbPadding() {
217 return 30;
218 }
219
220 /**
221 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
222 *
223 * @return int
224 */
225 protected function getGBPadding() {
226 return 5;
227 }
228
229 /**
230 * Get how much extra space the borders around the image takes up.
231 *
232 * For this mode, it is 2px borders on each side + 2px implied padding on
233 * each side from the stylesheet, giving us 2*2+2*2 = 8.
234 * @return int
235 */
236 protected function getGBBorders() {
237 return 8;
238 }
239
240 /**
241 * Get total padding.
242 *
243 * @return int Number of pixels of whitespace surrounding the thumbnail.
244 */
245 protected function getAllPadding() {
246 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
247 }
248
249 /**
250 * Get vertical padding for a thumbnail
251 *
252 * Generally this is the total height minus how high the thumb is.
253 *
254 * @param int $boxHeight How high we want the box to be.
255 * @param int $thumbHeight How high the thumbnail is.
256 * @return int Vertical padding to add on each side.
257 */
258 protected function getVPad( $boxHeight, $thumbHeight ) {
259 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
260 }
261
262 /**
263 * Get the transform parameters for a thumbnail.
264 *
265 * @param File $img The file in question. May be false for invalid image
266 * @return array
267 */
268 protected function getThumbParams( $img ) {
269 return array(
270 'width' => $this->mWidths,
271 'height' => $this->mHeights
272 );
273 }
274
275 /**
276 * Get the width of the inner div that contains the thumbnail in
277 * question. This is the div with the class of "thumb".
278 *
279 * @param int $thumbWidth The width of the thumbnail.
280 * @return int Width of inner thumb div.
281 */
282 protected function getThumbDivWidth( $thumbWidth ) {
283 return $this->mWidths + $this->getThumbPadding();
284 }
285
286 /**
287 * Width of gallerybox <li>.
288 *
289 * Generally is the width of the image, plus padding on image
290 * plus padding on gallerybox.
291 *
292 * @note Important: parameter will be false if no thumb used.
293 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
294 * @return int Width of gallerybox element
295 */
296 protected function getGBWidth( $thumb ) {
297 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
298 }
299
300 /**
301 * Get a list of modules to include in the page.
302 *
303 * Primarily intended for subclasses.
304 *
305 * @return array Modules to include
306 */
307 protected function getModules() {
308 return array();
309 }
310
311 /**
312 * Adjust the image parameters for a thumbnail.
313 *
314 * Used by a subclass to insert extra high resolution images.
315 * @param MediaTransformOutput $thumb The thumbnail
316 * @param array $imageParameters Array of options
317 */
318 protected function adjustImageParameters( $thumb, &$imageParameters ) {
319 }
320 }
321
322 /**
323 * Backwards compatibility. This always uses traditional mode
324 * if called the old way, for extensions that may expect traditional
325 * mode.
326 *
327 * @deprecated 1.22 Use ImageGalleryBase::factory instead.
328 */
329 class ImageGallery extends TraditionalImageGallery {
330 function __construct( $mode = 'traditional' ) {
331 wfDeprecated( __METHOD__, '1.22' );
332 parent::__construct( $mode );
333 }
334 }